[Zope-Checkins] SVN: Zope/trunk/ - Collector #1569/DateTime: Added
a new ISO8601-method that will
Jens Vagelpohl
jens at dataflake.org
Tue Nov 9 13:00:19 EST 2004
Log message for revision 28413:
- 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.
Changed:
U Zope/trunk/doc/CHANGES.txt
U Zope/trunk/lib/python/DateTime/DateTime.py
U Zope/trunk/lib/python/DateTime/tests/testDateTime.py
-=-
Modified: Zope/trunk/doc/CHANGES.txt
===================================================================
--- Zope/trunk/doc/CHANGES.txt 2004-11-09 12:37:18 UTC (rev 28412)
+++ Zope/trunk/doc/CHANGES.txt 2004-11-09 18:00:19 UTC (rev 28413)
@@ -39,6 +39,10 @@
- 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.
Modified: Zope/trunk/lib/python/DateTime/DateTime.py
===================================================================
--- Zope/trunk/lib/python/DateTime/DateTime.py 2004-11-09 12:37:18 UTC (rev 28412)
+++ Zope/trunk/lib/python/DateTime/DateTime.py 2004-11-09 18:00:19 UTC (rev 28413)
@@ -443,7 +443,13 @@
_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
interfaces for controlling its representation without
@@ -1574,7 +1580,9 @@
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 @@
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
Modified: Zope/trunk/lib/python/DateTime/tests/testDateTime.py
===================================================================
--- Zope/trunk/lib/python/DateTime/tests/testDateTime.py 2004-11-09 12:37:18 UTC (rev 28412)
+++ Zope/trunk/lib/python/DateTime/tests/testDateTime.py 2004-11-09 18:00:19 UTC (rev 28413)
@@ -272,6 +272,10 @@
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 """
More information about the Zope-Checkins
mailing list