# HG changeset patch # User Paul Boddie # Date 1367961400 -7200 # Node ID 8f36e0e8a04d2499810036118684e94d25825846 # Parent 4238d467e98004a4f840d7aed839fbc2438d9e15 Added ISO 8601 datetime parsing support. diff -r 4238d467e980 -r 8f36e0e8a04d DateSupport.py --- a/DateSupport.py Tue May 07 19:49:26 2013 +0200 +++ b/DateSupport.py Tue May 07 23:16:40 2013 +0200 @@ -49,8 +49,17 @@ ur'(?PZ)?' \ ur')?' +# ISO 8601 date and datetime parsing. + +timezone_iso8601_offset_str = ur'(?P(?:(?P[-+])(?P[0-9]{2}):(?P[0-9]{2})))' +datetime_iso8601_regexp_str = date_regexp_str + \ + ur'(?:T' + time_regexp_str + \ + ur'(?:(?PZ)|(?P' + timezone_iso8601_offset_str + '))' \ + ur')?' + date_icalendar_regexp = re.compile(date_icalendar_regexp_str, re.UNICODE) datetime_icalendar_regexp = re.compile(datetime_icalendar_regexp_str, re.UNICODE) +datetime_iso8601_regexp = re.compile(datetime_iso8601_regexp_str, re.UNICODE) # Utility functions. @@ -883,6 +892,22 @@ else: return None +def getDateTimeFromISO8601(s): + + """ + Parse the ISO 8601 format datetime string 's', returning a datetime object. + """ + + m = datetime_iso8601_regexp.search(s) + if m: + groups = list(m.groups()) + + # Convert date and time data to integer or None. + + return DateTime(map(int_or_none, groups[:6]) + [m.group("utc") and "UTC" or m.group("zone")]).as_datetime_or_date() + else: + return None + def getDateStrings(s): "Parse the string 's', extracting and returning all date strings." diff -r 4238d467e980 -r 8f36e0e8a04d README.txt --- a/README.txt Tue May 07 19:49:26 2013 +0200 +++ b/README.txt Tue May 07 23:16:40 2013 +0200 @@ -69,7 +69,8 @@ * Added a function to MoinRemoteSupport to read and return cached item metadata. - * Fixed the ISO 8601 representations of datetimes. + * Fixed the ISO 8601 representations of datetimes and added ISO 8601 + datetime parsing support. New in MoinSupport 0.3 (Changes since MoinSupport 0.2) ------------------------------------------------------ diff -r 4238d467e980 -r 8f36e0e8a04d tests/test_dates.py --- a/tests/test_dates.py Tue May 07 19:49:26 2013 +0200 +++ b/tests/test_dates.py Tue May 07 23:16:40 2013 +0200 @@ -7,8 +7,12 @@ ts1 = Timespan(d1, d1) ts2 = Timespan(d1, d2) -print "d1:", d1, d1.as_ISO8601_datetime_string() -print "d2:", d2, d2.as_ISO8601_datetime_string() +d1_roundtrip = getDateTimeFromISO8601(d1.as_ISO8601_datetime_string()) +d2_roundtrip = getDateTimeFromISO8601(d2.as_ISO8601_datetime_string()) + +# expected result operands +print "%r : %r <- %r == %r" % (d1 == d1_roundtrip, d1 == d1_roundtrip, d1, d1_roundtrip) +print "%r : %r <- %r == %r" % (d2 == d2_roundtrip, d2 == d2_roundtrip, d2, d2_roundtrip) # expected result operands print "%r : %r <- %r < %r" % (d1 < d2, d1 < d2, d1, d2)