From 3e8e5bbe2489138460cd65c021107ccf6c607e25 Mon Sep 17 00:00:00 2001 From: ptmcg Date: Sun, 14 Aug 2016 08:36:47 +0000 Subject: Add examples from pyparsing_common git-svn-id: svn://svn.code.sf.net/p/pyparsing/code/trunk@418 9bf210a0-9d2d-494c-87cf-cfb32e7dff7b --- src/examples/datetimeParseActions.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/examples/datetimeParseActions.py b/src/examples/datetimeParseActions.py index 4d726c5..34b9d2b 100644 --- a/src/examples/datetimeParseActions.py +++ b/src/examples/datetimeParseActions.py @@ -30,7 +30,7 @@ def convertToDatetime(s,loc,tokens): # on the integer expression above return datetime(tokens.year, tokens.month, tokens.day).date() except Exception as ve: - errmsg = "'%d/%d/%d' is not a valid date, %s" % \ + errmsg = "'%s/%s/%s' is not a valid date, %s" % \ (tokens.year, tokens.month, tokens.day, ve) raise ParseException(s, loc, errmsg) date_expr.setParseAction(convertToDatetime) @@ -41,4 +41,16 @@ date_expr.runTests("""\ 2000/13/1 # invalid month 1900/2/29 # 1900 was not a leap year 2000/2/29 # but 2000 was + """) + + +# if dates conform to ISO8601, use definitions in pyparsing_common +date_expr = pyparsing_common.iso8601_date.setParseAction(pyparsing_common.convertToDate()) +date_expr.ignore(pythonStyleComment) + +date_expr.runTests("""\ + 2000-01-01 + 2000-13-01 # invalid month + 1900-02-29 # 1900 was not a leap year + 2000-02-29 # but 2000 was """) \ No newline at end of file -- cgit v1.2.1