XBC LTE Cat 1 Verizon Micropython UTC Time

My goal is print the UTC time since the custom epoch (2000-01-01 00:00:00 UTC) to the console using micropython. I want to use utime ( https://docs.micropython.org/en/latest/library/utime.html )

micropython script to print UTC time

required packages

import utime, network

obtain cell network connection

conn = network.Cellular()

notify user

print(“starting to connect to network…”)

wait until connected

while not conn.isconnected():
# notify user
print(“waiting for network connection…”)
# add delay
utime.sleep(4)

continual loop to print time

def print_time():
# loop forever
while True:
try:
# fetch number of seconds time since 2001-01-01 epoch in UTC
seconds_since_epoch = utime.time()
# convert seconds to date
utc_tuple = utime.localtime(seconds_since_epoch)
# print
print("Seconds since epoch: " + str(seconds_since_epoch))
print("Time tuple: " + str(utc_tuple))
except Exception as e:
# print failure
print("Failure: " + str(e))
# add delay
utime.sleep(5)

print time

print_time()

###############################################

When this script is run, the output is as follows:

Loading /flash/main.mpy…
Running bytecode…
starting to connect to network…
Seconds since epoch: 611048250
Time tuple: (2019, 5, 13, 7, 37, 30, 0, 133)

Using a epoch converter ( https://www.dcode.fr/timestamp-converter ), I convert the custom epoch to a date format and get the same value as displayed in the tuple.

The problem is that the utime.time() appears to return number of seconds since the epoch with with my current timezone offset (UTC-6) instead of no offset (UTC).

In order to get the actual UTC time since the custom epoch of 2000-01-01 00:00:00 UTC, I have to add my current location’s UTC offset to the total number of seconds. In my case, it is 60 seconds * 60 minutes * 6 hours = 21600 seconds.

The utime documentation ( https://docs.micropython.org/en/latest/library/utime.html ) refers to some variation in the time but the Digi Micropython documentation ( https://www.digi.com/resources/documentation/digidocs/90002219/default.htm#container/cont_examples_time.htm%3FTocPath%3DTime%2520module%2520example%253A%25C2%25A0get%2520the%2520current%2520time|_____0 ) doesn’t provide any specifics.

My question: Is it possible to configure micropython on the cell xbee to return actual UTC time?

The XBee Cellular device gets the time reported by the cell tower. Most of the time this will be your local time. There currently isn’t a way to explicitly ask for the UTC time.

From the uP docs:
“In CPython, this function returns number of seconds since Unix epoch, 1970-01-01 00:00 UTC, as a floating-point, usually having microsecond precision. With MicroPython, only Unix port uses the same Epoch”

https://docs.micropython.org/en/latest/library/utime.html#utime.time

Just a side note:

fetch number of seconds time since 2001-01-01 epoch in UTC

seconds_since_epoch = utime.time()

convert seconds to date

utc_tuple = utime.localtime(seconds_since_epoch)

is equivalent to:
utc_tuple = utime.localtime()

2 Likes

Thank you for the helpful answer, DigiFan23!

You are welcome.

tylerkoldenjtp

We have a XBEE CAT1 connected to AT&T and the tower gives us local time UTC-5. Problem is we were unsure if Daylight savings would change that offset. And if our device is moved to a different timezone then that offset changes so it cant be hard coded.

We tried to get or convert to UTC several ways with no success. We also tried getting UTC time from a NTP server, but it sometimes took a while or would completely fail. We ended up creating a Lambda in AWS to give us the UTC time so we could calculate the offset from Local to UTC.

Hopefully this helps someone in the future.

Thank you, ajsPillar. That is a great idea to create a “lambda clock” which will return the UTC timestamp!