首页 > 解决方案 > Timedelta returned by tzinfo object

问题描述

I'm studying datetimes in python and have come to timezones. I created a Pacific timezone with a timedelta

pacific = datetime.timezone(datetime.timedelta(hours=8))

Then, I created an aware datetime object which I converted to pacific with the tzinfo object

aware = datetime.datetime(2020,12,20,11, tzinfo=pacific)

aware returns a timedelta and I would like to know what it's calculating. It's -1, 57600

print(aware)

>>> datetime.datetime(2020,12,20,11,0, tzinfo=datetime.timezone(datetime.timedelta(-1,57600))

标签: pythontimezone

解决方案


这里发生的是有两种方法来表示时区偏移。

您可以用 28800 秒的时间增量(比 UTC 时间晚 8 小时)来表示它。或者等效地,您可以通过返回 1 天(-1)然后添加 57600 秒来表示它。这也为太平洋地区的 UTC 提供了 28800 秒(8 小时)的时间。


推荐阅读