from re import IGNORECASE, ASCII
from re import escape as re_escape
from datetime import (date as datetime_date,
- datetime as datetime_datetime,
timedelta as datetime_timedelta,
timezone as datetime_timezone)
try:
def _strptime(data_string, format="%a %b %d %H:%M:%S %Y"):
- """Return a 2-tuple consisting of a time struct and an int containg
+ """Return a 2-tuple consisting of a time struct and an int containing
the number of microseconds based on the input string and the
format string."""
tt = _strptime(data_string, format)[0]
return time.struct_time(tt[:9])
-def _strptime_datetime(data_string, format="%a %b %d %H:%M:%S %Y"):
- """Return a datetime instace based on the input string and the
+def _strptime_datetime(class_, data_string, format="%a %b %d %H:%M:%S %Y"):
+ """Return a class_ instance based on the input string and the
format string."""
tt, fraction = _strptime(data_string, format)
gmtoff, tzname = tt[-2:]
tz = datetime_timezone(tzdelta)
args += (tz,)
- return datetime_datetime(*args)
+ return class_(*args)
self.assertEqual(b.__format__(fmt), 'B')
def test_resolution_info(self):
- self.assertIsInstance(self.theclass.min, self.theclass)
- self.assertIsInstance(self.theclass.max, self.theclass)
+ # XXX: Should min and max respect subclassing?
+ if issubclass(self.theclass, datetime):
+ expected_class = datetime
+ else:
+ expected_class = date
+ self.assertIsInstance(self.theclass.min, expected_class)
+ self.assertIsInstance(self.theclass.max, expected_class)
self.assertIsInstance(self.theclass.resolution, timedelta)
self.assertTrue(self.theclass.max > self.theclass.min)
string = '2004-12-01 13:02:47.197'
format = '%Y-%m-%d %H:%M:%S.%f'
- expected = _strptime._strptime_datetime(string, format)
+ expected = _strptime._strptime_datetime(self.theclass, string, format)
got = self.theclass.strptime(string, format)
self.assertEqual(expected, got)
+ self.assertIs(type(expected), self.theclass)
+ self.assertIs(type(got), self.theclass)
strptime = self.theclass.strptime
self.assertEqual(strptime("+0002", "%z").utcoffset(), 2 * MINUTE)
self.assertEqual(dt2.newmeth(-7), dt1.year + dt1.month +
dt1.second - 7)
+class TestSubclassDateTime(TestDateTime):
+ theclass = SubclassDatetime
+ # Override tests not designed for subclass
+ def test_roundtrip(self):
+ pass
+
class SubclassTime(time):
sub_var = 1