# coding=UTF-8 from __future__ import absolute_import import copy import datetime import pickle import pytest from iso8601 import iso8601 def test_iso8601_regex(): assert iso8601.ISO8601_REGEX.match("2006-10-11T00:14:33Z") def test_fixedoffset_eq(): # See https://bitbucket.org/micktwomey/pyiso8601/issues/19 datetime.tzinfo() == iso8601.FixedOffset(2, 0, '+2: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) assert d == datetime.datetime(2007, 1, 1, 8, 0, 0, 0, tz) assert d.tzinfo == tz def test_parse_utc_different_default(): """Z should mean 'UTC', not 'default'. """ tz = iso8601.FixedOffset(2, 0, "test offset") d = iso8601.parse_date("2007-01-01T08:00:00Z", default_timezone=tz) assert d == datetime.datetime(2007, 1, 1, 8, 0, 0, 0, iso8601.UTC) @pytest.mark.parametrize("invalid_date, error_string", [ ("2013-10-", "Unable to parse date string"), ("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"), ]) def test_parse_invalid_date(invalid_date, error_string): assert isinstance(invalid_date, str) or invalid_date is None # Why? 'cos I've screwed up the parametrize before :) with pytest.raises(iso8601.ParseError) as exc: iso8601.parse_date(invalid_date) assert exc.errisinstance(iso8601.ParseError) assert str(exc.value).startswith(error_string) @pytest.mark.parametrize("valid_date,expected_datetime,isoformat", [ ("2007-06-23 06:40:34.00Z", datetime.datetime(2007, 6, 23, 6, 40, 34, 0, iso8601.UTC), "2007-06-23T06:40:34+00:00"), # Handle a separator other than T ("1997-07-16T19:20+01:00", datetime.datetime(1997, 7, 16, 19, 20, 0, 0, iso8601.FixedOffset(1, 0, "+01:00")), "1997-07-16T19:20:00+01:00"), # Parse with no seconds ("2007-01-01T08:00:00", datetime.datetime(2007, 1, 1, 8, 0, 0, 0, iso8601.UTC), "2007-01-01T08:00:00+00:00"), # Handle timezone-less dates. Assumes UTC. http://code.google.com/p/pyiso8601/issues/detail?id=4 ("2006-10-20T15:34:56.123+02:30", datetime.datetime(2006, 10, 20, 15, 34, 56, 123000, iso8601.FixedOffset(2, 30, "+02:30")), None), ("2006-10-20T15:34:56Z", datetime.datetime(2006, 10, 20, 15, 34, 56, 0, iso8601.UTC), "2006-10-20T15:34:56+00:00"), ("2007-5-7T11:43:55.328Z", datetime.datetime(2007, 5, 7, 11, 43, 55, 328000, iso8601.UTC), "2007-05-07T11:43:55.328000+00:00"), # http://code.google.com/p/pyiso8601/issues/detail?id=6 ("2006-10-20T15:34:56.123Z", datetime.datetime(2006, 10, 20, 15, 34, 56, 123000, iso8601.UTC), "2006-10-20T15:34:56.123000+00:00"), ("2013-10-15T18:30Z", datetime.datetime(2013, 10, 15, 18, 30, 0, 0, iso8601.UTC), "2013-10-15T18:30:00+00:00"), ("2013-10-15T22:30+04", datetime.datetime(2013, 10, 15, 22, 30, 0, 0, iso8601.FixedOffset(4, 0, "+04:00")), "2013-10-15T22:30:00+04:00"), #