From b7dc65d503d0255cdbd585e4629cbbf191c38e59 Mon Sep 17 00:00:00 2001 From: Michael Twomey Date: Thu, 29 Aug 2013 15:25:29 +0100 Subject: Applied python 3 support patch from https://code.google.com/p/pyiso8601/issues/detail?id=23 Also updated to use tox and py.test --- iso8601/__init__.py | 2 +- iso8601/iso8601.py | 9 ++++++++- iso8601/test_iso8601.py | 10 ++++++---- 3 files changed, 15 insertions(+), 6 deletions(-) (limited to 'iso8601') diff --git a/iso8601/__init__.py b/iso8601/__init__.py index e72e356..11b1adc 100644 --- a/iso8601/__init__.py +++ b/iso8601/__init__.py @@ -1 +1 @@ -from iso8601 import * +from .iso8601 import * diff --git a/iso8601/iso8601.py b/iso8601/iso8601.py index f923938..38266c3 100644 --- a/iso8601/iso8601.py +++ b/iso8601/iso8601.py @@ -9,10 +9,17 @@ datetime.datetime(2007, 1, 25, 12, 0, tzinfo=) """ from datetime import datetime, timedelta, tzinfo +import sys import re __all__ = ["parse_date", "ParseError"] +if sys.version_info >= (3, 0, 0): + _basestring = str +else: + _basestring = basestring + + # Adapted from http://delete.me.uk/2005/03/iso8601.html ISO8601_REGEX = re.compile(r"(?P[0-9]{4})(-(?P[0-9]{1,2})(-(?P[0-9]{1,2})" r"((?P.)(?P[0-9]{2}):(?P[0-9]{2})(:(?P[0-9]{2})(\.(?P[0-9]+))?)?" @@ -86,7 +93,7 @@ def parse_date(datestring, default_timezone=UTC): default timezone specified in default_timezone is used. This is UTC by default. """ - if not isinstance(datestring, basestring): + if not isinstance(datestring, _basestring): raise ParseError("Expecting a string %r" % datestring) m = ISO8601_REGEX.match(datestring) if not m: diff --git a/iso8601/test_iso8601.py b/iso8601/test_iso8601.py index ff9e273..44cdc77 100644 --- a/iso8601/test_iso8601.py +++ b/iso8601/test_iso8601.py @@ -1,4 +1,6 @@ -import iso8601 +from __future__ import absolute_import + +from iso8601 import iso8601 def test_iso8601_regex(): assert iso8601.ISO8601_REGEX.match("2006-10-11T00:14:33Z") @@ -32,7 +34,7 @@ def test_parse_date_fraction(): def test_parse_date_fraction_2(): """From bug 6 - + """ d = iso8601.parse_date("2007-5-7T11:43:55.328Z'") assert d.year == 2007 @@ -76,7 +78,7 @@ def test_parse_invalid_date2(): def test_parse_no_timezone(): """issue 4 - Handle datetime string without timezone - + This tests what happens when you parse a date with no timezone. While not strictly correct this is quite common. I'll assume UTC for the time zone in this case. @@ -98,7 +100,7 @@ def test_parse_no_timezone_different_default(): def test_space_separator(): """Handle a separator other than T - + """ d = iso8601.parse_date("2007-06-23 06:40:34.00Z") assert d.year == 2007 -- cgit v1.2.1