不同日期时间字符串的相同 mktime() 结果

same mktime() result for different datetime string

我有这个:

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

int main()
{
    struct tm timeinfo;

    strptime( "2016-02-18 09:00:00", "%Y-%m-%d %H:%M:%S", &timeinfo );
    printf( "result 1=%ld\n", mktime( &timeinfo ) );

    strptime( "2016-02-18 08:00:00", "%Y-%m-%d %H:%M:%S", &timeinfo );
    printf( "result 2=%ld\n", mktime( &timeinfo ) );
}

这个 MCVE 的结果让我抓狂:

result 1=1455778800
result 2=1455778800

1455778800 是 2016-02-18 07:00:00 的 UTC 时间,因此第二个日期时间字符串是正确的。但为什么两个不同的字符串得到相同的结果?

现在这个:

{
    struct tm timeinfo;

    strptime( "2016-02-18 08:00:00", "%Y-%m-%d %H:%M:%S", &timeinfo );
    printf( "result 2=%ld\n", mktime( &timeinfo ) );
    strptime( "2016-02-18 09:00:00", "%Y-%m-%d %H:%M:%S", &timeinfo );
    printf( "result 1=%ld\n", mktime( &timeinfo ) );
}

这里我有这个结果:

result 2=1455775200
result 1=1455782400

result 1 现在是正确的,但是 result 2 早了一个小时。当我第一次调用它时,内部 glibc 结构似乎没有正确初始化。

我正在 link 使用另一个 glibc 版本而不是系统正在使用的版本。当我 link 它与系统版本一起工作时。我做错了什么?

OF,解决方案在 man 页:

In principle, this function does not initialize tm but stores only the values specified. This means that tm should be initialized
before the call. Details differ a bit between different UNIX
systems. The glibc implementation does not touch those fields which
are not explicitly specified, except that it recomputes the tm_wday
and tm_yday field if any of the year, month, or day elements changed.

bzero(&timeinfo, sizeof(timeinfo)); 现在可以了。