系统调用mktime忽略tm_isdst标志 [英] System call mktime ignores tm_isdst flag

查看:192
本文介绍了系统调用mktime忽略tm_isdst标志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于mktime和DST的另一个问题

another one question regarding mktime and DST

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

Linux, Ubuntu, time zone is set to Europe/Berlin i.e. current time is CEST:

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

到目前为止一切正常.

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

Now I try to run the following code:

#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小时):

which is in all three cases wrong (1 hour offset):

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

所以我现在有点困惑,我的时区被打破了吗?为什么tm_isdst标志被完全忽略?

So I am a bit puzzled now, is my timezone somehow broken? Why is tm_isdst flag ignored completely?

@名义动物有答案: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()具有签名

I think I can now see how one would find this confusing. Think of mktime() as having signature

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

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

where the time_t result is calculated based on (normalized) *src, and the normalized fields and whether daylight savings time applies at that time, is saved to *dst.

C语言开发人员历来只是选择只使用一个指针,将srcdst组合在一起.上面的逻辑仍然存在.

It is just that the C language developers historically chose to use only one pointer, combining both src and dst. The above logic still stands, though.

请参见`man mktime 手册页,部分:

mktime()函数可转换细分的时间结构, 以当地时间表示,以日历时间表示.这 函数将忽略调用方在tm_wday中提供的值,并且 tm_yday字段.在tm_isdst字段中指定的值通知 mktime()夏令时(DST)是否有效 tm结构中提供的时间:正值表示DST为 有效;零表示DST无效;和负值 表示mktime()应该(使用时区信息和系统 数据库)尝试确定DST在DST上是否有效 指定的时间.

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.

mktime()函数将tm结构的字段修改为 如下:tm_wday和tm_yday设置为从 其他领域的内容;如果结构成员不在他们的 有效间隔,它们将被归一化(例如,40 10月更改为11月9日);设置了tm_isdst(无论 其初始值)分别为正值或0 指示DST在指定时间是否生效. 调用mktime()还会将外部变量tzname设置为 有关当前时区的信息.

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.

如果指定的细分时间不能表示为日历 时间(自纪元以来的秒数),mktime()返回(time_t)-1并执行 不会改变细分时间结构的成员.

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.

换句话说,如果您稍微更改测试程序,请说出

In other words, if you change your test program a bit, say into

#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,但尚未检查).

In other words, it behaves exactly as described (in the quote above). This behaviour is documented in C89, C99, and POSIX.1 (I think C11 also, but haven't checked).

这篇关于系统调用mktime忽略tm_isdst标志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆