[Checkins] SVN: zc.iso8601/trunk/ add a date() function for completeness

Fred Drake fdrake at gmail.com
Mon Oct 10 15:20:45 EST 2011


Log message for revision 123048:
  add a date() function for completeness

Changed:
  U   zc.iso8601/trunk/CHANGES.txt
  U   zc.iso8601/trunk/README.txt
  U   zc.iso8601/trunk/src/zc/iso8601/README.txt
  U   zc.iso8601/trunk/src/zc/iso8601/parse.py

-=-
Modified: zc.iso8601/trunk/CHANGES.txt
===================================================================
--- zc.iso8601/trunk/CHANGES.txt	2011-10-10 15:10:24 UTC (rev 123047)
+++ zc.iso8601/trunk/CHANGES.txt	2011-10-10 20:20:44 UTC (rev 123048)
@@ -1,11 +1,13 @@
 Changes
 -------
 
+
 0.2.0 (unreleased)
 ~~~~~~~~~~~~~~~~~~
 
-* ...
+Added ``date`` function, for completeness.
 
+
 0.1.0 (2008-05-12)
 ~~~~~~~~~~~~~~~~~~
 

Modified: zc.iso8601/trunk/README.txt
===================================================================
--- zc.iso8601/trunk/README.txt	2011-10-10 15:10:24 UTC (rev 123047)
+++ zc.iso8601/trunk/README.txt	2011-10-10 20:20:44 UTC (rev 123048)
@@ -7,6 +7,10 @@
 
 The following functions are provided in the ``zc.iso8601.parse`` module:
 
+``date(s)``
+  Parse a date value that does not include time information.
+  Returns a Python date value.
+
 ``datetime(s)``
   Parse a date-time value that does not include time-zone information.
   Returns a Python datetime value.

Modified: zc.iso8601/trunk/src/zc/iso8601/README.txt
===================================================================
--- zc.iso8601/trunk/src/zc/iso8601/README.txt	2011-10-10 15:10:24 UTC (rev 123047)
+++ zc.iso8601/trunk/src/zc/iso8601/README.txt	2011-10-10 20:20:44 UTC (rev 123048)
@@ -13,6 +13,66 @@
 more readable for humans.
 
 
+Parsing date values
+-------------------
+
+There's a simple function that parses a date:
+
+    >>> from zc.iso8601.parse import date
+
+This function only accepts a date; no time information may be included:
+
+    >>> date(u"2006-12-02T23:40:42")
+    Traceback (most recent call last):
+    ValueError: could not parse ISO 8601 date: u'2006-12-02T23:40:42'
+
+    >>> date(u"2006-12-02T23:40:42Z")
+    Traceback (most recent call last):
+    ValueError: could not parse ISO 8601 date: u'2006-12-02T23:40:42Z'
+
+    >>> date(u"2006-12-02T23:40:42+00:00")
+    Traceback (most recent call last):
+    ValueError: could not parse ISO 8601 date: u'2006-12-02T23:40:42+00:00'
+
+    >>> date(u"2006-12-02T23:40:42-00:00")
+    Traceback (most recent call last):
+    ValueError: could not parse ISO 8601 date: u'2006-12-02T23:40:42-00:00'
+
+    >>> date(u"2006-12-02T23:40:42-01:00")
+    Traceback (most recent call last):
+    ValueError: could not parse ISO 8601 date: u'2006-12-02T23:40:42-01:00'
+
+A date without time information is parsed as expected:
+
+    >>> date(u"2011-10-10")
+    datetime.date(2011, 10, 10)
+    >>> date(u"20111010")
+    datetime.date(2011, 10, 10)
+
+    >>> date(u"0001-01-01")
+    datetime.date(1, 1, 1)
+    >>> date(u"00010101")
+    datetime.date(1, 1, 1)
+
+    >>> date(u"9999-12-31")
+    datetime.date(9999, 12, 31)
+    >>> date(u"99991231")
+    datetime.date(9999, 12, 31)
+
+Surrounding whitespace is ignored:
+
+    >>> date(u"\t\n\r 2011-10-10 \r\n\t")
+    datetime.date(2011, 10, 10)
+    >>> date(u"\t\n\r 20111010 \r\n\t")
+    datetime.date(2011, 10, 10)
+
+Embedded whitespace is not:
+
+    >>> date("2011 10 10")
+    Traceback (most recent call last):
+    ValueError: could not parse ISO 8601 date: '2011 10 10'
+
+
 Parsing date/time values
 ------------------------
 

Modified: zc.iso8601/trunk/src/zc/iso8601/parse.py
===================================================================
--- zc.iso8601/trunk/src/zc/iso8601/parse.py	2011-10-10 15:10:24 UTC (rev 123047)
+++ zc.iso8601/trunk/src/zc/iso8601/parse.py	2011-10-10 20:20:44 UTC (rev 123048)
@@ -23,13 +23,28 @@
 import re
 
 
-_tz_re = "(?:Z|(?P<tzdir>[-+])(?P<tzhour>\d\d):(?P<tzmin>\d\d))$"
-
 # "Verbose" ISO 8601, with hyphens and colons:
-_datetime_re1 = """\
+_date_re1 = """\
     (?P<year>\d\d\d\d)
     -(?P<month>\d\d)
     -(?P<day>\d\d)
+    """
+
+# "Compact" ISO 8601, without hyphens and colons:
+_date_re2 = """\
+    (?P<year>\d\d\d\d)
+    (?P<month>\d\d)
+    (?P<day>\d\d)
+    """
+
+_date_rx1 = re.compile(_date_re1 + "$", re.VERBOSE)
+_date_rx2 = re.compile(_date_re2 + "$", re.VERBOSE)
+_date_rxs = [_date_rx1, _date_rx2]
+
+_tz_re = "(?:Z|(?P<tzdir>[-+])(?P<tzhour>\d\d):(?P<tzmin>\d\d))$"
+
+# "Verbose" ISO 8601, with hyphens and colons:
+_datetime_re1 = _date_re1 + """\
     [T\ ]
     (?P<hour>\d\d)
     :(?P<minute>\d\d)
@@ -40,10 +55,7 @@
 _datetime_re1 += "$"
 
 # "Compact" ISO 8601, without hyphens and colons:
-_datetime_re2 = """\
-    (?P<year>\d\d\d\d)
-    (?P<month>\d\d)
-    (?P<day>\d\d)
+_datetime_re2 = _date_re2 + """\
     [T\ ]
     (?P<hour>\d\d)
     (?P<minute>\d\d)
@@ -62,6 +74,18 @@
 _datetimetz_rxs = [_datetimetz_rx1, _datetimetz_rx2]
 
 
+def date(string):
+    """Parse an ISO 8601 date without time information.
+
+    Returns a Python date object.
+
+    """
+    m = _find_match(string, _date_rxs, "date")
+    year, month, day = map(int, m.group("year", "month", "day"))
+    return _datetime.date(year, month, day)
+    
+
+
 def datetime(string):
     """Parse an ISO 8601 date without timezone information.
 
@@ -100,13 +124,13 @@
         tzinfo=pytz.UTC)
 
 
-def _find_match(string, rxs):
+def _find_match(string, rxs, what="datetime"):
     string = " ".join(string.split())
     for rx in rxs:
         m = rx.match(string)
         if m is not None:
             return m
-    raise ValueError("could not parse ISO 8601 datetime: %r" % string)
+    raise ValueError("could not parse ISO 8601 %s: %r" % (what, string))
 
 
 def _get_datetime_parts(m):



More information about the checkins mailing list