将float转换为datetime时参数无效

时间:2017-03-29 16:49:40

标签: python datetime time

我必须使用Python从浮点数中获取import time print (time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(float(63650571169.50261)))) 。我是tryint使用以下代码:

OSError: [Errno 22] Invalid argument

但是在执行此代码时,我收到错误:

sys.float_info.max

1.7976931348623157e+308显示datetime

如何将此浮点数转换为router.forRoot({hash: true})

2 个答案:

答案 0 :(得分:1)

看起来您的操作系统在第0年使用带有纪元的ISO 8601:2004 (clause 4.3.2.1 The Gregorian calendar)。可以通过应用正确的零偏移来转换:

<强>代码:

$access_key         = 'my-access-key';
$secretKey          = 'my-secret-key';

测试代码:

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))

答案 1 :(得分:0)

试试这个:

import datetime
print datetime.datetime.fromtimestamp(63650571169.50261)
#3987-01-03 05:12:49.502609