为什么mktime()转换31/03/2019 02:00到01:00,tm.tm_isdst = 1和CET时区?
我认为这是一个无效的组合。
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
void reset(struct tm* tm){
(*tm) = (const struct tm){0};
tm->tm_sec = 0;
tm->tm_min = 1;
tm->tm_hour = 2;
tm->tm_mon = 2;
tm->tm_mday = 31;
tm->tm_year = 2019 - 1900;
}
int main(int argc, char **argv)
{
struct tm tm;
int secs;
reset(&tm);
printf("Before mktime\n");
printf(" %04d-%02d-%02d %02d:%02d:%02d\n", tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday,
tm.tm_hour, tm.tm_min, tm.tm_sec);
tm.tm_isdst = 0;
secs = mktime(&tm);
printf("After mktime tm.tm_isdst = 0;\n");
printf(" %04d-%02d-%02d %02d:%02d:%02d TZ=%s , tm_isdst = %d, timestamp=%i\n", tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday,
tm.tm_hour, tm.tm_min, tm.tm_sec, tm.tm_zone, tm.tm_isdst, secs);
reset(&tm);
tm.tm_isdst = 1;
secs = mktime(&tm);
printf("After mktime tm.tm_isdst = 1;\n");
printf(" %04d-%02d-%02d %02d:%02d:%02d TZ=%s , tm_isdst = %d, timestamp=%i\n", tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday,
tm.tm_hour, tm.tm_min, tm.tm_sec, tm.tm_zone, tm.tm_isdst, secs);
reset(&tm);
tm.tm_isdst = -1;
secs = mktime(&tm);
printf("After mktime tm.tm_isdst = -1\n");
printf(" %04d-%02d-%02d %02d:%02d:%02d TZ=%s , tm_isdst = %d, timestamp=%i\n", tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday,
tm.tm_hour, tm.tm_min, tm.tm_sec, tm.tm_zone, tm.tm_isdst, secs);
return 0;
}
输出:
% date
Sun Mar 31 06:50:42 CEST 2019
% test_mktime
Before mktime
2019-03-31 02:01:00
After mktime tm.tm_isdst = 0;
2019-03-31 03:01:00 TZ=CEST , tm_isdst = 1, timestamp=1553994060
After mktime tm.tm_isdst = 1;
2019-03-31 01:01:00 TZ=CET , tm_isdst = 0, timestamp=1553990460
After mktime tm.tm_isdst = -1
2019-03-31 03:01:00 TZ=CEST , tm_isdst = 1, timestamp=1553994060
is_dst = 0
,UTC + 1)不存在,但相当于2019-03-31 01:01:00 UTC(纪元后1553994060秒),相当于2019-03-31 03:01:00 CEST(is_dst = 1
,UTC + 2)。is_dst = 1
,UTC + 2)不存在,但相当于2019-03-31 00:01:00 UTC(纪元后1553990460秒),相当于2019-03-31 01:01:00 CET(is_dst = 0
,UTC + 1)。 (注意:1553994060 - 1553990460 = 3600,相差1小时。)is_dst = -1
,实现试图弄清楚夏令时是否有效,但是自2019-03-31 02:01:00对于CET和CEST同样无效,它只选择其中一个。在这种情况下,它假设输入是在标准时间2019-03-31 02:01:00 CET(is_dst = 0
,UTC + 1),这不存在,但相当于2019-03-31 01:01: 00 UTC(纪元后1553994060秒),相当于2019-03-31 03:01:00 CEST(is_dst = 1
,UTC + 2)。在2019年3月31日02:00是DST更改CET / CEST区域的时间,该日期的第2小时的时间不用于该区域的计时。记录mktime
来规范化输入struct tm
中的数据,并在此处发挥作用。
特别,
struct tm
的规范化程度。返回值对应于该时间。struct tm
的规范化程度。返回值对应于该时间,并且表示比在另一种情况下返回的值早3600秒的时间。mktime
必须猜测。对于大多数时间和日期,它将根据DST是否在该日期和时间生效来选择,但这不会产生任何答案。事实上,它实际上解释了夏令时的标准时间,因为这似乎更有可能是提供输入的程序的期望。