将时间戳从epoch转换为datetime.datetime

时间:2022-01-25 02:47:21

I have the following timestamps since epoch:

我从epoch开始有以下时间戳:

Timestamp
1346114717972
1354087827000

How can I convert these timestamps to some specific output format, e.g., mm/dd/yyyy hr:min:sec?

如何将这些时间戳转换为某种特定的输出格式,例如mm / dd / yyyy hr:min:sec?

I have tried to convert them to datetime.datetime but it failed:

我试图将它们转换为datetime.datetime但它失败了:

 >>> datetime.datetime.fromtimestamp(1346114717972)
 Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
 ValueError: timestamp out of range for platform time_t

How can I do this?

我怎样才能做到这一点?

3 个解决方案

#1


49  

I would use the time module

我会使用时间模块

>>> import time
>>> time.gmtime(1346114717972/1000.)
time.struct_time(tm_year=2012, tm_mon=8, tm_mday=28, tm_hour=0, tm_min=45, tm_sec=17, tm_wday=1, tm_yday=241, tm_isdst=0)

the timestamp is divided by 1000 as the stamps you have provided are in milliseconds since the epoch, not seconds

时间戳除以1000,因为您提供的戳记是自纪元以来的毫秒数,而不是秒

then use strftime to format like so

然后使用strftime来格式化

>>> time.strftime('%m/%d/%Y %H:%M:%S',  time.gmtime(1346114717972/1000.))
'08/28/2012 00:45:17'

#2


35  

Assuming millisecond resolution:

假设毫秒分辨率:

import datetime

s = '1346114717972'
fmt = "%Y-%m-%d %H:%M:%S"

# local time
t = datetime.datetime.fromtimestamp(float(s)/1000.)
print t.strftime(fmt) # prints 2012-08-28 02:45:17

# utc time
t_utc = datetime.datetime.utcfromtimestamp(float(s)/1000.)
print t_utc.strftime(fmt) # prints 2012-08-28 00:45:17

Have a look at the documentation for the strftime() and strptime() behavior.

查看strftime()和strptime()行为的文档。

#3


0  

This is the most simplest method i've ever seen-

这是我见过的最简单的方法 -

$ python
Python 2.7.5 (default, Nov  6 2016, 00:28:07) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import time
>>> print(time.strftime('%Y-%m-%dT%H:%M:%S %Z',time.localtime(time.time())))
2018-05-02T13:21:44 IST
>>>

Check this out.

看一下这个。

#1


49  

I would use the time module

我会使用时间模块

>>> import time
>>> time.gmtime(1346114717972/1000.)
time.struct_time(tm_year=2012, tm_mon=8, tm_mday=28, tm_hour=0, tm_min=45, tm_sec=17, tm_wday=1, tm_yday=241, tm_isdst=0)

the timestamp is divided by 1000 as the stamps you have provided are in milliseconds since the epoch, not seconds

时间戳除以1000,因为您提供的戳记是自纪元以来的毫秒数,而不是秒

then use strftime to format like so

然后使用strftime来格式化

>>> time.strftime('%m/%d/%Y %H:%M:%S',  time.gmtime(1346114717972/1000.))
'08/28/2012 00:45:17'

#2


35  

Assuming millisecond resolution:

假设毫秒分辨率:

import datetime

s = '1346114717972'
fmt = "%Y-%m-%d %H:%M:%S"

# local time
t = datetime.datetime.fromtimestamp(float(s)/1000.)
print t.strftime(fmt) # prints 2012-08-28 02:45:17

# utc time
t_utc = datetime.datetime.utcfromtimestamp(float(s)/1000.)
print t_utc.strftime(fmt) # prints 2012-08-28 00:45:17

Have a look at the documentation for the strftime() and strptime() behavior.

查看strftime()和strptime()行为的文档。

#3


0  

This is the most simplest method i've ever seen-

这是我见过的最简单的方法 -

$ python
Python 2.7.5 (default, Nov  6 2016, 00:28:07) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import time
>>> print(time.strftime('%Y-%m-%dT%H:%M:%S %Z',time.localtime(time.time())))
2018-05-02T13:21:44 IST
>>>

Check this out.

看一下这个。