我想计算GMT时间和当前时间的时间差。为此,我使用mktime将tm时间(以格林威治时间为单位)转换为time_t格式。和使用time() api的当前时间。
struct tm = x; time_t t1, t2;
time(&t1);
/* here x will get in GMT format */
t2 = mktime(&x);
sec = difftime(t2 , t1);在使用相同时区的情况下,mktime()会负责转换为本地时间吗?或者我应该显式地添加sec = difftime(t2 , gmtime(&t1);感谢
发布于 2012-08-21 16:32:47
Yes mktime转换为本地时间,请阅读此文:
http://www.mkssoftware.com/docs/man3/mktime.3.asp
mktime() : convert local time to seconds since the Epoch编辑:要计算两个日期之间的差异时间,可以使用以下命令
time_t t1, t2;
struct tm my_target_date;
/* Construct your date */
my_target_date.tm_sec = 0;
my_target_date.tm_min = 0;
my_target_date.tm_hour = 0;
my_target_date.tm_mday = 20;
my_target_date.tm_mon = 7;
my_target_date.tm_year = 112; /* Date today */
t1 = mktime (&my_target_date);
t2 = time (NULL);
printf ("Number of days since target date : %ld\n", (t2 - t1) / 86400); /* 1 day = 86400 sec, use 3600 if you want hours */https://stackoverflow.com/questions/12050658
复制相似问题