Back to index

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

List of all members.

Public Member Functions

def test_empty
def test_zones
def test_hash_edge_cases
def test_pickling
def test_more_bool
def test_replace
def test_mixed_compare
def test_subclass_timetz
def test_basic_attributes
def test_basic_attributes_nonzero
def test_roundtrip
def test_comparing
def test_bad_constructor_arguments
def test_hash_equality
def test_isoformat
def test_1653736
def test_strftime
def test_format
def test_str
def test_repr
def test_resolution_info
def test_pickling_subclass_time
def test_bool
def test_subclass_time
def test_backdoor_resistance
def test_harmless_mixed_comparison
def test_harmful_mixed_comparison
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

Static Public Attributes

 theclass = time

Detailed Description

Definition at line 2430 of file datetimetester.py.


Member Function Documentation

def test.datetimetester.TestTime.test_1653736 (   self) [inherited]

Definition at line 2122 of file datetimetester.py.

02122 
02123     def test_1653736(self):
02124         # verify it doesn't accept extra keyword arguments
02125         t = self.theclass(second=1)
02126         self.assertRaises(TypeError, t.isoformat, foo=3)

Here is the call graph for this function:

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 2259 of file datetimetester.py.

02259 
02260     def test_backdoor_resistance(self):
02261         # see TestDate.test_backdoor_resistance().
02262         base = '2:59.0'
02263         for hour_byte in ' ', '9', chr(24), '\xff':
02264             self.assertRaises(TypeError, self.theclass,
02265                                          hour_byte + base[1:])
02266 
02267 # A mixin for classes with a tzinfo= argument.  Subclasses must define
02268 # theclass as a class atribute, and theclass(1, 1, 1, tzinfo=whatever)
# must be legit (which is true for time and datetime).

Here is the call graph for this function:

Definition at line 2044 of file datetimetester.py.

02044 
02045     def test_bad_constructor_arguments(self):
02046         # bad hours
02047         self.theclass(0, 0)    # no exception
02048         self.theclass(23, 0)   # no exception
02049         self.assertRaises(ValueError, self.theclass, -1, 0)
02050         self.assertRaises(ValueError, self.theclass, 24, 0)
02051         # bad minutes
02052         self.theclass(23, 0)    # no exception
02053         self.theclass(23, 59)   # no exception
02054         self.assertRaises(ValueError, self.theclass, 23, -1)
02055         self.assertRaises(ValueError, self.theclass, 23, 60)
02056         # bad seconds
02057         self.theclass(23, 59, 0)    # no exception
02058         self.theclass(23, 59, 59)   # no exception
02059         self.assertRaises(ValueError, self.theclass, 23, 59, -1)
02060         self.assertRaises(ValueError, self.theclass, 23, 59, 60)
02061         # bad microseconds
02062         self.theclass(23, 59, 59, 0)        # no exception
02063         self.theclass(23, 59, 59, 999999)   # no exception
02064         self.assertRaises(ValueError, self.theclass, 23, 59, 59, -1)
02065         self.assertRaises(ValueError, self.theclass, 23, 59, 59, 1000000)

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 1970 of file datetimetester.py.

01970 
01971     def test_basic_attributes(self):
01972         t = self.theclass(12, 0)
01973         self.assertEqual(t.hour, 12)
01974         self.assertEqual(t.minute, 0)
01975         self.assertEqual(t.second, 0)
01976         self.assertEqual(t.microsecond, 0)

Here is the call graph for this function:

Definition at line 1977 of file datetimetester.py.

01977 
01978     def test_basic_attributes_nonzero(self):
01979         # Make sure all attributes are non-zero so bugs in
01980         # bit-shifting access show up.
01981         t = self.theclass(12, 59, 59, 8000)
01982         self.assertEqual(t.hour, 12)
01983         self.assertEqual(t.minute, 59)
01984         self.assertEqual(t.second, 59)
01985         self.assertEqual(t.microsecond, 8000)

Here is the call graph for this function:

def test.datetimetester.TestTime.test_bool (   self) [inherited]

Definition at line 2199 of file datetimetester.py.

02199 
02200     def test_bool(self):
02201         cls = self.theclass
02202         self.assertTrue(cls(1))
02203         self.assertTrue(cls(0, 1))
02204         self.assertTrue(cls(0, 0, 1))
02205         self.assertTrue(cls(0, 0, 0, 1))
02206         self.assertTrue(not cls(0))
02207         self.assertTrue(not cls())

Here is the call graph for this function:

def test.datetimetester.TestTime.test_comparing (   self) [inherited]

Definition at line 2001 of file datetimetester.py.

02001 
02002     def test_comparing(self):
02003         args = [1, 2, 3, 4]
02004         t1 = self.theclass(*args)
02005         t2 = self.theclass(*args)
02006         self.assertEqual(t1, t2)
02007         self.assertTrue(t1 <= t2)
02008         self.assertTrue(t1 >= t2)
02009         self.assertTrue(not t1 != t2)
02010         self.assertTrue(not t1 < t2)
02011         self.assertTrue(not t1 > t2)
02012 
02013         for i in range(len(args)):
02014             newargs = args[:]
02015             newargs[i] = args[i] + 1
02016             t2 = self.theclass(*newargs)   # this is larger than t1
02017             self.assertTrue(t1 < t2)
02018             self.assertTrue(t2 > t1)
02019             self.assertTrue(t1 <= t2)
02020             self.assertTrue(t2 >= t1)
02021             self.assertTrue(t1 != t2)
02022             self.assertTrue(t2 != t1)
02023             self.assertTrue(not t1 == t2)
02024             self.assertTrue(not t2 == t1)
02025             self.assertTrue(not t1 > t2)
02026             self.assertTrue(not t2 < t1)
02027             self.assertTrue(not t1 >= t2)
02028             self.assertTrue(not t2 <= t1)
02029 
02030         for badarg in OTHERSTUFF:
02031             self.assertEqual(t1 == badarg, False)
02032             self.assertEqual(t1 != badarg, True)
02033             self.assertEqual(badarg == t1, False)
02034             self.assertEqual(badarg != t1, True)
02035 
02036             self.assertRaises(TypeError, lambda: t1 <= badarg)
02037             self.assertRaises(TypeError, lambda: t1 < badarg)
02038             self.assertRaises(TypeError, lambda: t1 > badarg)
02039             self.assertRaises(TypeError, lambda: t1 >= badarg)
02040             self.assertRaises(TypeError, lambda: badarg <= t1)
02041             self.assertRaises(TypeError, lambda: badarg < t1)
02042             self.assertRaises(TypeError, lambda: badarg > t1)
02043             self.assertRaises(TypeError, lambda: badarg >= t1)

Here is the call graph for this function:

Definition at line 2433 of file datetimetester.py.

02433 
02434     def test_empty(self):
02435         t = self.theclass()
02436         self.assertEqual(t.hour, 0)
02437         self.assertEqual(t.minute, 0)
02438         self.assertEqual(t.second, 0)
02439         self.assertEqual(t.microsecond, 0)
02440         self.assertTrue(t.tzinfo is None)

Here is the call graph for this function:

def test.datetimetester.TestTime.test_format (   self) [inherited]

Definition at line 2133 of file datetimetester.py.

02133 
02134     def test_format(self):
02135         t = self.theclass(1, 2, 3, 4)
02136         self.assertEqual(t.__format__(''), str(t))
02137 
02138         # check that a derived class's __str__() gets called
02139         class A(self.theclass):
02140             def __str__(self):
02141                 return 'A'
02142         a = A(1, 2, 3, 4)
02143         self.assertEqual(a.__format__(''), 'A')
02144 
02145         # check that a derived class's strftime gets called
02146         class B(self.theclass):
02147             def strftime(self, format_spec):
02148                 return 'B'
02149         b = B(1, 2, 3, 4)
02150         self.assertEqual(b.__format__(''), str(t))
02151 
02152         for fmt in ['%H %M %S',
02153                     ]:
02154             self.assertEqual(t.__format__(fmt), t.strftime(fmt))
02155             self.assertEqual(a.__format__(fmt), t.strftime(fmt))
02156             self.assertEqual(b.__format__(fmt), 'B')

Here is the call graph for this function:

Definition at line 271 of file datetimetester.py.

00271 
00272     def test_harmful_mixed_comparison(self):
00273         me = self.theclass(1, 1, 1)
00274 
00275         self.assertRaises(TypeError, lambda: me < ())
00276         self.assertRaises(TypeError, lambda: me <= ())
00277         self.assertRaises(TypeError, lambda: me > ())
00278         self.assertRaises(TypeError, lambda: me >= ())
00279 
00280         self.assertRaises(TypeError, lambda: () < me)
00281         self.assertRaises(TypeError, lambda: () <= me)
00282         self.assertRaises(TypeError, lambda: () > me)
00283         self.assertRaises(TypeError, lambda: () >= me)

Here is the call graph for this function:

Definition at line 260 of file datetimetester.py.

00260 
00261     def test_harmless_mixed_comparison(self):
00262         me = self.theclass(1, 1, 1)
00263 
00264         self.assertFalse(me == ())
00265         self.assertTrue(me != ())
00266         self.assertFalse(() == me)
00267         self.assertTrue(() != me)
00268 
00269         self.assertIn(me, [1, 20, [], me])
00270         self.assertIn([], [me, 1, 20, []])

Here is the call graph for this function:

Definition at line 2528 of file datetimetester.py.

02528 
02529     def test_hash_edge_cases(self):
02530         # Offsets that overflow a basic time.
02531         t1 = self.theclass(0, 1, 2, 3, tzinfo=FixedOffset(1439, ""))
02532         t2 = self.theclass(0, 0, 2, 3, tzinfo=FixedOffset(1438, ""))
02533         self.assertEqual(hash(t1), hash(t2))
02534 
02535         t1 = self.theclass(23, 58, 6, 100, tzinfo=FixedOffset(-1000, ""))
02536         t2 = self.theclass(23, 48, 6, 100, tzinfo=FixedOffset(-1010, ""))
02537         self.assertEqual(hash(t1), hash(t2))

Here is the call graph for this function:

Definition at line 2066 of file datetimetester.py.

02066 
02067     def test_hash_equality(self):
02068         d = self.theclass(23, 30, 17)
02069         e = self.theclass(23, 30, 17)
02070         self.assertEqual(d, e)
02071         self.assertEqual(hash(d), hash(e))
02072 
02073         dic = {d: 1}
02074         dic[e] = 2
02075         self.assertEqual(len(dic), 1)
02076         self.assertEqual(dic[d], 2)
02077         self.assertEqual(dic[e], 2)
02078 
02079         d = self.theclass(0,  5, 17)
02080         e = self.theclass(0,  5, 17)
02081         self.assertEqual(d, e)
02082         self.assertEqual(hash(d), hash(e))
02083 
02084         dic = {d: 1}
02085         dic[e] = 2
02086         self.assertEqual(len(dic), 1)
02087         self.assertEqual(dic[d], 2)
02088         self.assertEqual(dic[e], 2)

Here is the call graph for this function:

def test.datetimetester.TestTime.test_isoformat (   self) [inherited]

Definition at line 2089 of file datetimetester.py.

02089 
02090     def test_isoformat(self):
02091         t = self.theclass(4, 5, 1, 123)
02092         self.assertEqual(t.isoformat(), "04:05:01.000123")
02093         self.assertEqual(t.isoformat(), str(t))
02094 
02095         t = self.theclass()
02096         self.assertEqual(t.isoformat(), "00:00:00")
02097         self.assertEqual(t.isoformat(), str(t))
02098 
02099         t = self.theclass(microsecond=1)
02100         self.assertEqual(t.isoformat(), "00:00:00.000001")
02101         self.assertEqual(t.isoformat(), str(t))
02102 
02103         t = self.theclass(microsecond=10)
02104         self.assertEqual(t.isoformat(), "00:00:00.000010")
02105         self.assertEqual(t.isoformat(), str(t))
02106 
02107         t = self.theclass(microsecond=100)
02108         self.assertEqual(t.isoformat(), "00:00:00.000100")
02109         self.assertEqual(t.isoformat(), str(t))
02110 
02111         t = self.theclass(microsecond=1000)
02112         self.assertEqual(t.isoformat(), "00:00:00.001000")
02113         self.assertEqual(t.isoformat(), str(t))
02114 
02115         t = self.theclass(microsecond=10000)
02116         self.assertEqual(t.isoformat(), "00:00:00.010000")
02117         self.assertEqual(t.isoformat(), str(t))
02118 
02119         t = self.theclass(microsecond=100000)
02120         self.assertEqual(t.isoformat(), "00:00:00.100000")
02121         self.assertEqual(t.isoformat(), str(t))

Here is the call graph for this function:

Definition at line 2625 of file datetimetester.py.

02625 
02626     def test_mixed_compare(self):
02627         t1 = time(1, 2, 3)
02628         t2 = time(1, 2, 3)
02629         self.assertEqual(t1, t2)
02630         t2 = t2.replace(tzinfo=None)
02631         self.assertEqual(t1, t2)
02632         t2 = t2.replace(tzinfo=FixedOffset(None, ""))
02633         self.assertEqual(t1, t2)
02634         t2 = t2.replace(tzinfo=FixedOffset(0, ""))
02635         self.assertRaises(TypeError, lambda: t1 == t2)
02636 
02637         # In time w/ identical tzinfo objects, utcoffset is ignored.
02638         class Varies(tzinfo):
02639             def __init__(self):
02640                 self.offset = timedelta(minutes=22)
02641             def utcoffset(self, t):
02642                 self.offset += timedelta(minutes=1)
02643                 return self.offset
02644 
02645         v = Varies()
02646         t1 = t2.replace(tzinfo=v)
02647         t2 = t2.replace(tzinfo=v)
02648         self.assertEqual(t1.utcoffset(), timedelta(minutes=23))
02649         self.assertEqual(t2.utcoffset(), timedelta(minutes=24))
02650         self.assertEqual(t1, t2)
02651 
02652         # But if they're not identical, it isn't ignored.
02653         t2 = t2.replace(tzinfo=Varies())
02654         self.assertTrue(t1 < t2)  # t1's offset counter still going up

Here is the call graph for this function:

Definition at line 2558 of file datetimetester.py.

02558 
02559     def test_more_bool(self):
02560         # Test cases with non-None tzinfo.
02561         cls = self.theclass
02562 
02563         t = cls(0, tzinfo=FixedOffset(-300, ""))
02564         self.assertTrue(t)
02565 
02566         t = cls(5, tzinfo=FixedOffset(-300, ""))
02567         self.assertTrue(t)
02568 
02569         t = cls(5, tzinfo=FixedOffset(300, ""))
02570         self.assertTrue(not t)
02571 
02572         t = cls(23, 59, tzinfo=FixedOffset(23*60 + 59, ""))
02573         self.assertTrue(not t)
02574 
02575         # Mostly ensuring this doesn't overflow internally.
02576         t = cls(0, tzinfo=FixedOffset(23*60 + 59, ""))
02577         self.assertTrue(t)
02578 
02579         # But this should yield a value error -- the utcoffset is bogus.
02580         t = cls(0, tzinfo=FixedOffset(24*60, ""))
02581         self.assertRaises(ValueError, lambda: bool(t))
02582 
02583         # Likewise.
02584         t = cls(0, tzinfo=FixedOffset(-24*60, ""))
02585         self.assertRaises(ValueError, lambda: bool(t))

Here is the call graph for this function:

Reimplemented from test.datetimetester.TestTime.

Definition at line 2538 of file datetimetester.py.

02538 
02539     def test_pickling(self):
02540         # Try one without a tzinfo.
02541         args = 20, 59, 16, 64**2
02542         orig = self.theclass(*args)
02543         for pickler, unpickler, proto in pickle_choices:
02544             green = pickler.dumps(orig, proto)
02545             derived = unpickler.loads(green)
02546             self.assertEqual(orig, derived)
02547 
02548         # Try one with a tzinfo.
02549         tinfo = PicklableFixedOffset(-300, 'cookie')
02550         orig = self.theclass(5, 6, 7, tzinfo=tinfo)
02551         for pickler, unpickler, proto in pickle_choices:
02552             green = pickler.dumps(orig, proto)
02553             derived = unpickler.loads(green)
02554             self.assertEqual(orig, derived)
02555             self.assertIsInstance(derived.tzinfo, PicklableFixedOffset)
02556             self.assertEqual(derived.utcoffset(), timedelta(minutes=-300))
02557             self.assertEqual(derived.tzname(), 'cookie')

Here is the call graph for this function:

Definition at line 2191 of file datetimetester.py.

02191 
02192     def test_pickling_subclass_time(self):
02193         args = 20, 59, 16, 64**2
02194         orig = SubclassTime(*args)
02195         for pickler, unpickler, proto in pickle_choices:
02196             green = pickler.dumps(orig, proto)
02197             derived = unpickler.loads(green)
02198             self.assertEqual(orig, derived)

Here is the call graph for this function:

Reimplemented from test.datetimetester.TestTime.

Definition at line 2586 of file datetimetester.py.

02586 
02587     def test_replace(self):
02588         cls = self.theclass
02589         z100 = FixedOffset(100, "+100")
02590         zm200 = FixedOffset(timedelta(minutes=-200), "-200")
02591         args = [1, 2, 3, 4, z100]
02592         base = cls(*args)
02593         self.assertEqual(base, base.replace())
02594 
02595         i = 0
02596         for name, newval in (("hour", 5),
02597                              ("minute", 6),
02598                              ("second", 7),
02599                              ("microsecond", 8),
02600                              ("tzinfo", zm200)):
02601             newargs = args[:]
02602             newargs[i] = newval
02603             expected = cls(*newargs)
02604             got = base.replace(**{name: newval})
02605             self.assertEqual(expected, got)
02606             i += 1
02607 
02608         # Ensure we can get rid of a tzinfo.
02609         self.assertEqual(base.tzname(), "+100")
02610         base2 = base.replace(tzinfo=None)
02611         self.assertTrue(base2.tzinfo is None)
02612         self.assertTrue(base2.tzname() is None)
02613 
02614         # Ensure we can add one.
02615         base3 = base2.replace(tzinfo=z100)
02616         self.assertEqual(base, base3)
02617         self.assertTrue(base.tzinfo is base3.tzinfo)
02618 
02619         # Out of bounds.
02620         base = cls(1)
02621         self.assertRaises(ValueError, base.replace, hour=24)
02622         self.assertRaises(ValueError, base.replace, minute=-1)
02623         self.assertRaises(ValueError, base.replace, second=100)
02624         self.assertRaises(ValueError, base.replace, microsecond=1000000)

Here is the call graph for this function:

def test.datetimetester.TestTime.test_repr (   self) [inherited]

Definition at line 2164 of file datetimetester.py.

02164 
02165     def test_repr(self):
02166         name = 'datetime.' + self.theclass.__name__
02167         self.assertEqual(repr(self.theclass(1, 2, 3, 4)),
02168                          "%s(1, 2, 3, 4)" % name)
02169         self.assertEqual(repr(self.theclass(10, 2, 3, 4000)),
02170                          "%s(10, 2, 3, 4000)" % name)
02171         self.assertEqual(repr(self.theclass(0, 2, 3, 400000)),
02172                          "%s(0, 2, 3, 400000)" % name)
02173         self.assertEqual(repr(self.theclass(12, 2, 3, 0)),
02174                          "%s(12, 2, 3)" % name)
02175         self.assertEqual(repr(self.theclass(23, 15, 0, 0)),
02176                          "%s(23, 15)" % name)

Here is the call graph for this function:

Definition at line 2177 of file datetimetester.py.

02177 
02178     def test_resolution_info(self):
02179         self.assertIsInstance(self.theclass.min, self.theclass)
02180         self.assertIsInstance(self.theclass.max, self.theclass)
02181         self.assertIsInstance(self.theclass.resolution, timedelta)
02182         self.assertTrue(self.theclass.max > self.theclass.min)

Here is the call graph for this function:

def test.datetimetester.TestTime.test_roundtrip (   self) [inherited]

Definition at line 1986 of file datetimetester.py.

01986 
01987     def test_roundtrip(self):
01988         t = self.theclass(1, 2, 3, 4)
01989 
01990         # Verify t -> string -> time identity.
01991         s = repr(t)
01992         self.assertTrue(s.startswith('datetime.'))
01993         s = s[9:]
01994         t2 = eval(s)
01995         self.assertEqual(t, t2)
01996 
01997         # Verify identity via reconstructing from pieces.
01998         t2 = self.theclass(t.hour, t.minute, t.second,
01999                            t.microsecond)
02000         self.assertEqual(t, t2)

Here is the call graph for this function:

def test.datetimetester.TestTime.test_str (   self) [inherited]

Definition at line 2157 of file datetimetester.py.

02157 
02158     def test_str(self):
02159         self.assertEqual(str(self.theclass(1, 2, 3, 4)), "01:02:03.000004")
02160         self.assertEqual(str(self.theclass(10, 2, 3, 4000)), "10:02:03.004000")
02161         self.assertEqual(str(self.theclass(0, 2, 3, 400000)), "00:02:03.400000")
02162         self.assertEqual(str(self.theclass(12, 2, 3, 0)), "12:02:03")
02163         self.assertEqual(str(self.theclass(23, 15, 0, 0)), "23:15:00")

Here is the call graph for this function:

def test.datetimetester.TestTime.test_strftime (   self) [inherited]

Definition at line 2127 of file datetimetester.py.

02127 
02128     def test_strftime(self):
02129         t = self.theclass(1, 2, 3, 4)
02130         self.assertEqual(t.strftime('%H %M %S %f'), "01 02 03 000004")
02131         # A naive object replaces %z and %Z with empty strings.
02132         self.assertEqual(t.strftime("'%z' '%Z'"), "'' ''")

Here is the call graph for this function:

Definition at line 2233 of file datetimetester.py.

02233 
02234     def test_subclass_time(self):
02235 
02236         class C(self.theclass):
02237             theAnswer = 42
02238 
02239             def __new__(cls, *args, **kws):
02240                 temp = kws.copy()
02241                 extra = temp.pop('extra')
02242                 result = self.theclass.__new__(cls, *args, **temp)
02243                 result.extra = extra
02244                 return result
02245 
02246             def newmeth(self, start):
02247                 return start + self.hour + self.second
02248 
02249         args = 4, 5, 6
02250 
02251         dt1 = self.theclass(*args)
02252         dt2 = C(*args, **{'extra': 7})
02253 
02254         self.assertEqual(dt2.__class__, C)
02255         self.assertEqual(dt2.theAnswer, 42)
02256         self.assertEqual(dt2.extra, 7)
02257         self.assertEqual(dt1.isoformat(), dt2.isoformat())
02258         self.assertEqual(dt2.newmeth(-7), dt1.hour + dt1.second - 7)

Here is the call graph for this function:

Definition at line 2655 of file datetimetester.py.

02655 
02656     def test_subclass_timetz(self):
02657 
02658         class C(self.theclass):
02659             theAnswer = 42
02660 
02661             def __new__(cls, *args, **kws):
02662                 temp = kws.copy()
02663                 extra = temp.pop('extra')
02664                 result = self.theclass.__new__(cls, *args, **temp)
02665                 result.extra = extra
02666                 return result
02667 
02668             def newmeth(self, start):
02669                 return start + self.hour + self.second
02670 
02671         args = 4, 5, 6, 500, FixedOffset(-300, "EST", 1)
02672 
02673         dt1 = self.theclass(*args)
02674         dt2 = C(*args, **{'extra': 7})
02675 
02676         self.assertEqual(dt2.__class__, C)
02677         self.assertEqual(dt2.theAnswer, 42)
02678         self.assertEqual(dt2.extra, 7)
02679         self.assertEqual(dt1.utcoffset(), dt2.utcoffset())
02680         self.assertEqual(dt2.newmeth(-7), dt1.hour + dt1.second - 7)
02681 
02682 
02683 # Testing datetime objects with a non-None tzinfo.

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:

Definition at line 2441 of file datetimetester.py.

02441 
02442     def test_zones(self):
02443         est = FixedOffset(-300, "EST", 1)
02444         utc = FixedOffset(0, "UTC", -2)
02445         met = FixedOffset(60, "MET", 3)
02446         t1 = time( 7, 47, tzinfo=est)
02447         t2 = time(12, 47, tzinfo=utc)
02448         t3 = time(13, 47, tzinfo=met)
02449         t4 = time(microsecond=40)
02450         t5 = time(microsecond=40, tzinfo=utc)
02451 
02452         self.assertEqual(t1.tzinfo, est)
02453         self.assertEqual(t2.tzinfo, utc)
02454         self.assertEqual(t3.tzinfo, met)
02455         self.assertTrue(t4.tzinfo is None)
02456         self.assertEqual(t5.tzinfo, utc)
02457 
02458         self.assertEqual(t1.utcoffset(), timedelta(minutes=-300))
02459         self.assertEqual(t2.utcoffset(), timedelta(minutes=0))
02460         self.assertEqual(t3.utcoffset(), timedelta(minutes=60))
02461         self.assertTrue(t4.utcoffset() is None)
02462         self.assertRaises(TypeError, t1.utcoffset, "no args")
02463 
02464         self.assertEqual(t1.tzname(), "EST")
02465         self.assertEqual(t2.tzname(), "UTC")
02466         self.assertEqual(t3.tzname(), "MET")
02467         self.assertTrue(t4.tzname() is None)
02468         self.assertRaises(TypeError, t1.tzname, "no args")
02469 
02470         self.assertEqual(t1.dst(), timedelta(minutes=1))
02471         self.assertEqual(t2.dst(), timedelta(minutes=-2))
02472         self.assertEqual(t3.dst(), timedelta(minutes=3))
02473         self.assertTrue(t4.dst() is None)
02474         self.assertRaises(TypeError, t1.dst, "no args")
02475 
02476         self.assertEqual(hash(t1), hash(t2))
02477         self.assertEqual(hash(t1), hash(t3))
02478         self.assertEqual(hash(t2), hash(t3))
02479 
02480         self.assertEqual(t1, t2)
02481         self.assertEqual(t1, t3)
02482         self.assertEqual(t2, t3)
02483         self.assertRaises(TypeError, lambda: t4 == t5) # mixed tz-aware & naive
02484         self.assertRaises(TypeError, lambda: t4 < t5) # mixed tz-aware & naive
02485         self.assertRaises(TypeError, lambda: t5 < t4) # mixed tz-aware & naive
02486 
02487         self.assertEqual(str(t1), "07:47:00-05:00")
02488         self.assertEqual(str(t2), "12:47:00+00:00")
02489         self.assertEqual(str(t3), "13:47:00+01:00")
02490         self.assertEqual(str(t4), "00:00:00.000040")
02491         self.assertEqual(str(t5), "00:00:00.000040+00:00")
02492 
02493         self.assertEqual(t1.isoformat(), "07:47:00-05:00")
02494         self.assertEqual(t2.isoformat(), "12:47:00+00:00")
02495         self.assertEqual(t3.isoformat(), "13:47:00+01:00")
02496         self.assertEqual(t4.isoformat(), "00:00:00.000040")
02497         self.assertEqual(t5.isoformat(), "00:00:00.000040+00:00")
02498 
02499         d = 'datetime.time'
02500         self.assertEqual(repr(t1), d + "(7, 47, tzinfo=est)")
02501         self.assertEqual(repr(t2), d + "(12, 47, tzinfo=utc)")
02502         self.assertEqual(repr(t3), d + "(13, 47, tzinfo=met)")
02503         self.assertEqual(repr(t4), d + "(0, 0, 0, 40)")
02504         self.assertEqual(repr(t5), d + "(0, 0, 0, 40, tzinfo=utc)")
02505 
02506         self.assertEqual(t1.strftime("%H:%M:%S %%Z=%Z %%z=%z"),
02507                                      "07:47:00 %Z=EST %z=-0500")
02508         self.assertEqual(t2.strftime("%H:%M:%S %Z %z"), "12:47:00 UTC +0000")
02509         self.assertEqual(t3.strftime("%H:%M:%S %Z %z"), "13:47:00 MET +0100")
02510 
02511         yuck = FixedOffset(-1439, "%z %Z %%z%%Z")
02512         t1 = time(23, 59, tzinfo=yuck)
02513         self.assertEqual(t1.strftime("%H:%M %%Z='%Z' %%z='%z'"),
02514                                      "23:59 %Z='%z %Z %%z%%Z' %z='-2359'")
02515 
02516         # Check that an invalid tzname result raises an exception.
02517         class Badtzname(tzinfo):
02518             tz = 42
02519             def tzname(self, dt): return self.tz
02520         t = time(2, 3, 4, tzinfo=Badtzname())
02521         self.assertEqual(t.strftime("%H:%M:%S"), "02:03:04")
02522         self.assertRaises(TypeError, t.strftime, "%Z")
02523 
02524         # Issue #6697:
02525         if '_Fast' in str(type(self)):
02526             Badtzname.tz = '\ud800'
02527             self.assertRaises(ValueError, t.strftime, "%Z")

Here is the call graph for this function:


Member Data Documentation

Reimplemented from test.datetimetester.TZInfoBase.

Definition at line 2639 of file datetimetester.py.

Reimplemented from test.datetimetester.TestTime.

Definition at line 2431 of file datetimetester.py.


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