我必须使用
Python从浮点数获取日期时间.我是tryint使用以下代码:
import time
print (time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(float(63650571169.50261))))
但是在执行此代码时,我收到一个错误:
OSError: [Errno 22] Invalid argument
sys.float_info.max显示1.7976931348623157e 308.
如何将此浮点数转换为日期时间?
码:
import datetime as dt
# define some constants
epoch = dt.datetime(1970, 1, 1)
gregorian_8601_to_unix_epoch = 62167305600
def gregorian_8601_to_datetime(gregorian_8601_seconds):
# get number of seconds from epoch
from_epoch = gregorian_8601_seconds - gregorian_8601_to_unix_epoch
# convert to python datetime
return epoch + dt.timedelta(seconds=from_epoch)
测试代码:
print(gregorian_8601_to_datetime(63650571169.50261))
结果:
2017-01-01 10:12:49.502609