Handling Timezone for Embedded/IoT Devices in Python

embedded-systemsiotpythontime

I am writing some data collection software (in python) that will transmit data to the cloud. I am including a timestamp (using the standard datetime module included with python) in the data record so that the time is documented in the cloud datastore. It will run on an embedded linux device.

I am between a few architectural ideas for how to handle the timezone of the devices and the accuracy of the timestamps.

My thoughts are that I will use an NTP server to obtain the UTC time for the linux distribution which will provide an accurate time for all the applications running on the device. However I am unsure what I should set the timezone to on the device itself.

My concern is that if I set the timezone to the local time and it happens to be wrong, then not only will the local time be wrong, but when my application gets the UTC time from system time it will also be incorrect, even though it was negotiated previously by the NTP server. The timezone may be set incorrectly by whomever installs the deivce. I could mitigate this by including a GPS module in the device which can negotiate the GPS time to configure the system time, but this seems like extra hardware that would be unnecessary.

The other idea would be to have all devices timezone set to UTC and not change based on the location of the device. Then I can be sure that the UTC timestamps I send to the cloud are accurate, but I lose information about the local time unless I also send location info to the cloud database.

I'm not sure if there is a preferred method for setting system time on IoT devices such as this, but I would like some guidance.

Best Answer

You outline two distinct problems

  1. Find out (on the device) the correct time

    If you are logging data with a timestamp you will want to use the correct UTC datetime. As you note this can be discovered via a NTP server.

  2. Find the local timezone for the device

    Its less clear why you need this information. But it's next to impossible to retrieve as it is in some ways a user preference.

    In theory with a non-movable device, if you can locate the device with enough accuracy you can look up the timezone that applies to that location at that time.

    Since the only real need for local time is to present it to a user, I would simply ask them to choose a timezone.

Related Topic