Created
September 19, 2014 11:09
-
-
Save weijianwen/e3da1bed461ed2db52b2 to your computer and use it in GitHub Desktop.
Demonstrate how to get UTC seconds for a local time in Python, in both the right and wrong way.
This file contains 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
from datetime import datetime | |
from pytz import timezone | |
dt = timezone("Asia/Shanghai").localize(datetime(1970, 1, 1, 8, 0, 0, 0)) | |
UTC_EPOCH = datetime(1970, 1, 1, 0, 0, 0, 0, tzinfo=timezone("UTC")) | |
print int((dt - UTC_EPOCH).total_seconds()) |
This file contains 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
from datetime import datetime | |
from pytz import timezone | |
dt = datetime(1970, 1, 1, 8, 0, 0, 0, timezone("Asia/Shanghai")) | |
UTC_EPOCH = datetime(1970, 1, 1, 0, 0, 0, 0, timezone("UTC")) | |
print int((dt - UTC_EPOCH).total_seconds()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment