how to get tz_info object corresponding to current timezone?

random guy picture random guy · Nov 5, 2009 · Viewed 29.5k times · Source

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

Answer

Alex Martelli picture Alex Martelli · Nov 5, 2009
>>> 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'
>>>