我使用以下代码段计算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偏移量的最佳方法是什么?