获取时区GMT在C的失调 [英] Get the time zone GMT offset in C

查看:160
本文介绍了获取时区GMT在C的失调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是标准的 mktime 功能把一个结构TM 进入一个划时代的时间值。在 TM 字段是本地人口的,我需要得到划时代的时间为北京时间。 TM 有一个 gmtoff 字段,允许您设置的当地GMT以秒为抵消这个目的。

I'm using the standard mktime function to turn a struct tm into an epoch time value. The tm fields are populated locally, and I need to get the epoch time as GMT. tm has a gmtoff field to allow you to set the local GMT offset in seconds for just this purpose.

但我不能想出如何的获得的信息。当然,必须有一个标准功能的地方,将返回偏移?如何本地时间呢?

But I can't figure out how to get that information. Surely there must be a standard function somewhere that will return the offset? How does localtime do it?

推荐答案

我想我应该做得更多一点提问前搜索。原来,有一个鲜为人知的 timegm 功能,它确实 gmtime的相反。它支持GNU和BSD是够用了我的目的。一个更通用的解决方案是临时设置 TZ 环境变量UTC的值,然后使用 mktime ,然后设置 TZ 回来了。

I guess I should have done a bit more searching before asking. It turns out there's a little known timegm function which does the opposite of gmtime. It's supported on GNU and BSD which is good enough for my purposes. A more portable solution is to temporarily set the value of the TZ environment variable to "UTC" and then use mktime, then set TZ back.

timegm 为我工作。

这篇关于获取时区GMT在C的失调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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