Search code examples
windowstimezonepytz

Change system timezone with Pytz


I have a Timezone in the following format: country/city (for example: America/Sao_Paulo). I want to change with python my system's timezone (Win7) with it, is it possible to do it with Pytz and Datetime, or at all ?

Thank you !


Solution

  • In general, no. There is not a direct Python way to do this.

    It could be done, but you would have to jump through a lot of hoops:

    • Use CLDR data to translate the IANA zone to a Windows zone id.
      • Ex. America/Sao_Paulo => E. South America Standard Time
      • (See the Databases section of the timezone tag wiki for more details.)
    • Get the appropriate Win32 security permissions.
    • Call into the Win32 SetTimeZone API

    Caling Win32 functions from Python is done via ctypes.

    Here's a walkthrough of which Win32 permissions and calls to make, although it's in C# in this sample. You will need to translate for Python.

    Sounds like a lot of work to me. Not sure why you would want to do this.