我有一个时间戳,表示自1970年以来的毫秒数1432202088224
,它转换为Thursday, May 21, 2015 5:54:48 AM EDT
。我想写一个python函数,把时间戳转换成GMT中的毫秒,我不能天真地在现有的时间戳上加上4个小时(3600000
毫秒),因为每年的半年我都要休息一个小时。在
我试过用datetime
和pytz
编写一个函数
def convert_mills_GMT(milliseconds):
converted_raw = datetime.datetime.fromtimestamp(milliseconds/1000.0)
date_eastern = eastern.localize(converted_raw, is_dst=True)
date_utc = date_eastern.astimezone(utc)
return int(date_utc.strftime("%s")) * 1000
使用1432202088224
的输入,这个函数返回1432220088000
,当我想要的是9:54 AM时,它是Thursday, May 21, 2015 10:54:48 AM EDT
。我错过了什么?在
没有所谓的“EST时间戳”。如果你已经需要时间戳的话。在
要从给定毫秒数的POSIX时间戳中获取UTC时间:
我们可以通过将UTC时间转换回“EST”时区来检查结果是否正确:
^{pr2}$Don't use ^{}. It is not supported, and may silently fail. 相反,要将UTC日期时间转换为时间戳,请使用one of the methods shown here,具体取决于您的Python版本:
Python 3.3+:
Python3(<;3.3):
^{pr2}$Python 2.7+:
Python2号(<;2.7):
因此,您的
convert_mills_GMT
应该是例如,对于Python2.7
印刷品
相关问题 更多 >
编程相关推荐