Python datetime 和 tzinfo 对象(更改分钟而不是小时)
Python datetime and tzinfo objects (changing minutes instead of hours)
我正在尝试将 tzinfo 应用于日期时间对象。
In [1]: from datetime import datetime
In [2]: import pytz
In [3]: london = pytz.timezone("Europe/London")
In [4]: london
Out[5]: <DstTzInfo 'Europe/London' LMT-1 day, 23:59:00 STD>
In [6]: localized_date_object = datetime(2016, 1, 1, 11, 30, 0, 5000, london)
In [7]: localized_date_object
Out[8]: datetime.datetime(2016, 1, 1, 11, 30, 0, 5000, tzinfo=<DstTzInfo 'Europe/London' LMT-1 day, 23:59:00 STD>)
In [9]: utc_date_object = localized_date_object.astimezone(pytz.utc)
In [10]: utc_date_object
Out[11]: datetime.datetime(2016, 1, 1, 11, 31, 0, 5000, tzinfo=<UTC>)
In [16]: paris = pytz.timezone("Europe/Paris")
In [17]: localized_date_object = datetime(2016, 1, 1, 11, 30, 0, 5000, paris)
In [18]: utc_date_object = localized_date_object.astimezone(pytz.utc)
In [19]: utc_date_object
Out[19]: datetime.datetime(2016, 1, 1, 11, 21, 0, 5000, tzinfo=<UTC>)
如您所见,它将增量应用于分钟而不是小时。
谁能解释一下我做错了什么。
在第 5 行中,它显示了一个奇怪的输出 - <DstTzInfo 'Europe/London' LMT-1 day, 23:59:00 STD>
似乎是一分钟班次(减去 1 天 + 23:59:00 小时)。
我建议您在 pytz 中尝试其他几个时区定义以查看它们的声明。
时区 Europe/London 晚 UTC 1 分钟。时区 Europe/Paris 比 UTC 早 9 分钟。伦敦和巴黎在地理上靠近格林威治,所以时差很小。
如果你尝试
pytz.timezone("Asia/Shanghai")
,您会看到营业时间发生变化。
我认为巴黎时间应该使用 CET,伦敦时间应该使用 UTC。
我使用的方法有点不同,但对我有用:
from datetime import datetime
from pytz import timezone
ldo = datetime(2016, 1, 1, 11, 30, 0, 5000)
ldo = ldo.replace(tzinfo=timezone('Europe/London'))
udo = ldo.astimezone(timezone('UTC'))
print ldo
print udo
ldo = datetime(2016, 1, 1, 11, 30, 0, 5000)
ldo = ldo.replace(tzinfo=timezone('CET'))
udo = ldo.astimezone(timezone('UTC'))
print ldo
print udo
更新:
当您存储时间值时,还应该存储相关的时区信息。 IMO 最佳做法是将所有内容存储在 UTC 中并转换为 "user" 时区以供查看。顺便说一句,从 UTC 转换为 Europe/Paris 工作完美,试试这个:
winter = datetime(2016, 1, 1, 11, 30, 0, 5000, tzinfo=timezone("UTC"))
paris = winter.astimezone(timezone("Europe/Paris"))
print paris
# 2016-01-01 12:30:00.005000+01:00
summer = datetime(2016, 6, 1, 11, 30, 0, 5000, tzinfo=timezone("UTC"))
paris = summer.astimezone(timezone("Europe/Paris"))
print paris
# 2016-06-01 13:30:00.005000+02:00
pytz 文档说:
This library only supports two ways of building a localized time. The first is to use the localize() method provided by the pytz library. This is used to localize a naive datetime (datetime with no timezone information):
The second way of building a localized time is by converting an existing localized time using the standard astimezone() method:
Unfortunately using the tzinfo argument of the standard datetime constructors ‘’does not work’’ with pytz for many timezones.
在提供的代码示例中,您尝试使用 tzinfo
参数而不是 localize()
>>> london = pytz.timezone("Europe/London")
>>> datetime.datetime(2016, 1, 1, 11, 30, 0, 5000, london) # This is incorrect
datetime.datetime(2016, 1, 1, 11, 30, 0, 5000, tzinfo=<DstTzInfo 'Europe/London' LMT-1 day, 23:59:00 STD>)
>>> london.localize(datetime.datetime(2016, 1, 1, 11, 30, 0, 5000)) # This is correct
datetime.datetime(2016, 1, 1, 11, 30, 0, 5000, tzinfo=<DstTzInfo 'Europe/London' GMT0:00:00 STD>)
根据当时商定的标准,与 UTC 的偏移量通常有不止一种定义。
当使用带时区的数据时间函数构造日期时间时,使用哪种转换不是很聪明,并且往往会默认为旧的历史偏移量,在您的示例中为一分钟偏移量。它这样做是因为它在考虑要转换的日期之前选择时区增量。
此替代方法解决了提供更可靠(现代标准)结果的问题:
dt = mytz.localize(日期时间(2020, 4, 7, 16, 30))
我正在尝试将 tzinfo 应用于日期时间对象。
In [1]: from datetime import datetime
In [2]: import pytz
In [3]: london = pytz.timezone("Europe/London")
In [4]: london
Out[5]: <DstTzInfo 'Europe/London' LMT-1 day, 23:59:00 STD>
In [6]: localized_date_object = datetime(2016, 1, 1, 11, 30, 0, 5000, london)
In [7]: localized_date_object
Out[8]: datetime.datetime(2016, 1, 1, 11, 30, 0, 5000, tzinfo=<DstTzInfo 'Europe/London' LMT-1 day, 23:59:00 STD>)
In [9]: utc_date_object = localized_date_object.astimezone(pytz.utc)
In [10]: utc_date_object
Out[11]: datetime.datetime(2016, 1, 1, 11, 31, 0, 5000, tzinfo=<UTC>)
In [16]: paris = pytz.timezone("Europe/Paris")
In [17]: localized_date_object = datetime(2016, 1, 1, 11, 30, 0, 5000, paris)
In [18]: utc_date_object = localized_date_object.astimezone(pytz.utc)
In [19]: utc_date_object
Out[19]: datetime.datetime(2016, 1, 1, 11, 21, 0, 5000, tzinfo=<UTC>)
如您所见,它将增量应用于分钟而不是小时。
谁能解释一下我做错了什么。
在第 5 行中,它显示了一个奇怪的输出 - <DstTzInfo 'Europe/London' LMT-1 day, 23:59:00 STD>
似乎是一分钟班次(减去 1 天 + 23:59:00 小时)。
我建议您在 pytz 中尝试其他几个时区定义以查看它们的声明。
时区 Europe/London 晚 UTC 1 分钟。时区 Europe/Paris 比 UTC 早 9 分钟。伦敦和巴黎在地理上靠近格林威治,所以时差很小。
如果你尝试
pytz.timezone("Asia/Shanghai")
,您会看到营业时间发生变化。
我认为巴黎时间应该使用 CET,伦敦时间应该使用 UTC。 我使用的方法有点不同,但对我有用:
from datetime import datetime
from pytz import timezone
ldo = datetime(2016, 1, 1, 11, 30, 0, 5000)
ldo = ldo.replace(tzinfo=timezone('Europe/London'))
udo = ldo.astimezone(timezone('UTC'))
print ldo
print udo
ldo = datetime(2016, 1, 1, 11, 30, 0, 5000)
ldo = ldo.replace(tzinfo=timezone('CET'))
udo = ldo.astimezone(timezone('UTC'))
print ldo
print udo
更新:
当您存储时间值时,还应该存储相关的时区信息。 IMO 最佳做法是将所有内容存储在 UTC 中并转换为 "user" 时区以供查看。顺便说一句,从 UTC 转换为 Europe/Paris 工作完美,试试这个:
winter = datetime(2016, 1, 1, 11, 30, 0, 5000, tzinfo=timezone("UTC"))
paris = winter.astimezone(timezone("Europe/Paris"))
print paris
# 2016-01-01 12:30:00.005000+01:00
summer = datetime(2016, 6, 1, 11, 30, 0, 5000, tzinfo=timezone("UTC"))
paris = summer.astimezone(timezone("Europe/Paris"))
print paris
# 2016-06-01 13:30:00.005000+02:00
pytz 文档说:
This library only supports two ways of building a localized time. The first is to use the localize() method provided by the pytz library. This is used to localize a naive datetime (datetime with no timezone information):
The second way of building a localized time is by converting an existing localized time using the standard astimezone() method:
Unfortunately using the tzinfo argument of the standard datetime constructors ‘’does not work’’ with pytz for many timezones.
在提供的代码示例中,您尝试使用 tzinfo
参数而不是 localize()
>>> london = pytz.timezone("Europe/London")
>>> datetime.datetime(2016, 1, 1, 11, 30, 0, 5000, london) # This is incorrect
datetime.datetime(2016, 1, 1, 11, 30, 0, 5000, tzinfo=<DstTzInfo 'Europe/London' LMT-1 day, 23:59:00 STD>)
>>> london.localize(datetime.datetime(2016, 1, 1, 11, 30, 0, 5000)) # This is correct
datetime.datetime(2016, 1, 1, 11, 30, 0, 5000, tzinfo=<DstTzInfo 'Europe/London' GMT0:00:00 STD>)
根据当时商定的标准,与 UTC 的偏移量通常有不止一种定义。
当使用带时区的数据时间函数构造日期时间时,使用哪种转换不是很聪明,并且往往会默认为旧的历史偏移量,在您的示例中为一分钟偏移量。它这样做是因为它在考虑要转换的日期之前选择时区增量。
此替代方法解决了提供更可靠(现代标准)结果的问题:
dt = mytz.localize(日期时间(2020, 4, 7, 16, 30))