问题描述
我试图使用C ++中的 std :: mktime
将时间信息转换为UTC字符串的时间信息。我的问题是在< ctime>
/ < time.h>
没有函数转换为UTC ; mktime将只返回时间戳作为本地时间。
I'm trying to convert a time info I reveive as a UTC string to a timestamp using std::mktime
in C++. My problem is that in <ctime>
/ <time.h>
there is no function to convert to UTC; mktime will only return the timestamp as local time.
所以我需要弄清楚时区偏移量,并考虑到,但我找不到平台无关方法,不涉及将整个代码移植到 boost :: date_time
。有没有一些简单的解决方案,我忽略了?
So I need to figure out the timezone offset and take it into account, but I can't find a platform-independent way that doesn't involve porting the whole code to boost::date_time
. Is there some easy solution which I have overlooked?
推荐答案
mktime假定日期值是在本地时区。因此,您可以事先更改时区环境变量(setenv)并获取UTC时区。
mktime assumes that the date value is in the local time zone. Thus you can change the timezone environment variable beforehand (setenv) and get the UTC timezone.
也可以尝试看看各种自制的
Can also try looking at various home-made utc-mktimes, mktime-utcs, etc.
这篇关于std :: mktime和时区信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!