我正在尝试使用C++中的std::mktime将我接收的时间信息转换为时间戳。我的问题是,在<ctime> / <time.h>中,没有转换为UTC的函数;mktime只返回本地时间的时间戳。
因此,我需要计算出时区偏移并将其考虑在内,但我找不到一种独立于平台的方法,不涉及将整个代码移植到boost::date_time。有没有什么我忽略的简单的解决方案?
发布于 2012-07-04 15:25:17
mktime()使用tzname检测时区。tzset()从TZ环境变量初始化tzname变量。如果TZ变量出现在环境中,但它的值为空或无法正确解释它的值,则使用UTC。
根据timegm manpage的可移植(非线程安全)版本
#include <time.h>
#include <stdlib.h>
time_t
my_timegm(struct tm *tm)
{
time_t ret;
char *tz;
tz = getenv("TZ");
setenv("TZ", "", 1);
tzset();
ret = mktime(tm);
if (tz)
setenv("TZ", tz, 1);
else
unsetenv("TZ");
tzset();
return ret;
}Eric S Raymond在他的文章Time, Clock, and Calendar Programming In C中发布了一个线程安全版本
time_t my_timegm(register struct tm * t)
/* struct tm to seconds since Unix epoch */
{
register long year;
register time_t result;
#define MONTHSPERYEAR 12 /* months per calendar year */
static const int cumdays[MONTHSPERYEAR] =
{ 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
/*@ +matchanyintegral @*/
year = 1900 + t->tm_year + t->tm_mon / MONTHSPERYEAR;
result = (year - 1970) * 365 + cumdays[t->tm_mon % MONTHSPERYEAR];
result += (year - 1968) / 4;
result -= (year - 1900) / 100;
result += (year - 1600) / 400;
if ((year % 4) == 0 && ((year % 100) != 0 || (year % 400) == 0) &&
(t->tm_mon % MONTHSPERYEAR) < 2)
result--;
result += t->tm_mday - 1;
result *= 24;
result += t->tm_hour;
result *= 60;
result += t->tm_min;
result *= 60;
result += t->tm_sec;
if (t->tm_isdst == 1)
result -= 3600;
/*@ -matchanyintegral @*/
return (result);
}https://stackoverflow.com/questions/530519
复制相似问题