views:

195

answers:

3

I want to get the default timezone (PST) of my system from Python. What's the best way to do that? I'd like to avoid forking another process.

+1  A: 

Check out the Python Time Module.

from time import gmtime, strftime
print strftime("%z", gmtime())

Pacific Standard Time

Hawker
are we looking at different documents or is it `'%Z'`?
SilentGhost
@ SilentGhost: "%z" and '%z' are exactly the same in Python.
Hawker
yes, but **upper-case** `Z` and **lower-case** `z` are not.
SilentGhost
@ SilentGhost: Ah, my apologies, missed that. Just ran a new test and both %z and %Z both work in Python 2.6.
Hawker
**but produce different output**.
SilentGhost
>>> from time import gmtime, strftime>>> print strftime('%z', gmtime())Pacific Standard Time>>> print strftime('%Z', gmtime())Pacific Standard TimeI disagree.
Hawker
http://docs.python.org/library/time.html#time.strftime Also mentions nothing about case on the %Z format.
Hawker
well, 1. on my machine they produce different outputs; 2. docs don't say anything about lower-case z, because only upper-case Z is a valid literal and lower case is not supposed to be used. http://docs.python.org/library/datetime.html#strftime-behavior here are some details of what lower-case z produced (even in time module).
SilentGhost
furthermore, docs state (http://docs.python.org/library/time.html#id2) that use of %Z is deprecated.
SilentGhost
+3  A: 

That should work:

import time
time.tzname

it returns a tuple of two strings: the first is the name of the local non-DST timezone, the second is the name of the local DST timezone.

Johannes Weiß
A: 

If you prefer UTC offsets over strings:

time.timezone / -(60*60)
ThomasH