mktime and localtime do not work as expected

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
gmunari92
Posts: 2
Joined: Fri Jul 02, 2021 7:28 am

mktime and localtime do not work as expected

Post by gmunari92 » Fri Jul 02, 2021 8:55 am

Hello,
I'm using a ESP32 with esp32-20210623-v1.16.bin installed.
For my project I need to use the RTC clock and evaluate the difference between 2 dates.

I run against several problems with utime module, that I do not understand and they are not coherent with the official documentation (https://docs.micropython.org/en/latest/ ... utime.html):

1) Time Epoch used by the system is in a different format:

Code: Select all

(year, month, day, day_of_the_week, hour, minute, second, millisecond) 
instead of

Code: Select all

(year, month, mday, hour, minute, second, weekday, yearday)
this is just strange but not really a problem, but strange.

2) utime.localtime gives are output minutes instead of seconds as stated in the documentation. In fact:

Code: Select all

>>> utime.mktime((2021, 7, 3, 3, 4, 0, 0, 0)) - utime.mktime((2021, 7, 3, 3, 3, 0, 0, 0))
60
3) the localtime of a mktime is not the Epoch itself:

Code: Select all

>>> utime.localtime(utime.mktime((2021, 7, 1, 3, 26, 0, 0, 0)))
(2021, 7, 1, 3, 26, 0, 3, 182)
4) the difference between 2 mktime is correct inside the same day, but not between days:

Code: Select all

>>> utime.mktime((2021, 7, 3, 3, 23, 0, 0, 0)) - utime.mktime((2021, 7, 3, 3, 22, 0, 0, 0))
60
but

Code: Select all

>>> utime.mktime((2021, 7, 4, 4, 1, 0, 0, 0)) - utime.mktime((2021, 7, 3, 3, 23, 0, 0, 0))
88680
5) I can set epoch tuple with wrong weekday and get different values of mktime... how could I let the progam calculate the weekday?

what am I missing here? I'm referring to the wrong documentation?
thanks

Post Reply