time.strptime 不解析时区
time.strptime doesn't parse timezone
time.strptime('Sun, 22 May 2022 10:50:14 +0000','%a, %d %b %Y %H:%M:%S %z')
和
time.strptime('Sun, 22 May 2022 10:50:14 +0800','%a, %d %b %Y %H:%M:%S %z')
两者给出相同的结果
time.struct_time(tm_year=2022, tm_mon=5, tm_mday=22, tm_hour=10, tm_min=50, tm_sec=14, tm_wday=6, tm_yday=142, tm_isdst=-1)
并且它们的时间戳相同。
为什么 time.strptime
不解析时区?
我在 windows 11
上使用 python 3.10.4
你用过datetime.strptime方法吗?当我在本地测试时,即使比较来自不同时区的两个时间解析,它似乎也能够考虑时区
>>> from datetime import datetime
>>> t1 = datetime.strptime('Sun, 22 May 2022 10:50:14 +0800','%a, %d %b %Y %H:%M:%S %z')
>>> t2 = datetime.strptime('Sun, 22 May 2022 10:50:14 +0000','%a, %d %b %Y %H:%M:%S %z')
>>> t1 == t2
False
>>> t1
datetime.datetime(2022, 5, 22, 10, 50, 14, tzinfo=datetime.timezone(datetime.timedelta(seconds=28800)))
>>> t2
datetime.datetime(2022, 5, 22, 10, 50, 14, tzinfo=datetime.timezone.utc)
>>> t3 = datetime.strptime('Sun, 22 May 2022 10:50:14 +0000','%a, %d %b %Y %H:%M:%S %z')
>>> t4 = datetime.strptime('Sun, 22 May 2022 18:50:14 +0800','%a, %d %b %Y %H:%M:%S %z')
>>> t3
datetime.datetime(2022, 5, 22, 10, 50, 14, tzinfo=datetime.timezone.utc)
>>> t4
datetime.datetime(2022, 5, 22, 18, 50, 14, tzinfo=datetime.timezone(datetime.timedelta(seconds=28800)))
>>> t3 == t4
True
time.strptime('Sun, 22 May 2022 10:50:14 +0000','%a, %d %b %Y %H:%M:%S %z')
和
time.strptime('Sun, 22 May 2022 10:50:14 +0800','%a, %d %b %Y %H:%M:%S %z')
两者给出相同的结果
time.struct_time(tm_year=2022, tm_mon=5, tm_mday=22, tm_hour=10, tm_min=50, tm_sec=14, tm_wday=6, tm_yday=142, tm_isdst=-1)
并且它们的时间戳相同。
为什么 time.strptime
不解析时区?
我在 windows 11
上使用 python 3.10.4你用过datetime.strptime方法吗?当我在本地测试时,即使比较来自不同时区的两个时间解析,它似乎也能够考虑时区
>>> from datetime import datetime
>>> t1 = datetime.strptime('Sun, 22 May 2022 10:50:14 +0800','%a, %d %b %Y %H:%M:%S %z')
>>> t2 = datetime.strptime('Sun, 22 May 2022 10:50:14 +0000','%a, %d %b %Y %H:%M:%S %z')
>>> t1 == t2
False
>>> t1
datetime.datetime(2022, 5, 22, 10, 50, 14, tzinfo=datetime.timezone(datetime.timedelta(seconds=28800)))
>>> t2
datetime.datetime(2022, 5, 22, 10, 50, 14, tzinfo=datetime.timezone.utc)
>>> t3 = datetime.strptime('Sun, 22 May 2022 10:50:14 +0000','%a, %d %b %Y %H:%M:%S %z')
>>> t4 = datetime.strptime('Sun, 22 May 2022 18:50:14 +0800','%a, %d %b %Y %H:%M:%S %z')
>>> t3
datetime.datetime(2022, 5, 22, 10, 50, 14, tzinfo=datetime.timezone.utc)
>>> t4
datetime.datetime(2022, 5, 22, 18, 50, 14, tzinfo=datetime.timezone(datetime.timedelta(seconds=28800)))
>>> t3 == t4
True