Python datetime到utc转换错误

时间:2017-04-27 02:03:35

标签: python python-2.7 datetime

我有05:55:13格式的文件时间,我想在其中添加当前日期2017-04-27。然后我想用UTC格式的grafana,但似乎有四舍五入。这将如何运作?

我为05:55:1305:56:13获得相同的输出。

import datetime
import time

line =  str(datetime.date.today()) + " " + "05:55:13"
naive = datetime.datetime.strptime(line, '%Y-%m-%d %H:%M:%S')
utc_dt = naive.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ')
print naive
print utc_dt

line =  str(datetime.date.today()) + " " + "05:56:13"
naive = datetime.datetime.strptime(line, '%Y-%m-%d %H:%M:%S')
utc_dt = naive.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ')
print naive
print utc_dt

1 个答案:

答案 0 :(得分:0)

您正在呼叫utcnow returns current date and time in UTC timezone。当然你有相同的输出。不使用此方法使用您的代码:

line =  str(datetime.date.today()) + " " + "05:55:13"
naive = datetime.datetime.strptime(line, '%Y-%m-%d %H:%M:%S')
utc_dt = naive.strftime('%Y-%m-%dT%H:%M:%SZ')
print naive
print utc_dt

line =  str(datetime.date.today()) + " " + "05:56:13"
naive = datetime.datetime.strptime(line, '%Y-%m-%d %H:%M:%S')
utc_dt = naive.strftime('%Y-%m-%dT%H:%M:%SZ')
print naive
print utc_dt