Skip to content

Instantly share code, notes, and snippets.

@weijianwen
Created September 19, 2014 11:09
Show Gist options
  • Save weijianwen/e3da1bed461ed2db52b2 to your computer and use it in GitHub Desktop.
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.
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())
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