Skip to content

datetime.timestamp() returning incorrect time #24

Closed
@EssentiallyNoah

Description

@EssentiallyNoah

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

FoamyGuy commented on Nov 14, 2024

@FoamyGuy
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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @FoamyGuy@EssentiallyNoah

        Issue actions

          datetime.timestamp() returning incorrect time · Issue #24 · adafruit/Adafruit_CircuitPython_datetime