问题:如何将time.struct_time对象转换为datetime对象?

如何将Python time.struct_time对象转换为datetime.datetime对象?

我有一个提供第一个库的图书馆,一个提供第二个库的图书馆。

How do you convert a Python time.struct_time object into a datetime.datetime object?

I have a library that provides the first one and a second library that wants the second one.


回答 0

使用time.mktime()将时间元组(以本地时间表示)转换为自大纪元以来的秒数,然后使用datetime.fromtimestamp()获得datetime对象。

from datetime import datetime
from time import mktime

dt = datetime.fromtimestamp(mktime(struct))

Use time.mktime() to convert the time tuple (in localtime) into seconds since the Epoch, then use datetime.fromtimestamp() to get the datetime object.

from datetime import datetime
from time import mktime

dt = datetime.fromtimestamp(mktime(struct))

回答 1

像这样:

>>> structTime = time.localtime()
>>> datetime.datetime(*structTime[:6])
datetime.datetime(2009, 11, 8, 20, 32, 35)

Like this:

>>> structTime = time.localtime()
>>> datetime.datetime(*structTime[:6])
datetime.datetime(2009, 11, 8, 20, 32, 35)

回答 2

这不是您问题的直接答案(已经很好回答了)。但是,由于有几次时间在我的基础上咬了我一口,所以我不能过分强调,您应该仔细查看一下time.struct_time对象提供的内容,而不是其他时间字段可能提供的内容。

假设您同时拥有一个time.struct_time对象和其他一些日期/时间字符串,则将两者进行比较,并确保您不会丢失数据并无意中创建了一个简单的datetime对象,否则您可以这样做。

例如,出色的feedparser模块将返回一个“ published”字段,并可能在其“ published_pa​​rsed”字段中返回一个time.struct_time对象:

time.struct_time(tm_year=2013, tm_mon=9, tm_mday=9, tm_hour=23, tm_min=57, tm_sec=42, tm_wday=0, tm_yday=252, tm_isdst=0)

现在,请注意您在“已发布”字段中实际得到的内容。

Mon, 09 Sep 2013 19:57:42 -0400

斯托曼的胡子!时区信息!

在这种情况下,懒惰的人可能想要使用出色的dateutil模块来保留时区信息:

from dateutil import parser
dt = parser.parse(entry["published"])
print "published", entry["published"])
print "dt", dt
print "utcoffset", dt.utcoffset()
print "tzinfo", dt.tzinfo
print "dst", dt.dst()

这给了我们:

published Mon, 09 Sep 2013 19:57:42 -0400
dt 2013-09-09 19:57:42-04:00
utcoffset -1 day, 20:00:00
tzinfo tzoffset(None, -14400)
dst 0:00:00

然后,可以使用可识别时区的datetime对象将所有时间标准化为UTC或任何您认为很棒的东西。

This is not a direct answer to your question (which was answered pretty well already). However, having had times bite me on the fundament several times, I cannot stress enough that it would behoove you to look closely at what your time.struct_time object is providing, vs. what other time fields may have.

Assuming you have both a time.struct_time object, and some other date/time string, compare the two, and be sure you are not losing data and inadvertently creating a naive datetime object, when you can do otherwise.

For example, the excellent feedparser module will return a “published” field and may return a time.struct_time object in its “published_parsed” field:

time.struct_time(tm_year=2013, tm_mon=9, tm_mday=9, tm_hour=23, tm_min=57, tm_sec=42, tm_wday=0, tm_yday=252, tm_isdst=0)

Now note what you actually get with the “published” field.

Mon, 09 Sep 2013 19:57:42 -0400

By Stallman‘s Beard! Timezone information!

In this case, the lazy man might want to use the excellent dateutil module to keep the timezone information:

from dateutil import parser
dt = parser.parse(entry["published"])
print "published", entry["published"])
print "dt", dt
print "utcoffset", dt.utcoffset()
print "tzinfo", dt.tzinfo
print "dst", dt.dst()

which gives us:

published Mon, 09 Sep 2013 19:57:42 -0400
dt 2013-09-09 19:57:42-04:00
utcoffset -1 day, 20:00:00
tzinfo tzoffset(None, -14400)
dst 0:00:00

One could then use the timezone-aware datetime object to normalize all time to UTC or whatever you think is awesome.


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。