Difference between revisions of "Time zone offset"

From CodeCodex

(Python: restore more general version)
(add Ruby and change layout)
 
Line 1: Line 1:
===Determining the Local Time Zone Offset===
+
==Implementations==
 +
'''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 <TT>TimeZoneOffset</TT>.
 
The following snippets determine the '''number of seconds to add to UTC''' to obtain the current local time, assigning it to the variable <TT>TimeZoneOffset</TT>.
  
==In C/C++==
+
===C/C++===
  
 
Note that the <TT>timezone</TT> global variable mentioned in the <TT>tzset(3)</TT> man page '''does not take daylight saving into account'''. The following sequence does correctly account for daylight saving:
 
Note that the <TT>timezone</TT> global variable mentioned in the <TT>tzset(3)</TT> man page '''does not take daylight saving into account'''. The following sequence does correctly account for daylight saving:
Line 13: Line 14:
 
</pre>
 
</pre>
  
 +
===JavaScript===
  
==In Python==
+
This is the easiest one--just use the built in class method.
 +
 
 +
<pre>
 +
TimeZoneOffset = new Date().getTimezoneOffset()
 +
</pre>
 +
 
 +
===Python===
  
 
<pre>import time
 
<pre>import time
Line 24: Line 32:
 
Note the assignment to <tt>Now</tt> can be any UTC time, not just the present time, and the <tt>TimeZoneOffset</tt> computation will return the time zone offset for that time.
 
Note the assignment to <tt>Now</tt> can be any UTC time, not just the present time, and the <tt>TimeZoneOffset</tt> computation will return the time zone offset for that time.
  
==In JavaScript==
+
===Ruby===
 
+
<pre>
This is the easiest one--just use the built in class method.
+
time_zone_offset = Time.now.gmt_offset
 
+
<pre>TimeZoneOffset = new Date().getTimezoneOffset()
+
 
</pre>
 
</pre>
  
 
[[Category:C]]
 
[[Category:C]]
[[Category:Python]]
 
 
[[Category:JavaScript]]
 
[[Category:JavaScript]]
 +
[[Category:Python]]
 +
[[Category:Ruby]]
 
[[Category:Time]]
 
[[Category:Time]]

Latest revision as of 02:44, 15 August 2010

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