Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save geoffreybauduin/20477596f0013f46b87cd0cbaa310ce2 to your computer and use it in GitHub Desktop.
Save geoffreybauduin/20477596f0013f46b87cd0cbaa310ce2 to your computer and use it in GitHub Desktop.
Python datetime localizing
Python 2.7.6 (default, Jun 22 2015, 17:58:13)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import datetime
>>> import pytz
>>> dt = datetime.datetime(2002, 4, 7, 2, 30, 00)
>>> print dt
2002-04-07 02:30:00
>>> dt = pytz.utc.localize(dt)
>>> print dt
2002-04-07 02:30:00+00:00
>>> dt = pytz.utc.localize(dt)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "venv/local/lib/python2.7/site-packages/pytz/__init__.py", line 226, in localize
raise ValueError('Not naive datetime (tzinfo is already set)')
ValueError: Not naive datetime (tzinfo is already set)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment