Last active
January 11, 2016 16:45
-
-
Save johndrinkwater/1af5cca3b392f6cb558c to your computer and use it in GitHub Desktop.
Kinda confused on how messed Python datetime handling is, as I am importing strings and wanting to convert them to UTC, before potentially then outputting to users local tz
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
# mydate is loaded via yaml.load() from a blob of yaml on the next line | |
# Date: 2011-02-11 14:23:01 | |
print mydate, 'offset: ', mydate.utcoffset() | |
# astimezone() called here outputs ValueError: astimezone() cannot be applied to a naive datetime | |
mydate = mydate.replace( tzinfo=pytz.timezone( 'Europe/London' ) ) | |
print mydate, 'offset: ', mydate.utcoffset() | |
print mydate.astimezone(pytz.utc) | |
# outputs | |
2011-02-12 14:23:01 offset: None | |
2011-02-12 14:23:01-00:01 offset: -1 day, 23:59:00 | |
2011-02-12 14:24:01+00:00 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Right, how to fix: