def test_init_fromstring(self): self.assertIsInstance(DateTime.fromstring('2000-10-07T00:00:00'), DateTime) self.assertIsInstance(DateTime.fromstring('-2000-10-07T00:00:00'), DateTime) self.assertRaises(ValueError, DateTime.fromstring, '00-10-07') self.assertIsInstance(Date.fromstring('2000-10-07'), Date) self.assertIsInstance(Date.fromstring('-2000-10-07'), Date)
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 test_eq_operator(self): tz = Timezone.fromstring('-05:00') mkdt = DateTime.fromstring self.assertTrue(mkdt("2002-04-02T12:00:00-01:00") == mkdt("2002-04-02T17:00:00+04:00")) self.assertFalse(mkdt("2002-04-02T12:00:00") == mkdt("2002-04-02T23:00:00+06:00")) self.assertFalse(mkdt("2002-04-02T12:00:00") == mkdt("2002-04-02T17:00:00")) self.assertTrue(mkdt("2002-04-02T12:00:00") == mkdt("2002-04-02T12:00:00")) self.assertTrue(mkdt("2002-04-02T23:00:00-04:00") == mkdt("2002-04-03T02:00:00-01:00")) self.assertTrue(mkdt("1999-12-31T24:00:00") == mkdt("1999-12-31T00:00:00")) self.assertTrue(mkdt("2005-04-04T24:00:00") == mkdt("2005-04-04T00:00:00")) self.assertTrue(mkdt("2002-04-02T12:00:00-01:00", tz) == mkdt("2002-04-02T17:00:00+04:00", tz)) self.assertTrue(mkdt("2002-04-02T12:00:00", tz) == mkdt("2002-04-02T23:00:00+06:00", tz)) self.assertFalse(mkdt("2002-04-02T12:00:00", tz) == mkdt("2002-04-02T17:00:00", tz)) self.assertTrue(mkdt("2002-04-02T12:00:00", tz) == mkdt("2002-04-02T12:00:00", tz)) self.assertTrue(mkdt("2002-04-02T23:00:00-04:00", tz) == mkdt("2002-04-03T02:00:00-01:00", tz)) self.assertTrue(mkdt("1999-12-31T24:00:00", tz) == mkdt("1999-12-31T00:00:00", tz)) self.assertTrue(mkdt("2005-04-04T24:00:00", tz) == mkdt("2005-04-04T00:00:00", tz)) self.assertFalse(mkdt("2005-04-04T24:00:00", tz) != mkdt("2005-04-04T00:00:00", tz)) self.assertTrue(DateTime.fromstring("-1000-01-01") == DateTime.fromstring("-1000-01-01")) self.assertTrue(DateTime.fromstring("-10000-01-01") == DateTime.fromstring("-10000-01-01")) self.assertFalse(DateTime.fromstring("20000-01-01") != DateTime.fromstring("20000-01-01")) self.assertFalse(DateTime.fromstring("-10000-01-02") == DateTime.fromstring("-10000-01-01"))
def test_datetime_repr(self): dt = DateTime.fromstring('2000-10-07') self.assertEqual(repr(dt), "DateTime(2000, 10, 7, 0, 0, 0)") self.assertEqual(str(dt), '2000-10-07T00:00:00') dt = DateTime.fromstring('-0100-04-13T23:59:59') self.assertEqual(repr(dt), "DateTime(-101, 4, 13, 23, 59, 59)") self.assertEqual(str(dt), '-0100-04-13T23:59:59') dt = DateTime10.fromstring('-0100-04-13T10:30:00-04:00') if sys.version_info >= (3, 7): self.assertEqual(repr(dt), "DateTime10(-100, 4, 13, 10, 30, 0, " "tzinfo=Timezone(datetime.timedelta(days=-1, seconds=72000)))") else: self.assertEqual(repr(dt), "DateTime10(-100, 4, 13, 10, 30, 0, " "tzinfo=Timezone(datetime.timedelta(-1, 72000)))") self.assertEqual(str(dt), '-0100-04-13T10:30:00-04:00')
def test_datetime_constructor(self): tz1 = Timezone(datetime.timedelta(hours=5, minutes=24)) self.check_value('xs:dateTime(())', []) self.check_value('xs:dateTime("1969-07-20T20:18:00")', DateTime10(1969, 7, 20, 20, 18)) self.check_value( 'xs:dateTime(xs:untypedAtomic("1969-07-20T20:18:00"))', DateTime10(1969, 7, 20, 20, 18)) self.check_value( 'xs:dateTime("2000-05-10T21:30:00+05:24")', datetime.datetime(2000, 5, 10, hour=21, minute=30, tzinfo=tz1)) self.check_value('xs:dateTime("1999-12-31T24:00:00")', datetime.datetime(2000, 1, 1, 0, 0)) self.check_value('xs:dateTime(xs:date("1969-07-20"))', DateTime10(1969, 7, 20)) self.check_value('xs:dateTime(xs:date("1969-07-20"))', DateTime10) with self.assertRaises(AssertionError): self.check_value('xs:dateTime(xs:date("1969-07-20"))', DateTime) self.parser._xsd_version = '1.1' try: self.check_value('xs:dateTime(xs:date("1969-07-20"))', DateTime(1969, 7, 20)) self.check_value('xs:dateTime(xs:date("1969-07-20"))', DateTime) finally: self.parser._xsd_version = '1.0' self.wrong_value('xs:dateTime("2000-05-10t21:30:00+05:24")') self.wrong_value('xs:dateTime("2000-5-10T21:30:00+05:24")') self.wrong_value('xs:dateTime("2000-05-10T21:3:00+05:24")') self.wrong_value('xs:dateTime("2000-05-10T21:13:0+05:24")') self.wrong_value('xs:dateTime("2000-05-10T21:13:0")') self.check_value('xs:dateTime("-25252734927766554-12-31T12:00:00")', OverflowError) self.wrong_type('xs:dateTime(50)', 'FORG0006', '1st argument has an invalid type') self.wrong_type('xs:dateTime("2000-05-10T21:30:00", "+05:24")', 'XPST0017') root = self.etree.XML('<root a="1969-07-20T20:18:00"/>') context = XPathContext(root) self.check_value('xs:dateTime(@a)', DateTime10(1969, 7, 20, 20, 18), context=context) context.item = AttributeNode('a', str(DateTime10(1969, 7, 20, 20, 18))) self.check_value('xs:dateTime(.)', DateTime10(1969, 7, 20, 20, 18), context=context) context.item = AttributeNode('a', 'true') self.check_value('xs:dateTime(.)', ValueError, context=context) context.item = DateTime10(1969, 7, 20, 20, 18) self.check_value('xs:dateTime(.)', DateTime10(1969, 7, 20, 20, 18), context=context)
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_hashing(self): dt = DateTime.fromstring("2002-04-02T12:00:00-01:00") self.assertIsInstance(hash(dt), int)
def test_24_hour_datetime(self): dt = DateTime.fromstring('0000-09-19T24:00:00Z') self.assertEqual(str(dt), '0000-09-20T00:00:00Z')