struct tm {
int tm_sec; /* seconds after the minute - [0,59] */
int tm_min; /* minutes after the hour - [0,59] */
int tm_hour; /* hours since midnight - [0,23] */
int tm_mday; /* day of the month - [1,31] */
int tm_mon; /* months since January - [0,11] */
int tm_year; /* years since 1900 */
int tm_wday; /* days since Sunday - [0,6] */
int tm_yday; /* days since January 1 - [0,365] */
int tm_isdst; /* daylight savings time flag */
};
struct timeval {
time_t tv_sec; /* seconds */
suseconds_t tv_usec; /* microseconds */
};
time_t time(time_t *t); //取得从1970年1月1日至今的秒数 char *asctime(const struct tm *tm); //将结构中的信息转换为真实世界的时间,以字符串的形式显示 char *ctime(const time_t *timep); //将timep转换为真是世界的时间,以字符串显示,它和asctime不同就在于传入的参数形式不一样 struct tm *gmtime(const time_t *timep); //将time_t表示的时间转换为没有经过时区转换的UTC时间,是一个struct tm结构指针 struct tm *localtime(const time_t *timep); //和gmtime类似,但是它是经过时区转换的时间。 time_t mktime(struct tm *tm); //将struct tm 结构的时间转换为从1970年至今的秒数 int gettimeofday(struct timeval *tv, struct timezone *tz); //返回当前距离1970年的秒数和微妙数,后面的tz是时区,一般不用 double difftime(time_t time1, time_t time2); //返回两个时间相差的秒数
time_t t; //秒时间
tm* local; //本地时间
tm* gmt; //格林威治时间
char buf[128]= {0};
t = time(NULL); //或者time(&t);//获取目前秒时间
local = localtime(&t); //转为本地时间
strftime(buf, 64, "%Y-%m-%d %H:%M:%S", local);
std::cout << buf << std::endl;
gmt = gmtime(&t);//转为格林威治时间
strftime(buf, 64, "%Y-%m-%d %H:%M:%S", gmt);
std::cout << buf << std::endl;
tm tm_;
time_t t_;
char buf[128]= {0};
strcpy(buf, "2012-01-01 14:00:00");
strptime(buf, "%Y-%m-%d %H:%M:%S", &tm_); //将字符串转换为tm时间
tm_.tm_isdst = -1;
t_ = mktime(&tm_); //将tm时间转换为秒时间
t_ += 3600; //秒数加3600
tm_ = *localtime(&t_);//输出时间
strftime(buf, 64, "%Y-%m-%d %H:%M:%S", &tm_);
std::cout << buf << std::endl;
time_t StringToDatetime(char *str)
{
tm tm_;
int year, month, day, hour, minute,second;
sscanf(str,"%d-%d-%d %d:%d:%d", &year, &month, &day, &hour, &minute, &second);
tm_.tm_year = year-1900;
tm_.tm_mon = month-1;
tm_.tm_mday = day;
tm_.tm_hour = hour;
tm_.tm_min = minute;
tm_.tm_sec = second;
tm_.tm_isdst = 0;
time_t t_ = mktime(&tm_); //已经减了8个时区
return t_; //秒时间
}
struct timeval start_time, over_time, consume_time;
gettimeofday(&over_time, NULL);//get the current time
start_time = over_time;
do something.....
gettimeofday(&over_time, NULL);
timeval_subtract(&consume_time, &start_time, &over_time);//计算时间差104./**
* 计算两个时间的间隔,得到时间差
* @param struct timeval* resule 返回计算出来的时间
* @param struct timeval* x 需要计算的前一个时间
* @param struct timeval* y 需要计算的后一个时间
* return -1 failure ,0 success
**/
timeval_subtract(struct timeval* result, struct timeval* x, struct timeval* y)
{
if ( x->tv_sec>y->tv_sec )
return -1;
if ( (x->tv_sec==y->tv_sec) && (x->tv_usec>y->tv_usec) )
return -1;
result->tv_sec = ( y->tv_sec-x->tv_sec );
result->tv_usec = ( y->tv_usec-x->tv_usec );
if (result->tv_usec<0)
{
result->tv_sec--;
result->tv_usec+=1000000;
}
return 0;
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有