def testSubtract(self): """YMD.__sub__ => YMD - YMD.""" t1 = dict(y=2000, m=10, dd=16.789, hh=15.654, mm=1.345, ss=9.45) t2 = dict(y=2010, m=11, dd=19.34, hh=29.3, mm=1.345, ss=10.8) ymd1 = tpm.YMD() ymd2 = tpm.YMD() ymd1.y = t1['y'] ymd1.m = t1['m'] ymd1.dd = t1['dd'] ymd1.hh = t1['hh'] ymd1.mm = t1['mm'] ymd1.ss = t1['ss'] ymd2.y = t2['y'] ymd2.m = t2['m'] ymd2.dd = t2['dd'] ymd2.hh = t2['hh'] ymd2.mm = t2['mm'] ymd2.ss = t2['ss'] ymd = ymd1 - ymd2 self.assertAlmostEqual(ymd.y, t1['y'] - t2['y']) self.assertAlmostEqual(ymd.m, t1['m'] - t2['m']) self.assertAlmostEqual(ymd.dd, t1['dd'] - t2['dd']) self.assertAlmostEqual(ymd.hh, t1['hh'] - t2['hh']) self.assertAlmostEqual(ymd.mm, t1['mm'] - t2['mm']) self.assertAlmostEqual(ymd.ss, t1['ss'] - t2['ss'])
def testRawStr(self): """ymd.raw_str() => 'raw' representation of YMD.""" t = dict(y=2010, m=10, dd=16.789, hh=15.654, mm=1.345, ss=9.45) ymd = tpm.YMD(**t) self.assertEqual(ymd.raw_str(), "2010 10 16.789 15.654 1.345 9.45") t = dict(y=-1, m=10, dd=1.0, hh=23.9999, mm=54.0, ss=9.45) ymd = tpm.YMD(**t) self.assertEqual(ymd.raw_str(), "-1 10 1 23.9999 54 9.45")
def verify(t, t_norm): ymd = tpm.YMD(**t) jd = ymd.to_jd() self.assertAlmostEqual(jd.dd, t_norm['dd']) self.assertAlmostEqual(jd.hh, t_norm['hh']) self.assertAlmostEqual(jd.mm, t_norm['mm']) self.assertAlmostEqual(jd.ss, t_norm['ss'])
def testToJ(self): """YMD.to_j() => Convert YMD into a scalar Julian date.""" def verify(t, t_norm): ymd = tpm.YMD(**t) self.assertAlmostEqual(ymd.to_j(), t_norm) # See pytpm/tests/c_tests/ymd2j_test.c. t = dict(y=1858, m=11, dd=17.0, hh=0.0, mm=0.0, ss=0.0) verify(t, 2400000.5) t = dict(y=1949, m=12, dd=31.923459, hh=0.0, mm=0.0, ss=0.0) verify(t, 2433282.42345900) t = dict(y=2000, m=1, dd=1.0, hh=0.0, mm=0.0, ss=0.0) verify(t, 2451544.5) t = dict(y=1984, m=1, dd=1.0, hh=0.0, mm=0.0, ss=0.0) verify(t, 2445700.5) t = dict(y=1985, m=12, dd=31.0, hh=23.0, mm=59.0, ss=60.0) verify(t, 2446431.50) # Check if YMD initialized with Julian date gives back the # Julian date; added after initialization was set to accept a # Julian date. self.assertAlmostEqual( tpm.YMD(j=2433282.42345900).to_j(), 2433282.42345900)
def verify(t, t_norm): ymd = tpm.YMD(**t) ymd.normalize() self.assertAlmostEqual(ymd.y, t_norm['y']) self.assertAlmostEqual(ymd.m, t_norm['m']) self.assertAlmostEqual(ymd.dd, t_norm['dd']) self.assertAlmostEqual(ymd.mm, t_norm['mm']) self.assertAlmostEqual(ymd.ss, t_norm['ss'])
def testSubtractNonYMD(self): """YMD - x => Exception if subtraction involves non YMD value.""" ymd = tpm.YMD() # All zeros. def sub_ymd(ymd, x): return ymd - x self.assertRaises(TypeError, sub_ymd, ymd, 1)
def testSetFieldValuesAtInit(self): """YMD(**vals) => initialize field values.""" def verify(t, t_norm): ymd = tpm.YMD(**t) self.assertAlmostEqual(ymd.y, t_norm['y']) self.assertAlmostEqual(ymd.m, t_norm['m']) self.assertAlmostEqual(ymd.dd, t_norm['dd']) self.assertAlmostEqual(ymd.hh, t_norm['hh']) self.assertAlmostEqual(ymd.mm, t_norm['mm']) self.assertAlmostEqual(ymd.ss, t_norm['ss']) t = dict(y=2000, m=10, dd=16.789, hh=15.654, mm=1.345, ss=9.45) verify(t, t) # For initialization with year see # pytpm/tests/c_tests/y2ymd_test.c t_norm = dict(y=1858, m=1, dd=321.0, hh=0.0, mm=0.0, ss=0.0) verify(dict(year=1858.879452054794), t_norm) t_norm = dict(y=1950, m=1, dd=0.923459, hh=0.0, mm=0.0, ss=0.0) verify(dict(year=1950.002530024794), t_norm) # For initialization with Julian date. t_norm = dict(y=1949, m=12, dd=31.923459, hh=0.0, mm=0.0, ss=0.0) verify(dict(j=2433282.42345900), t_norm) t_norm = dict(y=2000, m=1, dd=1.0, hh=0.0, mm=0.0, ss=0.0) verify(dict(j=2451544.5), t_norm) # Check to see if initialization with year and day number gives # correct results. # See pytpm/tests/c_tests/ydd2ymd_test.c. t_norm = dict(y=2000, m=1, dd=1.5, hh=0.0, mm=0.0, ss=0.0) verify(dict(ydd=(2000, 1.5)), t_norm) t_norm = dict(y=1858, m=1, dd=321.0, hh=0.0, mm=0.0, ss=0.0) verify(dict(ydd=(1858, 321.0)), t_norm) t_norm = dict(y=1949, m=1, dd=365.923459, hh=0.0, mm=0.0, ss=0.0) verify(dict(ydd=(1949, 365.9234590)), t_norm) # Verify that using non-integer year and month raises # AssertionError. self.assertRaises(AssertionError, lambda: tpm.YMD(y=2000.12)) self.assertRaises(AssertionError, lambda: tpm.YMD(m=12.0))
def testNonIntegerYearMonth(self): """YMD.x => Exception for non-integer year and month.""" ymd = tpm.YMD() def non_int(ymd, y, m): ymd.y = y ymd.m = m self.assertRaises(AssertionError, non_int, ymd, 1.0, 1.0)
def testGetFields(self): """YMD.x => retrieve field values.""" ymd = tpm.YMD() self.assertAlmostEqual(ymd.y, 2000) self.assertAlmostEqual(ymd.m, 1) self.assertAlmostEqual(ymd.dd, 0.0) self.assertAlmostEqual(ymd.hh, 0.0) self.assertAlmostEqual(ymd.mm, 0.0) self.assertAlmostEqual(ymd.ss, 0.0)
def testUTCNow(self): """tpm.utc_now() => the current Julian date; nearest second.""" from datetime import datetime j = tpm.utc_now() d = datetime.utcnow() ymd = tpm.YMD(j=j) ymd.normalize() self.assertAlmostEqual(ymd.y, d.year) self.assertAlmostEqual(ymd.m, d.month) self.assertAlmostEqual(ymd.dd, d.day) self.assertAlmostEqual(ymd.hh, d.hour) self.assertAlmostEqual(ymd.mm, d.minute) self.assertAlmostEqual(ymd.ss, d.second, 1)
def testUnicodeAndStr(self): """YMD.__str__ => string representations.""" # The ouput strings were obtained from # pytpm/tests/c_tests/fmt_ymd_test.c t = dict(y=2010, m=10, dd=16.789, hh=15.654, mm=1.345, ss=9.45) t1 = dict(y=-1, m=10, dd=1.0, hh=23.9999, mm=54.0, ss=9.45) ymd = tpm.YMD() ymd.y = t['y'] ymd.m = t['m'] ymd.dd = t['dd'] ymd.hh = t['hh'] ymd.mm = t['mm'] ymd.ss = t['ss'] s = str(ymd) self.assertEqual(s, "Sun Oct 17 10:36:54.149 2010") ymd = tpm.YMD() ymd.y = t1['y'] ymd.m = t1['m'] ymd.dd = t1['dd'] ymd.hh = t1['hh'] ymd.mm = t1['mm'] ymd.ss = t1['ss'] s = str(ymd) self.assertEqual(s, "Sat Oct 2 00:54:09.090 2 BC")
def testSetFieldValues(self): """YMD.x => set field values.""" t = dict(y=2010, m=11, dd=19.34, hh=29.3, mm=1.345, ss=10.8) ymd = tpm.YMD() ymd.y = t['y'] ymd.m = t['m'] ymd.dd = t['dd'] ymd.hh = t['hh'] ymd.mm = t['mm'] ymd.ss = t['ss'] self.assertAlmostEqual(ymd.y, t['y']) self.assertAlmostEqual(ymd.m, t['m']) self.assertAlmostEqual(ymd.dd, t['dd']) self.assertAlmostEqual(ymd.hh, t['hh']) self.assertAlmostEqual(ymd.mm, t['mm']) self.assertAlmostEqual(ymd.ss, t['ss'])
def testToYear(self): """YMD.to_year() => YMD into a year number.""" def verify(t, t_norm): ymd = tpm.YMD(**t) self.assertAlmostEqual(ymd.to_year(), t_norm) # See pytpm/tests/c_tests/ymd2y_test.c. t = dict(y=1858, m=11, dd=17.0, hh=0.0, mm=0.0, ss=0.0) verify(t, 1858.87945205) t = dict(y=1949, m=12, dd=31.923459, hh=0.0, mm=0.0, ss=0.0) verify(t, 1950.00253002) t = dict(y=2000, m=1, dd=1.0, hh=0.0, mm=0.0, ss=0.0) verify(t, 2000.00273224) t = dict(y=1984, m=1, dd=1.0, hh=0.0, mm=0.0, ss=0.0) verify(t, 1984.00273224) t = dict(y=1984, m=1, dd=0.0, hh=0.0, mm=0.0, ss=0.0) verify(t, 1984.0) t = dict(y=1984, m=12, dd=31.0, hh=23.0, mm=59.0, ss=60.0) verify(t, 1985.00273973) t = dict(y=1985, m=1, dd=1.0, hh=0.0, mm=0.0, ss=0.0) verify(t, 1985.00273973) t = dict(y=1985, m=1, dd=0.0, hh=0.0, mm=0.0, ss=0.0) verify(t, 1985.0) t = dict(y=1985, m=12, dd=31.0, hh=23.0, mm=59.0, ss=60.0) verify(t, 1986.00273973) # To and from year; this test was added after the # initialization with "year=value" was defined. self.assertAlmostEqual( tpm.YMD(year=1986.00273973).to_year(), 1986.00273973)
def verify(t, t_norm): ymd = tpm.YMD(**t) self.assertAlmostEqual(ymd.to_j(), t_norm)
def testCreate(self): """YMD() => create YMD object.""" ymd = tpm.YMD() self.assertEqual(type(ymd), tpm.YMD)
def testInvalidKeywordAtInit(self): """YMD(key=value) => raise TypeError if key is invalid.""" self.assertRaises(TypeError, lambda: tpm.YMD(h=12.0)) self.assertRaises(TypeError, lambda: tpm.YMD(years=12.0))
def testJ2DOW(self): """tpm.j2dow() => day of the week for the Julian date.""" self.assertEqual(tpm.j2dow(tpm.J2000), 6) self.assertEqual(tpm.j2dow(tpm.YMD(y=2010, m=11, dd=13).to_j()), 6) self.assertEqual(tpm.j2dow(tpm.YMD(y=2010, m=11, dd=12).to_j()), 5)