Search code examples
pythondatetimetimezonepytz

Why does creating a datetime with a tzinfo from pytz show a weird time offset?


Can someone explain me why I do not get the same result in those?

import datetime,pytz
var1 = datetime.datetime(2017,10,25,20,10,50,tzinfo=pytz.timezone("Europe/Athens")))
print(var1)

The output of this code is: 2017-10-25 20:10:50+01:35

import datetime,pytz
var1 = datetime.datetime(2017,10,25,20,10,50)
var1 = pytz.timezone("Europe/Athens").localize(var1)
print(var1)

The output of this code is: 2017-10-25 20:10:50+03:00

My question is why they have different timezones (1:35 and 3:00). I know that the second code is true because my UTC is 3:00. But can you tell me why I am getting 1:35 in the first one?


Solution

  • There is no problem, datetime just happily reports the offset of the tzinfo in whatever reference frame.

    By default pytz.timezone doesn't give the UTC offset but the LMT (local mean time) offset:

    >>> pytz.timezone("Europe/Athens")
    <DstTzInfo 'Europe/Athens' LMT+1:35:00 STD>
    #                          ^^^-------------------- local mean time
    

    However when you localize it:

    >>> var1 = datetime.datetime(2017,10,25,20,10,50)
    >>> var1 = pytz.timezone("Europe/Athens").localize(var1)
    >>> var1.tzinfo
    <DstTzInfo 'Europe/Athens' EEST+3:00:00 DST>
    #                          ^^^^-------------------- eastern european summer time
    

    A different offset is now reported, this time based on the EEST.