Last active
June 6, 2017 12:09
-
-
Save xiazhibin/737339da484f14c622620fc70332e284 to your computer and use it in GitHub Desktop.
python timestamp
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
关键:timestamp是不带时区的,但是拿timestamp转datetime的时候你的datetime是带时区的 | |
或者说拿着这个timestamp是没有用的,因为需要根据不同的时区才能转化成对应时区的时间 | |
datetime = timestamp(时间戳) + tzinfo(地区) | |
1 · 换成带时区的datetime计算 | |
epoch = datetime(1970, 1, 1) | |
def datetime_to_timestamp(dt): | |
dt = dt - dt.tzinfo.utcoffset(dt) | |
td = dt - epoch.replace(tzinfo=dt.tzinfo) | |
return ((td.microseconds + (td.seconds + td.days * 86400) * 10 ** 6) / 10 ** 6) * 10 ** 3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment