Skip to content

Commit

Permalink
Merge pull request #31 from FoamyGuy/timestamp_remove_microseconds
Browse files Browse the repository at this point in the history
return _mktime() from timestamp()
  • Loading branch information
tannewt authored Jan 17, 2025
2 parents 1795aff + 7315cd7 commit afd11b7
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 12 deletions.
12 changes: 2 additions & 10 deletions adafruit_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -1520,18 +1520,10 @@ def toordinal(self) -> int:
return _ymd2ord(self._year, self._month, self._day)

def timestamp(self) -> float:
"""Return POSIX timestamp as float.
Note that Floats on most boards are encoded in 30 bits
internally, with effectively 22 bits of precision. As a result,
for modern dates this value can be off by several minutes.
As a workaround you can access the function ``_mktime()``
to get an int version of the timestamp.
"""
"""Return POSIX timestamp as int, similar to the value returned by ``time.time()``."""
if not self._tzinfo is None:
return (self - _EPOCH).total_seconds()
s = self._mktime()
return s + self.microsecond / 1e6
return self._mktime()

def weekday(self) -> int:
"""Return the day of the week as an integer, where Monday is 0 and Sunday is 6."""
Expand Down
4 changes: 2 additions & 2 deletions tests/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -527,8 +527,8 @@ def test_utcfromtimestamp(self):
def test_timestamp_naive(self):
t = self.theclass(1970, 1, 1)
self.assertEqual(t.timestamp(), 18000.0)
t = self.theclass(1970, 1, 1, 1, 2, 3, 4)
self.assertEqual(t.timestamp(), 18000.0 + 3600 + 2 * 60 + 3 + 4 * 1e-6)
t = self.theclass(1970, 1, 1, 1, 2, 3)
self.assertEqual(t.timestamp(), 18000.0 + 3600 + 2 * 60 + 3)
# Missing hour
t0 = self.theclass(2012, 3, 11, 2, 30)
t1 = t0.replace(fold=1)
Expand Down

0 comments on commit afd11b7

Please sign in to comment.