1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Is the python datetime/pytz module faulty or have I made a simple error somewhere?

Discussão em 'Python' iniciado por Stack, Setembro 14, 2024.

  1. Stack

    Stack Membro Participativo

    I think there may be a problem with the pytz/datetime module in python. Either that or I am not using it correctly.

    Please look at the coding below which highlights the issues I'm currently facing. I've also given a quick synposis below of the 2 issues I'm having trouble solving at the moment.


    1. The first issue I am facing is that python datetime module seems to think that on that date of 1/4/2024, the london timezone is behind UTC timezone by 1 minute! In actual reality on the 1/4/2024, london timezone is 1 hour ahead of UTC because this when the UK is using DST.


    2. The 2nd issue is that the 'astimezone' method seems to incorrectly convert the london time of midnight on the 1/4/24 to 3am on the 1/4/24 in the Eastern European Timezone (EET). The correct coverted time should be 2am on the 1/4/2024.

    Is the python datetime/pytz module faulty or have I made a simple error somewhere?

    Coding:

    from datetime import datetime
    import pytz

    # 1) Creating & outputting datetime object for the date 1/4/2024 in the london timezone
    lndn_timezone=pytz.timezone("Europe/London")
    lndn_datetime=datetime(2024,4,1,0,0,0,tzinfo=lndn_timezone)
    print(lndn_datetime) # This shows that lndn_datetime is 1 minute behind UTC. Why? London time on 1/4/2024 should be UTC+01:00. NOT UTC-00:01 as is displayed.

    # 2) Converting lndn_datetime to a datetime in EET (Eastern European Timezone) and displaying result.
    eet_timezone=pytz.timezone("EET")
    eet_datetime=lndn_datetime.astimezone(eet_timezone)
    print(eet_datetime) # This has incorrectly converted london datetime to eet timezone. The result should be 1/4/2024 2am. NOT 3am as is displayed.

    Continue reading...

Compartilhe esta Página