我有以下整数:
int y, mon, d, h, min, s;
他们的价值观是:2012
, 06
, 27
, 12
, 47
, 53
分别。如果我在应用程序中的其他地方选择了“UTC”,我想表示“2012/06/27 12:47:53 UTC”的日期时间,或者如果我在应用程序中的其他地方选择了“2012/06/27 12:47:53 AEST”在我的申请中的其他地方选择了“AEST”。
我想把它转换成time_t
,这是我当前使用的代码:
struct tm timeinfo;
timeinfo.tm_year = year - 1900;
timeinfo.tm_mon = mon - 1;
timeinfo.tm_mday = day;
timeinfo.tm_hour = hour;
timeinfo.tm_min = min;
timeinfo.tm_sec = sec;
//timeinfo.tm_isdst = 0; //TODO should this be set?
//TODO find POSIX or C standard way to do covert tm to time_t without in UTC instead of local time
#ifdef UNIX
return timegm(&timeinfo);
#else
return mktime(&timeinfo); //FIXME Still incorrect
#endif
所以我正在使用tm struct
and mktime
,但是这效果不佳,因为它总是假设我当地的时区。
这样做的正确方法是什么?
以下是我迄今为止提出的解决方案。
它基本上执行以下三件事之一:
- 如果是 UNIX,只需使用
timegm
- If not UNIX
- Either, do math using the difference between UTC epoch and local epoch as an offset
- Or, set the "TZ" environment variable to UTC temporarily
namespace tmUtil
{
int const tm_yearCorrection = -1900;
int const tm_monthCorrection = -1;
int const tm_isdst_dontKnow = -1;
#if !defined(DEBUG_DATETIME_TIMEGM_ENVVARTZ) && !(defined(UNIX) && !defined(DEBUG_DATETIME_TIMEGM))
static bool isLeap(int year)
{
return
(year % 4) ? false
: (year % 100) ? true
: (year % 400) ? false
: true;
}
static int daysIn(int year)
{
return isLeap(year) ? 366 : 365;
}
#endif
}
time_t utc(int year, int mon, int day, int hour, int min, int sec)
{
struct tm time = {0};
time.tm_year = year + tmUtil::tm_yearCorrection;
time.tm_mon = mon + tmUtil::tm_monthCorrection;
time.tm_mday = day;
time.tm_hour = hour;
time.tm_min = min;
time.tm_sec = sec;
time.tm_isdst = tmUtil::tm_isdst_dontKnow;
#if defined(UNIX) && !defined(DEBUG_DATETIME_TIMEGM) //TODO remove && 00
time_t result;
result = timegm(&time);
return result;
#else
#if !defined(DEBUG_DATETIME_TIMEGM_ENVVARTZ)
//TODO check that math is correct
time_t fromEpochUtc = mktime(&time);
struct tm localData;
struct tm utcData;
struct tm* loc = localtime_r (&fromEpochUtc, &localData);
struct tm* utc = gmtime_r (&fromEpochUtc, &utcData);
int utcYear = utc->tm_year - tmUtil::tm_yearCorrection;
int gmtOff =
(loc-> tm_sec - utc-> tm_sec)
+ (loc-> tm_min - utc-> tm_min) * 60
+ (loc->tm_hour - utc->tm_hour) * 60 * 60
+ (loc->tm_yday - utc->tm_yday) * 60 * 60 * 24
+ (loc->tm_year - utc->tm_year) * 60 * 60 * 24 * tmUtil::daysIn(utcYear);
#ifdef UNIX
if (loc->tm_gmtoff != gmtOff)
{
StringBuilder err("loc->tm_gmtoff=", StringBuilder((int)(loc->tm_gmtoff)), " but gmtOff=", StringBuilder(gmtOff));
THROWEXCEPTION(err);
}
#endif
int resultInt = fromEpochUtc + gmtOff;
time_t result;
result = (time_t)resultInt;
return result;
#else
//TODO Find a way to do this without manipulating environment variables
time_t result;
char *tz;
tz = getenv("TZ");
setenv("TZ", "", 1);
tzset();
result = mktime(&time);
if (tz)
setenv("TZ", tz, 1);
else
unsetenv("TZ");
tzset();
return result;
#endif
#endif
}
N.B. StringBuilder
是一个内部类,对于这个问题的目的来说并不重要。
更多信息:
我知道这可以使用 boost 等轻松完成。但这不是选择。我需要以数学方式完成它,或者使用 c 或 c++ 标准函数或其组合。
timegm
似乎解决了这个问题,但是,它似乎并不是 C/POSIX 标准的一部分。该代码目前在多个平台(Linux、OSX、WIndows、iOS、Android (NDK))上编译,因此我需要找到一种方法使其在所有这些平台上工作,即使解决方案涉及#ifdef $PLATFORM
键入东西。