diff options
author | Tim Peters <tim.peters@gmail.com> | 2003-01-02 21:28:08 (GMT) |
---|---|---|
committer | Tim Peters <tim.peters@gmail.com> | 2003-01-02 21:28:08 (GMT) |
commit | 397301eccb945ea98d03d3022882900a9fd2046f (patch) | |
tree | 3990642065d13899df8b7ba51d7aeec68836be28 /Lib/test/test_datetime.py | |
parent | 4abd5f0fce54d32fbe01207e505047bd82ff9ca3 (diff) | |
download | cpython-397301eccb945ea98d03d3022882900a9fd2046f.zip cpython-397301eccb945ea98d03d3022882900a9fd2046f.tar.gz cpython-397301eccb945ea98d03d3022882900a9fd2046f.tar.bz2 |
The tzinfo methods utcoffset() and dst() must return a timedelta object
(or None) now. In 2.3a1 they could also return an int or long, but that
was an unhelpfully redundant leftover from an earlier version wherein
they couldn't return a timedelta. TOOWTDI.
Diffstat (limited to 'Lib/test/test_datetime.py')
-rw-r--r-- | Lib/test/test_datetime.py | 72 |
1 files changed, 40 insertions, 32 deletions
diff --git a/Lib/test/test_datetime.py b/Lib/test/test_datetime.py index 29f81f1..c9f7674 100644 --- a/Lib/test/test_datetime.py +++ b/Lib/test/test_datetime.py @@ -26,6 +26,10 @@ class TestModule(unittest.TestCase): class FixedOffset(tzinfo): def __init__(self, offset, name, dstoffset=42): + if isinstance(offset, int): + offset = timedelta(minutes=offset) + if isinstance(dstoffset, int): + dstoffset = timedelta(minutes=dstoffset) self.__offset = offset self.__name = name self.__dstoffset = dstoffset @@ -72,9 +76,9 @@ class TestTZInfo(unittest.TestCase): fo = FixedOffset(3, "Three") self.failUnless(isinstance(fo, tzinfo)) for dt in datetime.now(), None: - self.assertEqual(fo.utcoffset(dt), 3) + self.assertEqual(fo.utcoffset(dt), timedelta(minutes=3)) self.assertEqual(fo.tzname(dt), "Three") - self.assertEqual(fo.dst(dt), 42) + self.assertEqual(fo.dst(dt), timedelta(minutes=42)) def test_pickling_base(self): import pickle, cPickle @@ -94,10 +98,11 @@ class TestTZInfo(unittest.TestCase): import pickle, cPickle # Make sure we can pickle/unpickle an instance of a subclass. - orig = PicklableFixedOffset(-300, 'cookie') + offset = timedelta(minutes=-300) + orig = PicklableFixedOffset(offset, 'cookie') self.failUnless(isinstance(orig, tzinfo)) self.failUnless(type(orig) is PicklableFixedOffset) - self.assertEqual(orig.utcoffset(None), -300) + self.assertEqual(orig.utcoffset(None), offset) self.assertEqual(orig.tzname(None), 'cookie') for pickler in pickle, cPickle: for binary in 0, 1: @@ -105,7 +110,7 @@ class TestTZInfo(unittest.TestCase): derived = pickler.loads(green) self.failUnless(isinstance(derived, tzinfo)) self.failUnless(type(derived) is PicklableFixedOffset) - self.assertEqual(derived.utcoffset(None), -300) + self.assertEqual(derived.utcoffset(None), offset) self.assertEqual(derived.tzname(None), 'cookie') ############################################################################# @@ -1562,7 +1567,8 @@ class TZInfoBase(unittest.TestCase): # A datetimetz passes itself on, a timetz passes None. class introspective(tzinfo): def tzname(self, dt): return dt and "real" or "none" - def utcoffset(self, dt): return dt and 42 or -42 + def utcoffset(self, dt): + return timedelta(minutes = dt and 42 or -42) dst = utcoffset obj = cls(1, 2, 3, tzinfo=introspective()) @@ -1593,7 +1599,7 @@ class TZInfoBase(unittest.TestCase): def test_utc_offset_out_of_bounds(self): class Edgy(tzinfo): def __init__(self, offset): - self.offset = offset + self.offset = timedelta(minutes=offset) def utcoffset(self, dt): return self.offset @@ -1629,23 +1635,19 @@ class TZInfoBase(unittest.TestCase): self.failUnless(t.dst() is None) self.failUnless(t.tzname() is None) - class C2(tzinfo): - def utcoffset(self, dt): return -1439 - def dst(self, dt): return 1439 - def tzname(self, dt): return "aname" class C3(tzinfo): def utcoffset(self, dt): return timedelta(minutes=-1439) def dst(self, dt): return timedelta(minutes=1439) def tzname(self, dt): return "aname" - for t in cls(1, 1, 1, tzinfo=C2()), cls(1, 1, 1, tzinfo=C3()): - self.assertEqual(t.utcoffset(), timedelta(minutes=-1439)) - self.assertEqual(t.dst(), timedelta(minutes=1439)) - self.assertEqual(t.tzname(), "aname") + t = cls(1, 1, 1, tzinfo=C3()) + self.assertEqual(t.utcoffset(), timedelta(minutes=-1439)) + self.assertEqual(t.dst(), timedelta(minutes=1439)) + self.assertEqual(t.tzname(), "aname") # Wrong types. class C4(tzinfo): def utcoffset(self, dt): return "aname" - def dst(self, dt): return () + def dst(self, dt): return 7 def tzname(self, dt): return 0 t = cls(1, 1, 1, tzinfo=C4()) self.assertRaises(TypeError, t.utcoffset) @@ -1653,15 +1655,12 @@ class TZInfoBase(unittest.TestCase): self.assertRaises(TypeError, t.tzname) # Offset out of range. - class C5(tzinfo): - def utcoffset(self, dt): return -1440 - def dst(self, dt): return 1440 class C6(tzinfo): def utcoffset(self, dt): return timedelta(hours=-24) def dst(self, dt): return timedelta(hours=24) - for t in cls(1, 1, 1, tzinfo=C5()), cls(1, 1, 1, tzinfo=C6()): - self.assertRaises(ValueError, t.utcoffset) - self.assertRaises(ValueError, t.dst) + t = cls(1, 1, 1, tzinfo=C6()) + self.assertRaises(ValueError, t.utcoffset) + self.assertRaises(ValueError, t.dst) # Not a whole number of minutes. class C7(tzinfo): @@ -1679,9 +1678,11 @@ class TZInfoBase(unittest.TestCase): class OperandDependentOffset(tzinfo): def utcoffset(self, t): if t.minute < 10: - return t.minute # d0 and d1 equal after adjustment + # d0 and d1 equal after adjustment + return timedelta(minutes=t.minute) else: - return 59 # d2 off in the weeds + # d2 off in the weeds + return timedelta(minutes=59) base = cls(8, 9, 10, tzinfo=OperandDependentOffset()) d0 = base.replace(minute=3) @@ -1937,9 +1938,9 @@ class TestTimeTZ(TestTime, TZInfoBase): # In timetz w/ identical tzinfo objects, utcoffset is ignored. class Varies(tzinfo): def __init__(self): - self.offset = 22 + self.offset = timedelta(minutes=22) def utcoffset(self, t): - self.offset += 1 + self.offset += timedelta(minutes=1) return self.offset v = Varies() @@ -2028,7 +2029,8 @@ class TestDateTimeTZ(TestDateTime, TZInfoBase): # Try a bogus uctoffset. class Bogus(tzinfo): - def utcoffset(self, dt): return 1440 # out of bounds + def utcoffset(self, dt): + return timedelta(minutes=1440) # out of bounds t1 = self.theclass(2, 2, 2, tzinfo=Bogus()) t2 = self.theclass(2, 2, 2, tzinfo=FixedOffset(0, "")) self.assertRaises(ValueError, lambda: t1 == t2) @@ -2259,6 +2261,8 @@ class TestDateTimeTZ(TestDateTime, TZInfoBase): # DST flag. class DST(tzinfo): def __init__(self, dstvalue): + if isinstance(dstvalue, int): + dstvalue = timedelta(minutes=dstvalue) self.dstvalue = dstvalue def dst(self, dt): return self.dstvalue @@ -2291,6 +2295,8 @@ class TestDateTimeTZ(TestDateTime, TZInfoBase): def test_utctimetuple(self): class DST(tzinfo): def __init__(self, dstvalue): + if isinstance(dstvalue, int): + dstvalue = timedelta(minutes=dstvalue) self.dstvalue = dstvalue def dst(self, dt): return self.dstvalue @@ -2303,7 +2309,7 @@ class TestDateTimeTZ(TestDateTime, TZInfoBase): class UOFS(DST): def __init__(self, uofs, dofs=None): DST.__init__(self, dofs) - self.uofs = uofs + self.uofs = timedelta(minutes=uofs) def utcoffset(self, dt): return self.uofs @@ -2454,9 +2460,11 @@ class TestDateTimeTZ(TestDateTime, TZInfoBase): class OperandDependentOffset(tzinfo): def utcoffset(self, t): if t.minute < 10: - return t.minute # d0 and d1 equal after adjustment + # d0 and d1 equal after adjustment + return timedelta(minutes=t.minute) else: - return 59 # d2 off in the weeds + # d2 off in the weeds + return timedelta(minutes=59) base = cls(8, 9, 10, 11, 12, 13, 14, tzinfo=OperandDependentOffset()) d0 = base.replace(minute=3) @@ -2502,9 +2510,9 @@ class TestDateTimeTZ(TestDateTime, TZInfoBase): # In datetimetz w/ identical tzinfo objects, utcoffset is ignored. class Varies(tzinfo): def __init__(self): - self.offset = 22 + self.offset = timedelta(minutes=22) def utcoffset(self, t): - self.offset += 1 + self.offset += timedelta(minutes=1) return self.offset v = Varies() |