Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes
test.datetimetester.TZInfoBase Class Reference
Inheritance diagram for test.datetimetester.TZInfoBase:
Inheritance graph
[legend]

List of all members.

Public Member Functions

def test_argument_passing
def test_bad_tzinfo_classes
def test_utc_offset_out_of_bounds
def test_tzinfo_classes
def test_aware_compare

Public Attributes

 offset

Detailed Description

Definition at line 2269 of file datetimetester.py.


Member Function Documentation

Definition at line 2271 of file datetimetester.py.

02271 
02272     def test_argument_passing(self):
02273         cls = self.theclass
02274         # A datetime passes itself on, a time passes None.
02275         class introspective(tzinfo):
02276             def tzname(self, dt):    return dt and "real" or "none"
02277             def utcoffset(self, dt):
02278                 return timedelta(minutes = dt and 42 or -42)
02279             dst = utcoffset
02280 
02281         obj = cls(1, 2, 3, tzinfo=introspective())
02282 
02283         expected = cls is time and "none" or "real"
02284         self.assertEqual(obj.tzname(), expected)
02285 
02286         expected = timedelta(minutes=(cls is time and -42 or 42))
02287         self.assertEqual(obj.utcoffset(), expected)
02288         self.assertEqual(obj.dst(), expected)

Here is the call graph for this function:

Definition at line 2381 of file datetimetester.py.

02381 
02382     def test_aware_compare(self):
02383         cls = self.theclass
02384 
02385         # Ensure that utcoffset() gets ignored if the comparands have
02386         # the same tzinfo member.
02387         class OperandDependentOffset(tzinfo):
02388             def utcoffset(self, t):
02389                 if t.minute < 10:
02390                     # d0 and d1 equal after adjustment
02391                     return timedelta(minutes=t.minute)
02392                 else:
02393                     # d2 off in the weeds
02394                     return timedelta(minutes=59)
02395 
02396         base = cls(8, 9, 10, tzinfo=OperandDependentOffset())
02397         d0 = base.replace(minute=3)
02398         d1 = base.replace(minute=9)
02399         d2 = base.replace(minute=11)
02400         for x in d0, d1, d2:
02401             for y in d0, d1, d2:
02402                 for op in lt, le, gt, ge, eq, ne:
02403                     got = op(x, y)
02404                     expected = op(x.minute, y.minute)
02405                     self.assertEqual(got, expected)
02406 
02407         # However, if they're different members, uctoffset is not ignored.
02408         # Note that a time can't actually have an operand-depedent offset,
02409         # though (and time.utcoffset() passes None to tzinfo.utcoffset()),
02410         # so skip this test for time.
02411         if cls is not time:
02412             d0 = base.replace(minute=3, tzinfo=OperandDependentOffset())
02413             d1 = base.replace(minute=9, tzinfo=OperandDependentOffset())
02414             d2 = base.replace(minute=11, tzinfo=OperandDependentOffset())
02415             for x in d0, d1, d2:
02416                 for y in d0, d1, d2:
02417                     got = (x > y) - (x < y)
02418                     if (x is d0 or x is d1) and (y is d0 or y is d1):
02419                         expected = 0
02420                     elif x is y is d2:
02421                         expected = 0
02422                     elif x is d2:
02423                         expected = -1
02424                     else:
02425                         assert y is d2
02426                         expected = 1
02427                     self.assertEqual(got, expected)
02428 
02429 
# Testing time objects with a non-None tzinfo.

Here is the call graph for this function:

Definition at line 2289 of file datetimetester.py.

02289 
02290     def test_bad_tzinfo_classes(self):
02291         cls = self.theclass
02292         self.assertRaises(TypeError, cls, 1, 1, 1, tzinfo=12)
02293 
02294         class NiceTry(object):
02295             def __init__(self): pass
02296             def utcoffset(self, dt): pass
02297         self.assertRaises(TypeError, cls, 1, 1, 1, tzinfo=NiceTry)
02298 
02299         class BetterTry(tzinfo):
02300             def __init__(self): pass
02301             def utcoffset(self, dt): pass
02302         b = BetterTry()
02303         t = cls(1, 1, 1, tzinfo=b)
02304         self.assertTrue(t.tzinfo is b)

Here is the call graph for this function:

Definition at line 2333 of file datetimetester.py.

02333 
02334     def test_tzinfo_classes(self):
02335         cls = self.theclass
02336         class C1(tzinfo):
02337             def utcoffset(self, dt): return None
02338             def dst(self, dt): return None
02339             def tzname(self, dt): return None
02340         for t in (cls(1, 1, 1),
02341                   cls(1, 1, 1, tzinfo=None),
02342                   cls(1, 1, 1, tzinfo=C1())):
02343             self.assertTrue(t.utcoffset() is None)
02344             self.assertTrue(t.dst() is None)
02345             self.assertTrue(t.tzname() is None)
02346 
02347         class C3(tzinfo):
02348             def utcoffset(self, dt): return timedelta(minutes=-1439)
02349             def dst(self, dt): return timedelta(minutes=1439)
02350             def tzname(self, dt): return "aname"
02351         t = cls(1, 1, 1, tzinfo=C3())
02352         self.assertEqual(t.utcoffset(), timedelta(minutes=-1439))
02353         self.assertEqual(t.dst(), timedelta(minutes=1439))
02354         self.assertEqual(t.tzname(), "aname")
02355 
02356         # Wrong types.
02357         class C4(tzinfo):
02358             def utcoffset(self, dt): return "aname"
02359             def dst(self, dt): return 7
02360             def tzname(self, dt): return 0
02361         t = cls(1, 1, 1, tzinfo=C4())
02362         self.assertRaises(TypeError, t.utcoffset)
02363         self.assertRaises(TypeError, t.dst)
02364         self.assertRaises(TypeError, t.tzname)
02365 
02366         # Offset out of range.
02367         class C6(tzinfo):
02368             def utcoffset(self, dt): return timedelta(hours=-24)
02369             def dst(self, dt): return timedelta(hours=24)
02370         t = cls(1, 1, 1, tzinfo=C6())
02371         self.assertRaises(ValueError, t.utcoffset)
02372         self.assertRaises(ValueError, t.dst)
02373 
02374         # Not a whole number of minutes.
02375         class C7(tzinfo):
02376             def utcoffset(self, dt): return timedelta(seconds=61)
02377             def dst(self, dt): return timedelta(microseconds=-81)
02378         t = cls(1, 1, 1, tzinfo=C7())
02379         self.assertRaises(ValueError, t.utcoffset)
02380         self.assertRaises(ValueError, t.dst)

Here is the call graph for this function:

Definition at line 2305 of file datetimetester.py.

02305 
02306     def test_utc_offset_out_of_bounds(self):
02307         class Edgy(tzinfo):
02308             def __init__(self, offset):
02309                 self.offset = timedelta(minutes=offset)
02310             def utcoffset(self, dt):
02311                 return self.offset
02312 
02313         cls = self.theclass
02314         for offset, legit in ((-1440, False),
02315                               (-1439, True),
02316                               (1439, True),
02317                               (1440, False)):
02318             if cls is time:
02319                 t = cls(1, 2, 3, tzinfo=Edgy(offset))
02320             elif cls is datetime:
02321                 t = cls(6, 6, 6, 1, 2, 3, tzinfo=Edgy(offset))
02322             else:
02323                 assert 0, "impossible"
02324             if legit:
02325                 aofs = abs(offset)
02326                 h, m = divmod(aofs, 60)
02327                 tag = "%c%02d:%02d" % (offset < 0 and '-' or '+', h, m)
02328                 if isinstance(t, datetime):
02329                     t = t.timetz()
02330                 self.assertEqual(str(t), "01:02:03" + tag)
02331             else:
02332                 self.assertRaises(ValueError, str, t)

Here is the call graph for this function:


Member Data Documentation


The documentation for this class was generated from the following file: