Skip to content

Instantly share code, notes, and snippets.

@sesh
Last active August 29, 2015 13:57
Show Gist options
  • Save sesh/9523021 to your computer and use it in GitHub Desktop.
Save sesh/9523021 to your computer and use it in GitHub Desktop.
Python timezone aware datetime oddity
In [1]: import pytz
In [2]: from datetime import datetime
In [3]: melb = pytz.timezone('Australia/Melbourne')
In [4]: datetime.now().replace(tzinfo=melb)
Out[4]: datetime.datetime(2014, 3, 13, 17, 50, 33, 889107, tzinfo=<DstTzInfo 'Australia/Melbourne' EST+10:00:00 STD>)
In [5]: melb.localize(datetime.now())
Out[5]: datetime.datetime(2014, 3, 13, 17, 50, 42, 585441, tzinfo=<DstTzInfo 'Australia/Melbourne' EST+11:00:00 DST>)
In [6]: datetime.now(melb)
Out[6]: datetime.datetime(2014, 3, 13, 17, 50, 47, 337021, tzinfo=<DstTzInfo 'Australia/Melbourne' EST+11:00:00 DST>)
In [7]: melb.localize(datetime(2014, 6, 1))
Out[7]: datetime.datetime(2014, 6, 1, 0, 0, tzinfo=<DstTzInfo 'Australia/Melbourne' EST+10:00:00 STD>)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment