linux程序mktime无权限,在Windows和GNU / Linux上c mktime是不同的?

以下代码:

#include

#include

#include

#include

#include

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

", retval);

return retval;

}

void print_time(const char *msg, struct tm *t)

{

printf("%s %s, %02d.%02d.%2d %2d:%02d

", 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 ===

");

printf("TZ = %s

", 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 ===

");

time_t tt = get_test_time();

printf("fix test (16.6.2011 04:53) --> %s

", ctime(&tt));

printf("done.

");

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,daylight = 1,timezone = -3600

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值