Back to index

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

List of all members.

Public Member Functions

def test_trivial
def test_even_more_compare
def test_pickling
def test_extreme_hashes
def test_zones
def test_combine
def test_extract
def test_tz_aware_arithmetic
def test_tzinfo_now
def test_tzinfo_fromtimestamp
def test_tzinfo_utcnow
def test_tzinfo_utcfromtimestamp
def test_tzinfo_timetuple
def test_utctimetuple
def test_tzinfo_isoformat
def test_replace
def test_more_astimezone
def test_aware_subtract
def test_mixed_compare
def test_subclass_datetimetz
def test_basic_attributes
def test_basic_attributes_nonzero
def test_roundtrip
def test_isoformat
def test_format
def test_more_ctime
def test_tz_independent_comparing
def test_strftime_with_bad_tzname_replace
def test_bad_constructor_arguments
def test_hash_equality
def test_computations
def test_more_pickling
def test_pickling_subclass_datetime
def test_more_compare
def verify_field_equality
def test_fromtimestamp
def test_utcfromtimestamp
def test_microsecond_rounding
def test_insane_fromtimestamp
def test_insane_utcfromtimestamp
def test_negative_float_fromtimestamp
def test_negative_float_utcfromtimestamp
def test_utcnow
def test_strptime
def test_more_timetuple
def test_more_strftime
def test_astimezone
def test_subclass_datetime
def test_ordinal_conversions
def test_extreme_ordinals
def test_overflow
def test_today
def test_weekday
def test_isocalendar
def test_iso_long_years
def test_ctime
def test_strftime
def test_resolution_info
def test_extreme_timedelta
def test_timetuple
def test_compare
def test_bool
def test_strftime_out_of_range
def test_subclass_date
def test_pickling_subclass_date
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

 dstvalue
 uofs
 offset

Static Public Attributes

 theclass = datetime

Detailed Description

Definition at line 2684 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 1905 of file datetimetester.py.

01905 
01906     def test_astimezone(self):
01907         # Pretty boring!  The TZ test is more interesting here.  astimezone()
01908         # simply can't be applied to a naive object.
01909         dt = self.theclass.now()
01910         f = FixedOffset(44, "")
01911         self.assertRaises(TypeError, dt.astimezone) # not enough args
01912         self.assertRaises(TypeError, dt.astimezone, f, f) # too many args
01913         self.assertRaises(TypeError, dt.astimezone, dt) # arg wrong type
01914         self.assertRaises(ValueError, dt.astimezone, f) # naive
01915         self.assertRaises(ValueError, dt.astimezone, tz=f)  # naive
01916 
01917         class Bogus(tzinfo):
01918             def utcoffset(self, dt): return None
01919             def dst(self, dt): return timedelta(0)
01920         bog = Bogus()
01921         self.assertRaises(ValueError, dt.astimezone, bog)   # naive
01922         self.assertRaises(ValueError,
01923                           dt.replace(tzinfo=bog).astimezone, f)
01924 
01925         class AlsoBogus(tzinfo):
01926             def utcoffset(self, dt): return timedelta(0)
01927             def dst(self, dt): return None
01928         alsobog = AlsoBogus()
01929         self.assertRaises(ValueError, dt.astimezone, alsobog) # also naive

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

03212 
03213     def test_aware_subtract(self):
03214         cls = self.theclass
03215 
03216         # Ensure that utcoffset() is ignored when the operands have the
03217         # same tzinfo member.
03218         class OperandDependentOffset(tzinfo):
03219             def utcoffset(self, t):
03220                 if t.minute < 10:
03221                     # d0 and d1 equal after adjustment
03222                     return timedelta(minutes=t.minute)
03223                 else:
03224                     # d2 off in the weeds
03225                     return timedelta(minutes=59)
03226 
03227         base = cls(8, 9, 10, 11, 12, 13, 14, tzinfo=OperandDependentOffset())
03228         d0 = base.replace(minute=3)
03229         d1 = base.replace(minute=9)
03230         d2 = base.replace(minute=11)
03231         for x in d0, d1, d2:
03232             for y in d0, d1, d2:
03233                 got = x - y
03234                 expected = timedelta(minutes=x.minute - y.minute)
03235                 self.assertEqual(got, expected)
03236 
03237         # OTOH, if the tzinfo members are distinct, utcoffsets aren't
03238         # ignored.
03239         base = cls(8, 9, 10, 11, 12, 13, 14)
03240         d0 = base.replace(minute=3, tzinfo=OperandDependentOffset())
03241         d1 = base.replace(minute=9, tzinfo=OperandDependentOffset())
03242         d2 = base.replace(minute=11, tzinfo=OperandDependentOffset())
03243         for x in d0, d1, d2:
03244             for y in d0, d1, d2:
03245                 got = x - y
03246                 if (x is d0 or x is d1) and (y is d0 or y is d1):
03247                     expected = timedelta(0)
03248                 elif x is y is d2:
03249                     expected = timedelta(0)
03250                 elif x is d2:
03251                     expected = timedelta(minutes=(11-59)-0)
03252                 else:
03253                     assert y is d2
03254                     expected = timedelta(minutes=0-(11-59))
03255                 self.assertEqual(got, expected)

Here is the call graph for this function:

Definition at line 1355 of file datetimetester.py.

01355 
01356     def test_backdoor_resistance(self):
01357         # For fast unpickling, the constructor accepts a pickle byte string.
01358         # This is a low-overhead backdoor.  A user can (by intent or
01359         # mistake) pass a string directly, which (if it's the right length)
01360         # will get treated like a pickle, and bypass the normal sanity
01361         # checks in the constructor.  This can create insane objects.
01362         # The constructor doesn't want to burn the time to validate all
01363         # fields, but does check the month field.  This stops, e.g.,
01364         # datetime.datetime('1995-03-25') from yielding an insane object.
01365         base = b'1995-03-25'
01366         if not issubclass(self.theclass, datetime):
01367             base = base[:4]
01368         for month_byte in b'9', b'\0', b'\r', b'\xff':
01369             self.assertRaises(TypeError, self.theclass,
01370                                          base[:2] + month_byte + base[3:])
01371         # Good bytes, but bad tzinfo:
01372         self.assertRaises(TypeError, self.theclass,
01373                           bytes([1] * len(base)), 'EST')
01374 
01375         for ord_byte in range(1, 13):
01376             # This shouldn't blow up because of the month byte alone.  If
01377             # the implementation changes to do more-careful checking, it may
01378             # blow up because other fields are insane.
01379             self.theclass(base[:2] + bytes([ord_byte]) + base[3:])

Here is the call graph for this function:

Reimplemented from test.datetimetester.TestDate.

Definition at line 1515 of file datetimetester.py.

01515 
01516     def test_bad_constructor_arguments(self):
01517         # bad years
01518         self.theclass(MINYEAR, 1, 1)  # no exception
01519         self.theclass(MAXYEAR, 1, 1)  # no exception
01520         self.assertRaises(ValueError, self.theclass, MINYEAR-1, 1, 1)
01521         self.assertRaises(ValueError, self.theclass, MAXYEAR+1, 1, 1)
01522         # bad months
01523         self.theclass(2000, 1, 1)    # no exception
01524         self.theclass(2000, 12, 1)   # no exception
01525         self.assertRaises(ValueError, self.theclass, 2000, 0, 1)
01526         self.assertRaises(ValueError, self.theclass, 2000, 13, 1)
01527         # bad days
01528         self.theclass(2000, 2, 29)   # no exception
01529         self.theclass(2004, 2, 29)   # no exception
01530         self.theclass(2400, 2, 29)   # no exception
01531         self.assertRaises(ValueError, self.theclass, 2000, 2, 30)
01532         self.assertRaises(ValueError, self.theclass, 2001, 2, 29)
01533         self.assertRaises(ValueError, self.theclass, 2100, 2, 29)
01534         self.assertRaises(ValueError, self.theclass, 1900, 2, 29)
01535         self.assertRaises(ValueError, self.theclass, 2000, 1, 0)
01536         self.assertRaises(ValueError, self.theclass, 2000, 1, 32)
01537         # bad hours
01538         self.theclass(2000, 1, 31, 0)    # no exception
01539         self.theclass(2000, 1, 31, 23)   # no exception
01540         self.assertRaises(ValueError, self.theclass, 2000, 1, 31, -1)
01541         self.assertRaises(ValueError, self.theclass, 2000, 1, 31, 24)
01542         # bad minutes
01543         self.theclass(2000, 1, 31, 23, 0)    # no exception
01544         self.theclass(2000, 1, 31, 23, 59)   # no exception
01545         self.assertRaises(ValueError, self.theclass, 2000, 1, 31, 23, -1)
01546         self.assertRaises(ValueError, self.theclass, 2000, 1, 31, 23, 60)
01547         # bad seconds
01548         self.theclass(2000, 1, 31, 23, 59, 0)    # no exception
01549         self.theclass(2000, 1, 31, 23, 59, 59)   # no exception
01550         self.assertRaises(ValueError, self.theclass, 2000, 1, 31, 23, 59, -1)
01551         self.assertRaises(ValueError, self.theclass, 2000, 1, 31, 23, 59, 60)
01552         # bad microseconds
01553         self.theclass(2000, 1, 31, 23, 59, 59, 0)    # no exception
01554         self.theclass(2000, 1, 31, 23, 59, 59, 999999)   # no exception
01555         self.assertRaises(ValueError, self.theclass,
01556                           2000, 1, 31, 23, 59, 59, -1)
01557         self.assertRaises(ValueError, self.theclass,
01558                           2000, 1, 31, 23, 59, 59,
01559                           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:

Reimplemented from test.datetimetester.TestDate.

Definition at line 1390 of file datetimetester.py.

01390 
01391     def test_basic_attributes(self):
01392         dt = self.theclass(2002, 3, 1, 12, 0)
01393         self.assertEqual(dt.year, 2002)
01394         self.assertEqual(dt.month, 3)
01395         self.assertEqual(dt.day, 1)
01396         self.assertEqual(dt.hour, 12)
01397         self.assertEqual(dt.minute, 0)
01398         self.assertEqual(dt.second, 0)
01399         self.assertEqual(dt.microsecond, 0)

Here is the call graph for this function:

Definition at line 1400 of file datetimetester.py.

01400 
01401     def test_basic_attributes_nonzero(self):
01402         # Make sure all attributes are non-zero so bugs in
01403         # bit-shifting access show up.
01404         dt = self.theclass(2002, 3, 1, 12, 59, 59, 8000)
01405         self.assertEqual(dt.year, 2002)
01406         self.assertEqual(dt.month, 3)
01407         self.assertEqual(dt.day, 1)
01408         self.assertEqual(dt.hour, 12)
01409         self.assertEqual(dt.minute, 59)
01410         self.assertEqual(dt.second, 59)
01411         self.assertEqual(dt.microsecond, 8000)

Here is the call graph for this function:

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

Definition at line 1287 of file datetimetester.py.

01287 
01288     def test_bool(self):
01289         # All dates are considered true.
01290         self.assertTrue(self.theclass.min)
01291         self.assertTrue(self.theclass.max)

Here is the call graph for this function:

Reimplemented from test.datetimetester.TestDateTime.

Definition at line 2829 of file datetimetester.py.

02829 
02830     def test_combine(self):
02831         met = FixedOffset(60, "MET")
02832         d = date(2002, 3, 4)
02833         tz = time(18, 45, 3, 1234, tzinfo=met)
02834         dt = datetime.combine(d, tz)
02835         self.assertEqual(dt, datetime(2002, 3, 4, 18, 45, 3, 1234,
02836                                         tzinfo=met))

Here is the call graph for this function:

def test.datetimetester.TestDate.test_compare (   self) [inherited]

Definition at line 1197 of file datetimetester.py.

01197 
01198     def test_compare(self):
01199         t1 = self.theclass(2, 3, 4)
01200         t2 = self.theclass(2, 3, 4)
01201         self.assertEqual(t1, t2)
01202         self.assertTrue(t1 <= t2)
01203         self.assertTrue(t1 >= t2)
01204         self.assertTrue(not t1 != t2)
01205         self.assertTrue(not t1 < t2)
01206         self.assertTrue(not t1 > t2)
01207 
01208         for args in (3, 3, 3), (2, 4, 4), (2, 3, 5):
01209             t2 = self.theclass(*args)   # this is larger than t1
01210             self.assertTrue(t1 < t2)
01211             self.assertTrue(t2 > t1)
01212             self.assertTrue(t1 <= t2)
01213             self.assertTrue(t2 >= t1)
01214             self.assertTrue(t1 != t2)
01215             self.assertTrue(t2 != t1)
01216             self.assertTrue(not t1 == t2)
01217             self.assertTrue(not t2 == t1)
01218             self.assertTrue(not t1 > t2)
01219             self.assertTrue(not t2 < t1)
01220             self.assertTrue(not t1 >= t2)
01221             self.assertTrue(not t2 <= t1)
01222 
01223         for badarg in OTHERSTUFF:
01224             self.assertEqual(t1 == badarg, False)
01225             self.assertEqual(t1 != badarg, True)
01226             self.assertEqual(badarg == t1, False)
01227             self.assertEqual(badarg != t1, True)
01228 
01229             self.assertRaises(TypeError, lambda: t1 < badarg)
01230             self.assertRaises(TypeError, lambda: t1 > badarg)
01231             self.assertRaises(TypeError, lambda: t1 >= badarg)
01232             self.assertRaises(TypeError, lambda: badarg <= t1)
01233             self.assertRaises(TypeError, lambda: badarg < t1)
01234             self.assertRaises(TypeError, lambda: badarg > t1)
01235             self.assertRaises(TypeError, lambda: badarg >= t1)

Here is the call graph for this function:

Reimplemented from test.datetimetester.TestDate.

Definition at line 1583 of file datetimetester.py.

01583 
01584     def test_computations(self):
01585         a = self.theclass(2002, 1, 31)
01586         b = self.theclass(1956, 1, 31)
01587         diff = a-b
01588         self.assertEqual(diff.days, 46*365 + len(range(1956, 2002, 4)))
01589         self.assertEqual(diff.seconds, 0)
01590         self.assertEqual(diff.microseconds, 0)
01591         a = self.theclass(2002, 3, 2, 17, 6)
01592         millisec = timedelta(0, 0, 1000)
01593         hour = timedelta(0, 3600)
01594         day = timedelta(1)
01595         week = timedelta(7)
01596         self.assertEqual(a + hour, self.theclass(2002, 3, 2, 18, 6))
01597         self.assertEqual(hour + a, self.theclass(2002, 3, 2, 18, 6))
01598         self.assertEqual(a + 10*hour, self.theclass(2002, 3, 3, 3, 6))
01599         self.assertEqual(a - hour, self.theclass(2002, 3, 2, 16, 6))
01600         self.assertEqual(-hour + a, self.theclass(2002, 3, 2, 16, 6))
01601         self.assertEqual(a - hour, a + -hour)
01602         self.assertEqual(a - 20*hour, self.theclass(2002, 3, 1, 21, 6))
01603         self.assertEqual(a + day, self.theclass(2002, 3, 3, 17, 6))
01604         self.assertEqual(a - day, self.theclass(2002, 3, 1, 17, 6))
01605         self.assertEqual(a + week, self.theclass(2002, 3, 9, 17, 6))
01606         self.assertEqual(a - week, self.theclass(2002, 2, 23, 17, 6))
01607         self.assertEqual(a + 52*week, self.theclass(2003, 3, 1, 17, 6))
01608         self.assertEqual(a - 52*week, self.theclass(2001, 3, 3, 17, 6))
01609         self.assertEqual((a + week) - a, week)
01610         self.assertEqual((a + day) - a, day)
01611         self.assertEqual((a + hour) - a, hour)
01612         self.assertEqual((a + millisec) - a, millisec)
01613         self.assertEqual((a - week) - a, -week)
01614         self.assertEqual((a - day) - a, -day)
01615         self.assertEqual((a - hour) - a, -hour)
01616         self.assertEqual((a - millisec) - a, -millisec)
01617         self.assertEqual(a - (a + week), -week)
01618         self.assertEqual(a - (a + day), -day)
01619         self.assertEqual(a - (a + hour), -hour)
01620         self.assertEqual(a - (a + millisec), -millisec)
01621         self.assertEqual(a - (a - week), week)
01622         self.assertEqual(a - (a - day), day)
01623         self.assertEqual(a - (a - hour), hour)
01624         self.assertEqual(a - (a - millisec), millisec)
01625         self.assertEqual(a + (week + day + hour + millisec),
01626                          self.theclass(2002, 3, 10, 18, 6, 0, 1000))
01627         self.assertEqual(a + (week + day + hour + millisec),
01628                          (((a + week) + day) + hour) + millisec)
01629         self.assertEqual(a - (week + day + hour + millisec),
01630                          self.theclass(2002, 2, 22, 16, 5, 59, 999000))
01631         self.assertEqual(a - (week + day + hour + millisec),
01632                          (((a - week) - day) - hour) - millisec)
01633         # Add/sub ints or floats should be illegal
01634         for i in 1, 1.0:
01635             self.assertRaises(TypeError, lambda: a+i)
01636             self.assertRaises(TypeError, lambda: a-i)
01637             self.assertRaises(TypeError, lambda: i+a)
01638             self.assertRaises(TypeError, lambda: i-a)
01639 
01640         # delta - datetime is senseless.
01641         self.assertRaises(TypeError, lambda: day - a)
01642         # mixing datetime and (delta or datetime) via * or // is senseless
01643         self.assertRaises(TypeError, lambda: day * a)
01644         self.assertRaises(TypeError, lambda: a * day)
01645         self.assertRaises(TypeError, lambda: day // a)
01646         self.assertRaises(TypeError, lambda: a // day)
01647         self.assertRaises(TypeError, lambda: a * a)
01648         self.assertRaises(TypeError, lambda: a // a)
01649         # datetime + datetime is senseless
01650         self.assertRaises(TypeError, lambda: a + a)

Here is the call graph for this function:

def test.datetimetester.TestDate.test_ctime (   self) [inherited]

Definition at line 1079 of file datetimetester.py.

01079 
01080     def test_ctime(self):
01081         t = self.theclass(2002, 3, 2)
01082         self.assertEqual(t.ctime(), "Sat Mar  2 00:00:00 2002")

Here is the call graph for this function:

Definition at line 2698 of file datetimetester.py.

02698 
02699     def test_even_more_compare(self):
02700         # The test_compare() and test_more_compare() inherited from TestDate
02701         # and TestDateTime covered non-tzinfo cases.
02702 
02703         # Smallest possible after UTC adjustment.
02704         t1 = self.theclass(1, 1, 1, tzinfo=FixedOffset(1439, ""))
02705         # Largest possible after UTC adjustment.
02706         t2 = self.theclass(MAXYEAR, 12, 31, 23, 59, 59, 999999,
02707                            tzinfo=FixedOffset(-1439, ""))
02708 
02709         # Make sure those compare correctly, and w/o overflow.
02710         self.assertTrue(t1 < t2)
02711         self.assertTrue(t1 != t2)
02712         self.assertTrue(t2 > t1)
02713 
02714         self.assertEqual(t1, t1)
02715         self.assertEqual(t2, t2)
02716 
02717         # Equal afer adjustment.
02718         t1 = self.theclass(1, 12, 31, 23, 59, tzinfo=FixedOffset(1, ""))
02719         t2 = self.theclass(2, 1, 1, 3, 13, tzinfo=FixedOffset(3*60+13+2, ""))
02720         self.assertEqual(t1, t2)
02721 
02722         # Change t1 not to subtract a minute, and t1 should be larger.
02723         t1 = self.theclass(1, 12, 31, 23, 59, tzinfo=FixedOffset(0, ""))
02724         self.assertTrue(t1 > t2)
02725 
02726         # Change t1 to subtract 2 minutes, and t1 should be smaller.
02727         t1 = self.theclass(1, 12, 31, 23, 59, tzinfo=FixedOffset(2, ""))
02728         self.assertTrue(t1 < t2)
02729 
02730         # Back to the original t1, but make seconds resolve it.
02731         t1 = self.theclass(1, 12, 31, 23, 59, tzinfo=FixedOffset(1, ""),
02732                            second=1)
02733         self.assertTrue(t1 > t2)
02734 
02735         # Likewise, but make microseconds resolve it.
02736         t1 = self.theclass(1, 12, 31, 23, 59, tzinfo=FixedOffset(1, ""),
02737                            microsecond=1)
02738         self.assertTrue(t1 > t2)
02739 
02740         # Make t2 naive and it should fail.
02741         t2 = self.theclass.min
02742         self.assertRaises(TypeError, lambda: t1 == t2)
02743         self.assertEqual(t2, t2)
02744 
02745         # It's also naive if it has tzinfo but tzinfo.utcoffset() is None.
02746         class Naive(tzinfo):
02747             def utcoffset(self, dt): return None
02748         t2 = self.theclass(5, 6, 7, tzinfo=Naive())
02749         self.assertRaises(TypeError, lambda: t1 == t2)
02750         self.assertEqual(t2, t2)
02751 
02752         # OTOH, it's OK to compare two of these mixing the two ways of being
02753         # naive.
02754         t1 = self.theclass(5, 6, 7)
02755         self.assertEqual(t1, t2)
02756 
02757         # Try a bogus uctoffset.
02758         class Bogus(tzinfo):
02759             def utcoffset(self, dt):
02760                 return timedelta(minutes=1440) # out of bounds
02761         t1 = self.theclass(2, 2, 2, tzinfo=Bogus())
02762         t2 = self.theclass(2, 2, 2, tzinfo=FixedOffset(0, ""))
02763         self.assertRaises(ValueError, lambda: t1 == t2)

Here is the call graph for this function:

Reimplemented from test.datetimetester.TestDateTime.

Definition at line 2837 of file datetimetester.py.

02837 
02838     def test_extract(self):
02839         met = FixedOffset(60, "MET")
02840         dt = self.theclass(2002, 3, 4, 18, 45, 3, 1234, tzinfo=met)
02841         self.assertEqual(dt.date(), date(2002, 3, 4))
02842         self.assertEqual(dt.time(), time(18, 45, 3, 1234))
02843         self.assertEqual(dt.timetz(), time(18, 45, 3, 1234, tzinfo=met))

Here is the call graph for this function:

Definition at line 2785 of file datetimetester.py.

02785 
02786     def test_extreme_hashes(self):
02787         # If an attempt is made to hash these via subtracting the offset
02788         # then hashing a datetime object, OverflowError results.  The
02789         # Python implementation used to blow up here.
02790         t = self.theclass(1, 1, 1, tzinfo=FixedOffset(1439, ""))
02791         hash(t)
02792         t = self.theclass(MAXYEAR, 12, 31, 23, 59, 59, 999999,
02793                           tzinfo=FixedOffset(-1439, ""))
02794         hash(t)
02795 
02796         # OTOH, an OOB offset should blow up.
02797         t = self.theclass(5, 5, 5, tzinfo=FixedOffset(-1440, ""))
02798         self.assertRaises(ValueError, hash, t)

Here is the call graph for this function:

Definition at line 827 of file datetimetester.py.

00827 
00828     def test_extreme_ordinals(self):
00829         a = self.theclass.min
00830         a = self.theclass(a.year, a.month, a.day)  # get rid of time parts
00831         aord = a.toordinal()
00832         b = a.fromordinal(aord)
00833         self.assertEqual(a, b)
00834 
00835         self.assertRaises(ValueError, lambda: a.fromordinal(aord - 1))
00836 
00837         b = a + timedelta(days=1)
00838         self.assertEqual(b.toordinal(), aord + 1)
00839         self.assertEqual(b, self.theclass.fromordinal(aord + 1))
00840 
00841         a = self.theclass.max
00842         a = self.theclass(a.year, a.month, a.day)  # get rid of time parts
00843         aord = a.toordinal()
00844         b = a.fromordinal(aord)
00845         self.assertEqual(a, b)
00846 
00847         self.assertRaises(ValueError, lambda: a.fromordinal(aord + 1))
00848 
00849         b = a - timedelta(days=1)
00850         self.assertEqual(b.toordinal(), aord - 1)
00851         self.assertEqual(b, self.theclass.fromordinal(aord - 1))

Here is the call graph for this function:

Definition at line 1154 of file datetimetester.py.

01154 
01155     def test_extreme_timedelta(self):
01156         big = self.theclass.max - self.theclass.min
01157         # 3652058 days, 23 hours, 59 minutes, 59 seconds, 999999 microseconds
01158         n = (big.days*24*3600 + big.seconds)*1000000 + big.microseconds
01159         # n == 315537897599999999 ~= 2**58.13
01160         justasbig = timedelta(0, 0, n)
01161         self.assertEqual(big, justasbig)
01162         self.assertEqual(self.theclass.min + big, self.theclass.max)
01163         self.assertEqual(self.theclass.max - big, self.theclass.min)

Here is the call graph for this function:

Reimplemented from test.datetimetester.TestDate.

Definition at line 1444 of file datetimetester.py.

01444 
01445     def test_format(self):
01446         dt = self.theclass(2007, 9, 10, 4, 5, 1, 123)
01447         self.assertEqual(dt.__format__(''), str(dt))
01448 
01449         # check that a derived class's __str__() gets called
01450         class A(self.theclass):
01451             def __str__(self):
01452                 return 'A'
01453         a = A(2007, 9, 10, 4, 5, 1, 123)
01454         self.assertEqual(a.__format__(''), 'A')
01455 
01456         # check that a derived class's strftime gets called
01457         class B(self.theclass):
01458             def strftime(self, format_spec):
01459                 return 'B'
01460         b = B(2007, 9, 10, 4, 5, 1, 123)
01461         self.assertEqual(b.__format__(''), str(dt))
01462 
01463         for fmt in ["m:%m d:%d y:%y",
01464                     "m:%m d:%d y:%y H:%H M:%M S:%S",
01465                     "%z %Z",
01466                     ]:
01467             self.assertEqual(dt.__format__(fmt), dt.strftime(fmt))
01468             self.assertEqual(a.__format__(fmt), dt.strftime(fmt))
01469             self.assertEqual(b.__format__(fmt), 'B')

Here is the call graph for this function:

Reimplemented from test.datetimetester.TestDate.

Definition at line 1716 of file datetimetester.py.

01716 
01717     def test_fromtimestamp(self):
01718         import time
01719 
01720         ts = time.time()
01721         expected = time.localtime(ts)
01722         got = self.theclass.fromtimestamp(ts)
01723         self.verify_field_equality(expected, got)

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:

Reimplemented from test.datetimetester.TestDate.

Definition at line 1560 of file datetimetester.py.

01560 
01561     def test_hash_equality(self):
01562         d = self.theclass(2000, 12, 31, 23, 30, 17)
01563         e = self.theclass(2000, 12, 31, 23, 30, 17)
01564         self.assertEqual(d, e)
01565         self.assertEqual(hash(d), hash(e))
01566 
01567         dic = {d: 1}
01568         dic[e] = 2
01569         self.assertEqual(len(dic), 1)
01570         self.assertEqual(dic[d], 2)
01571         self.assertEqual(dic[e], 2)
01572 
01573         d = self.theclass(2001,  1,  1,  0,  5, 17)
01574         e = self.theclass(2001,  1,  1,  0,  5, 17)
01575         self.assertEqual(d, e)
01576         self.assertEqual(hash(d), hash(e))
01577 
01578         dic = {d: 1}
01579         dic[e] = 2
01580         self.assertEqual(len(dic), 1)
01581         self.assertEqual(dic[d], 2)
01582         self.assertEqual(dic[e], 2)

Here is the call graph for this function:

Reimplemented from test.datetimetester.TestDate.

Definition at line 1740 of file datetimetester.py.

01740 
01741     def test_insane_fromtimestamp(self):
01742         # It's possible that some platform maps time_t to double,
01743         # and that this test will fail there.  This test should
01744         # exempt such platforms (provided they return reasonable
01745         # results!).
01746         for insane in -1e200, 1e200:
01747             self.assertRaises(ValueError, self.theclass.fromtimestamp,
01748                               insane)

Here is the call graph for this function:

Definition at line 1749 of file datetimetester.py.

01749 
01750     def test_insane_utcfromtimestamp(self):
01751         # It's possible that some platform maps time_t to double,
01752         # and that this test will fail there.  This test should
01753         # exempt such platforms (provided they return reasonable
01754         # results!).
01755         for insane in -1e200, 1e200:
01756             self.assertRaises(ValueError, self.theclass.utcfromtimestamp,
                              insane)

Here is the call graph for this function:

Definition at line 1037 of file datetimetester.py.

01037 
01038     def test_iso_long_years(self):
01039         # Calculate long ISO years and compare to table from
01040         # http://www.phys.uu.nl/~vgent/calendar/isocalendar.htm
01041         ISO_LONG_YEARS_TABLE = """
01042               4   32   60   88
01043               9   37   65   93
01044              15   43   71   99
01045              20   48   76
01046              26   54   82
01047 
01048             105  133  161  189
01049             111  139  167  195
01050             116  144  172
01051             122  150  178
01052             128  156  184
01053 
01054             201  229  257  285
01055             207  235  263  291
01056             212  240  268  296
01057             218  246  274
01058             224  252  280
01059 
01060             303  331  359  387
01061             308  336  364  392
01062             314  342  370  398
01063             320  348  376
01064             325  353  381
01065         """
01066         iso_long_years = sorted(map(int, ISO_LONG_YEARS_TABLE.split()))
01067         L = []
01068         for i in range(400):
01069             d = self.theclass(2000+i, 12, 31)
01070             d1 = self.theclass(1600+i, 12, 31)
01071             self.assertEqual(d.isocalendar()[1:], d1.isocalendar()[1:])
01072             if d.isocalendar()[1] == 53:
01073                 L.append(i)
01074         self.assertEqual(L, iso_long_years)

Here is the call graph for this function:

Definition at line 1020 of file datetimetester.py.

01020 
01021     def test_isocalendar(self):
01022         # Check examples from
01023         # http://www.phys.uu.nl/~vgent/calendar/isocalendar.htm
01024         for i in range(7):
01025             d = self.theclass(2003, 12, 22+i)
01026             self.assertEqual(d.isocalendar(), (2003, 52, i+1))
01027             d = self.theclass(2003, 12, 29) + timedelta(i)
01028             self.assertEqual(d.isocalendar(), (2004, 1, i+1))
01029             d = self.theclass(2004, 1, 5+i)
01030             self.assertEqual(d.isocalendar(), (2004, 2, i+1))
01031             d = self.theclass(2009, 12, 21+i)
01032             self.assertEqual(d.isocalendar(), (2009, 52, i+1))
01033             d = self.theclass(2009, 12, 28) + timedelta(i)
01034             self.assertEqual(d.isocalendar(), (2009, 53, i+1))
01035             d = self.theclass(2010, 1, 4+i)
01036             self.assertEqual(d.isocalendar(), (2010, 1, i+1))

Here is the call graph for this function:

Reimplemented from test.datetimetester.TestDate.

Definition at line 1428 of file datetimetester.py.

01428 
01429     def test_isoformat(self):
01430         t = self.theclass(2, 3, 2, 4, 5, 1, 123)
01431         self.assertEqual(t.isoformat(),    "0002-03-02T04:05:01.000123")
01432         self.assertEqual(t.isoformat('T'), "0002-03-02T04:05:01.000123")
01433         self.assertEqual(t.isoformat(' '), "0002-03-02 04:05:01.000123")
01434         self.assertEqual(t.isoformat('\x00'), "0002-03-02\x0004:05:01.000123")
01435         # str is ISO format with the separator forced to a blank.
01436         self.assertEqual(str(t), "0002-03-02 04:05:01.000123")
01437 
01438         t = self.theclass(2, 3, 2)
01439         self.assertEqual(t.isoformat(),    "0002-03-02T00:00:00")
01440         self.assertEqual(t.isoformat('T'), "0002-03-02T00:00:00")
01441         self.assertEqual(t.isoformat(' '), "0002-03-02 00:00:00")
01442         # str is ISO format with the separator forced to a blank.
01443         self.assertEqual(str(t), "0002-03-02 00:00:00")

Here is the call graph for this function:

Definition at line 1732 of file datetimetester.py.

01732 
01733     def test_microsecond_rounding(self):
01734         # Test whether fromtimestamp "rounds up" floats that are less
01735         # than 1/2 microsecond smaller than an integer.
01736         for fts in [self.theclass.fromtimestamp,
01737                     self.theclass.utcfromtimestamp]:
01738             self.assertEqual(fts(0.9999999), fts(1))
01739             self.assertEqual(fts(0.99999949).microsecond, 999999)

Here is the call graph for this function:

Reimplemented from test.datetimetester.TestDate.

Definition at line 3256 of file datetimetester.py.

03256 
03257     def test_mixed_compare(self):
03258         t1 = datetime(1, 2, 3, 4, 5, 6, 7)
03259         t2 = datetime(1, 2, 3, 4, 5, 6, 7)
03260         self.assertEqual(t1, t2)
03261         t2 = t2.replace(tzinfo=None)
03262         self.assertEqual(t1, t2)
03263         t2 = t2.replace(tzinfo=FixedOffset(None, ""))
03264         self.assertEqual(t1, t2)
03265         t2 = t2.replace(tzinfo=FixedOffset(0, ""))
03266         self.assertRaises(TypeError, lambda: t1 == t2)
03267 
03268         # In datetime w/ identical tzinfo objects, utcoffset is ignored.
03269         class Varies(tzinfo):
03270             def __init__(self):
03271                 self.offset = timedelta(minutes=22)
03272             def utcoffset(self, t):
03273                 self.offset += timedelta(minutes=1)
03274                 return self.offset
03275 
03276         v = Varies()
03277         t1 = t2.replace(tzinfo=v)
03278         t2 = t2.replace(tzinfo=v)
03279         self.assertEqual(t1.utcoffset(), timedelta(minutes=23))
03280         self.assertEqual(t2.utcoffset(), timedelta(minutes=24))
03281         self.assertEqual(t1, t2)
03282 
03283         # But if they're not identical, it isn't ignored.
03284         t2 = t2.replace(tzinfo=Varies())
03285         self.assertTrue(t1 < t2)  # t1's offset counter still going up

Here is the call graph for this function:

Definition at line 3181 of file datetimetester.py.

03181 
03182     def test_more_astimezone(self):
03183         # The inherited test_astimezone covered some trivial and error cases.
03184         fnone = FixedOffset(None, "None")
03185         f44m = FixedOffset(44, "44")
03186         fm5h = FixedOffset(-timedelta(hours=5), "m300")
03187 
03188         dt = self.theclass.now(tz=f44m)
03189         self.assertTrue(dt.tzinfo is f44m)
03190         # Replacing with degenerate tzinfo raises an exception.
03191         self.assertRaises(ValueError, dt.astimezone, fnone)
03192         # Ditto with None tz.
03193         self.assertRaises(TypeError, dt.astimezone, None)
03194         # Replacing with same tzinfo makes no change.
03195         x = dt.astimezone(dt.tzinfo)
03196         self.assertTrue(x.tzinfo is f44m)
03197         self.assertEqual(x.date(), dt.date())
03198         self.assertEqual(x.time(), dt.time())
03199 
03200         # Replacing with different tzinfo does adjust.
03201         got = dt.astimezone(fm5h)
03202         self.assertTrue(got.tzinfo is fm5h)
03203         self.assertEqual(got.utcoffset(), timedelta(hours=-5))
03204         expected = dt - dt.utcoffset()  # in effect, convert to UTC
03205         expected += fm5h.utcoffset(dt)  # and from there to local time
03206         expected = expected.replace(tzinfo=fm5h) # and attach new tzinfo
03207         self.assertEqual(got.date(), expected.date())
03208         self.assertEqual(got.time(), expected.time())
03209         self.assertEqual(got.timetz(), expected.timetz())
03210         self.assertTrue(got.tzinfo is expected.tzinfo)
03211         self.assertEqual(got, expected)

Here is the call graph for this function:

Definition at line 1675 of file datetimetester.py.

01675 
01676     def test_more_compare(self):
01677         # The test_compare() inherited from TestDate covers the error cases.
01678         # We just want to test lexicographic ordering on the members datetime
01679         # has that date lacks.
01680         args = [2000, 11, 29, 20, 58, 16, 999998]
01681         t1 = self.theclass(*args)
01682         t2 = self.theclass(*args)
01683         self.assertEqual(t1, t2)
01684         self.assertTrue(t1 <= t2)
01685         self.assertTrue(t1 >= t2)
01686         self.assertTrue(not t1 != t2)
01687         self.assertTrue(not t1 < t2)
01688         self.assertTrue(not t1 > t2)
01689 
01690         for i in range(len(args)):
01691             newargs = args[:]
01692             newargs[i] = args[i] + 1
01693             t2 = self.theclass(*newargs)   # this is larger than t1
01694             self.assertTrue(t1 < t2)
01695             self.assertTrue(t2 > t1)
01696             self.assertTrue(t1 <= t2)
01697             self.assertTrue(t2 >= t1)
01698             self.assertTrue(t1 != t2)
01699             self.assertTrue(t2 != t1)
01700             self.assertTrue(not t1 == t2)
01701             self.assertTrue(not t2 == t1)
01702             self.assertTrue(not t1 > t2)
01703             self.assertTrue(not t2 < t1)
01704             self.assertTrue(not t1 >= t2)
01705             self.assertTrue(not t2 <= t1)
01706 

Here is the call graph for this function:

Definition at line 1470 of file datetimetester.py.

01470 
01471     def test_more_ctime(self):
01472         # Test fields that TestDate doesn't touch.
01473         import time
01474 
01475         t = self.theclass(2002, 3, 2, 18, 3, 5, 123)
01476         self.assertEqual(t.ctime(), "Sat Mar  2 18:03:05 2002")
01477         # Oops!  The next line fails on Win2K under MSVC 6, so it's commented
01478         # out.  The difference is that t.ctime() produces " 2" for the day,
01479         # but platform ctime() produces "02" for the day.  According to
01480         # C99, t.ctime() is correct here.
01481         # self.assertEqual(t.ctime(), time.ctime(time.mktime(t.timetuple())))
01482 
01483         # So test a case where that difference doesn't matter.
01484         t = self.theclass(2002, 3, 22, 18, 3, 5, 123)
01485         self.assertEqual(t.ctime(), time.ctime(time.mktime(t.timetuple())))

Here is the call graph for this function:

Definition at line 1659 of file datetimetester.py.

01659 
01660     def test_more_pickling(self):
01661         a = self.theclass(2003, 2, 7, 16, 48, 37, 444116)
01662         s = pickle.dumps(a)
01663         b = pickle.loads(s)
01664         self.assertEqual(b.year, 2003)
01665         self.assertEqual(b.month, 2)
01666         self.assertEqual(b.day, 7)

Here is the call graph for this function:

Definition at line 1846 of file datetimetester.py.

01846 
01847     def test_more_strftime(self):
01848         # This tests fields beyond those tested by the TestDate.test_strftime.
01849         t = self.theclass(2004, 12, 31, 6, 22, 33, 47)
01850         self.assertEqual(t.strftime("%m %d %y %f %S %M %H %j"),
01851                                     "12 31 04 000047 33 22 06 366")

Here is the call graph for this function:

Definition at line 1824 of file datetimetester.py.

01824 
01825     def test_more_timetuple(self):
01826         # This tests fields beyond those tested by the TestDate.test_timetuple.
01827         t = self.theclass(2004, 12, 31, 6, 22, 33)
01828         self.assertEqual(t.timetuple(), (2004, 12, 31, 6, 22, 33, 4, 366, -1))
01829         self.assertEqual(t.timetuple(),
01830                          (t.year, t.month, t.day,
01831                           t.hour, t.minute, t.second,
01832                           t.weekday(),
01833                           t.toordinal() - date(t.year, 1, 1).toordinal() + 1,
01834                           -1))
01835         tt = t.timetuple()
01836         self.assertEqual(tt.tm_year, t.year)
01837         self.assertEqual(tt.tm_mon, t.month)
01838         self.assertEqual(tt.tm_mday, t.day)
01839         self.assertEqual(tt.tm_hour, t.hour)
01840         self.assertEqual(tt.tm_min, t.minute)
01841         self.assertEqual(tt.tm_sec, t.second)
01842         self.assertEqual(tt.tm_wday, t.weekday())
01843         self.assertEqual(tt.tm_yday, t.toordinal() -
01844                                      date(t.year, 1, 1).toordinal() + 1)
01845         self.assertEqual(tt.tm_isdst, -1)

Here is the call graph for this function:

Definition at line 1758 of file datetimetester.py.

01758 
01759     def test_negative_float_fromtimestamp(self):
01760         # The result is tz-dependent; at least test that this doesn't
01761         # fail (like it did before bug 1646728 was fixed).
01762         self.theclass.fromtimestamp(-1.05)

Definition at line 1764 of file datetimetester.py.

01764 
01765     def test_negative_float_utcfromtimestamp(self):
01766         d = self.theclass.utcfromtimestamp(-1.05)
01767         self.assertEqual(d, self.theclass(1969, 12, 31, 23, 59, 58, 950000))

Here is the call graph for this function:

Definition at line 780 of file datetimetester.py.

00780 
00781     def test_ordinal_conversions(self):
00782         # Check some fixed values.
00783         for y, m, d, n in [(1, 1, 1, 1),      # calendar origin
00784                            (1, 12, 31, 365),
00785                            (2, 1, 1, 366),
00786                            # first example from "Calendrical Calculations"
00787                            (1945, 11, 12, 710347)]:
00788             d = self.theclass(y, m, d)
00789             self.assertEqual(n, d.toordinal())
00790             fromord = self.theclass.fromordinal(n)
00791             self.assertEqual(d, fromord)
00792             if hasattr(fromord, "hour"):
00793             # if we're checking something fancier than a date, verify
00794             # the extra fields have been zeroed out
00795                 self.assertEqual(fromord.hour, 0)
00796                 self.assertEqual(fromord.minute, 0)
00797                 self.assertEqual(fromord.second, 0)
00798                 self.assertEqual(fromord.microsecond, 0)
00799 
00800         # Check first and last days of year spottily across the whole
00801         # range of years supported.
00802         for year in range(MINYEAR, MAXYEAR+1, 7):
00803             # Verify (year, 1, 1) -> ordinal -> y, m, d is identity.
00804             d = self.theclass(year, 1, 1)
00805             n = d.toordinal()
00806             d2 = self.theclass.fromordinal(n)
00807             self.assertEqual(d, d2)
00808             # Verify that moving back a day gets to the end of year-1.
00809             if year > 1:
00810                 d = self.theclass.fromordinal(n-1)
00811                 d2 = self.theclass(year-1, 12, 31)
00812                 self.assertEqual(d, d2)
00813                 self.assertEqual(d2.toordinal(), n-1)
00814 
00815         # Test every day in a leap-year and a non-leap year.
00816         dim = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
00817         for year, isleap in (2000, True), (2002, False):
00818             n = self.theclass(year, 1, 1).toordinal()
00819             for month, maxday in zip(range(1, 13), dim):
00820                 if month == 2 and isleap:
00821                     maxday += 1
00822                 for day in range(1, maxday+1):
00823                     d = self.theclass(year, month, day)
00824                     self.assertEqual(d.toordinal(), n)
00825                     self.assertEqual(d, self.theclass.fromordinal(n))
00826                     n += 1

Here is the call graph for this function:

def test.datetimetester.TestDate.test_overflow (   self) [inherited]

Definition at line 949 of file datetimetester.py.

00949 
00950     def test_overflow(self):
00951         tiny = self.theclass.resolution
00952 
00953         for delta in [tiny, timedelta(1), timedelta(2)]:
00954             dt = self.theclass.min + delta
00955             dt -= delta  # no problem
00956             self.assertRaises(OverflowError, dt.__sub__, delta)
00957             self.assertRaises(OverflowError, dt.__add__, -delta)
00958 
00959             dt = self.theclass.max - delta
00960             dt += delta  # no problem
00961             self.assertRaises(OverflowError, dt.__add__, delta)
00962             self.assertRaises(OverflowError, dt.__sub__, -delta)

Here is the call graph for this function:

Reimplemented from test.datetimetester.TestDateTime.

Definition at line 2764 of file datetimetester.py.

02764 
02765     def test_pickling(self):
02766         # Try one without a tzinfo.
02767         args = 6, 7, 23, 20, 59, 1, 64**2
02768         orig = self.theclass(*args)
02769         for pickler, unpickler, proto in pickle_choices:
02770             green = pickler.dumps(orig, proto)
02771             derived = unpickler.loads(green)
02772             self.assertEqual(orig, derived)
02773 
02774         # Try one with a tzinfo.
02775         tinfo = PicklableFixedOffset(-300, 'cookie')
02776         orig = self.theclass(*args, **{'tzinfo': tinfo})
02777         derived = self.theclass(1, 1, 1, tzinfo=FixedOffset(0, "", 0))
02778         for pickler, unpickler, proto in pickle_choices:
02779             green = pickler.dumps(orig, proto)
02780             derived = unpickler.loads(green)
02781             self.assertEqual(orig, derived)
02782             self.assertIsInstance(derived.tzinfo, PicklableFixedOffset)
02783             self.assertEqual(derived.utcoffset(), timedelta(minutes=-300))
02784             self.assertEqual(derived.tzname(), 'cookie')

Here is the call graph for this function:

Definition at line 1346 of file datetimetester.py.

01346 
01347     def test_pickling_subclass_date(self):
01348 
01349         args = 6, 7, 23
01350         orig = SubclassDate(*args)
01351         for pickler, unpickler, proto in pickle_choices:
01352             green = pickler.dumps(orig, proto)
01353             derived = unpickler.loads(green)
01354             self.assertEqual(orig, derived)

Here is the call graph for this function:

Definition at line 1667 of file datetimetester.py.

01667 
01668     def test_pickling_subclass_datetime(self):
01669         args = 6, 7, 23, 20, 59, 1, 64**2
01670         orig = SubclassDatetime(*args)
01671         for pickler, unpickler, proto in pickle_choices:
01672             green = pickler.dumps(orig, proto)
01673             derived = unpickler.loads(green)
01674             self.assertEqual(orig, derived)

Here is the call graph for this function:

Reimplemented from test.datetimetester.TestDateTime.

Definition at line 3142 of file datetimetester.py.

03142 
03143     def test_replace(self):
03144         cls = self.theclass
03145         z100 = FixedOffset(100, "+100")
03146         zm200 = FixedOffset(timedelta(minutes=-200), "-200")
03147         args = [1, 2, 3, 4, 5, 6, 7, z100]
03148         base = cls(*args)
03149         self.assertEqual(base, base.replace())
03150 
03151         i = 0
03152         for name, newval in (("year", 2),
03153                              ("month", 3),
03154                              ("day", 4),
03155                              ("hour", 5),
03156                              ("minute", 6),
03157                              ("second", 7),
03158                              ("microsecond", 8),
03159                              ("tzinfo", zm200)):
03160             newargs = args[:]
03161             newargs[i] = newval
03162             expected = cls(*newargs)
03163             got = base.replace(**{name: newval})
03164             self.assertEqual(expected, got)
03165             i += 1
03166 
03167         # Ensure we can get rid of a tzinfo.
03168         self.assertEqual(base.tzname(), "+100")
03169         base2 = base.replace(tzinfo=None)
03170         self.assertTrue(base2.tzinfo is None)
03171         self.assertTrue(base2.tzname() is None)
03172 
03173         # Ensure we can add one.
03174         base3 = base2.replace(tzinfo=z100)
03175         self.assertEqual(base, base3)
03176         self.assertTrue(base.tzinfo is base3.tzinfo)
03177 
03178         # Out of bounds.
03179         base = cls(2000, 2, 29)
03180         self.assertRaises(ValueError, base.replace, year=2001)

Here is the call graph for this function:

Definition at line 1143 of file datetimetester.py.

01143 
01144     def test_resolution_info(self):
01145         # XXX: Should min and max respect subclassing?
01146         if issubclass(self.theclass, datetime):
01147             expected_class = datetime
01148         else:
01149             expected_class = date
01150         self.assertIsInstance(self.theclass.min, expected_class)
01151         self.assertIsInstance(self.theclass.max, expected_class)
01152         self.assertIsInstance(self.theclass.resolution, timedelta)
01153         self.assertTrue(self.theclass.max > self.theclass.min)

Here is the call graph for this function:

Reimplemented from test.datetimetester.TestDate.

Reimplemented in test.datetimetester.TestSubclassDateTime.

Definition at line 1412 of file datetimetester.py.

01412 
01413     def test_roundtrip(self):
01414         for dt in (self.theclass(1, 2, 3, 4, 5, 6, 7),
01415                    self.theclass.now()):
01416             # Verify dt -> string -> datetime identity.
01417             s = repr(dt)
01418             self.assertTrue(s.startswith('datetime.'))
01419             s = s[9:]
01420             dt2 = eval(s)
01421             self.assertEqual(dt, dt2)
01422 
01423             # Verify identity via reconstructing from pieces.
01424             dt2 = self.theclass(dt.year, dt.month, dt.day,
01425                                 dt.hour, dt.minute, dt.second,
01426                                 dt.microsecond)
01427             self.assertEqual(dt, dt2)

Here is the call graph for this function:

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

Definition at line 1083 of file datetimetester.py.

01083 
01084     def test_strftime(self):
01085         t = self.theclass(2005, 3, 2)
01086         self.assertEqual(t.strftime("m:%m d:%d y:%y"), "m:03 d:02 y:05")
01087         self.assertEqual(t.strftime(""), "") # SF bug #761337
01088         self.assertEqual(t.strftime('x'*1000), 'x'*1000) # SF bug #1556784
01089 
01090         self.assertRaises(TypeError, t.strftime) # needs an arg
01091         self.assertRaises(TypeError, t.strftime, "one", "two") # too many args
01092         self.assertRaises(TypeError, t.strftime, 42) # arg wrong type
01093 
01094         # test that unicode input is allowed (issue 2782)
01095         self.assertEqual(t.strftime("%m"), "03")
01096 
01097         # A naive object replaces %z and %Z w/ empty strings.
01098         self.assertEqual(t.strftime("'%z' '%Z'"), "'' ''")
01099 
01100         #make sure that invalid format specifiers are handled correctly
01101         #self.assertRaises(ValueError, t.strftime, "%e")
01102         #self.assertRaises(ValueError, t.strftime, "%")
01103         #self.assertRaises(ValueError, t.strftime, "%#")
01104 
01105         #oh well, some systems just ignore those invalid ones.
01106         #at least, excercise them to make sure that no crashes
01107         #are generated
01108         for f in ["%e", "%", "%#"]:
01109             try:
01110                 t.strftime(f)
01111             except ValueError:
01112                 pass
01113 
01114         #check that this standard extension works
01115         t.strftime("%f")
01116 

Here is the call graph for this function:

Definition at line 1292 of file datetimetester.py.

01292 
01293     def test_strftime_out_of_range(self):
01294         # For nasty technical reasons, we can't handle years before 1000.
01295         cls = self.theclass
01296         self.assertEqual(cls(1000, 1, 1).strftime("%Y"), "1000")
01297         for y in 1, 49, 51, 99, 100, 999:
01298             self.assertRaises(ValueError, cls(y, 1, 1).strftime, "%Y")

Here is the call graph for this function:

Definition at line 1504 of file datetimetester.py.

01504 
01505     def test_strftime_with_bad_tzname_replace(self):
01506         # verify ok if tzinfo.tzname().replace() returns a non-string
01507         class MyTzInfo(FixedOffset):
01508             def tzname(self, dt):
01509                 class MyStr(str):
01510                     def replace(self, *args):
01511                         return None
01512                 return MyStr('name')
01513         t = self.theclass(2005, 3, 2, 0, 0, 0, 0, MyTzInfo(3, 'name'))
01514         self.assertRaises(TypeError, t.strftime, '%Z')

Here is the call graph for this function:

Definition at line 1782 of file datetimetester.py.

01782 
01783     def test_strptime(self):
01784         import _strptime
01785 
01786         string = '2004-12-01 13:02:47.197'
01787         format = '%Y-%m-%d %H:%M:%S.%f'
01788         expected = _strptime._strptime_datetime(self.theclass, string, format)
01789         got = self.theclass.strptime(string, format)
01790         self.assertEqual(expected, got)
01791         self.assertIs(type(expected), self.theclass)
01792         self.assertIs(type(got), self.theclass)
01793 
01794         strptime = self.theclass.strptime
01795         self.assertEqual(strptime("+0002", "%z").utcoffset(), 2 * MINUTE)
01796         self.assertEqual(strptime("-0002", "%z").utcoffset(), -2 * MINUTE)
01797         # Only local timezone and UTC are supported
01798         for tzseconds, tzname in ((0, 'UTC'), (0, 'GMT'),
01799                                  (-_time.timezone, _time.tzname[0])):
01800             if tzseconds < 0:
01801                 sign = '-'
01802                 seconds = -tzseconds
01803             else:
01804                 sign ='+'
01805                 seconds = tzseconds
01806             hours, minutes = divmod(seconds//60, 60)
01807             dtstr = "{}{:02d}{:02d} {}".format(sign, hours, minutes, tzname)
01808             dt = strptime(dtstr, "%z %Z")
01809             self.assertEqual(dt.utcoffset(), timedelta(seconds=tzseconds))
01810             self.assertEqual(dt.tzname(), tzname)
01811         # Can produce inconsistent datetime
01812         dtstr, fmt = "+1234 UTC", "%z %Z"
01813         dt = strptime(dtstr, fmt)
01814         self.assertEqual(dt.utcoffset(), 12 * HOUR + 34 * MINUTE)
01815         self.assertEqual(dt.tzname(), 'UTC')
01816         # yet will roundtrip
01817         self.assertEqual(dt.strftime(fmt), dtstr)
01818 
01819         # Produce naive datetime if no %z is provided
01820         self.assertEqual(strptime("UTC", "%Z").tzinfo, None)
01821 
01822         with self.assertRaises(ValueError): strptime("-2400", "%z")
01823         with self.assertRaises(ValueError): strptime("-000", "%z")

Here is the call graph for this function:

Definition at line 1320 of file datetimetester.py.

01320 
01321     def test_subclass_date(self):
01322 
01323         class C(self.theclass):
01324             theAnswer = 42
01325 
01326             def __new__(cls, *args, **kws):
01327                 temp = kws.copy()
01328                 extra = temp.pop('extra')
01329                 result = self.theclass.__new__(cls, *args, **temp)
01330                 result.extra = extra
01331                 return result
01332 
01333             def newmeth(self, start):
01334                 return start + self.year + self.month
01335 
01336         args = 2003, 4, 14
01337 
01338         dt1 = self.theclass(*args)
01339         dt2 = C(*args, **{'extra': 7})
01340 
01341         self.assertEqual(dt2.__class__, C)
01342         self.assertEqual(dt2.theAnswer, 42)
01343         self.assertEqual(dt2.extra, 7)
01344         self.assertEqual(dt1.toordinal(), dt2.toordinal())
01345         self.assertEqual(dt2.newmeth(-7), dt1.year + dt1.month - 7)

Here is the call graph for this function:

Definition at line 1930 of file datetimetester.py.

01930 
01931     def test_subclass_datetime(self):
01932 
01933         class C(self.theclass):
01934             theAnswer = 42
01935 
01936             def __new__(cls, *args, **kws):
01937                 temp = kws.copy()
01938                 extra = temp.pop('extra')
01939                 result = self.theclass.__new__(cls, *args, **temp)
01940                 result.extra = extra
01941                 return result
01942 
01943             def newmeth(self, start):
01944                 return start + self.year + self.month + self.second
01945 
01946         args = 2003, 4, 14, 12, 13, 41
01947 
01948         dt1 = self.theclass(*args)
01949         dt2 = C(*args, **{'extra': 7})
01950 
01951         self.assertEqual(dt2.__class__, C)
01952         self.assertEqual(dt2.theAnswer, 42)
01953         self.assertEqual(dt2.extra, 7)
01954         self.assertEqual(dt1.toordinal(), dt2.toordinal())
01955         self.assertEqual(dt2.newmeth(-7), dt1.year + dt1.month +
01956                                           dt1.second - 7)

Here is the call graph for this function:

Definition at line 3286 of file datetimetester.py.

03286 
03287     def test_subclass_datetimetz(self):
03288 
03289         class C(self.theclass):
03290             theAnswer = 42
03291 
03292             def __new__(cls, *args, **kws):
03293                 temp = kws.copy()
03294                 extra = temp.pop('extra')
03295                 result = self.theclass.__new__(cls, *args, **temp)
03296                 result.extra = extra
03297                 return result
03298 
03299             def newmeth(self, start):
03300                 return start + self.hour + self.year
03301 
03302         args = 2002, 12, 31, 4, 5, 6, 500, FixedOffset(-300, "EST", 1)
03303 
03304         dt1 = self.theclass(*args)
03305         dt2 = C(*args, **{'extra': 7})
03306 
03307         self.assertEqual(dt2.__class__, C)
03308         self.assertEqual(dt2.theAnswer, 42)
03309         self.assertEqual(dt2.extra, 7)
03310         self.assertEqual(dt1.utcoffset(), dt2.utcoffset())
03311         self.assertEqual(dt2.newmeth(-7), dt1.hour + dt1.year - 7)
03312 
03313 # Pain to set up DST-aware tzinfo classes.

Here is the call graph for this function:

def test.datetimetester.TestDate.test_timetuple (   self) [inherited]

Definition at line 1164 of file datetimetester.py.

01164 
01165     def test_timetuple(self):
01166         for i in range(7):
01167             # January 2, 1956 is a Monday (0)
01168             d = self.theclass(1956, 1, 2+i)
01169             t = d.timetuple()
01170             self.assertEqual(t, (1956, 1, 2+i, 0, 0, 0, i, 2+i, -1))
01171             # February 1, 1956 is a Wednesday (2)
01172             d = self.theclass(1956, 2, 1+i)
01173             t = d.timetuple()
01174             self.assertEqual(t, (1956, 2, 1+i, 0, 0, 0, (2+i)%7, 32+i, -1))
01175             # March 1, 1956 is a Thursday (3), and is the 31+29+1 = 61st day
01176             # of the year.
01177             d = self.theclass(1956, 3, 1+i)
01178             t = d.timetuple()
01179             self.assertEqual(t, (1956, 3, 1+i, 0, 0, 0, (3+i)%7, 61+i, -1))
01180             self.assertEqual(t.tm_year, 1956)
01181             self.assertEqual(t.tm_mon, 3)
01182             self.assertEqual(t.tm_mday, 1+i)
01183             self.assertEqual(t.tm_hour, 0)
01184             self.assertEqual(t.tm_min, 0)
01185             self.assertEqual(t.tm_sec, 0)
01186             self.assertEqual(t.tm_wday, (3+i)%7)
01187             self.assertEqual(t.tm_yday, 61+i)
01188             self.assertEqual(t.tm_isdst, -1)

Here is the call graph for this function:

def test.datetimetester.TestDate.test_today (   self) [inherited]

Definition at line 983 of file datetimetester.py.

00983 
00984     def test_today(self):
00985         import time
00986 
00987         # We claim that today() is like fromtimestamp(time.time()), so
00988         # prove it.
00989         for dummy in range(3):
00990             today = self.theclass.today()
00991             ts = time.time()
00992             todayagain = self.theclass.fromtimestamp(ts)
00993             if today == todayagain:
00994                 break
00995             # There are several legit reasons that could fail:
00996             # 1. It recently became midnight, between the today() and the
00997             #    time() calls.
00998             # 2. The platform time() has such fine resolution that we'll
00999             #    never get the same value twice.
01000             # 3. The platform time() has poor resolution, and we just
01001             #    happened to call today() right before a resolution quantum
01002             #    boundary.
01003             # 4. The system clock got fiddled between calls.
01004             # In any case, wait a little while and try again.
01005             time.sleep(0.1)
01006 
01007         # It worked or it didn't.  If it didn't, assume it's reason #2, and
01008         # let the test pass if they're within half a second of each other.
01009         self.assertTrue(today == todayagain or
01010                         abs(todayagain - today) < timedelta(seconds=0.5))

Here is the call graph for this function:

Definition at line 2687 of file datetimetester.py.

02687 
02688     def test_trivial(self):
02689         dt = self.theclass(1, 2, 3, 4, 5, 6, 7)
02690         self.assertEqual(dt.year, 1)
02691         self.assertEqual(dt.month, 2)
02692         self.assertEqual(dt.day, 3)
02693         self.assertEqual(dt.hour, 4)
02694         self.assertEqual(dt.minute, 5)
02695         self.assertEqual(dt.second, 6)
02696         self.assertEqual(dt.microsecond, 7)
02697         self.assertEqual(dt.tzinfo, None)

Here is the call graph for this function:

Definition at line 2844 of file datetimetester.py.

02844 
02845     def test_tz_aware_arithmetic(self):
02846         import random
02847 
02848         now = self.theclass.now()
02849         tz55 = FixedOffset(-330, "west 5:30")
02850         timeaware = now.time().replace(tzinfo=tz55)
02851         nowaware = self.theclass.combine(now.date(), timeaware)
02852         self.assertTrue(nowaware.tzinfo is tz55)
02853         self.assertEqual(nowaware.timetz(), timeaware)
02854 
02855         # Can't mix aware and non-aware.
02856         self.assertRaises(TypeError, lambda: now - nowaware)
02857         self.assertRaises(TypeError, lambda: nowaware - now)
02858 
02859         # And adding datetime's doesn't make sense, aware or not.
02860         self.assertRaises(TypeError, lambda: now + nowaware)
02861         self.assertRaises(TypeError, lambda: nowaware + now)
02862         self.assertRaises(TypeError, lambda: nowaware + nowaware)
02863 
02864         # Subtracting should yield 0.
02865         self.assertEqual(now - now, timedelta(0))
02866         self.assertEqual(nowaware - nowaware, timedelta(0))
02867 
02868         # Adding a delta should preserve tzinfo.
02869         delta = timedelta(weeks=1, minutes=12, microseconds=5678)
02870         nowawareplus = nowaware + delta
02871         self.assertTrue(nowaware.tzinfo is tz55)
02872         nowawareplus2 = delta + nowaware
02873         self.assertTrue(nowawareplus2.tzinfo is tz55)
02874         self.assertEqual(nowawareplus, nowawareplus2)
02875 
02876         # that - delta should be what we started with, and that - what we
02877         # started with should be delta.
02878         diff = nowawareplus - delta
02879         self.assertTrue(diff.tzinfo is tz55)
02880         self.assertEqual(nowaware, diff)
02881         self.assertRaises(TypeError, lambda: delta - nowawareplus)
02882         self.assertEqual(nowawareplus - nowaware, delta)
02883 
02884         # Make up a random timezone.
02885         tzr = FixedOffset(random.randrange(-1439, 1440), "randomtimezone")
02886         # Attach it to nowawareplus.
02887         nowawareplus = nowawareplus.replace(tzinfo=tzr)
02888         self.assertTrue(nowawareplus.tzinfo is tzr)
02889         # Make sure the difference takes the timezone adjustments into account.
02890         got = nowaware - nowawareplus
02891         # Expected:  (nowaware base - nowaware offset) -
02892         #            (nowawareplus base - nowawareplus offset) =
02893         #            (nowaware base - nowawareplus base) +
02894         #            (nowawareplus offset - nowaware offset) =
02895         #            -delta + nowawareplus offset - nowaware offset
02896         expected = nowawareplus.utcoffset() - nowaware.utcoffset() - delta
02897         self.assertEqual(got, expected)
02898 
02899         # Try max possible difference.
02900         min = self.theclass(1, 1, 1, tzinfo=FixedOffset(1439, "min"))
02901         max = self.theclass(MAXYEAR, 12, 31, 23, 59, 59, 999999,
02902                             tzinfo=FixedOffset(-1439, "max"))
02903         maxdiff = max - min
02904         self.assertEqual(maxdiff, self.theclass.max - self.theclass.min +
02905                                   timedelta(minutes=2*1439))
02906         # Different tzinfo, but the same offset
02907         tza = timezone(HOUR, 'A')
02908         tzb = timezone(HOUR, 'B')
02909         delta = min.replace(tzinfo=tza) - max.replace(tzinfo=tzb)
02910         self.assertEqual(delta, self.theclass.min - self.theclass.max)

Here is the call graph for this function:

Definition at line 1486 of file datetimetester.py.

01486 
01487     def test_tz_independent_comparing(self):
01488         dt1 = self.theclass(2002, 3, 1, 9, 0, 0)
01489         dt2 = self.theclass(2002, 3, 1, 10, 0, 0)
01490         dt3 = self.theclass(2002, 3, 1, 9, 0, 0)
01491         self.assertEqual(dt1, dt3)
01492         self.assertTrue(dt2 > dt3)
01493 
01494         # Make sure comparison doesn't forget microseconds, and isn't done
01495         # via comparing a float timestamp (an IEEE double doesn't have enough
01496         # precision to span microsecond resolution across years 1 thru 9999,
01497         # so comparing via timestamp necessarily calls some distinct values
01498         # equal).
01499         dt1 = self.theclass(MAXYEAR, 12, 31, 23, 59, 59, 999998)
01500         us = timedelta(microseconds=1)
01501         dt2 = dt1 + us
01502         self.assertEqual(dt2 - dt1, us)
01503         self.assertTrue(dt1 < dt2)

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

02948 
02949     def test_tzinfo_fromtimestamp(self):
02950         import time
02951         meth = self.theclass.fromtimestamp
02952         ts = time.time()
02953         # Ensure it doesn't require tzinfo (i.e., that this doesn't blow up).
02954         base = meth(ts)
02955         # Try with and without naming the keyword.
02956         off42 = FixedOffset(42, "42")
02957         another = meth(ts, off42)
02958         again = meth(ts, tz=off42)
02959         self.assertTrue(another.tzinfo is again.tzinfo)
02960         self.assertEqual(another.utcoffset(), timedelta(minutes=42))
02961         # Bad argument with and w/o naming the keyword.
02962         self.assertRaises(TypeError, meth, ts, 16)
02963         self.assertRaises(TypeError, meth, ts, tzinfo=16)
02964         # Bad keyword name.
02965         self.assertRaises(TypeError, meth, ts, tinfo=off42)
02966         # Too many args.
02967         self.assertRaises(TypeError, meth, ts, off42, off42)
02968         # Too few args.
02969         self.assertRaises(TypeError, meth)
02970 
02971         # Try to make sure tz= actually does some conversion.
02972         timestamp = 1000000000
02973         utcdatetime = datetime.utcfromtimestamp(timestamp)
02974         # In POSIX (epoch 1970), that's 2001-09-09 01:46:40 UTC, give or take.
02975         # But on some flavor of Mac, it's nowhere near that.  So we can't have
02976         # any idea here what time that actually is, we can only test that
02977         # relative changes match.
02978         utcoffset = timedelta(hours=-15, minutes=39) # arbitrary, but not zero
02979         tz = FixedOffset(utcoffset, "tz", 0)
02980         expected = utcdatetime + utcoffset
02981         got = datetime.fromtimestamp(timestamp, tz)
02982         self.assertEqual(expected, got.replace(tzinfo=None))

Here is the call graph for this function:

Definition at line 3121 of file datetimetester.py.

03121 
03122     def test_tzinfo_isoformat(self):
03123         zero = FixedOffset(0, "+00:00")
03124         plus = FixedOffset(220, "+03:40")
03125         minus = FixedOffset(-231, "-03:51")
03126         unknown = FixedOffset(None, "")
03127 
03128         cls = self.theclass
03129         datestr = '0001-02-03'
03130         for ofs in None, zero, plus, minus, unknown:
03131             for us in 0, 987001:
03132                 d = cls(1, 2, 3, 4, 5, 59, us, tzinfo=ofs)
03133                 timestr = '04:05:59' + (us and '.987001' or '')
03134                 ofsstr = ofs is not None and d.tzname() or ''
03135                 tailstr = timestr + ofsstr
03136                 iso = d.isoformat()
03137                 self.assertEqual(iso, datestr + 'T' + tailstr)
03138                 self.assertEqual(iso, d.isoformat('T'))
03139                 self.assertEqual(d.isoformat('k'), datestr + 'k' + tailstr)
03140                 self.assertEqual(d.isoformat('\u1234'), datestr + '\u1234' + tailstr)
03141                 self.assertEqual(str(d), datestr + ' ' + tailstr)

Here is the call graph for this function:

Definition at line 2911 of file datetimetester.py.

02911 
02912     def test_tzinfo_now(self):
02913         meth = self.theclass.now
02914         # Ensure it doesn't require tzinfo (i.e., that this doesn't blow up).
02915         base = meth()
02916         # Try with and without naming the keyword.
02917         off42 = FixedOffset(42, "42")
02918         another = meth(off42)
02919         again = meth(tz=off42)
02920         self.assertTrue(another.tzinfo is again.tzinfo)
02921         self.assertEqual(another.utcoffset(), timedelta(minutes=42))
02922         # Bad argument with and w/o naming the keyword.
02923         self.assertRaises(TypeError, meth, 16)
02924         self.assertRaises(TypeError, meth, tzinfo=16)
02925         # Bad keyword name.
02926         self.assertRaises(TypeError, meth, tinfo=off42)
02927         # Too many args.
02928         self.assertRaises(TypeError, meth, off42, off42)
02929 
02930         # We don't know which time zone we're in, and don't have a tzinfo
02931         # class to represent it, so seeing whether a tz argument actually
02932         # does a conversion is tricky.
02933         utc = FixedOffset(0, "utc", 0)
02934         for weirdtz in [FixedOffset(timedelta(hours=15, minutes=58), "weirdtz", 0),
02935                         timezone(timedelta(hours=15, minutes=58), "weirdtz"),]:
02936             for dummy in range(3):
02937                 now = datetime.now(weirdtz)
02938                 self.assertTrue(now.tzinfo is weirdtz)
02939                 utcnow = datetime.utcnow().replace(tzinfo=utc)
02940                 now2 = utcnow.astimezone(weirdtz)
02941                 if abs(now - now2) < timedelta(seconds=30):
02942                     break
02943                 # Else the code is broken, or more than 30 seconds passed between
02944                 # calls; assuming the latter, just try again.
02945             else:
02946                 # Three strikes and we're out.
02947                 self.fail("utcnow(), now(tz), or astimezone() may be broken")

Here is the call graph for this function:

Definition at line 3005 of file datetimetester.py.

03005 
03006     def test_tzinfo_timetuple(self):
03007         # TestDateTime tested most of this.  datetime adds a twist to the
03008         # DST flag.
03009         class DST(tzinfo):
03010             def __init__(self, dstvalue):
03011                 if isinstance(dstvalue, int):
03012                     dstvalue = timedelta(minutes=dstvalue)
03013                 self.dstvalue = dstvalue
03014             def dst(self, dt):
03015                 return self.dstvalue
03016 
03017         cls = self.theclass
03018         for dstvalue, flag in (-33, 1), (33, 1), (0, 0), (None, -1):
03019             d = cls(1, 1, 1, 10, 20, 30, 40, tzinfo=DST(dstvalue))
03020             t = d.timetuple()
03021             self.assertEqual(1, t.tm_year)
03022             self.assertEqual(1, t.tm_mon)
03023             self.assertEqual(1, t.tm_mday)
03024             self.assertEqual(10, t.tm_hour)
03025             self.assertEqual(20, t.tm_min)
03026             self.assertEqual(30, t.tm_sec)
03027             self.assertEqual(0, t.tm_wday)
03028             self.assertEqual(1, t.tm_yday)
03029             self.assertEqual(flag, t.tm_isdst)
03030 
03031         # dst() returns wrong type.
03032         self.assertRaises(TypeError, cls(1, 1, 1, tzinfo=DST("x")).timetuple)
03033 
03034         # dst() at the edge.
03035         self.assertEqual(cls(1,1,1, tzinfo=DST(1439)).timetuple().tm_isdst, 1)
03036         self.assertEqual(cls(1,1,1, tzinfo=DST(-1439)).timetuple().tm_isdst, 1)
03037 
03038         # dst() out of range.
03039         self.assertRaises(ValueError, cls(1,1,1, tzinfo=DST(1440)).timetuple)
03040         self.assertRaises(ValueError, cls(1,1,1, tzinfo=DST(-1440)).timetuple)

Here is the call graph for this function:

Definition at line 2993 of file datetimetester.py.

02993 
02994     def test_tzinfo_utcfromtimestamp(self):
02995         import time
02996         meth = self.theclass.utcfromtimestamp
02997         ts = time.time()
02998         # Ensure it doesn't require tzinfo (i.e., that this doesn't blow up).
02999         base = meth(ts)
03000         # Try with and without naming the keyword; for whatever reason,
03001         # utcfromtimestamp() doesn't accept a tzinfo argument.
03002         off42 = FixedOffset(42, "42")
03003         self.assertRaises(TypeError, meth, ts, off42)
03004         self.assertRaises(TypeError, meth, ts, tzinfo=off42)

Here is the call graph for this function:

Definition at line 2983 of file datetimetester.py.

02983 
02984     def test_tzinfo_utcnow(self):
02985         meth = self.theclass.utcnow
02986         # Ensure it doesn't require tzinfo (i.e., that this doesn't blow up).
02987         base = meth()
02988         # Try with and without naming the keyword; for whatever reason,
02989         # utcnow() doesn't accept a tzinfo argument.
02990         off42 = FixedOffset(42, "42")
02991         self.assertRaises(TypeError, meth, off42)
02992         self.assertRaises(TypeError, meth, tzinfo=off42)

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

01724 
01725     def test_utcfromtimestamp(self):
01726         import time
01727 
01728         ts = time.time()
01729         expected = time.gmtime(ts)
01730         got = self.theclass.utcfromtimestamp(ts)
01731         self.verify_field_equality(expected, got)

Here is the call graph for this function:

Definition at line 1768 of file datetimetester.py.

01768 
01769     def test_utcnow(self):
01770         import time
01771 
01772         # Call it a success if utcnow() and utcfromtimestamp() are within
01773         # a second of each other.
01774         tolerance = timedelta(seconds=1)
01775         for dummy in range(3):
01776             from_now = self.theclass.utcnow()
01777             from_timestamp = self.theclass.utcfromtimestamp(time.time())
01778             if abs(from_timestamp - from_now) <= tolerance:
01779                 break
01780             # Else try again a few times.
01781         self.assertTrue(abs(from_timestamp - from_now) <= tolerance)

Here is the call graph for this function:

Definition at line 3041 of file datetimetester.py.

03041 
03042     def test_utctimetuple(self):
03043         class DST(tzinfo):
03044             def __init__(self, dstvalue=0):
03045                 if isinstance(dstvalue, int):
03046                     dstvalue = timedelta(minutes=dstvalue)
03047                 self.dstvalue = dstvalue
03048             def dst(self, dt):
03049                 return self.dstvalue
03050 
03051         cls = self.theclass
03052         # This can't work:  DST didn't implement utcoffset.
03053         self.assertRaises(NotImplementedError,
03054                           cls(1, 1, 1, tzinfo=DST(0)).utcoffset)
03055 
03056         class UOFS(DST):
03057             def __init__(self, uofs, dofs=None):
03058                 DST.__init__(self, dofs)
03059                 self.uofs = timedelta(minutes=uofs)
03060             def utcoffset(self, dt):
03061                 return self.uofs
03062 
03063         for dstvalue in -33, 33, 0, None:
03064             d = cls(1, 2, 3, 10, 20, 30, 40, tzinfo=UOFS(-53, dstvalue))
03065             t = d.utctimetuple()
03066             self.assertEqual(d.year, t.tm_year)
03067             self.assertEqual(d.month, t.tm_mon)
03068             self.assertEqual(d.day, t.tm_mday)
03069             self.assertEqual(11, t.tm_hour) # 20mm + 53mm = 1hn + 13mm
03070             self.assertEqual(13, t.tm_min)
03071             self.assertEqual(d.second, t.tm_sec)
03072             self.assertEqual(d.weekday(), t.tm_wday)
03073             self.assertEqual(d.toordinal() - date(1, 1, 1).toordinal() + 1,
03074                              t.tm_yday)
03075             # Ensure tm_isdst is 0 regardless of what dst() says: DST
03076             # is never in effect for a UTC time.
03077             self.assertEqual(0, t.tm_isdst)
03078 
03079         # For naive datetime, utctimetuple == timetuple except for isdst
03080         d = cls(1, 2, 3, 10, 20, 30, 40)
03081         t = d.utctimetuple()
03082         self.assertEqual(t[:-1], d.timetuple()[:-1])
03083         self.assertEqual(0, t.tm_isdst)
03084         # Same if utcoffset is None
03085         class NOFS(DST):
03086             def utcoffset(self, dt):
03087                 return None
03088         d = cls(1, 2, 3, 10, 20, 30, 40, tzinfo=NOFS())
03089         t = d.utctimetuple()
03090         self.assertEqual(t[:-1], d.timetuple()[:-1])
03091         self.assertEqual(0, t.tm_isdst)
03092         # Check that bad tzinfo is detected
03093         class BOFS(DST):
03094             def utcoffset(self, dt):
03095                 return "EST"
03096         d = cls(1, 2, 3, 10, 20, 30, 40, tzinfo=BOFS())
03097         self.assertRaises(TypeError, d.utctimetuple)
03098 
03099         # Check that utctimetuple() is the same as
03100         # astimezone(utc).timetuple()
03101         d = cls(2010, 11, 13, 14, 15, 16, 171819)
03102         for tz in [timezone.min, timezone.utc, timezone.max]:
03103             dtz = d.replace(tzinfo=tz)
03104             self.assertEqual(dtz.utctimetuple()[:-1],
03105                              dtz.astimezone(timezone.utc).timetuple()[:-1])
03106         # At the edges, UTC adjustment can produce years out-of-range
03107         # for a datetime object.  Ensure that an OverflowError is
03108         # raised.
03109         tiny = cls(MINYEAR, 1, 1, 0, 0, 37, tzinfo=UOFS(1439))
03110         # That goes back 1 minute less than a full day.
03111         self.assertRaises(OverflowError, tiny.utctimetuple)
03112 
03113         huge = cls(MAXYEAR, 12, 31, 23, 59, 37, 999999, tzinfo=UOFS(-1439))
03114         # That goes forward 1 minute less than a full day.
03115         self.assertRaises(OverflowError, huge.utctimetuple)
03116         # More overflow cases
03117         tiny = cls.min.replace(tzinfo=timezone(MINUTE))
03118         self.assertRaises(OverflowError, tiny.utctimetuple)
03119         huge = cls.max.replace(tzinfo=timezone(-MINUTE))
03120         self.assertRaises(OverflowError, huge.utctimetuple)

Here is the call graph for this function:

def test.datetimetester.TestDate.test_weekday (   self) [inherited]

Definition at line 1011 of file datetimetester.py.

01011 
01012     def test_weekday(self):
01013         for i in range(7):
01014             # March 4, 2002 is a Monday
01015             self.assertEqual(self.theclass(2002, 3, 4+i).weekday(), i)
01016             self.assertEqual(self.theclass(2002, 3, 4+i).isoweekday(), i+1)
01017             # January 2, 1956 is a Monday
01018             self.assertEqual(self.theclass(1956, 1, 2+i).weekday(), i)
01019             self.assertEqual(self.theclass(1956, 1, 2+i).isoweekday(), i+1)

Here is the call graph for this function:

Definition at line 2799 of file datetimetester.py.

02799 
02800     def test_zones(self):
02801         est = FixedOffset(-300, "EST")
02802         utc = FixedOffset(0, "UTC")
02803         met = FixedOffset(60, "MET")
02804         t1 = datetime(2002, 3, 19,  7, 47, tzinfo=est)
02805         t2 = datetime(2002, 3, 19, 12, 47, tzinfo=utc)
02806         t3 = datetime(2002, 3, 19, 13, 47, tzinfo=met)
02807         self.assertEqual(t1.tzinfo, est)
02808         self.assertEqual(t2.tzinfo, utc)
02809         self.assertEqual(t3.tzinfo, met)
02810         self.assertEqual(t1.utcoffset(), timedelta(minutes=-300))
02811         self.assertEqual(t2.utcoffset(), timedelta(minutes=0))
02812         self.assertEqual(t3.utcoffset(), timedelta(minutes=60))
02813         self.assertEqual(t1.tzname(), "EST")
02814         self.assertEqual(t2.tzname(), "UTC")
02815         self.assertEqual(t3.tzname(), "MET")
02816         self.assertEqual(hash(t1), hash(t2))
02817         self.assertEqual(hash(t1), hash(t3))
02818         self.assertEqual(hash(t2), hash(t3))
02819         self.assertEqual(t1, t2)
02820         self.assertEqual(t1, t3)
02821         self.assertEqual(t2, t3)
02822         self.assertEqual(str(t1), "2002-03-19 07:47:00-05:00")
02823         self.assertEqual(str(t2), "2002-03-19 12:47:00+00:00")
02824         self.assertEqual(str(t3), "2002-03-19 13:47:00+01:00")
02825         d = 'datetime.datetime(2002, 3, 19, '
02826         self.assertEqual(repr(t1), d + "7, 47, tzinfo=est)")
02827         self.assertEqual(repr(t2), d + "12, 47, tzinfo=utc)")
02828         self.assertEqual(repr(t3), d + "13, 47, tzinfo=met)")

Here is the call graph for this function:

def test.datetimetester.TestDateTime.verify_field_equality (   self,
  expected,
  got 
) [inherited]

Definition at line 1708 of file datetimetester.py.

01708 
01709     def verify_field_equality(self, expected, got):
01710         self.assertEqual(expected.tm_year, got.year)
01711         self.assertEqual(expected.tm_mon, got.month)
01712         self.assertEqual(expected.tm_mday, got.day)
01713         self.assertEqual(expected.tm_hour, got.hour)
01714         self.assertEqual(expected.tm_min, got.minute)
01715         self.assertEqual(expected.tm_sec, got.second)

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Definition at line 3012 of file datetimetester.py.

Reimplemented from test.datetimetester.TZInfoBase.

Definition at line 3270 of file datetimetester.py.

Reimplemented from test.datetimetester.TestDateTime.

Definition at line 2685 of file datetimetester.py.

Definition at line 3058 of file datetimetester.py.


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