difftime 在特定日期返回奇怪的值
我不明白为什么 difftime 返回一个奇怪的值,所以这里是数据集和我使用的代码.
I don't understand why difftime returns a strange value, so here is the dataset and the code I am using.
代码:
struct tm currentTime;
currentTime.tm_year = 2014 - 1900;
currentTime.tm_mon = 9 - 1;
currentTime.tm_mday = 6;
currentTime.tm_hour = 23;
currentTime.tm_min = 59;
currentTime.tm_sec = 0;
currentTime.tm_wday = 7 - 1;
struct tm previousTime;
previousTime.tm_year = 2014 - 1900;
previousTime.tm_mon = 9 - 1;
previousTime.tm_mday = 6;
previousTime.tm_hour = 23;
previousTime.tm_min = 58;
previousTime.tm_sec = 0;
previousTime.tm_wday = 7 - 1;
cout << difftime(mktime(¤tTime), mktime(&previousTime)) << endl;
这会打印:
3660
任何想法为什么我得到这个值?我应该得到 60,因为有一分钟的差异.我尝试了其他一些值,它们都有效..我正在使用带有 mingw 的 CodeBlocks.
Any ideas why I get this value ? I should get 60, as there is a one minute difference. I tried with some other values, and they all work.. I'm using CodeBlocks with mingw.
答案:使用 tm_isdst 解决了问题!血腥夏令时:P
EDIT : Answer : use of tm_isdst solved the problem ! Bloody DST :P
struct tm currentTime;
currentTime.tm_year = 2014 - 1900;
currentTime.tm_mon = 9 - 1;
currentTime.tm_mday = 6;
currentTime.tm_hour = 23;
currentTime.tm_min = 59;
currentTime.tm_sec = 0;
currentTime.tm_wday = 7 - 1;
currentTime.tm_isdst = - 1;
struct tm previousTime;
previousTime.tm_year = 2014 - 1900;
previousTime.tm_mon = 9 - 1;
previousTime.tm_mday = 6;
previousTime.tm_hour = 23;
previousTime.tm_min = 58;
previousTime.tm_sec = 0;
previousTime.tm_wday = 7 - 1;
previousTime.tm_isdst = - 1;
cout << difftime(mktime(¤tTime), mktime(&previousTime)) << endl;
在调用 mktime()
之前,通常需要设置 struct tm
的 7 个字段.由于 OP 只设置了其中的 6 个,未初始化的数据位于 tm_isdst
字段中,导致 3600 秒的意外偏移.
Before calling mktime()
, typically 7 fields of struct tm
need to be set. As OP only set 6 of those, uninitialized data was in field tm_isdst
causing an unexpected shift of 3600 seconds.
struct tm currentTime;
currentTime.tm_year = 2014 - 1900;
currentTime.tm_mon = 9 - 1;
currentTime.tm_mday = 6;
currentTime.tm_hour = 23;
currentTime.tm_min = 59;
currentTime.tm_sec = 0;
currentTime.tm_isdst = -1; // **
// currentTime.tm_wday = 7 - 1;
mktime(¤tTime);
建议零填充 struct tm
如 struct tm currentTime = { 0 };
以确保所有字段都指定为 struct tm
可能包含除 9 之外的字段:int tm_sec tm_min tm_hour tm_mday tm_mon tm_year tm_wday tm_yday tm_isdst
.
Recommend to zero-fill struct tm
as in struct tm currentTime = { 0 };
to insure all fields are specified as struct tm
may contain fields in addition to the 9: int tm_sec tm_min tm_hour tm_mday tm_mon tm_year tm_wday tm_yday tm_isdst
.
注意事项:
tm_wday
和 tm_yday
中的原始值被 mktime()
忽略并重新计算.其他字段的原始值不受其正常范围的限制,也会重新计算.
The original values in tm_wday
and tm_yday
are ignored and re-calculated by mktime()
. The other fields' original values are not restricted to their normal range and are recalculated too.
** tm_isdst
的正值或零值会导致 mktime
函数最初分别假定夏令时在指定时间内有效或无效.负值会导致它尝试确定夏令时是否在指定时间内生效.
** a positive or zero value for tm_isdst
causes the mktime
function to presume initially that Daylight Saving Time, respectively, is or is not in effect for the specified time. A negative value causes it to attempt to determine whether Daylight Saving Time is in effect for the specified time.