为c mktime在Windows和GNU / Linux有什么不同? [英] is c mktime different on Windows and GNU/Linux?

查看:783
本文介绍了为c mktime在Windows和GNU / Linux有什么不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

    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上运行它产生:

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.

Win7上运行它产生:

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.

这两个系统有一个时区UTC + 1,包括DST(使UTC + 2的效应),这两个系统都没有任何时间的问题,在所有的 - 除了显示的差异

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.

正如你所看到的,最后msg_time缺失正是3600秒;所以它不是一个的ctime问题。

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

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

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

编辑:结果
这两种系统(呼叫后 tzset())的报告TZNAME [0] = CET,TZNAME [1] = CEST,白天= 1时区= -3600


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

推荐答案

我mb_timegm是基于对男子3 timegm规定的code 和它说
设置TZ环境变量UTC来做到这一点 SETENV(TZ,); 被称为

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.

不过 - 这并不工作在Windows上

However - this does not work on windows.

使用 SETENV(TZ,UTC); (或者,在上述情况下,mb_setenv),而不是解决问​​题。

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

这篇关于为c mktime在Windows和GNU / Linux有什么不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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