从 Python 中的纪元时间戳转换而来的混淆
Confusion converting from epoch timestamp in Python
我有以下简单脚本(使用 Python 3.4)。
from dateutil.parser import *
import datetime
d1 = "1490917274299"
d2 = "1490917274"
1) 执行datetime.datetime.fromtimestamp(int(d1)).strftime('%c')
时出现以下错误:
>>> datetime.datetime.fromtimestamp(int(d1)).strftime('%c')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OverflowError: timestamp out of range for platform time_t
2) 我读到避免毫秒可以解决问题。所以我除以 d1/1000
得到 d2
瞧!
>>> datetime.datetime.fromtimestamp(int(d2)).strftime('%c')
'Thu Mar 30 23:41:14 2017'
3) 但是,如果我想使用 parse(d2)
,我会得到一个错误。
>>> parse(d2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.4/dist-packages/dateutil/parser.py", line 1168, in parse
return DEFAULTPARSER.parse(timestr, **kwargs)
File "/usr/local/lib/python3.4/dist-packages/dateutil/parser.py", line 578, in parse
if cday > monthrange(cyear, cmonth)[1]:
File "/usr/lib/python3.4/calendar.py", line 121, in monthrange
day1 = weekday(year, month, 1)
File "/usr/lib/python3.4/calendar.py", line 113, in weekday
return datetime.date(year, month, day).weekday()
ValueError: year is out of range
4) 如果你尝试 parse(d1)
你也会得到一个错误:
>>> parse(d1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.4/dist-packages/dateutil/parser.py", line 1168, in parse
return DEFAULTPARSER.parse(timestr, **kwargs)
File "/usr/local/lib/python3.4/dist-packages/dateutil/parser.py", line 578, in parse
if cday > monthrange(cyear, cmonth)[1]:
File "/usr/lib/python3.4/calendar.py", line 121, in monthrange
day1 = weekday(year, month, 1)
File "/usr/lib/python3.4/calendar.py", line 113, in weekday
return datetime.date(year, month, day).weekday()
OverflowError: Python int too large to convert to C long
5) 最后,如果您在 https://www.epochconverter.com/ 中使用 d1
,您将正确获得预期日期。
为什么会发生这种情况?我只是想要一种通过使用 parse()
检查字符串是否为日期时间的方法但是不起作用 eventhouhg 纪元字符串很好(至少 d2)。
另一方面,为什么 d1 作为纪元不好?
谢谢!
卢卡斯
你现在可能已经想通了,但无论如何:
dateutil.parser 的 parse
函数无法解析 Unix 时间 — See this somewhat related issue.
这样就回答了 3 和 4。
现在继续 1 和 2。您无法解析 d1 的原因是因为它不是 Unix 时间。 Unix 时间定义为自 1970 年 1 月 1 日星期四 00:00:00 协调世界时 (UTC) 以来经过的 秒数 减去闰秒数从那时起(感谢维基百科!)。如果要包含指定的毫秒数,请在小数点后添加它们,如下所示:
d1 = "1490917274.299"
一定要在解析时使用float
而不是int
datetime.datetime.fromtimestamp(float(d1)).strftime('%c')
我有以下简单脚本(使用 Python 3.4)。
from dateutil.parser import *
import datetime
d1 = "1490917274299"
d2 = "1490917274"
1) 执行datetime.datetime.fromtimestamp(int(d1)).strftime('%c')
时出现以下错误:
>>> datetime.datetime.fromtimestamp(int(d1)).strftime('%c')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OverflowError: timestamp out of range for platform time_t
2) 我读到避免毫秒可以解决问题。所以我除以 d1/1000
得到 d2
瞧!
>>> datetime.datetime.fromtimestamp(int(d2)).strftime('%c')
'Thu Mar 30 23:41:14 2017'
3) 但是,如果我想使用 parse(d2)
,我会得到一个错误。
>>> parse(d2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.4/dist-packages/dateutil/parser.py", line 1168, in parse
return DEFAULTPARSER.parse(timestr, **kwargs)
File "/usr/local/lib/python3.4/dist-packages/dateutil/parser.py", line 578, in parse
if cday > monthrange(cyear, cmonth)[1]:
File "/usr/lib/python3.4/calendar.py", line 121, in monthrange
day1 = weekday(year, month, 1)
File "/usr/lib/python3.4/calendar.py", line 113, in weekday
return datetime.date(year, month, day).weekday()
ValueError: year is out of range
4) 如果你尝试 parse(d1)
你也会得到一个错误:
>>> parse(d1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.4/dist-packages/dateutil/parser.py", line 1168, in parse
return DEFAULTPARSER.parse(timestr, **kwargs)
File "/usr/local/lib/python3.4/dist-packages/dateutil/parser.py", line 578, in parse
if cday > monthrange(cyear, cmonth)[1]:
File "/usr/lib/python3.4/calendar.py", line 121, in monthrange
day1 = weekday(year, month, 1)
File "/usr/lib/python3.4/calendar.py", line 113, in weekday
return datetime.date(year, month, day).weekday()
OverflowError: Python int too large to convert to C long
5) 最后,如果您在 https://www.epochconverter.com/ 中使用 d1
,您将正确获得预期日期。
为什么会发生这种情况?我只是想要一种通过使用 parse()
检查字符串是否为日期时间的方法但是不起作用 eventhouhg 纪元字符串很好(至少 d2)。
另一方面,为什么 d1 作为纪元不好?
谢谢!
卢卡斯
你现在可能已经想通了,但无论如何:
dateutil.parser 的 parse
函数无法解析 Unix 时间 — See this somewhat related issue.
这样就回答了 3 和 4。
现在继续 1 和 2。您无法解析 d1 的原因是因为它不是 Unix 时间。 Unix 时间定义为自 1970 年 1 月 1 日星期四 00:00:00 协调世界时 (UTC) 以来经过的 秒数 减去闰秒数从那时起(感谢维基百科!)。如果要包含指定的毫秒数,请在小数点后添加它们,如下所示:
d1 = "1490917274.299"
一定要在解析时使用float
而不是int
datetime.datetime.fromtimestamp(float(d1)).strftime('%c')