Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/commoncode/timeutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def tstamp2time(stamp):
except Exception:
microsec = None
if microsec:
microsec = int(microsec)
microsec = int(microsec.ljust(6, "0")[:6])
if 0 <= microsec <= 999999:
datim = datim.replace(microsecond=microsec)
return datim
Expand Down
15 changes: 15 additions & 0 deletions tests/test_timeutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,18 @@ def test_tstamp2time9(self):

def test_tstamp2time_raise(self):
self.assertRaises(ValueError, tstamp2time, "201011A12T13:14:15Z")


def test_tstamp2time_scales_fractional_seconds(self):
for fraction, expected in (
("1", 100000),
("12", 120000),
("123", 123000),
("001", 1000),
("123456", 123456),
("1234567", 123456),
):
for prefix in ("2010-11-12T13:14:15", "20101112T131415"):
with self.subTest(fraction=fraction, prefix=prefix):
result = tstamp2time(f"{prefix}.{fraction}Z")
assert result.microsecond == expected
Loading