我试图将美国/东部时区日期转换为UTC,以便上传到仅接受UTC时间但在本地时区显示时间的网站。我有以下代码示例,当我转换最近的日期时,偏移(5:00或4:00)没有问题,但是当我转换日期(例如1900-01-01)时,偏移变为某些4:56等。
import pytz
import tzlocal
from datetime import datetime
usest=tzlocal.get_localzone()#My local zone is US/Eastern. I could directly use that here as pytz.timezone("US/Eastern")
dt=datetime(1900,1,1,0,0,0)
dt_aware=usest.localize(dt,is_dst=True)
utcdate=dt_aware.astimezone(pytz.utc)
print (utcdate)
1900-01-01 04:56:00+00:00
dt1=datetime(2016,1,1,0,0,0)
dt1_aware=usest.localize(dt1,is_dst=True)
utcdate1=dt1_aware.astimezone(pytz.utc)
print (utcdate1)
2016-01-01 05:00:00+00:00
如果是第一次约会,我期待1900-01-01 05:00:00 + 00:00。为什么会这样?这是对的吗?
重要的是要注意,当我转换UTC时间" 1900-01-01 04:56:00 + 00:00"回到美国/东部时间,它转换回" 1900-01-01 00:00:00"。所以,几分钟的差异必然会有一些逻辑。
我在维基百科网站上发现了这一点" https://en.wikipedia.org/wiki/Tz_database"在"示例区域和规则行"
下区域名称GMTOFF规则格式[UNTIL]
Zone America / New_York -4:56:02 - LMT 1883 Nov 18 12:03:58
我不必理解他们在谈论什么,但我看到那里的偏移4:56。我觉得我更接近答案,但需要一些帮助。
更新:我在我的Mac专业版上尝试了zdump,并从1901年获得了输出。但是,在此之前的几年里,我们得到了详细信息。我已经研究了tz数据库中提供的详细信息,但找不到明确的答案。出于我当前项目的目的,我使用了一个替代的区域转换器模块(钟摆),它不会试图对历史真实,但遵循当前的夏令时循环,这解决了我的问题。但问题仍然存在。为什么在东部时区和UTC之间转换1900年之前的日期时,时间偏差不同于5:00/4:00小时?
答案 0 :(得分:0)
假设您使用的是Linux或BSD,如果您运行的是zdump -v US / Eastern'您将看到完整的过渡列表。直到1918年,美国才转向整小时抵消,可能是因为铁路或电报的需求。
US/Eastern Sun Nov 18 16:59:59 1883 UT = Sun Nov 18 12:03:57 1883 LMT isdst=0 gmtoff=-17762
US/Eastern Sun Nov 18 17:00:00 1883 UT = Sun Nov 18 12:00:00 1883 EST isdst=0 gmtoff=-18000
US/Eastern Sun Mar 31 06:59:59 1918 UT = Sun Mar 31 01:59:59 1918 EST isdst=0 gmtoff=-18000
US/Eastern Sun Mar 31 07:00:00 1918 UT = Sun Mar 31 03:00:00 1918 EDT isdst=1 gmtoff=-14400
US/Eastern Sun Oct 27 05:59:59 1918 UT = Sun Oct 27 01:59:59 1918 EDT isdst=1 gmtoff=-14400
US/Eastern Sun Oct 27 06:00:00 1918 UT = Sun Oct 27 01:00:00 1918 EST isdst=0 gmtoff=-18000
US/Eastern Sun Mar 30 06:59:59 1919 UT = Sun Mar 30 01:59:59 1919 EST isdst=0 gmtoff=-18000
US/Eastern Sun Mar 30 07:00:00 1919 UT = Sun Mar 30 03:00:00 1919 EDT isdst=1 gmtoff=-14400
US/Eastern Sun Oct 26 05:59:59 1919 UT = Sun Oct 26 01:59:59 1919 EDT isdst=1 gmtoff=-14400
US/Eastern Sun Oct 26 06:00:00 1919 UT = Sun Oct 26 01:00:00 1919 EST isdst=0 gmtoff=-18000
US/Eastern Sun Mar 28 06:59:59 1920 UT = Sun Mar 28 01:59:59 1920 EST isdst=0 gmtoff=-18000
US/Eastern Sun Mar 28 07:00:00 1920 UT = Sun Mar 28 03:00:00 1920 EDT isdst=1 gmtoff=-14400
US/Eastern Sun Oct 31 05:59:59 1920 UT = Sun Oct 31 01:59:59 1920 EDT isdst=1 gmtoff=-14400
US/Eastern Sun Oct 31 06:00:00 1920 UT = Sun Oct 31 01:00:00 1920 EST isdst=0 gmtoff=-18000
[...]
US/Eastern Sun Mar 8 06:59:59 2015 UT = Sun Mar 8 01:59:59 2015 EST isdst=0 gmtoff=-18000
US/Eastern Sun Mar 8 07:00:00 2015 UT = Sun Mar 8 03:00:00 2015 EDT isdst=1 gmtoff=-14400
US/Eastern Sun Nov 1 05:59:59 2015 UT = Sun Nov 1 01:59:59 2015 EDT isdst=1 gmtoff=-14400
US/Eastern Sun Nov 1 06:00:00 2015 UT = Sun Nov 1 01:00:00 2015 EST isdst=0 gmtoff=-18000
US/Eastern Sun Mar 13 06:59:59 2016 UT = Sun Mar 13 01:59:59 2016 EST isdst=0 gmtoff=-18000
US/Eastern Sun Mar 13 07:00:00 2016 UT = Sun Mar 13 03:00:00 2016 EDT isdst=1 gmtoff=-14400
US/Eastern Sun Nov 6 05:59:59 2016 UT = Sun Nov 6 01:59:59 2016 EDT isdst=1 gmtoff=-14400
US/Eastern Sun Nov 6 06:00:00 2016 UT = Sun Nov 6 01:00:00 2016 EST isdst=0 gmtoff=-18000
US/Eastern Sun Mar 12 06:59:59 2017 UT = Sun Mar 12 01:59:59 2017 EST isdst=0 gmtoff=-18000
US/Eastern Sun Mar 12 07:00:00 2017 UT = Sun Mar 12 03:00:00 2017 EDT isdst=1 gmtoff=-14400
US/Eastern Sun Nov 5 05:59:59 2017 UT = Sun Nov 5 01:59:59 2017 EDT isdst=1 gmtoff=-14400
US/Eastern Sun Nov 5 06:00:00 2017 UT = Sun Nov 5 01:00:00 2017 EST isdst=0 gmtoff=-18000
US/Eastern Sun Mar 11 06:59:59 2018 UT = Sun Mar 11 01:59:59 2018 EST isdst=0 gmtoff=-18000
US/Eastern Sun Mar 11 07:00:00 2018 UT = Sun Mar 11 03:00:00 2018 EDT isdst=1 gmtoff=-14400
US/Eastern Sun Nov 4 05:59:59 2018 UT = Sun Nov 4 01:59:59 2018 EDT isdst=1 gmtoff=-14400
US/Eastern Sun Nov 4 06:00:00 2018 UT = Sun Nov 4 01:00:00 2018 EST isdst=0 gmtoff=-18000
[...]
US/Eastern Sun Mar 9 06:59:59 2498 UT = Sun Mar 9 01:59:59 2498 EST isdst=0 gmtoff=-18000
US/Eastern Sun Mar 9 07:00:00 2498 UT = Sun Mar 9 03:00:00 2498 EDT isdst=1 gmtoff=-14400
US/Eastern Sun Nov 2 05:59:59 2498 UT = Sun Nov 2 01:59:59 2498 EDT isdst=1 gmtoff=-14400
US/Eastern Sun Nov 2 06:00:00 2498 UT = Sun Nov 2 01:00:00 2498 EST isdst=0 gmtoff=-18000
US/Eastern Sun Mar 8 06:59:59 2499 UT = Sun Mar 8 01:59:59 2499 EST isdst=0 gmtoff=-18000
US/Eastern Sun Mar 8 07:00:00 2499 UT = Sun Mar 8 03:00:00 2499 EDT isdst=1 gmtoff=-14400
US/Eastern Sun Nov 1 05:59:59 2499 UT = Sun Nov 1 01:59:59 2499 EDT isdst=1 gmtoff=-14400
US/Eastern Sun Nov 1 06:00:00 2499 UT = Sun Nov 1 01:00:00 2499 EST isdst=0 gmtoff=-18000