Python - Datetime不适合闰秒? [英] Python - Datetime not accounting for leap second properly?
本文介绍了Python - Datetime不适合闰秒?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在解析一些数据,其中有时间戳datetime 2012-06-30T23:59:60.209215
。我使用以下代码解析该字符串并转换为datetime对象:
I am parsing some data that has the leapsecond timestampe datetime 2012-06-30T23:59:60.209215
. I used following code to parse that string and convert to a datetime object:
nofrag, frag = t.split('.')
nofrag_dt = datetime.datetime.strptime(nofrag, "%Y-%m-%dT%H:%M:%S")
dt = nofrag_dt.replace(microsecond=int(frag))
Python文档声称这不应该是一个问题,因为%S
接受 [0,61]
。但是,我收到上述时间戳的错误
Python documentation claims that this shouldn't be an issue as %S
accepts [0, 61]
. But, I get this error with the above timestamp
nofrag_dt = datetime.datetime.strptime(nofrag, "%Y-%m-%dT%H:%M:%S")
ValueError: second must be in 0..59
谢谢
推荐答案
执行此操作:
import time
import datetime
t = '2012-06-30T23:59:60.209215'
nofrag, frag = t.split('.')
nofrag_dt = time.strptime(nofrag, "%Y-%m-%dT%H:%M:%S")
ts = datetime.datetime.fromtimestamp(time.mktime(nofrag_dt))
dt = ts.replace(microsecond=int(frag))
print(dt)
输出是:
2012-07-01 00:00:00.209215
这篇关于Python - Datetime不适合闰秒?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文