我想计算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);
感谢
答案 0 :(得分:1)
是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 */