c - 系统调用 mktime 忽略 tm_isdst 标志

标签 c linux unix posix mktime

另一个关于mktime和DST的问题

Linux、Ubuntu,时区设置为欧洲/柏林,即当前时间为 CEST:

>date
Mon Aug 22 16:08:10 CEST 2016
>date --utc
Mon Aug 22 14:08:14 UTC 2016

到目前为止一切正常。

现在我尝试运行以下代码:

#include <stdio.h>
#include <time.h>
int main()
{

    struct tm   tm = {0};
    int secs;

    tm.tm_sec = 0;
    tm.tm_min = 0;
    tm.tm_hour = 12;
    tm.tm_mon = 9 - 1;
    tm.tm_mday = 30;
    tm.tm_year = 2016 - 1900;

    tm.tm_isdst = 0;
    secs = mktime(&tm);
    printf("%i\n", secs);

    tm.tm_isdst = 1;
    secs = mktime(&tm);
    printf("%i\n", secs);

    tm.tm_isdst = -1;
    secs = mktime(&tm);
    printf("%i\n", secs);

    return 0;
}

得到

1475233200
1475233200
1475233200

在所有三种情况下都是错误的(偏移 1 小时):

>date -d @1475233200
Fri Sep 30 13:00:00 CEST 2016

所以我现在有点疑惑,我的时区是不是坏了?为什么 tm_isdst 标志被完全忽略?

编辑:@Nominal Animal 给出了答案:mktime 修改 tm_hour!我想知道它在哪里记录?!

#include <stdio.h>
#include <time.h>

void reset(struct tm* tm){
    (*tm) = (const struct tm){0};

    tm->tm_sec = 0;
    tm->tm_min = 0;
    tm->tm_hour = 12;
    tm->tm_mon = 9 - 1;
    tm->tm_mday = 30;
    tm->tm_year = 2016 - 1900;
}

int main()
{

    struct tm   tm;
    int secs;

    reset(&tm);
    tm.tm_isdst = 0;
    secs = mktime(&tm);
    printf("%i\n", secs);

    reset(&tm);
    tm.tm_isdst = 1;
    secs = mktime(&tm);
    printf("%i\n", secs);

    reset(&tm);    
    tm.tm_isdst = -1;
    secs = mktime(&tm);
    printf("%i\n", secs);

    return 0;
}

给予

1475233200
1475229600
1475229600

最佳答案

我想我现在可以明白人们会如何发现这令人困惑。将 mktime() 视为具有签名

time_t mktime_actual(struct tm *dst, const struct tm *src);

其中time_t结果是根据(规范化)*src计算的,规范化的字段和当时是否适用夏令时,保存到*dst.

只是C语言开发者历来选择只使用一个指针,结合了srcdst。不过,上述逻辑仍然成立。

参见 `man mktime手册页,尤其是这部分:

The mktime() function converts a broken-down time structure, expressed as local time, to calendar time representation. The function ignores the values supplied by the caller in the tm_wday and tm_yday fields. The value specified in the tm_isdst field informs mktime() whether or not daylight saving time (DST) is in effect for the time supplied in the tm structure: a positive value means DST is in effect; zero means that DST is not in effect; and a negative value means that mktime() should (use timezone information and system databases to) attempt to determine whether DST is in effect at the specified time.

The mktime() function modifies the fields of the tm structure as follows: tm_wday and tm_yday are set to values determined from the contents of the other fields; if structure members are outside their valid interval, they will be normalized (so that, for example, 40 October is changed into 9 November); tm_isdst is set (regardless of its initial value) to a positive value or to 0, respectively, to indicate whether DST is or is not in effect at the specified time. Calling mktime() also sets the external variable tzname with information about the current timezone.

If the specified broken-down time cannot be represented as calendar time (seconds since the Epoch), mktime() returns (time_t) -1 and does not alter the members of the broken-down time structure.

换句话说,如果你稍微改变你的测试程序,就变成

#include <stdlib.h>
#include <stdio.h>
#include <time.h>

static const char *dst(const int flag)
{
    if (flag > 0)
        return "(>0: is DST)";
    else
    if (flag < 0)
        return "(<0: Unknown if DST)";
    else
        return "(=0: not DST)";
}

static struct tm newtm(const int year, const int month, const int day,
                       const int hour, const int min, const int sec,
                       const int isdst)
{
    struct tm t = { .tm_year  = year - 1900,
                    .tm_mon   = month - 1,
                    .tm_mday  = day,
                    .tm_hour  = hour,
                    .tm_min   = min,
                    .tm_sec   = sec,
                    .tm_isdst = isdst };
    return t;
}

int main(void)
{
    struct tm   tm = {0};
    time_t secs;

    tm = newtm(2016,9,30, 12,0,0, -1);
    secs = mktime(&tm);
    printf("-1: %04d-%02d-%02d %02d:%02d:%02d %s %lld\n",
           tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday,
           tm.tm_hour, tm.tm_min, tm.tm_sec, dst(tm.tm_isdst), (long long)secs);

    tm = newtm(2016,9,30, 12,0,0, 0);
    secs = mktime(&tm);
    printf(" 0: %04d-%02d-%02d %02d:%02d:%02d %s %lld\n",
           tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday,
           tm.tm_hour, tm.tm_min, tm.tm_sec, dst(tm.tm_isdst), (long long)secs);

    tm = newtm(2016,9,30, 12,0,0, 1);
    secs = mktime(&tm);
    printf("+1: %04d-%02d-%02d %02d:%02d:%02d %s %lld\n",
           tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday,
           tm.tm_hour, tm.tm_min, tm.tm_sec, dst(tm.tm_isdst), (long long)secs);

    return EXIT_SUCCESS;
}

然后运行它产生输出

-1: 2016-09-30 12:00:00 (>0: is DST) 1475226000
 0: 2016-09-30 13:00:00 (>0: is DST) 1475229600
+1: 2016-09-30 12:00:00 (>0: is DST) 1475226000

换句话说,它的行为与描述的完全一致(在上面的引述中)。此行为记录在 C89、C99 和 POSIX.1 中(我认为 C11 也有,但尚未检查)。

关于c - 系统调用 mktime 忽略 tm_isdst 标志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39081835/

相关文章:

c - Shellcode中的Syscall无法运行

linux - 如何在Linux中的Thorn字符 'þ'上分割一行?

linux - 当软重启 Linux 内核卡在 "Uncompressing Linux... done, booting the kernel"

linux - 如何使用 AWK 来唯一化一个表(为每个唯一 ID 保留最大值)?

c - DNS 客户端实现中的格式错误的数据包

c - 如何打印 C 语言 main 中的函数?

c - OpenMP 和减少 ()

android - 如何在 Android 应用程序中区分蜂窝运营商?

c - Unix Domain Socket 两端并发读/写

c - 我的消费者线程没有正确读取生产者线程的产品