我正在使用以下代码段来计算 utc 偏移量,但我意识到有时它会返回错误的结果:
double DateTime::getUTCOffset()
{
time_t currtime;
struct tm * timeinfo;
time ( &currtime );
timeinfo = gmtime ( &currtime );
time_t utc = mktime( timeinfo );
timeinfo = localtime ( &currtime );
time_t local = mktime( timeinfo );
// Get offset in hours from UTC
double offsetFromUTC = ((difftime(local, utc) / HOUR_IN_SECONDS) );
// Adjust for DST
if (timeinfo->tm_isdst)
{
offsetFromUTC += 1;
}
return offsetFromUTC;
}
90% 的时间它是正确的,计算 UTC 偏移量的最佳方法是什么?