Time zone offset

From CodeCodex

Implementations[edit]

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.

C/C++[edit]

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;

JavaScript[edit]

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

TimeZoneOffset = new Date().getTimezoneOffset()

Python[edit]

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.

Ruby[edit]

time_zone_offset = Time.now.gmt_offset