Skip to content

Instantly share code, notes, and snippets.

@dmdavis
Created March 20, 2014 18:31
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save dmdavis/9670681 to your computer and use it in GitHub Desktop.
Save dmdavis/9670681 to your computer and use it in GitHub Desktop.
Example of converting UTC to local time using python-dateutil
#!/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))
@awaisdar001
Copy link

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.

@zed
Copy link

zed commented Apr 18, 2015

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