Created
March 20, 2014 18:31
-
-
Save dmdavis/9670681 to your computer and use it in GitHub Desktop.
Example of converting UTC to local time using python-dateutil
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# encoding: utf-8 | |
""" | |
utc_to_local.py | |
Example of converting UTC to local time using python-dateutil. | |
https://pypi.python.org/pypi/python-dateutil | |
""" | |
from datetime import datetime | |
from dateutil.tz import tzutc, tzlocal | |
utc = datetime.now(tzutc()) | |
print('UTC: ' + str(utc)) | |
local = utc.astimezone(tzlocal()) | |
print('Local: ' + str(local)) |
dateutil.tz.tzlocal
may fail. Use localtz.get_localzone()
instead. See how to get tz_info object corresponding to current timezone?.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How can i do that in templates? I know i can write a template filter, but Why Django converting it one hour ahead but your piece of code is generating correct local time?
Confused.