Skip to content

datetime.timestamp() returning incorrect time #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
EssentiallyNoah opened this issue Jan 26, 2024 · 1 comment
Closed

datetime.timestamp() returning incorrect time #24

EssentiallyNoah opened this issue Jan 26, 2024 · 1 comment

Comments

@EssentiallyNoah
Copy link

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.

@FoamyGuy
Copy link
Contributor

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:

>>> float(1706230923)
1.70623e+09
>>> int(float(1706230923))
1706230784

As a workaround you can call dt._mktime() instead of dt.timestamp() which will basically give you the int version without making the detour into float type, that appears to yield the same result as time.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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants