How to check the Windows time zone in real time using Python?

Asked

Viewed 30 times

1

I’m trying to get the time zone of the system in real time using Python, I made the code below:

import tzlocal
tzlocal.get_localzone().zone

The problem is, by changing the Windows time zone, the code keeps getting the same time zone, that is, I open the Python IDLE and run the code, change the time zone in Windows and run the code again in the same IDLE window and keep getting the same result before changing the time zone.

How could I give a "refresh" without having to close and open IDLE?

1 answer

4

That library localtz is not part of Python - it is open source, and its code is here:

https://github.com/regebro/tzlocal

Fortunately it is a fairly simple code, which only does the job of encoding the calls to each operating system, taking the "raw" TZ information, and converting to a format usable by Python

Navigating the code until the part that picks up the timzone in Windows, in this URL, we arrived at this function:

https://github.com/regebro/tzlocal/blob/master/tzlocal/win32.py

def get_localzone():
    """Returns the zoneinfo-based tzinfo object that matches the Windows-configured timezone."""
    global _cache_tz
    if _cache_tz is None:
        _cache_tz = ZoneInfo(get_localzone_name())

    utils.assert_tz_offset(_cache_tz)
    return _cache_tz

And then it becomes apparent why the update doesn’t happen: it this cache variable after the first call.

Then you have two options: you can make the call replicate the call he makes there in your code:

from tzlocal.win32 import Zoneinfo, get_localzone_name

ZoneInfo(get_localzone_name())

Or, before each call to get_localzone() you explicitly delete the cache:

import tzlocal.win32

tzlocal.win32._cache_tz = None

Both ways will work - but you have to keep in mind that: you are using the part directly related to Windows - if you want your program to be independent of the operating system, you need to check and do the equivalent operations on unix.py - and, second, that it is using a form that is not officially documented in the package - then it is subject to that code being changed into another version of tzlocal, and its code may stop working. This second part is resolved by "pinning" the version on your project, if it is a complete project: you determine which versions of tzlocal are accepted, explicitly.

Just to be complete the answer: on the "Unix" side (Linux and Mac OS) is the same thing, one has to reset the cache variable in

import tzlocal.unix
uniz._tz_cache = None

tzlocal.get_localzone()
  • Thanks, but when I do as suggested an error is returned. Valueerror: Timezone offset does not match system offset: -10800 != -14400. Please, check your config files.

  • good - this error is certainly due to that call to utils.assert_tz_offset(_cache_tz) - have to go down in his code (I can’t right now). If instead of deleting the cache, you call ro ZoneInfo the error also happens? (the chance is that lib has one more cache variable in another module, so the error)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.