[Python] Python doesn't register change in timezone after being started

Discussão em 'Python' iniciado por Stack, Junho 10, 2025.

  1. Stack

    Stack Membro Participativo

    I have the a program running on a Raspberry Pi which, for the purpose of this question, can be boiled down to the following:

    import datetime
    import subprocess

    #Change system timzone
    subprocess.run('sudo timedatectl set-timezone America/Toronto', shell=True)

    #Print date and time and timezone
    print(datetime.datetime.now())


    If I set my timezone to, let's say, Los Angeles, and then run this program, even though subprocess successfully runs the command to change the system timezone, the print command shows the time in the original timezone, as if it wasn't changed.

    Now, if run:

    import datetime

    #Print date and time and timezone
    print(datetime.datetime.now())


    It shows the time with the changed timezone.

    Why does Python (seemingly) not register the timezone changing if it was done while Python was running (using subprocess), and how can I avoid this problem?

    Continue reading...

Compartilhe esta Página