summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris Down <chris@chrisdown.name>2013-09-08 20:55:34 +0200
committerChris Down <chris@chrisdown.name>2013-09-08 20:55:34 +0200
commit2c3931eb2074df93d8a1c61c27352e7262b7fe49 (patch)
treeb7da48ad080f5d81d654e8779cfb4647215748df
parentac5470ec9e68332ebc499c4f9f21e7e3cb31ecb6 (diff)
downloadpyiso8601-2c3931eb2074df93d8a1c61c27352e7262b7fe49.tar.gz
Make the inclusion of seconds optional in the date format.
ISO 8601 specifies that the inclusion of seconds in the date format is optional. To accommodate this, if the seconds are not included, it is considered to be 0.
-rw-r--r--iso8601/iso8601.py12
-rw-r--r--iso8601/test_iso8601.py11
2 files changed, 18 insertions, 5 deletions
diff --git a/iso8601/iso8601.py b/iso8601/iso8601.py
index 38266c3..c852062 100644
--- a/iso8601/iso8601.py
+++ b/iso8601/iso8601.py
@@ -34,7 +34,7 @@ class ParseError(Exception):
ZERO = timedelta(0)
class Utc(tzinfo):
"""UTC
-
+
"""
def utcoffset(self, dt):
return ZERO
@@ -48,7 +48,7 @@ UTC = Utc()
class FixedOffset(tzinfo):
"""Fixed offset in hours and minutes from UTC
-
+
"""
def __init__(self, offset_hours, offset_minutes, name):
self.__offset = timedelta(hours=offset_hours, minutes=offset_minutes)
@@ -62,13 +62,13 @@ class FixedOffset(tzinfo):
def dst(self, dt):
return ZERO
-
+
def __repr__(self):
return "<FixedOffset %r>" % self.__name
def parse_timezone(tzstring, default_timezone=UTC):
"""Parses ISO 8601 time zone specs into tzinfo offsets
-
+
"""
if tzstring == "Z":
return default_timezone
@@ -87,7 +87,7 @@ def parse_timezone(tzstring, default_timezone=UTC):
def parse_date(datestring, default_timezone=UTC):
"""Parses ISO 8601 dates into datetime objects
-
+
The timezone is parsed from the date string. However it is quite common to
have dates without a timezone (not strictly correct). In this case the
default timezone specified in default_timezone is used. This is UTC by
@@ -104,6 +104,8 @@ def parse_date(datestring, default_timezone=UTC):
groups["fraction"] = 0
else:
groups["fraction"] = int(float("0.%s" % groups["fraction"]) * 1e6)
+ if groups["second"] is None:
+ groups["second"] = 0
return datetime(int(groups["year"]), int(groups["month"]), int(groups["day"]),
int(groups["hour"]), int(groups["minute"]), int(groups["second"]),
int(groups["fraction"]), tz)
diff --git a/iso8601/test_iso8601.py b/iso8601/test_iso8601.py
index 44cdc77..caa6b4c 100644
--- a/iso8601/test_iso8601.py
+++ b/iso8601/test_iso8601.py
@@ -93,6 +93,17 @@ def test_parse_no_timezone():
assert d.microsecond == 0
assert d.tzinfo == iso8601.UTC
+def test_parse_no_seconds():
+ d = iso8601.parse_date("1997-07-16T19:20+01:00")
+ assert d.year == 1997
+ assert d.month == 7
+ assert d.day == 16
+ assert d.hour == 19
+ assert d.minute == 20
+ assert d.second == 0
+ assert d.microsecond == 0
+ assert d.tzinfo.tzname(None) == "+01:00"
+
def test_parse_no_timezone_different_default():
tz = iso8601.FixedOffset(2, 0, "test offset")
d = iso8601.parse_date("2007-01-01T08:00:00", default_timezone=tz)