Does device configuration set the OS timezone?

I saw in a previous thread that the OS is not timezone aware, but that is back in 2019. It looks like device configuration has a field to specify the timezone now (e.g., America/New_York):

However, a Python service indicates it’s still using UTC.

>>> import time
>>> time.tzname
('UTC', 'UTC')

If that’s expected, is the typical way to handle this a node configuration option? I was wondering if there was a recommended way, since the configuration option could be difficult if you had info-beamer devices in multiple time zones (though I don’t need that for this application).

Edit: Actually this seems to indicate the OS should know the timezone from the device setup. It looks like I’m running stable-0013, which released June 2023, and appears to be the latest from the news. Where do you find the version 19 referenced in that thread?

The timezone you can now assign to each device ends up being provided to the package via the config.json’s __metadata field. Unfortunately the documentation is currently missing for that, but it’s already fixed and will be updated with the next release. You can see the assigned timezone in the generated config.json on the device.

In theory, a package service might be provided with a TZ environment variable containing that value, but that won’t handle changes made to the timezone, as services are not automatically restarted on config change.

If you’re using the hosted.py lib, which I also just pushed to github, the following should work:

from hosted import config
print(config.metadata_timezone)

Thanks for adding the config.metadata_timezone property.

I’m trying to figure out how to get this to work on both my Linux desktop and on the Pi. On the Pi, datetime.now() is in UTC, which can then be converted to EST using localize then astimezone. However, on the desktop, datetime.now() is already in EST, so that approach on the desktop will double adjust.

This is definitely a pretty big edge-case; I don’t think a lot of people are doing most of their development on the desktop. However, I was wondering if there is an obvious solution I’m missing?

Edit: I think I can do something like the following, but I may have underthought how datetimes are encoded all throughout my package. Since all display logic is in local time, I’m converting everything to local, including the UTC data I fetch from an API.

def system_timezone():
    is_dst = time.localtime().tm_isdst
    tzname = time.tzname[is_dst]
    return timezone(tzname)

def determine_now_local(debug_datetime_text, local_tz):
    try:
        # If we can parse the debug datetime, use that as local time.
        fmt = '%Y-%m-%dT%H:%M:%S'
        now = datetime.strptime(debug_datetime_text, fmt)
        return local_tz.localize(now)
    except Exception:
        pass
    
    now = datetime.now()

    if system_timezone() == utc:
        # We are probably on a Raspberry Pi and need to get to local time
        now = utc.localize(now).astimezone(local_tz)
    else:
        now = system_timezone().localize(now)

    return now

One approach would be to run everything in UTC and only convert to local for display purposes. That’s the approach info-beamer takes as it avoids all the weirdness when using local time for calculations. One way to do this is to set the TZ environment variable to UTC:

export TZ=UTC
python stuff.py # time.tzname => ('UTC', 'UTC')
1 Like

export TZ=UTC

That’s working well to make my development environment more like the device, thanks!