summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Twomey <michael.twomey@fieldaware.com>2014-02-27 13:15:02 +0000
committerMichael Twomey <michael.twomey@fieldaware.com>2014-02-27 13:15:02 +0000
commitd57401e6c36a84923863116c90a7c2ba0d00ae70 (patch)
tree80383f8b9eaa1cf99a687b21c4cd0ef3cd6100f0
parent2186788c5b39f4a3f456319ead89254daed49584 (diff)
downloadpyiso8601-d57401e6c36a84923863116c90a7c2ba0d00ae70.tar.gz
Fix parsing of YYYY-MM and add YYYY
Fixes #14 (https://bitbucket.org/micktwomey/pyiso8601/issue/14/regression-yyyy-mm-no-longer-parses) Bump version to 1.10
-rw-r--r--README.rst8
-rw-r--r--iso8601/iso8601.py53
-rw-r--r--iso8601/test_iso8601.py5
-rw-r--r--setup.py2
4 files changed, 47 insertions, 21 deletions
diff --git a/README.rst b/README.rst
index b924342..e854dee 100644
--- a/README.rst
+++ b/README.rst
@@ -25,6 +25,7 @@ Dates
- YYYY-MM-DD
- YYYYMMDD
- YYYY-MM (defaults to 1 for the day)
+- YYYY (defaults to 1 for month and day)
Times
-----
@@ -102,6 +103,13 @@ Python 2.5 is not supported (too old for the tests for the most part). It could
Changes
=======
+0.1.10
+------
+
+* Fixes https://bitbucket.org/micktwomey/pyiso8601/issue/14/regression-yyyy-mm-no-longer-parses (thanks to Kevin Gill for reporting)
+* Adds YYYY as a valid date (uses 1 for both month and day)
+* Woo, semantic versioning, .10 at last.
+
0.1.9
-----
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)
diff --git a/setup.py b/setup.py
index 1d1c35b..3e60572 100644
--- a/setup.py
+++ b/setup.py
@@ -9,7 +9,7 @@ long_description = open(os.path.join(os.path.dirname(__file__), "README.rst")).r
setup(
name="iso8601",
- version="0.1.9",
+ version="0.1.10",
description=long_description.split("\n")[0],
long_description=long_description,
author="Michael Twomey",