获取C中的时区GMT偏移量 [英] Get the time zone GMT offset in C
问题描述
我正在使用标准的 mktime
功能将 struct tm
转换为时代时间值。 tm
字段在本地填充,我需要将时代时间作为GMT。 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
适用于我
这篇关于获取C中的时区GMT偏移量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!