views:

77

answers:

3

Is there a cross-platform function in python (or pytz) that returns a tzinfo object corresponding to the timezone currently set on the computer?

environment variables cannot be counted on as they are not cross-platform

+1  A: 

time.timezone returns current timezone offset. there is also a datetime.tzinfo, if you need more complicated structure.

SilentGhost
time.timezone() just returns the offset from UTC. i want a tzinfo object. so i would probably would what the function to:1. define a class corresponding to the tzinfo object2. instanciate the object3. return it
random guy
`datetime` docs have an example that explains the use of `tzinfo`.
SilentGhost
+1  A: 

I have not used it myself, but dateutil.tz.tzlocal() should do the trick.

http://labix.org/python-dateutil#head-50221b5226c3ccb97daa06ea7d9abf0533ec0310

Jesper Saron
+2  A: 
>>> import datetime
>>> today = datetime.datetime.now()
>>> insummer = datetime.datetime(2009,8,15,10,0,0)
>>> from pytz import reference
>>> localtime = reference.LocalTimezone()
>>> localtime.tzname(today)
'PST'
>>> localtime.tzname(insummer)
'PDT'
>>>
Alex Martelli