我有一个包含大约 400 个时间系列的列表 start_time,我将其转换为以下形式的时间格式:
(2017, 10, 22, 15, 19, 23)
(2017, 10, 22, 15, 19, 35)
(2017, 10, 22, 15, 19, 35)
(2017, 10, 22, 15, 19, 35)
..
..
..
这次是太平洋夏令时。我想以 UTC 时区转换此列表。
我必须将列表转换为时间格式的代码:
y=[tuple(dparse(x).timetuple())[:6] for x in start_time]
我必须将其转换为 UTC 的代码:
y_utc = [tuple(dparse(x).astimezone(pytz.utc).timetuple())[:6] for x in y]
使用这个我得到错误:
TypeError: Parser must be a string or character stream, not tuple
我也尝试过使用钟摆:
y = [pendulum.parse(x, tz='US/Pacific') for x in start_time]
y_utc = [z.astimezone("UTC") for z in y
但是内核要么停止,要么显示 astimezone 不可迭代。
所以我需要 utc 时区的最终列表。
最佳答案
这次尝试使用了datetime
和pytz
模块。首先将它们解析为“US/Pacific”,然后简单地将它们转换为 UTC
。此外,您需要解压元组(使用 *),然后将它们提供给 datetime
构造函数。
from datetime import datetime
import pytz
time_stamps = [(2017, 10, 22, 15, 19, 23), (2017, 10, 22, 15, 19, 35)]
my_timezone = pytz.timezone('US/Pacific')
for time_stamp in time_stamps:
date = datetime(*time_stamp).replace(tzinfo=my_timezone)
print (date.astimezone(pytz.utc))
关于python - 将 PDT 中的时间列表转换为 python 中的 UTC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48220819/