Created
June 16, 2014 19:01
-
-
Save mcdonc/90b3df9a51c35269ded6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
>>> import pytz | |
>>> eastern = pytz.timezone('US/Eastern') | |
>>> import datetime | |
>>> now = datetime.datetime.now().replace(tzinfo=eastern) | |
>>> now.strftime('%a, %d %b %Y %H:%M:%S %z') | |
'Mon, 16 Jun 2014 15:01:02 -0456' | |
456. 456. What the fuck. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@tholo while your code is correct in terms of localize usage, it is incorrect in terms of the way "now" is being used. Unless machine this code is running is set to US/Eastern this code will always be wrong:
And even on a machine that has it's timezone set to US/Eastern using now + localize will produce wrong timestamp for 1 hour a year (notice 1 hour 1 minute difference between 2 timestamps that would be produced by
datetime.datetime.now()
withing 1 minute):This is because pytz treats ambiguous timestamps as non-dst due to
is_dst
being false by default inlocalize
:and
datetime.datetime.now()
produces naive datetime objects so it does not provide dst/non dst hints.You can force pytz to raise errors on ambiguous/non-existent timestamps in case you are dealing with user input and want to handle it: