c - 奇怪的 mktime() 行为

标签 c dst mktime

为什么 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

最佳答案

  1. 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)。
  2. 2019-03-31 02:01:00 CEST(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小时。)
  3. 对于 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) .

关于c - 奇怪的 mktime() 行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55903387/

相关文章:

c - 将字符串值赋予整数时发生了什么?

c - 在 C 中将数组作为参数传递

sql - 使用夏令时计算时间戳

PHP - mktime 在 mysql 中存储错误的日期

C - 按引用调用

c - 插入链表

java - 夏令时持续时间

java - 我可以对具有 'Z' 和 '+1:00' 后缀的日期使用一种 SimpleDateFormat 模式吗?

php - MKTIME,PHP 日期/时间戳 YYYY-MM-DD

php - 如何以毫秒为单位制作 `mktime` 格式?