Skip to content

Commit 6bfb321

Browse files
committed
datetime: fix _fromtimestamp for small-range floats
A typical timestamp is 1614803568. A CircuitPython float's granularity at this magnitude is several minutes! By avoiding the use of modf when t is an integer, the `datetime.now()` classmethod is usable, i.e., it reutnrs a time that increases from second to second instead of mostly being stuck and occasionally jumping forward.
1 parent 99f35da commit 6bfb321

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

adafruit_datetime.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1283,7 +1283,10 @@ def _fromtimestamp(cls, t, utc, tz):
12831283
"""Construct a datetime from a POSIX timestamp (like time.time()).
12841284
A timezone info object may be passed in as well.
12851285
"""
1286-
frac, t = _math.modf(t)
1286+
if isinstance(t, float):
1287+
frac, t = _math.modf(t)
1288+
else:
1289+
frac, t = 0, t
12871290
us = round(frac * 1e6)
12881291
if us >= 1000000:
12891292
t += 1

0 commit comments

Comments
 (0)