Unable to modify timezone on BalenaOS

Hi,

I’m trying to modify the timezone of my Balena device and I have been unable to do it so far… I’ve tried using DBus (preferred way), some accepted commands like timedatectl set-timezone America/New_York and the Balena way command from https://github.com/balena-io-playground/balena-timezone/blob/master/start.sh --> dpkg-reconfigure tzdata and and adaptation of that one from Ubuntu forums dpkg-reconfigure --frontend noninteractive tzdata

After trying all 4 ways I’ve been unable to make a single modification, my base image is “intel-nuc-python:3.6.9-run” which uses Ubuntu.

My python code to interact with DBus is:

class Time1ManagerDbus:
    def __init__(self):
        logger.info("Initializing Dbus controller to handle Time")
        self.bus = dbus.SystemBus()
        self.manager_proxy = self.bus.get_object(TIMEDATE1_NAME, TIMEDATE1_NAME_PATH)
        self.manager_interface = dbus.Interface(self.manager_proxy, TIMEDATE1_NAME)
    def set_timezone(self, zone, user_interaction=False):
        try:
            self.manager_interface.SetTimezone(zone, user_interaction)
        except Exception as e:
            logger.error("Error configuring device timezone")
            logger.error(traceback.format_exc())
            return None
        else:
            return True

if __name__ == "__main__":
    time_manager_manager = Time1ManagerDbus()
    time_manager_manager.set_timezone("America/New_York")

And the error that I get is:

2019-11-19 18:11:05,271 - root - ERROR - Traceback (most recent call last):
  File "/src/src/handlers/dbus_manager.py", line 43, in set_timezone
    self.manager_interface.SetTimezone(zone, user_interaction)
  File "/usr/local/lib/python3.6/site-packages/dbus/proxies.py", line 72, in __call__
    return self._proxy_method(*args, **keywords)
  File "/usr/local/lib/python3.6/site-packages/dbus/proxies.py", line 147, in __call__
    **keywords)
  File "/usr/local/lib/python3.6/site-packages/dbus/connection.py", line 653, in call_blocking
    message, timeout)
dbus.exceptions.DBusException: org.freedesktop.DBus.Error.InvalidArgs: Invalid time zone 'America/New_York'

Has anyone ever done something like this? I already checked the available timezones in /usr/share/zoneinfo/zone.tab and all timezones are there, but for some reason, Balena does not accept them.

Thanks in advance,

Hi there,

Just to clarify, are you trying to adjust the time in your container or in the host OS? Presumably you just care about your container, in which case you simply should be able to set an environment variable ENV TZ=America/New_York in your Dockerfile to permanently adjust that for the container.

I have produced a small example below:

❯ cat Dockerfile 
FROM balenalib/intel-nuc-python:3.6.9-run

ENV TZ=America/New_York

CMD ["/bin/date"]
[Logs]    [11/19/2019, 10:55:47 AM] [main] Tue Nov 19 13:55:47 EST 2019

You should not need to mess with DBus or anything on the host OS unless I have misunderstood your use case! Please let us know how this works for you.

Hi @xginn8,

I tried to use DBus because I thought it would be a good idea to always query OS time, not indiviual container time… Setting that in a Dockerfile doesn’t work for my use case, because it has to be set by the user of my device, not all devices with the same Timezone.

I could also set it as a Service Environment Variable, but every time that is changed it will restart my service, resulting in poor user experience or weird anomalies…

I found a way to modify it ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone, now I just need to persist the files /etc/localtime and /etc/timezone. I tried named volumes in my docker-compose but my service never started with the volume configuration below:

manager:
  ...
  volumes:
    - network_manager_local_time:/etc/localtime
    - network_manager_time_zone:/etc/timezone

volumes:
  network_manager_local_time:
  network_manager_time_zone:

Do you have any suggestions?

Thanks in advance,