diff options
author | Julien Danjou <julien@danjou.info> | 2014-02-27 15:11:34 +0100 |
---|---|---|
committer | Julien Danjou <julien@danjou.info> | 2014-02-27 15:11:34 +0100 |
commit | 64e3a0d4929542baceccf8fcb53f7e2f5538d8af (patch) | |
tree | 80383f8b9eaa1cf99a687b21c4cd0ef3cd6100f0 /iso8601 | |
parent | 2186788c5b39f4a3f456319ead89254daed49584 (diff) | |
parent | d57401e6c36a84923863116c90a7c2ba0d00ae70 (diff) | |
download | pyiso8601-64e3a0d4929542baceccf8fcb53f7e2f5538d8af.tar.gz |
Merged in issue-14-YYYY-MM (pull request #4)
Fix parsing of YYYY-MM and add YYYY
Diffstat (limited to 'iso8601')
-rw-r--r-- | iso8601/iso8601.py | 53 | ||||
-rw-r--r-- | iso8601/test_iso8601.py | 5 |
2 files changed, 38 insertions, 20 deletions
diff --git a/iso8601/iso8601.py b/iso8601/iso8601.py index b72d7b4..becdd95 100644 --- a/iso8601/iso8601.py +++ b/iso8601/iso8601.py @@ -32,29 +32,42 @@ else: ISO8601_REGEX = re.compile( r""" (?P<year>[0-9]{4}) - ((-(?P<monthdash>[0-9]{1,2}))|(?P<month>[0-9]{2})) - ((-(?P<daydash>[0-9]{1,2}))|(?P<day>[0-9]{2})) ( ( - (?P<separator>[ T]) - (?P<hour>[0-9]{2}) - (:{0,1}(?P<minute>[0-9]{2})){0,1} + (-(?P<monthdash>[0-9]{1,2})) + | + (?P<month>[0-9]{2}) + (?!$) # Don't allow YYYYMM + ) + ( ( - :{0,1}(?P<second>[0-9]{1,2}) - (\.(?P<second_fraction>[0-9]+)){0,1} - ){0,1} - (?P<timezone> - Z + (-(?P<daydash>[0-9]{1,2})) | + (?P<day>[0-9]{2}) + ) + ( ( - (?P<tz_sign>[-+]) - (?P<tz_hour>[0-9]{2}) - :{0,1} - (?P<tz_minute>[0-9]{2}){0,1} - ) - ){0,1} - ){0,1} - ) + (?P<separator>[ T]) + (?P<hour>[0-9]{2}) + (:{0,1}(?P<minute>[0-9]{2})){0,1} + ( + :{0,1}(?P<second>[0-9]{1,2}) + (\.(?P<second_fraction>[0-9]+)){0,1} + ){0,1} + (?P<timezone> + Z + | + ( + (?P<tz_sign>[-+]) + (?P<tz_hour>[0-9]{2}) + :{0,1} + (?P<tz_minute>[0-9]{2}){0,1} + ) + ){0,1} + ){0,1} + ) + ){0,1} # YYYY-MM + ){0,1} # YYYY only $ """, re.VERBOSE @@ -177,8 +190,8 @@ def parse_date(datestring, default_timezone=UTC): try: return datetime( year=to_int(groups, "year"), - month=to_int(groups, "month", default=to_int(groups, "monthdash", required=False)), - day=to_int(groups, "day", default=to_int(groups, "daydash", required=False)), + month=to_int(groups, "month", default=to_int(groups, "monthdash", required=False, default=1)), + day=to_int(groups, "day", default=to_int(groups, "daydash", required=False, default=1)), hour=to_int(groups, "hour", default_to_zero=True), minute=to_int(groups, "minute", default_to_zero=True), second=to_int(groups, "second", default_to_zero=True), diff --git a/iso8601/test_iso8601.py b/iso8601/test_iso8601.py index 18e5ff0..ed2d45a 100644 --- a/iso8601/test_iso8601.py +++ b/iso8601/test_iso8601.py @@ -31,9 +31,12 @@ def test_parse_utc_different_default(): ("2013-", "Unable to parse date string"), ("", "Unable to parse date string"), (None, "Expecting a string"), + ("wibble", "Unable to parse date string"), ("23", "Unable to parse date string"), ("131015T142533Z", "Unable to parse date string"), ("131015", "Unable to parse date string"), + ("20141", "Unable to parse date string"), + ("201402", "Unable to parse date string"), ("2007-06-23X06:40:34.00Z", "Unable to parse date string"), # https://code.google.com/p/pyiso8601/issues/detail?id=14 ("2007-06-23 06:40:34.00Zrubbish", "Unable to parse date string"), # https://code.google.com/p/pyiso8601/issues/detail?id=14 ("20114-01-03T01:45:49", "Unable to parse date string"), @@ -72,6 +75,8 @@ def test_parse_invalid_date(invalid_date, error_string): ("2010-06-12", datetime.datetime(2010, 6, 12, tzinfo=iso8601.UTC), "2010-06-12T00:00:00+00:00"), # https://code.google.com/p/pyiso8601/issues/detail?id=16 ("1985-04-12T23:20:50.52-05:30", datetime.datetime(1985, 4, 12, 23, 20, 50, 520000, iso8601.FixedOffset(-5, -30, "-05:30")), "1985-04-12T23:20:50.520000-05:30"), # https://bitbucket.org/micktwomey/pyiso8601/issue/8/015-parses-negative-timezones-incorrectly ("1997-08-29T06:14:00.000123Z", datetime.datetime(1997, 8, 29, 6, 14, 0, 123, iso8601.UTC), "1997-08-29T06:14:00.000123+00:00"), # https://bitbucket.org/micktwomey/pyiso8601/issue/9/regression-parsing-microseconds + ("2014-02", datetime.datetime(2014, 2, 1, 0, 0, 0, 0, iso8601.UTC), "2014-02-01T00:00:00+00:00"), # https://bitbucket.org/micktwomey/pyiso8601/issue/14/regression-yyyy-mm-no-longer-parses + ("2014", datetime.datetime(2014, 1, 1, 0, 0, 0, 0, iso8601.UTC), "2014-01-01T00:00:00+00:00"), # YYYY ]) def test_parse_valid_date(valid_date, expected_datetime, isoformat): parsed = iso8601.parse_date(valid_date) |