年份和年份的时间戳?

时间:2017-07-04 16:10:55

标签: python date timestamp

我必须从Python 2.7中的项目列表中推断出时间戳编号。

对于每个项目,我都有年份和创建年份的日期,例如:

year = 2015
days = 154

我没有关于小时,分钟,秒和毫秒的任何数据,因此我假设它们都等于0

有人可以帮我推断时间戳吗?

1 个答案:

答案 0 :(得分:1)

提供一年,您可以指定日期,月份和年份的日期。但要获得时间戳,您仍需要小时,分钟,秒,毫秒和时区。

在下面的示例中,我将时间字段(小时/分钟/秒/毫秒)设置为零,将时区设置为UTC:

from datetime import datetime
from pytz import timezone

year = 2017
day_of_year = 154
# strptime sets the hours/minutes/secs to zero
d = datetime.strptime("%s %s" % (year, day_of_year), "%Y %j").replace(tzinfo = pytz.utc)
print(d.timestamp())
print(d)

日期d将为2017-06-03 00:00:00+00:00,相应的时间戳将为1496448000 - 来自unix世纪的{strong>秒的数量<{1}} 。如果您希望时间戳为毫秒,只需将此值乘以1000即可。

虽然我使用了 UTC ,但您可以将其更改为使用最适合您情况的时区。

相关问题