Closed
Description
Running this:
import time
from adafruit_datetime import datetime
dt = datetime(2024, 1, 26, 1, 2, 3)
print(int(dt.timestamp()))
print(time.mktime(dt.timetuple()))
Returns this:
>>> 1706230784
>>> 1706230923
The epoch timestamp from time.mktime() is correct while the timestamp from dt.timestamp() is 139 seconds early. This code is running on the Challenger RP2040 WIFI MkII with CircuitPython 8.2.9.
Activity
FoamyGuy commentedon Nov 14, 2024
I am not positive but I believe the difference is the effect of converting the float coming out of
timestamp()
to an int. I think there is more limited precision in CircuitPython than CPython. As an example removing this library and time concepts all together you can see it with this in the REPL:As a workaround you can call
dt._mktime()
instead ofdt.timestamp()
which will basically give you the int version without making the detour into float type, that appears to yield the same result astime.mktime(dt.timetuple())
in my testing.I'm going to close this as I believe it's a limitation of the platform more than an issue with this library specifically. If you ask in the discord or an issue in the core you may be able to get a better explanation of the specifics from one of the core devs.