MCPcopy Index your code
hub / github.com/python/cpython / test_ordinal_conversions

Method test_ordinal_conversions

Lib/test/datetimetester.py:1257–1302  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

1255 self.assertEqual(repr(td), "SubclassDate(2010, 10, 10)")
1256
1257 def test_ordinal_conversions(self):
1258 # Check some fixed values.
1259 for y, m, d, n in [(1, 1, 1, 1), # calendar origin
1260 (1, 12, 31, 365),
1261 (2, 1, 1, 366),
1262 # first example from "Calendrical Calculations"
1263 (1945, 11, 12, 710347)]:
1264 d = self.theclass(y, m, d)
1265 self.assertEqual(n, d.toordinal())
1266 fromord = self.theclass.fromordinal(n)
1267 self.assertEqual(d, fromord)
1268 if hasattr(fromord, "hour"):
1269 # if we're checking something fancier than a date, verify
1270 # the extra fields have been zeroed out
1271 self.assertEqual(fromord.hour, 0)
1272 self.assertEqual(fromord.minute, 0)
1273 self.assertEqual(fromord.second, 0)
1274 self.assertEqual(fromord.microsecond, 0)
1275
1276 # Check first and last days of year spottily across the whole
1277 # range of years supported.
1278 for year in range(MINYEAR, MAXYEAR+1, 7):
1279 # Verify (year, 1, 1) -> ordinal -> y, m, d is identity.
1280 d = self.theclass(year, 1, 1)
1281 n = d.toordinal()
1282 d2 = self.theclass.fromordinal(n)
1283 self.assertEqual(d, d2)
1284 # Verify that moving back a day gets to the end of year-1.
1285 if year > 1:
1286 d = self.theclass.fromordinal(n-1)
1287 d2 = self.theclass(year-1, 12, 31)
1288 self.assertEqual(d, d2)
1289 self.assertEqual(d2.toordinal(), n-1)
1290
1291 # Test every day in a leap-year and a non-leap year.
1292 dim = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
1293 for year, isleap in (2000, True), (2002, False):
1294 n = self.theclass(year, 1, 1).toordinal()
1295 for month, maxday in zip(range(1, 13), dim):
1296 if month == 2 and isleap:
1297 maxday += 1
1298 for day in range(1, maxday+1):
1299 d = self.theclass(year, month, day)
1300 self.assertEqual(d.toordinal(), n)
1301 self.assertEqual(d, self.theclass.fromordinal(n))
1302 n += 1
1303
1304 def test_extreme_ordinals(self):
1305 a = self.theclass.min

Callers

nothing calls this directly

Calls 3

toordinalMethod · 0.80
fromordinalMethod · 0.80
assertEqualMethod · 0.45

Tested by

no test coverage detected