如何将python时间戳字符串转换为纪元?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30468371/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert python timestamp string to epoch?
提问by Rolando
I have the following string:
我有以下字符串:
mytime = "2009-03-08T00:27:31.807Z"
How do I convert it to epoch in python?
如何在python中将其转换为纪元?
I tried:
我试过:
import time
p = '%Y-%m-%dT%H:%M:%S'
int(time.mktime(time.strptime(s, p)))
But it does not work with the 31.807Z
.
但它不适用于31.807Z
.
回答by Padraic Cunningham
You are missing .%fZ
from your format string.
您.%fZ
的格式字符串中缺少您。
p = '%Y-%m-%dT%H:%M:%S.%fZ'
The correct way to convert to epoch is to use datetime
:
转换为纪元的正确方法是使用datetime
:
from datetime import datetime
p = '%Y-%m-%dT%H:%M:%S.%fZ'
mytime = "2009-03-08T00:27:31.807Z"
epoch = datetime(1970, 1, 1)
print((datetime.strptime(mytime, p) - epoch).total_seconds())
Or call int if you want to ignore fractions.
或者,如果您想忽略分数,请调用 int。
回答by Nayuki
Code:
代码:
import datetime
epoch = datetime.datetime(1970, 1, 1)
mytime = "2009-03-08T00:27:31.807Z"
myformat = "%Y-%m-%dT%H:%M:%S.%fZ"
mydt = datetime.datetime.strptime(mytime, myformat)
val = (mydt - epoch).total_seconds()
print(val)
> 1236472051.81
repr(val)
> '1236472051.807'
Notes:
笔记:
- When using
time.strptime()
, the returnedtime.struct_time
does not support sub-second precision. - The
%f
formatis for microseconds. When parsing it need not be the full 6 digits.
- 使用时
time.strptime()
,返回的time.struct_time
不支持亚秒级精度。 - 该
%f
格式是微秒。解析时不需要是完整的 6 位数字。
回答by Joran Beasley
dateutil is the only library i have found that correctly deals with the timezone offset identitifier (Z
)
dateutil 是我发现的唯一一个正确处理时区偏移标识符 ( Z
) 的库
pip install python-dateutil
then
然后
from dateutil.parser import parse as date_parse
print date_parse("2009-03-08T00:27:31.807Z")
#get timestamp
import calendar
dt = date_parse("2009-03-08T00:27:31.807Z")
timestamp1 = calendar.timegm(dt.timetuple())
回答by jfs
There are two parts:
有两个部分:
- Convert the time string into a broken-down time. See How to parse ISO formatted date in python?
- Convert the UTC time to "seconds since the Epoch" (POSIX timestamp).
- 将时间字符串转换为分解时间。请参阅如何在 python 中解析 ISO 格式的日期?
- 将 UTC 时间转换为“自纪元以来的秒数”(POSIX 时间戳)。
#!/usr/bin/env python
from datetime import datetime
utc_time = datetime.strptime("2009-03-08T00:27:31.807Z", "%Y-%m-%dT%H:%M:%S.%fZ")
epoch_time = (utc_time - datetime(1970, 1, 1)).total_seconds()
# -> 1236472051.807
If you are sure that you want to ignore fractions of a second and to get an integer result:
如果您确定要忽略几分之一秒并获得整数结果:
#!/usr/bin/env python
import time
from calendar import timegm
utc_time = time.strptime("2009-03-08T00:27:31.807Z", "%Y-%m-%dT%H:%M:%S.%fZ")
epoch_time = timegm(utc_time)
# -> 1236472051
To support timestamps that correspond to a leap second such as Wed July 1 2:59:60 MSK 2015
, you could use a combination of time.strptime()
and datetime
(if you care about leap seconds you should take into account the microseconds too).
为了支持时间戳对应于一个闰秒例如Wed July 1 2:59:60 MSK 2015
,你可以使用的组合time.strptime()
以及datetime
(如果你关心闰秒,你应该考虑到微秒太)。