summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdrian Coveney <adrian.coveney@stfc.ac.uk>2014-08-19 14:33:21 +0100
committerAdrian Coveney <adrian.coveney@stfc.ac.uk>2014-08-19 15:02:34 +0100
commit343fcaee01b3908ded45f2c28e8e1a00d839ad5f (patch)
treeec23b856017f155e890dddd8894f1f0f55c01c1d
parent0f2e76b427d039bbc82f18b545f74db66d1c35c0 (diff)
downloadisodate-343fcaee01b3908ded45f2c28e8e1a00d839ad5f.tar.gz
Make parse_datetime raise ISO8601Error if no 'T'
- Make parse_datetime raise ISO8601Error if time designator 'T' is missing as before it would raise a ValueError that was hard to interpret. - Alter test_parse for test_datetime to check that bad inputs raise an ISO8601 error. The other tests did this, but test_datetime didn't. - Add test case where the 'T' time designator is missing from input datetime strings.
-rw-r--r--src/isodate/isodatetime.py7
-rw-r--r--src/isodate/tests/test_datetime.py12
2 files changed, 15 insertions, 4 deletions
diff --git a/src/isodate/isodatetime.py b/src/isodate/isodatetime.py
index 147474b..9a5fce0 100644
--- a/src/isodate/isodatetime.py
+++ b/src/isodate/isodatetime.py
@@ -35,6 +35,7 @@ from datetime import datetime
from isodate.isostrf import strftime
from isodate.isostrf import DATE_EXT_COMPLETE, TIME_EXT_COMPLETE, TZ_EXT
from isodate.isodates import parse_date
+from isodate.isoerror import ISO8601Error
from isodate.isotime import parse_time
@@ -46,7 +47,11 @@ def parse_datetime(datetimestring):
more combinations of date and time representations, than the actual
ISO 8601:2004 standard allows.
'''
- datestring, timestring = datetimestring.split('T')
+ try:
+ datestring, timestring = datetimestring.split('T')
+ except ValueError:
+ raise ISO8601Error("ISO 8601 time designator 'T' missing. Unable to"
+ " parse datetime string %r" % datetimestring)
tmpdate = parse_date(datestring)
tmptime = parse_time(timestring)
return datetime.combine(tmpdate, tmptime)
diff --git a/src/isodate/tests/test_datetime.py b/src/isodate/tests/test_datetime.py
index 73b1982..ddad5da 100644
--- a/src/isodate/tests/test_datetime.py
+++ b/src/isodate/tests/test_datetime.py
@@ -31,6 +31,7 @@ import unittest
from datetime import datetime
from isodate import parse_datetime, UTC, FixedOffset, datetime_isoformat
+from isodate import ISO8601Error
from isodate import DATE_BAS_COMPLETE, TIME_BAS_MINUTE, TIME_BAS_COMPLETE
from isodate import DATE_EXT_COMPLETE, TIME_EXT_MINUTE, TIME_EXT_COMPLETE
from isodate import TZ_BAS, TZ_EXT, TZ_HOUR
@@ -81,7 +82,10 @@ TEST_CASES = [('19850412T1015', datetime(1985, 4, 12, 10, 15),
('2012-10-30T08:55:22.1234561Z',
datetime(2012, 10, 30, 8, 55, 22, 123456, tzinfo=UTC),
DATE_EXT_COMPLETE + 'T' + TIME_EXT_COMPLETE + '.%f' + TZ_BAS,
- '2012-10-30T08:55:22.123456Z')
+ '2012-10-30T08:55:22.123456Z'),
+ ('2014-08-18 14:55:22.123456Z', None,
+ DATE_EXT_COMPLETE + 'T' + TIME_EXT_COMPLETE + '.%f' + TZ_BAS,
+ '2014-08-18T14:55:22.123456Z'),
]
@@ -103,8 +107,10 @@ def create_testcase(datetimestring, expectation, format, output):
'''
Parse an ISO datetime string and compare it to the expected value.
'''
- result = parse_datetime(datetimestring)
- self.assertEqual(result, expectation)
+ if expectation is None:
+ self.assertRaises(ISO8601Error, parse_datetime, datetimestring)
+ else:
+ self.assertEqual(parse_datetime(datetimestring), expectation)
def test_format(self):
'''