初始化datetime对象

时间:2022-12-02 11:27:11

The time module can be initialized using seconds since epoch:

时间模块可以用秒初始化,从历元开始:

>>> import time
>>> t1=time.gmtime(1284286794)
>>> t1
time.struct_time(tm_year=2010, tm_mon=9, tm_mday=12, tm_hour=10, tm_min=19, 
                 tm_sec=54, tm_wday=6, tm_yday=255, tm_isdst=0)

Is there an elegant way to initialize a datetime.datetime object in the same way?

是否有一种优雅的方法来初始化一个datetime。datetime对象也是这样吗?

4 个解决方案

#1


284  

datetime.datetime.fromtimestamp will do, if you know the time zone, you could produce the same output as with time.gmtime

datetime.datetime.fromtimestamp将做,如果您知道时区,您可以产生与时间相同的输出。

>>> datetime.datetime.fromtimestamp(1284286794)
datetime.datetime(2010, 9, 12, 11, 19, 54)

or

>>> datetime.datetime.utcfromtimestamp(1284286794)
datetime.datetime(2010, 9, 12, 10, 19, 54)

#2


24  

Seconds since epoch to datetime to strftime:

从epoch到datetime到strftime的时间:

>>> ts_epoch = 1362301382
>>> ts = datetime.datetime.fromtimestamp(ts_epoch).strftime('%Y-%m-%d %H:%M:%S')
>>> ts
'2013-03-03 01:03:02'

#3


12  

From the docs, the recommended way of getting a timezone aware datetime object from seconds since epoch is:

从文档中,建议从epoch以来的秒中获取一个时区感知datetime对象的方法是:

Python 3:

Python 3:

from datetime import datetime, timezone
datetime.fromtimestamp(timestamp, timezone.utc)

Python 2, using pytz:

使用pytz Python 2:

from datetime import datetime
import pytz
datetime.fromtimestamp(timestamp, pytz.utc)

#4


7  

Note that datetime.datetime.fromtimestamp(timestamp) and .utcfromtimestamp(timestamp) fail on windows for dates before Jan. 1, 1970 while negative unix timestamps seem to work on unix-based platforms. The docs say this:

请注意,datetime.datetime.fromtimestamp(timestamp)和.utcfromtimestamp(时间戳)在windows上失败的日期为1970年1月1日,而负面的unix时间戳似乎在基于unix的平台上运行。医生说:

"This may raise ValueError, if the timestamp is out of the range of values supported by the platform C gmtime() function. It’s common for this to be restricted to years in 1970 through 2038"

如果时间戳超出了平台C gmtime()函数支持的值范围,这可能会导致ValueError错误。1970年到2038年,这很常见"

See also Issue1646728

参见Issue1646728

#1


284  

datetime.datetime.fromtimestamp will do, if you know the time zone, you could produce the same output as with time.gmtime

datetime.datetime.fromtimestamp将做,如果您知道时区,您可以产生与时间相同的输出。

>>> datetime.datetime.fromtimestamp(1284286794)
datetime.datetime(2010, 9, 12, 11, 19, 54)

or

>>> datetime.datetime.utcfromtimestamp(1284286794)
datetime.datetime(2010, 9, 12, 10, 19, 54)

#2


24  

Seconds since epoch to datetime to strftime:

从epoch到datetime到strftime的时间:

>>> ts_epoch = 1362301382
>>> ts = datetime.datetime.fromtimestamp(ts_epoch).strftime('%Y-%m-%d %H:%M:%S')
>>> ts
'2013-03-03 01:03:02'

#3


12  

From the docs, the recommended way of getting a timezone aware datetime object from seconds since epoch is:

从文档中,建议从epoch以来的秒中获取一个时区感知datetime对象的方法是:

Python 3:

Python 3:

from datetime import datetime, timezone
datetime.fromtimestamp(timestamp, timezone.utc)

Python 2, using pytz:

使用pytz Python 2:

from datetime import datetime
import pytz
datetime.fromtimestamp(timestamp, pytz.utc)

#4


7  

Note that datetime.datetime.fromtimestamp(timestamp) and .utcfromtimestamp(timestamp) fail on windows for dates before Jan. 1, 1970 while negative unix timestamps seem to work on unix-based platforms. The docs say this:

请注意,datetime.datetime.fromtimestamp(timestamp)和.utcfromtimestamp(时间戳)在windows上失败的日期为1970年1月1日,而负面的unix时间戳似乎在基于unix的平台上运行。医生说:

"This may raise ValueError, if the timestamp is out of the range of values supported by the platform C gmtime() function. It’s common for this to be restricted to years in 1970 through 2038"

如果时间戳超出了平台C gmtime()函数支持的值范围,这可能会导致ValueError错误。1970年到2038年,这很常见"

See also Issue1646728

参见Issue1646728