Python pytz时区转换返回的值与不同日期的时区偏移量不同 [英] Python pytz timezone conversion returns values that differ from timezone offset for different dates

查看:58
本文介绍了Python pytz时区转换返回的值与不同日期的时区偏移量不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将美国/东部时区日期转换为UTC,以上传到仅接受UTC时间,但以当地时区显示时间的网站。我有以下代码示例,其中当我转换最近的日期时,偏移量(5:00或4:00)没有问题,但是当我转换日期(如1900-01-01)时,偏移量变成4:56等。

I was trying to convert US/Eastern timezone dates to UTC to upload to a website that only accepts UTC times, but displays time in local timezone. I have the following code example where when I convert a recent date, there is no issue with the offset (5:00 or 4:00), but when I convert a date such as 1900-01-01, the offset becomes something around 4:56 etc.

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。为什么会这样呢?这是正确的吗?

I am expecting 1900-01-01 05:00:00+00:00 in case of the first date as well. Why is this happening? Is this correct?

请务必注意,当我将UTC时间 1900-01-01 04:56:00 + 00:00转换回美国时/东部时间,它将转换回 1900-01-01 00:00:00。因此,相差几分钟的时间必须有一定的逻辑。

It is important to note that when I convert the UTC Time "1900-01-01 04:56:00+00:00" back to US/Eastern time, it converts back to "1900-01-01 00:00:00". So, the difference of a few minutes must have some logic behind it.

我在Wikipedia网站 https://en.wikipedia.org/wiki/Tz_database 在示例区域和规则行部分下

I found this on Wikipedia site "https://en.wikipedia.org/wiki/Tz_database" under the section "Example zone and rule lines"

区域名称GMTOFF规则格式[UNTIL]

Zone NAME GMTOFF RULES FORMAT [UNTIL]

美国/纽约特区-4:56:02-LMT 1883 Nov 18 12:03:58

Zone America/New_York -4:56:02 - LMT 1883 Nov 18 12:03:58

我不一定了解他们在说什么,但我在那看到了偏移量4:56。我觉得我更接近答案了,但需要一些帮助。

I don't necessarily understand what they were talking about, but I see the offset 4:56 there. I feel that I am closer to an answer but need some help.

更新:我在Mac book pro上尝试了zdump,并获得了1901年的输出。无法获取之前几年的详细信息。我已经研究了tz数据库中提供的详细信息,但是找不到确切的答案。就我当前的项目而言,我使用了一个替代的区域转换器模块(摆锤),该模块并非真实地记录历史,而是遵循当前的夏令时周期,从而解决了我的问题。但是,问题仍然存在。为什么在东部时区和UTC之间转换1900年以前的日期时,时间偏移会不同于5:00/4:00小时?

Update: I tried the zdump on my Mac book pro, and got the output from the year 1901. Could not, get the details for the years before that. I have researched the details provided in the tz database, but could not find a definitive answer. For the purpose of my current project, I used an alternative zone converter module (pendulum) that doesn't try to be truthful to the history but follows the current daylight savings cycle which resolved my issue. But, the question remains. Why is there a time offset different from 5:00/4:00 hours when converting dates before the year 1900 between Eastern time zone and UTC?

推荐答案

假设您使用的是Linux或BSD,如果运行'zdump -v US / Eastern',则将看到转换的完整列表。直到1918年,美国才转换为整时制,这可能是由于铁路或电报的需要。

Assuming you are on Linux or a BSD, if you run 'zdump -v US/Eastern' you will see the complete list of transitions. It wasn't until 1918 that the US switched to whole hour offsets, probably because of the needs of railroads or telegraph.

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

这篇关于Python pytz时区转换返回的值与不同日期的时区偏移量不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆