Commit 35414135 authored by CharlieC's avatar CharlieC
Browse files

Add a timedelta with milliseconds and simplify rounding.

--HG--
branch : 3.0
parent 6864e4886592
Pipeline #18757 passed with stage
in 3 minutes and 21 seconds
......@@ -92,13 +92,15 @@ def from_excel(value, epoch=WINDOWS_EPOCH, timedelta=False):
"""Convert Excel serial to Python datetime"""
if value is None:
return
if timedelta:
td = datetime.timedelta(days=value)
if td.microseconds:
# round to millisecond precision
td += datetime.timedelta(microseconds=-td.microseconds +
round(td.microseconds/1000)*1000)
td = datetime.timedelta(seconds=int(td.total_seconds()),
microseconds=round(td.microseconds, -3))
return td
day, fraction = divmod(value, 1)
diff = datetime.timedelta(milliseconds=round(fraction * SECS_PER_DAY * 1000))
if 0 <= value < 1 and diff.days == 0:
......
......@@ -135,6 +135,7 @@ def test_from_excel(value, expected):
(61.5, timedelta(days=61, hours=12)),
(0.9999999995, timedelta(days=1)),
(1.0000000005, timedelta(days=1)),
(1.0000026378, timedelta(days=1, microseconds=228000)),
(None, None),
])
def test_from_excel_timedelta(value, expected):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment