Commit 1781bccc authored by Jens Vagelpohl's avatar Jens Vagelpohl

- Collector #1569/DateTime: Added a new ISO8601-method that will

  return correctly formatted ISO 8601-representations to augment
  the ISO method which isn't compliant with ISO 8601.
parent e29bbfd4
......@@ -39,6 +39,10 @@ Zope Changes
- Collector #1127: strftime did not take timezone into account.
- Collector #1569/DateTime: Added a new ISO8601-method that will
return correctly formatted ISO 8601-representations to augment
the ISO method which isn't compliant with ISO 8601.
- ZPublisher: changed some hardcoded 'latin1' arguments to 'iso-8859-15'
since latin1 is obsolete.
......
......@@ -443,6 +443,12 @@ def _tzoffset2rfc822zone(seconds):
_tzoffset() is the negative of what time.localzone and time.altzone is."""
return "%+03d%02d" % divmod( (seconds/60), 60)
def _tzoffset2iso8601zone(seconds):
"""Takes an offset, such as from _tzoffset(), and returns an ISO 8601
compliant zone specification. Please note that the result of
_tzoffset() is the negative of what time.localzone and time.altzone is."""
return "%+03d:%02d" % divmod( (seconds/60), 60)
class DateTime:
"""DateTime objects represent instants in time and provide
......@@ -1574,7 +1580,9 @@ class DateTime:
def ISO(self):
"""Return the object in ISO standard format
"""Return the object in ISO standard format. Note:
this is *not* ISO 8601-format! See the ISO8601 and
HTML4 methods below for ISO 8601-compliant output
Dates are output as: YYYY-MM-DD HH:MM:SS
"""
......@@ -1582,6 +1590,24 @@ class DateTime:
self._year, self._month, self._day,
self._hour, self._minute, self._second)
def ISO8601(self):
"""Return the object in ISO 8601-compatible format containing
the date, time with seconds-precision and the time zone
identifier - see http://www.w3.org/TR/NOTE-datetime
Dates are output as: YYYY-MM-DDTHH:MM:SSTZD
T is a literal character.
TZD is Time Zone Designator, format +HH:MM or -HH:MM
The HTML4 method below offers the same formatting, but converts
to UTC before returning the value and sets the TZD "Z"
"""
tzoffset = _tzoffset2iso8601zone(_tzoffset(self._tz, self._t))
return "%0.4d-%0.2d-%0.2dT%0.2d:%0.2d:%0.2d%s" % (
self._year, self._month, self._day,
self._hour, self._minute, self._second, tzoffset)
def HTML4(self):
"""Return the object in the format used in the HTML4.0 specification,
one of the standard forms in ISO8601. See
......
......@@ -272,6 +272,10 @@ class DateTimeTests(unittest.TestCase):
for tbad in '08:00', 'T8:00': #, 'T08:00Z-04:00':
self.assertRaises(DateTime.SyntaxError, DateTime, dgood + tbad)
iso8601_string = '2002-05-02T08:00:00-04:00'
iso8601DT = DateTime(iso8601_string)
self.assertEqual(iso8601_string, iso8601DT.ISO8601())
def testJulianWeek(self):
""" check JulianDayWeek function """
......
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