Setting timezone in Python

Setting timezone in Python

>>> import os, time
>>> time.strftime(%X %x %Z)
12:45:20 08/19/09 CDT
>>> os.environ[TZ] = Europe/London
>>> time.tzset()
>>> time.strftime(%X %x %Z)
18:45:39 08/19/09 BST

To get the specific values youve listed:

>>> year = time.strftime(%Y)
>>> month = time.strftime(%m)
>>> day = time.strftime(%d)
>>> hour = time.strftime(%H)
>>> minute = time.strftime(%M)

See here for a complete list of directives. Keep in mind that the strftime() function will always return a string, not an integer or other type.

Be aware that running

import os
os.system(tzutil /s Central Standard Time);

will alter Windows system time, NOT just the local python environment time (so is definitley NOT the same as:

>>> os.environ[TZ] = Europe/London
>>> time.tzset()

which will only set in the current environment time (in Unix only)

Setting timezone in Python

For windows you can use:

Running Windows command prompt commands in python.

import os
os.system(tzutil /s Central Standard Time)

In windows command prompt try:

This gives current timezone:

tzutil /g

This gives a list of timezones:

tzutil /l

This will set the timezone:

tzutil /s Central America Standard Time

For further reference:

Leave a Reply

Your email address will not be published. Required fields are marked *