我正在尝试将Unix时间戳转换为AoE时间。我尝试了pytz,但似乎在pytz中没有AoE时间。有什么办法可以做到?
tz = pytz.timezone('AOE')
timestamp_aoe = datetime.fromtimestamp(timestamp, tz).isoformat()
AoE
时区(Anywhere on Earth; UTC-12)未在pytz
时区中定义,参考。 pytz.all_timezones
(v2019.3)。
旁注:datetime.fromtimestamp()
仅接受一个参数;您稍后必须使用例如,指定结果tzinfo
对象的datetime
。 replace()
或localize()
(有关更多信息,请参见here。)>
建议如何处理您的问题
将时间戳转换为UTC datetime
对象。由于AoE是UTC-12,据我所知,它没有DST,因此您可以这样做
from datetime import datetime, timezone ts = 1582013600.5 # timestamp from tz 'AoE', UTC-12 # add 43200 seconds to get a UTC timestamp ts_utcoffset_s = 43200 # 12 h * 60 min * 60 s # ...and convert to datetime object, adding UTC as tzinfo dt = datetime.utcfromtimestamp(ts + ts_utcoffset_s).replace(tzinfo=timezone.utc) print(dt.isoformat()) # 2020-02-18T20:13:20.500000+00:00
由于
pytz
中没有AoE时区,因此您将无法使用tzinfo = AoE创建可识别时区的日期时间对象。您可能得到的只是保存AoE时间但不知道它的原始日期时间对象。我不建议您使用它,因为它可能导致混乱。尽可能坚持使用UTC。