4

以下代码:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>
    #include <sys/time.h>

    static const char * wday_abb_names[] =
    {
        "Mon",
        "Tue",
        "Wed",
        "Thu",
        "Fri",
        "Sat",
        "Sun",
    };

    static void mb_setenv(const char *name, const char *value)
    {
    #if !(defined _WIN32) || defined HAVE_SETENV
        setenv(name, value, 1);
    #else
        int len = strlen(name)+1+strlen(value)+1;
        char *str = malloc(len);
        sprintf(str, "%s=%s", name, value);
        putenv(str);
    #endif
    }

    static void mb_unsetenv(const char *name)
    {
    #if !(defined _WIN32) || defined HAVE_SETENV
        unsetenv(name);
    #else
        int len = strlen(name)+2;
        char *str = malloc(len);
        sprintf(str, "%s=", name);
        putenv(str);
                    free(str);
    #endif
    }

    time_t mb_timegm(struct tm *tm)
    {
        time_t ret;
        char *tz;

        tz = getenv("TZ");
        mb_setenv("TZ", "");
        tzset();
        ret = mktime(tm);
        if (tz)
        {
            mb_setenv("TZ", tz);
        }
        else
        {
            mb_unsetenv("TZ");
        }
        tzset();
        return ret;
    }

    time_t get_test_time()
    {
        struct tm msg_time;
        msg_time.tm_isdst = 0;
        msg_time.tm_wday = 4;
        msg_time.tm_mon = 5;
        msg_time.tm_mday = 16;
        msg_time.tm_hour = 4;
        msg_time.tm_min = 53;
        msg_time.tm_sec = 0;
        msg_time.tm_year = 111; //2011 - 1900
        time_t retval = mb_timegm(&msg_time);
        printf("final msg_time = %ld\n", retval);
        return retval;
    }

    void print_time(const char *msg, struct tm *t)
      {
        printf("%s %s, %02d.%02d.%2d %2d:%02d\n", msg,
               wday_abb_names[t->tm_wday],  t->tm_mday, t->tm_mon, t->tm_year,
               t->tm_hour, t->tm_min);
      }

    int main()
    {
        printf( "=== ENVIRON ===\n");
        printf("TZ = %s\n", getenv("TZ"));
        time_t now;
        struct tm l, g;
        time(&now);
        l = *localtime(&now);
        g = *gmtime(&now);

        print_time("Local time :", &l);
        print_time("utc        :", &g);
        printf("=== END ENVIRON ===\n\n");

        time_t tt = get_test_time();
        printf("fix test (16.6.2011 04:53) --> %s\n", ctime(&tt));

        printf("done.\n");
        return 0;
    }

在 GNU/Linux 上运行它会产生:

=== ENVIRON ===
TZ = (null)
Local time : Sat, 24.05.111 14:20
utc        : Sat, 24.05.111 12:20
=== END ENVIRON ===

final msg_time = 1308199980
fix test (16.6.2011 04:53) --> Thu Jun 16 06:53:00 2011

done.

在 Win7 上运行它会产生:

=== ENVIRON ===
TZ = (null)
Local time : Sat, 24.05.111 14:25
utc        : Sat, 24.05.111 12:25
=== END ENVIRON ===

final msg_time = 1308196380
fix test (16.6.2011 04:53) --> Thu Jun 16 05:53:00 2011

done.

两个系统的时区均为 UTC+1,包括 DST(使 UTC+2 生效),并且两个系统都没有任何时间问题 - 除了显示的差异。

如您所见,“最终 msg_time”正好缺少 3600 秒,因此在 ctime 中不是问题。

任何人都可以向我解释为什么 mktime 在 GNU/Linux 和 Windows 上似乎表现不同 - 或者如何纠正它?

编辑:
两个系统(调用后tzset())都报告 tzname[0] = CET, tzname[1] = CEST, daytime=1, timezone = -3600

4

2 回答 2

4

My mb_timegm was based on the code stated in man 3 timegm and it stated "set the TZ environment variable to UTC" to do this setenv("TZ", ""); is called.

However - this does not work on windows.

Using setenv("TZ", "UTC"); (or, in the above case mb_setenv) instead fixes the problem.

于 2011-06-24T21:45:34.157 回答
2

我假设根据您提供的夏令时生效的信息,您需要将值设置为msg_time.tm_isdst而不是. 这可能是缺少时间的问题。要么这样,要么您可以将其设置为并允许系统尝试确定您是否处于夏令时,对于给定的输入值。get_test_time()10-1

于 2011-06-24T15:58:40.330 回答