← index #16490Issue #11148
Related · high · value 0.240
QUERY · ISSUE

time.mktime() returns incorrect Unix timestamp on ESP32

openby jurijpopened 2024-12-28updated 2025-11-06
docs

Port, board and/or hardware

sysname='esp32', nodename='esp32', release='1.24.1', version='v1.24.1 on 2024-11-29', machine='Generic ESP32 module with ESP32. Unique ID b'\x94\xe6\x86\x13o$' (not sure what this is

MicroPython version

release='1.24.1', version='v1.24.1 on 2024-11-29',
When using this board and setting time using the ntp library thus ntptime.settime() this synchronizes the RTC to UTC using an NTP server, and it seems to be working correctly since time.localtime() returns (2024, 12, 28, 18, 11, 1, 5, 363) (UTC time). As I understand it, this should reset the real time clock to the current time. If I then set time.mktime(time.localtime()) which should provide the Unix time, it does not, instead is spits out 788726670 which is 1994-12-29 18:44:30 UTC. Can someone please explain this ? Is it a a bug? Bottom line, how can the realtime clock be set to a specific time?

Reproduction

from machine import RTC
import time

Manually set the RTC to a known time (e.g., 2024-01-28 15:30:00 UTC)

rtc = RTC()
rtc.datetime((2024, 1, 28, 0, 15, 30, 0, 0)) # (year, month, day, weekday, hour, minute, second, subseconds)

Get the Unix timestamp

unix_time = time.mktime(time.localtime())

print("Unix time:", unix_time)

Result: Unix time: 759771000 it should be closer to 1735413856.203176

Expected behaviour

Expected the unix time to be determined correctly. That is it should look something like this 1735413022.697727

Observed behaviour

The output is incorrect - see Expected behaviour result above

Additional Information

No, I've provided everything above.

Code of Conduct

Yes, I agree

CANDIDATE · ISSUE

docs: for time.localtime()

closedby hasenradballopened 2023-03-29updated 2024-07-13

Hi Together,

I would remark that this description in the Doc is a little bit missleading.

'''
time.mktime()
https://docs.micropython.org/en/latest/library/time.html
This is inverse function of localtime. It’s argument is a full 8-tuple which expresses a time as per localtime. It returns an integer which is the number of seconds since Jan 1, 2000.
''''

Because on the Pico W the RTC works with the epoch since 1970.
Maybe a small redesign of this description would be perfect, my opinion.

A other point is that I want to aks what the actual status is about:
time.gmtime()
time.localtime()

Is there actually a Timezone information available in micropython?
If not Iam right that these two functions leads to the same result in time structure?

Thanks
Frank

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied