Time zone offset

From CodeCodex


Determining the Local Time Zone Offset

The following snippets determine the number of seconds to add to UTC to obtain the current local time, assigning it to the variable TimeZoneOffset.


Note that the timezone global variable mentioned in the tzset(3) man page does not take daylight saving into account. The following sequence does correctly account for daylight saving:

struct timeval Ignore;
struct timezone TimeZone;
gettimeofday(&Ignore, &TimeZone);
TimeZoneOffset = - TimeZone.tz_minuteswest * 60;


This is the easiest one--just use the built in class method.

TimeZoneOffset = new Date().getTimezoneOffset()


import time

Now = int(time.time())
TimeZoneOffset = Now - int(time.mktime(time.gmtime(Now)[:8] + time.localtime(Now)[8:]))

Note the assignment to Now can be any UTC time, not just the present time, and the TimeZoneOffset computation will return the time zone offset for that time.


time_zone_offset = Time.now.gmt_offset