1.1 --- a/DateSupport.py Tue May 07 19:49:26 2013 +0200
1.2 +++ b/DateSupport.py Tue May 07 23:16:40 2013 +0200
1.3 @@ -49,8 +49,17 @@
1.4 ur'(?P<utc>Z)?' \
1.5 ur')?'
1.6
1.7 +# ISO 8601 date and datetime parsing.
1.8 +
1.9 +timezone_iso8601_offset_str = ur'(?P<offset>(?:(?P<sign>[-+])(?P<hours>[0-9]{2}):(?P<minutes>[0-9]{2})))'
1.10 +datetime_iso8601_regexp_str = date_regexp_str + \
1.11 + ur'(?:T' + time_regexp_str + \
1.12 + ur'(?:(?P<utc>Z)|(?P<zone>' + timezone_iso8601_offset_str + '))' \
1.13 + ur')?'
1.14 +
1.15 date_icalendar_regexp = re.compile(date_icalendar_regexp_str, re.UNICODE)
1.16 datetime_icalendar_regexp = re.compile(datetime_icalendar_regexp_str, re.UNICODE)
1.17 +datetime_iso8601_regexp = re.compile(datetime_iso8601_regexp_str, re.UNICODE)
1.18
1.19 # Utility functions.
1.20
1.21 @@ -883,6 +892,22 @@
1.22 else:
1.23 return None
1.24
1.25 +def getDateTimeFromISO8601(s):
1.26 +
1.27 + """
1.28 + Parse the ISO 8601 format datetime string 's', returning a datetime object.
1.29 + """
1.30 +
1.31 + m = datetime_iso8601_regexp.search(s)
1.32 + if m:
1.33 + groups = list(m.groups())
1.34 +
1.35 + # Convert date and time data to integer or None.
1.36 +
1.37 + return DateTime(map(int_or_none, groups[:6]) + [m.group("utc") and "UTC" or m.group("zone")]).as_datetime_or_date()
1.38 + else:
1.39 + return None
1.40 +
1.41 def getDateStrings(s):
1.42
1.43 "Parse the string 's', extracting and returning all date strings."
2.1 --- a/README.txt Tue May 07 19:49:26 2013 +0200
2.2 +++ b/README.txt Tue May 07 23:16:40 2013 +0200
2.3 @@ -69,7 +69,8 @@
2.4
2.5 * Added a function to MoinRemoteSupport to read and return cached item
2.6 metadata.
2.7 - * Fixed the ISO 8601 representations of datetimes.
2.8 + * Fixed the ISO 8601 representations of datetimes and added ISO 8601
2.9 + datetime parsing support.
2.10
2.11 New in MoinSupport 0.3 (Changes since MoinSupport 0.2)
2.12 ------------------------------------------------------
3.1 --- a/tests/test_dates.py Tue May 07 19:49:26 2013 +0200
3.2 +++ b/tests/test_dates.py Tue May 07 23:16:40 2013 +0200
3.3 @@ -7,8 +7,12 @@
3.4 ts1 = Timespan(d1, d1)
3.5 ts2 = Timespan(d1, d2)
3.6
3.7 -print "d1:", d1, d1.as_ISO8601_datetime_string()
3.8 -print "d2:", d2, d2.as_ISO8601_datetime_string()
3.9 +d1_roundtrip = getDateTimeFromISO8601(d1.as_ISO8601_datetime_string())
3.10 +d2_roundtrip = getDateTimeFromISO8601(d2.as_ISO8601_datetime_string())
3.11 +
3.12 +# expected result operands
3.13 +print "%r : %r <- %r == %r" % (d1 == d1_roundtrip, d1 == d1_roundtrip, d1, d1_roundtrip)
3.14 +print "%r : %r <- %r == %r" % (d2 == d2_roundtrip, d2 == d2_roundtrip, d2, d2_roundtrip)
3.15
3.16 # expected result operands
3.17 print "%r : %r <- %r < %r" % (d1 < d2, d1 < d2, d1, d2)