Commit 86c6d25f authored by nmartensen's avatar nmartensen
Browse files

write ISO 8601 time strings with millisecond precision

--HG--
branch : 3.0
parent 299bf67d9fb2
Pipeline #18702 failed with stage
in 2 minutes and 46 seconds
......@@ -3,7 +3,7 @@
from openpyxl.compat import safe_string
from openpyxl.xml.functions import Element, SubElement, whitespace, XML_NS, REL_NS
from openpyxl import LXML
from openpyxl.utils.datetime import to_excel, days_to_time
from openpyxl.utils.datetime import to_excel, days_to_time, to_ISO8601
from datetime import timedelta
......@@ -25,7 +25,7 @@ def _set_attributes(cell, styled=None):
if cell.data_type == "d":
if cell.parent.parent.iso_dates and not isinstance(value, timedelta):
value = value.isoformat()
value = to_ISO8601(value)
else:
attrs['t'] = "n"
value = to_excel(value, cell.parent.parent.epoch)
......
......@@ -29,7 +29,9 @@ ISO_REGEX = re.compile(r'''
def to_ISO8601(dt):
"""Convert from a datetime to a timestamp string."""
return datetime.datetime.strftime(dt, ISO_FORMAT)
if hasattr(dt, "microsecond") and dt.microsecond:
return dt.isoformat(timespec="milliseconds")
return dt.isoformat()
def from_ISO8601(formatted_string):
......
......@@ -12,9 +12,17 @@ from datetime import (
import pytest
def test_to_iso():
@pytest.mark.parametrize("value, expected",
[
(datetime(2013, 7, 15, 6, 52, 33), "2013-07-15T06:52:33"),
(datetime(2013, 7, 15, 6, 52, 33, 123456), "2013-07-15T06:52:33.123"),
(date(2013, 7, 15), "2013-07-15"),
(time(0, 1, 42), "00:01:42"),
]
)
def test_to_iso(value, expected):
from ..datetime import to_ISO8601
assert to_ISO8601(datetime(2013, 7, 15, 6, 52, 33)) == "2013-07-15T06:52:33Z"
assert to_ISO8601(value) == expected
@pytest.mark.parametrize("value, group, 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