def test_to_and_from_delta_bce(self): for month, day in [(1, 1), (1, 2), (2, 1), (2, 28), (3, 10), (5, 26), (6, 30), (12, 31)]: fmt1 = '-{:04}-%s' % '{:02}-{:02}'.format(month, day) fmt2 = '{}-%s' % '{:02}-{:02}'.format(month, day) days = -sum(MONTH_DAYS_LEAP[m] for m in range(month, 13)) + day - 1 for year in range(-1, -15000, -1): if year >= -500 or -9900 >= year >= -10100 or random.randint( 1, 20) == 1: date_string = fmt1.format( abs(year)) if year > -10000 else fmt2.format(year) dt1 = Date10.fromstring(date_string) delta1 = dt1.todelta() delta2 = datetime.timedelta(days=days) self.assertEqual(delta1, delta2, msg="Failed for %r: %r != %r" % (dt1, delta1, delta2)) dt2 = Date10.fromdelta(delta2) self.assertEqual(dt1, dt2, msg="Failed for year %d: %r != %r" % (year, dt1, dt2)) days -= 366 if isleap(year if month <= 2 else year + 1) else 365
def test_to_and_from_delta(self): for month, day in [(1, 1), (1, 2), (2, 1), (2, 28), (3, 10), (6, 30), (12, 31)]: fmt1 = '{:04}-%s' % '{:02}-{:02}'.format(month, day) fmt2 = '{}-%s' % '{:02}-{:02}'.format(month, day) days = sum(MONTH_DAYS[m] for m in range(1, month)) + day - 1 for year in range(1, 15000): if year <= 500 or 9900 <= year <= 10100 or random.randint( 1, 20) == 1: date_string = fmt1.format( year) if year < 10000 else fmt2.format(year) dt1 = Date10.fromstring(date_string) delta1 = dt1.todelta() delta2 = datetime.timedelta(days=days) self.assertEqual(delta1, delta2, msg="Failed for %r: %r != %r" % (dt1, delta1, delta2)) dt2 = Date10.fromdelta(delta2) self.assertEqual(dt1, dt2, msg="Failed for year %d: %r != %r" % (year, dt1, dt2)) days += 366 if isleap(year if month <= 2 else year + 1) else 365
def test_todelta(self): self.assertEqual( Date.fromstring("0001-01-01").todelta(), datetime.timedelta(days=0)) self.assertEqual( Date.fromstring("0001-02-01").todelta(), datetime.timedelta(days=31)) self.assertEqual( Date.fromstring("0001-03-01").todelta(), datetime.timedelta(days=59)) self.assertEqual( Date.fromstring("0001-06-01").todelta(), datetime.timedelta(days=151)) self.assertEqual( Date.fromstring("0001-06-03").todelta(), datetime.timedelta(days=153)) self.assertEqual( DateTime.fromstring("0001-06-03T20:00:00").todelta(), datetime.timedelta(days=153, seconds=72000)) self.assertEqual( Date.fromstring("0002-01-01").todelta(), datetime.timedelta(days=365)) self.assertEqual( Date.fromstring("0002-02-01").todelta(), datetime.timedelta(days=396)) self.assertEqual( Date.fromstring("-0000-01-01").todelta(), datetime.timedelta(days=-366)) self.assertEqual( Date.fromstring("-0000-02-01").todelta(), datetime.timedelta(days=-335)) self.assertEqual( Date.fromstring("-0000-12-31").todelta(), datetime.timedelta(days=-1)) self.assertEqual( Date10.fromstring("-0001-01-01").todelta(), datetime.timedelta(days=-366)) self.assertEqual( Date10.fromstring("-0001-02-10").todelta(), datetime.timedelta(days=-326)) self.assertEqual( Date10.fromstring("-0001-12-31Z").todelta(), datetime.timedelta(days=-1)) self.assertEqual( Date10.fromstring("-0001-12-31-02:00").todelta(), datetime.timedelta(hours=-22)) self.assertEqual( Date10.fromstring("-0001-12-31+03:00").todelta(), datetime.timedelta(hours=-27)) self.assertEqual( Date10.fromstring("-0001-12-31+03:00").todelta(), datetime.timedelta(hours=-27)) self.assertEqual( Date10.fromstring("-0001-12-31+03:12").todelta(), datetime.timedelta(hours=-27, minutes=-12))
def test_date_repr(self): dt = Date.fromstring('2000-10-07') self.assertEqual(repr(dt), "Date(2000, 10, 7)") self.assertEqual(str(dt), '2000-10-07') dt = Date.fromstring('-0100-04-13') self.assertEqual(repr(dt), "Date(-101, 4, 13)") self.assertEqual(str(dt), '-0100-04-13') dt = Date10.fromstring('-0100-04-13') self.assertEqual(repr(dt), "Date10(-100, 4, 13)") self.assertEqual(str(dt), '-0100-04-13')
def test_fromdelta(self): self.assertIsNotNone(Date.fromstring('10000-02-28')) self.assertEqual(Date.fromdelta(datetime.timedelta(days=0)), Date.fromstring("0001-01-01")) self.assertEqual(Date.fromdelta(datetime.timedelta(days=31)), Date.fromstring("0001-02-01")) self.assertEqual(Date.fromdelta(datetime.timedelta(days=59)), Date.fromstring("0001-03-01")) self.assertEqual(Date.fromdelta(datetime.timedelta(days=151)), Date.fromstring("0001-06-01")) self.assertEqual(Date.fromdelta(datetime.timedelta(days=153)), Date.fromstring("0001-06-03")) self.assertEqual(DateTime.fromdelta(datetime.timedelta(days=153, seconds=72000)), DateTime.fromstring("0001-06-03T20:00:00")) self.assertEqual(Date.fromdelta(datetime.timedelta(days=365)), Date.fromstring("0002-01-01")) self.assertEqual(Date.fromdelta(datetime.timedelta(days=396)), Date.fromstring("0002-02-01")) self.assertEqual(Date.fromdelta(datetime.timedelta(days=-366)), Date.fromstring("-0000-01-01")) self.assertEqual(Date.fromdelta(datetime.timedelta(days=-1)), Date.fromstring("-0000-12-31")) self.assertEqual(Date.fromdelta(datetime.timedelta(days=-335)), Date.fromstring("-0000-02-01")) self.assertEqual(Date.fromdelta(datetime.timedelta(days=-1)), Date.fromstring("-0000-12-31")) self.assertEqual(Date10.fromdelta(datetime.timedelta(days=-366)), Date10.fromstring("-0001-01-01")) self.assertEqual(Date10.fromdelta(datetime.timedelta(days=-326)), Date10.fromstring("-0001-02-10")) self.assertEqual(Date10.fromdelta(datetime.timedelta(days=-1)), Date10.fromstring("-0001-12-31Z")) # With timezone adjusting self.assertEqual(Date10.fromdelta(datetime.timedelta(hours=-22), adjust_timezone=True), Date10.fromstring("-0001-12-31-02:00")) self.assertEqual(Date10.fromdelta(datetime.timedelta(hours=-27), adjust_timezone=True), Date10.fromstring("-0001-12-31+03:00")) self.assertEqual(Date10.fromdelta(datetime.timedelta(hours=-27, minutes=-12), adjust_timezone=True), Date10.fromstring("-0001-12-31+03:12")) self.assertEqual(DateTime10.fromdelta(datetime.timedelta(hours=-27, minutes=-12, seconds=-5)), DateTime10.fromstring("-0001-12-30T20:47:55"))
def decode(self, obj, *args, **kwargs): return Date10.fromstring(obj)