← index #16490Issue #4145
Related · high · value 0.983
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

utime.localtime() returns UTC and local time in some cases

closedby lumbricopened 2018-09-15updated 2018-09-24

In Python time.localtime() returns local time, but utime.localtime() usually returns a UTC time stamp, only in the unix port it returns local time. I guess the correct way should be local time of course, but not having a consistent way localtime() behaves between ports seems to be even worse. I don't have a good suggestion how to fix it, except implementing #2130 (and breaking backward compatiblity?). But maybe unix port can be improved as a first step?

(If you consider this ticket part/duplicate of #2130, please close as won't fix.)

Also related: #3087.

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