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
2009-07-10 18:04:15
are we looking at different documents or is it `'%Z'`?
SilentGhost
2009-07-10 18:15:20
@ SilentGhost: "%z" and '%z' are exactly the same in Python.
Hawker
2009-07-10 18:17:11
yes, but **upper-case** `Z` and **lower-case** `z` are not.
SilentGhost
2009-07-10 18:33:53
@ SilentGhost: Ah, my apologies, missed that. Just ran a new test and both %z and %Z both work in Python 2.6.
Hawker
2009-07-10 18:38:56
**but produce different output**.
SilentGhost
2009-07-10 22:20:15
>>> from time import gmtime, strftime>>> print strftime('%z', gmtime())Pacific Standard Time>>> print strftime('%Z', gmtime())Pacific Standard TimeI disagree.
Hawker
2009-07-10 23:23:48
http://docs.python.org/library/time.html#time.strftime Also mentions nothing about case on the %Z format.
Hawker
2009-07-10 23:33:06
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
2009-07-11 09:25:41
furthermore, docs state (http://docs.python.org/library/time.html#id2) that use of %Z is deprecated.
SilentGhost
2009-07-11 09:29:24
+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ß
2009-07-10 18:09:09