在 C/C++ 中在本地时间和 GMT/UTC 之间转换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/761791/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 17:10:02  来源:igfitidea点击:

Converting Between Local Times and GMT/UTC in C/C++

c++ctimezonedst

提问by Kristopher Johnson

What's the best way to convert datetimes between local time and UTC in C/C++?

在 C/C++ 中,在本地时间和 UTC 之间转换日期时间的最佳方法是什么?

By "datetime", I mean some time representation that contains date and time-of-day. I'll be happy with time_t, struct tm, or any other representation that makes it possible.

“日期时间”是指包含日期和时间的某种时间表示。我会很乐意用time_tstruct tm或任何其他的表示,使之成为可能。

My platform is Linux.

我的平台是Linux。

Here's the specific problem I'm trying to solve: I get a pair of values containing a julian date and a number of seconds into the day. Those values are in GMT. I need to convert that to a local-timezone "YYYYMMDDHHMMSS" value. I know how to convert the julian date to Y-M-D, and obviously it is easy to convert seconds into HHMMSS. However, the tricky part is the timezone conversion. I'm sure I can figure out a solution, but I'd prefer to find a "standard" or "well-known" way rather than stumbling around.

这是我要解决的具体问题:我得到一对包含朱利安日期和一天中的秒数的值。这些值采用格林威治标准时间。我需要将其转换为本地时区“YYYYMMDDHHMMSS”值。我知道如何将朱利安日期转换为 YMD,显然很容易将秒转换为 HHMMSS。然而,棘手的部分是时区转换。我确信我能找到解决方案,但我更愿意找到一种“标准”或“众所周知”的方式,而不是四处游荡。



A possibly related question is Get Daylight Saving Transition Dates For Time Zones in C

一个可能相关的问题是在 C 中获取时区的夏令时转换日期

采纳答案by Rick C. Petty

You're supposed to use combinations of gmtime/localtimeand timegm/mktime. That should give you the orthogonal tools to do conversions between struct tmand time_t.

您应该使用gmtime/localtimetimegm/ 的组合mktime。这应该为您提供在struct tm和之间进行转换的正交工具time_t

For UTC/GMT:

对于 UTC/GMT:

time_t t;
struct tm tm;
struct tm * tmp;
...
t = timegm(&tm);
...
tmp = gmtime(t);

For localtime:

对于本地时间:

t = mktime(&tm);
...
tmp = localtime(t);

All tzset()does is set the internal timezone variable from the TZenvironment variable. I don't think this is supposed to be called more than once.

所有tzset()没有设置为从内部时区变量TZ的环境变量。我不认为这应该被多次调用。

If you're trying to convert between timezones, you should modify the struct tm's tm_gmtoff.

如果你想时区之间进行转换,您应该修改struct tmtm_gmtoff

回答by Rick C. Petty

If on Windows, you don't have timegm() available to you:

如果在 Windows 上,您没有 timegm() 可用:

struct tm *tptr;
time_t secs, local_secs, gmt_secs;
time( &secs );  // Current time in GMT
// Remember that localtime/gmtime overwrite same location
tptr = localtime( &secs );
local_secs = mktime( tptr );
tptr = gmtime( &secs );
gmt_secs = mktime( tptr );
long diff_secs = long(local_secs - gmt_secs);

or something similar...

或类似的东西...

回答by chrish

If you need to worry about converting date/time with timezone rules, you might want to look into ICU.

如果您需要担心使用时区规则转换日期/时间,您可能需要查看ICU