Skip to content

Commit 8705c9d

Browse files
Scale fractional timestamp seconds to microseconds
Signed-off-by: Ali Zulfiqar <codewithfourtix@gmail.com>
1 parent a109ca4 commit 8705c9d

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

src/commoncode/timeutils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def tstamp2time(stamp):
8686
except Exception:
8787
microsec = None
8888
if microsec:
89-
microsec = int(microsec)
89+
microsec = int(microsec.ljust(6, "0")[:6])
9090
if 0 <= microsec <= 999999:
9191
datim = datim.replace(microsecond=microsec)
9292
return datim

tests/test_timeutils.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,3 +192,18 @@ def test_tstamp2time9(self):
192192

193193
def test_tstamp2time_raise(self):
194194
self.assertRaises(ValueError, tstamp2time, "201011A12T13:14:15Z")
195+
196+
197+
def test_tstamp2time_scales_fractional_seconds(self):
198+
for fraction, expected in (
199+
("1", 100000),
200+
("12", 120000),
201+
("123", 123000),
202+
("001", 1000),
203+
("123456", 123456),
204+
("1234567", 123456),
205+
):
206+
for prefix in ("2010-11-12T13:14:15", "20101112T131415"):
207+
with self.subTest(fraction=fraction, prefix=prefix):
208+
result = tstamp2time(f"{prefix}.{fraction}Z")
209+
assert result.microsecond == expected

0 commit comments

Comments
 (0)