Setting windows/win32 timezone from python



I know there are many threads on this topic but I haven't yet found an
answer to this question. I'd like to map from a utc to a local time in
an arbitrary timezone (not necessary the default local timezone).

To do this on UNIX is relatively straightforward by setting the TZ
environment variable (and calling time.tzset()). On Windows, one can
get close to the answer using the pytz module (to load the timezone
based on the standard UNIX names like US/Eastern and Australia/Sydney).
However, the mapping from utc -> local time won't have the correct
notion of DST for the utc you specify because the OS only knows about
the machine's true localtime. I believe that the only way to really
get the right answer is to temporarily set the machine's local
timezone, which is the standard UNIX approach.

Here's something that _almost_ works in Windows:

import datetime, pytz
d = datetime.datetime.fromtimestamp(1143408899,
pytz.timezone("Australia/Sydney"))
d.strftime("%Y%m%d %H:%M:%S")

-> 20060327 07:34:59, but it _should_ be 08:34:59

Here's code that does work in UNIX.

import datetime
import os
import time

tz_orig = os.environ['TZ']

# this code only works on unix
def utc_to_date_time(utc, tz=tz_orig):

try:
if (tz != tz_orig):
os.environ['TZ'] = tz
time.tzset()

dt = datetime.datetime.fromtimestamp(utc)
return (dt.strftime("%Y%m%d"), dt.strftime("%H%M%S"))

finally:

if (os.environ['TZ'] != tz_orig):
os.environ['TZ'] = tz_orig
time.tzset()

Thanks,

- Joe

.



Relevant Pages

  • Re: Time Bases and Timekeeping (was: Re: Valentins day :-)
    ... UNIX systems usually store the time as UTC, and then adjust for the local time zone. ... The display of UTC/GMT is common when servers don't have a good mechanism for displaying local time on a per-process basis, or when there's a specific reason to use UTC/GMT for all external operations; when the end-users are operating across time zones. ... Yes, I know that UTC and GMT aren't quite the same time, and that time_t only approximates UTC, and that the approximation of UTC isn't quite the same as TAI, either. ...
    (comp.os.vms)
  • Re: timezone setting
    ... afaik the Linux kernel keeps the systemtime in UTC. ... corrected by the value in /etc/timezone to the local time. ... whatever timezone you want with all the historic idiosyncracies of each ... It is a copy of one of the zoneinfo files found ...
    (Ubuntu)
  • Re: UTCNow <> GMT
    ... Unless you are working in the actual GMT timezone,, then forget about GMT. ... Store your timestamps as 'local time' in relation to the defined timezone of the computer along with some information about the timezone. ... If you do this you need to store the timezone information in such a manner that a 'local time' in say, Brazil, can be correctly displayed as a 'local time' in, say, India. ... This entails converting the Brazilian value to UTC and then converting the result to the appropriate Indian value. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Can or should the NTP protocol eventually serve timezone data?
    ... How you torture UTC to get your preferred local time is entirely up to you. ... Kludging local timezone conversions into the NTP protocol somehow would be a nightmare if you could persuade anyone to do it! ...
    (comp.protocols.time.ntp)
  • Re: solar local time
    ... To convert a time in UTC to a local time in the America/New_York timezone, ... include TZInfo ... Note that the Time returned will look like it is UTC. ...
    (comp.lang.ruby)

Loading