I was using following code segment to calculate utc offset, but I realize sometimes it's returning wrong results:
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 of the time it's correct though, what's the best way of calculating utc offset?