Commit 6b8c77b6 authored by CharlieC's avatar CharlieC
Browse files

Merge branch 'topic/3.0/isoformat_millis' into 'branch/3.0'

write ISO 8601 time strings with millisecond precision

See merge request !399

--HG--
branch : 3.0
Pipeline #18858 passed with stage
in 2 minutes and 54 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)
......
......@@ -3,11 +3,6 @@
import datetime
from openpyxl.compat import safe_string
from openpyxl.utils.datetime import (
CALENDAR_WINDOWS_1900,
to_ISO8601,
from_ISO8601,
)
from openpyxl.descriptors import (
String,
DateTime,
......@@ -34,7 +29,7 @@ class NestedDateTime(DateTime, NestedText):
tagname = "{%s}%s" % (namespace, tagname)
el = Element(tagname)
if value is not None:
el.text = to_ISO8601(value)
el.text = value.isoformat(timespec="seconds") + 'Z'
return el
......
......@@ -78,7 +78,7 @@ def test_from_tree(datadir, SampleProperties):
def test_qualified_datetime():
from ..core import QualifiedDateTime
dt = QualifiedDateTime()
tree = dt.to_tree("time", datetime.datetime(2015, 7, 20, 12, 30))
tree = dt.to_tree("time", datetime.datetime(2015, 7, 20, 12, 30, 00, 123456))
xml = tostring(tree)
expected = """
<time xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="dcterms:W3CDTF">
......@@ -100,7 +100,7 @@ def dcterms_prefix(request):
def test_qualified_datetime_ns(dcterms_prefix):
from ..core import QualifiedDateTime
dt = QualifiedDateTime()
tree = dt.to_tree("time", datetime.datetime(2015, 7, 20, 12, 30))
tree = dt.to_tree("time", datetime.datetime(2015, 7, 20, 12, 30, 00, 987654))
xml = tostring(tree) # serialise to make remove QName
tree = fromstring(xml)
xsi = tree.attrib["{%s}type" % XSI_NS]
......
......@@ -30,7 +30,9 @@ ISO_DURATION = re.compile(r'PT((?P<hours>\d+)H)?((?P<minutes>\d+)M)?((?P<seconds
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,18 @@ 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"),
(time(0, 1, 42, 123456), "00:01:42.123"),
]
)
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