开发者

is c mktime different on Windows and GNU/Linux?

the following code:

    #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;
   开发者_Go百科 }

    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;
    }

running on GNU/Linux it produces:

=== 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.

running on Win7 it produces:

=== 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.

Both Systems have a Timezone of UTC+1 including DST (that makes UTC+2 in effect) and both systems are not having any time-problems at all - except for the difference displayed.

As you can see, the "final msg_time" is missing exactly 3600 seconds, so it is not a problem in ctime.

Can anybody explain to me why mktime seems to behave different on GNU/Linux and Windows - or how to correct that?

Edit:

Both systems (after calling tzset()) are reporting tzname[0] = CET, tzname[1] = CEST, daylight=1, timezone = -3600


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.


I'm assuming based on the info your provided where daylight savings time is in effect, that you would need to set msg_time.tm_isdst in get_test_time() to a value of 1 rather than 0. This may be the issue accounting for the missing hour. Either that, or you could set it to -1 and allow the system to attempt to figure out if you are in daylight savings time or not for the given input value.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜