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
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?
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.
I found this on Wikipedia site "https://en.wikipedia.org/wiki/Tz_database" under the section "Example zone and rule lines"
Zone NAME GMTOFF RULES FORMAT [UNTIL]
Zone America/New_York -4:56:02 - LMT 1883 Nov 18 12:03:58
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.
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?
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