Skip to content

datetime time offset converting datetime-to-timestamp-back-to-datetime #29

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
schaefer01 opened this issue Jan 15, 2025 · 9 comments
Closed

Comments

@schaefer01
Copy link

schaefer01 commented Jan 15, 2025

hi,

I'm using this: adafruit-circuitpython-bundle-9.x-mpy-20240625 and get a minutes and seconds offset with my conversion
Is this a me problem or a you problem?

>>> from adafruit_datetime import datetime as dt
  ... <creation of object deleted>
>>> print(dtobj) 
2025-01-15 14:09:13                                  -> note: 9 minutes 13 seconds
>>> ts = dtobj.timestamp()
>>> dt.fromtimestamp(ts)
datetime.datetime(2025, 1, 15, 14, 2, 40)  -> note: 2 minutes 40 seconds
>>> dt.fromtimestamp(ts)
datetime.datetime(2025, 1, 15, 14, 2, 40)

thank you for your consideration
bob s.

@FoamyGuy FoamyGuy transferred this issue from adafruit/Adafruit_CircuitPython_Bundle Jan 15, 2025
@FoamyGuy
Copy link
Contributor

@schaefer01 I moved this to the datetime library as the potential issue is with it rather than the bundle at large.

Can you post the contents of the boot_out.txt file on the device you ran this on?

@schaefer01
Copy link
Author

[
boot_out.txt
]
Adafruit CircuitPython 9.0.5 on 2024-05-22; Adafruit Feather RP2040 with rp2040
Board ID:adafruit_feather_rp2040
UID:E461BCC207361A28

@Neradoc
Copy link
Contributor

Neradoc commented Jan 15, 2025

This because dtobj.timestamp() returns a float that is above the values where we lose the precision at the unit level, which in this case is the second.

>>> from adafruit_datetime import datetime as dt
>>> tt = dt.fromtimestamp(1736958434)
>>> tt
datetime.datetime(2025, 1, 15, 16, 27, 14)
>>> tt.timestamp()
1.73696e+09
>>> f"{tt.timestamp():f}"
'1736958464.000000'

This matches the float value of 1736958434.

>>> f"{1736958434.0:f}"
'1736958464.000000'

@FoamyGuy
Copy link
Contributor

Agree'd with Neradoc.

This is essentially the same issue reported in #24

As a workaround you can use dt._mktime() instead of dt.timestamp() to get the timestamp as an int which seems to keep it close enough to get the right datetime down to minute and second.

dt = datetime(2025, 1, 15, 14, 9, 13)
print(dt)

#ts = dt.timestamp()
ts = dt._mktime()
print(f"ts: {ts}")

new_dt = dt.fromtimestamp(ts)
print(new_dt)



code.py output:
2025-01-15 14:09:13
ts: 1736950153
2025-01-15 14:09:13

@Neradoc
Copy link
Contributor

Neradoc commented Jan 15, 2025

We should mention it in the docs.
Borrowing from the explanation on time.monotonic() maybe.

@Neradoc
Copy link
Contributor

Neradoc commented Jan 15, 2025

You get the timestamp from a datetime object. In your code above, dt is the module.

dtobj._mktime()

@schaefer01
Copy link
Author

note: _mktime() takes the datetime object as its parameter, i.e. ts = dt._mktime(dtobj)

@schaefer01
Copy link
Author

This works now, I'll close unless someone want to add more

@FoamyGuy
Copy link
Contributor

Ah sorry about that, I mixed up variable names with module imports in your code.

I do think this can be closed. I'll add a note to the timestamp() documentation about it.

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

3 participants