def test_dict_to_timedelta(self): td = make_timedelta({}) self.assertEqual(td.total_seconds(), 0) self.assertEqual(td.days, 0) self.assertEqual(td.seconds, 0) self.assertEqual(td.microseconds, 0) td = make_timedelta({'seconds': 0}) self.assertEqual(td.total_seconds(), 0) self.assertEqual(td.days, 0) self.assertEqual(td.seconds, 0) self.assertEqual(td.microseconds, 0) td = make_timedelta({'seconds': 24 * 3600 + 61.5}) self.assertEqual(td.total_seconds(), 24 * 3600 + 61.5) self.assertEqual(td.days, 1) self.assertEqual(td.seconds, 61) self.assertEqual(td.microseconds, 500000) td = make_timedelta({'days': 1, 'seconds': 61.5}) self.assertEqual(td.total_seconds(), 24 * 3600 + 61.5) self.assertEqual(td.days, 1) self.assertEqual(td.seconds, 61) self.assertEqual(td.microseconds, 500000) td = make_timedelta({'days': 3, 'seconds': 61, 'microseconds': 500000}) self.assertEqual(td.total_seconds(), 3 * 24 * 3600 + 61.5) self.assertEqual(td.days, 3) self.assertEqual(td.seconds, 61) self.assertEqual(td.microseconds, 500000)
def test_seconds_to_timedelta(self): td = make_timedelta(0) self.assertEqual(td.total_seconds(), 0) self.assertEqual(td.days, 0) self.assertEqual(td.seconds, 0) self.assertEqual(td.microseconds, 0) td = make_timedelta(61.5) self.assertEqual(td.total_seconds(), 61.5) self.assertEqual(td.days, 0) self.assertEqual(td.seconds, 61) self.assertEqual(td.microseconds, 500000) td = make_timedelta(24 * 3600 + 61.5) self.assertEqual(td.total_seconds(), 24 * 3600 + 61.5) self.assertEqual(td.days, 1) self.assertEqual(td.seconds, 61) self.assertEqual(td.microseconds, 500000)
def test_iso_to_timedelta(self): td = make_timedelta('PT0S') self.assertEqual(td.total_seconds(), 0) self.assertEqual(td.seconds, 0) self.assertEqual(td.microseconds, 0) td = make_timedelta('PT1M1S') self.assertEqual(td.total_seconds(), 61) self.assertEqual(td.seconds, 61) self.assertEqual(td.microseconds, 0) td = make_timedelta('PT00.225S') self.assertEqual(td.total_seconds(), 0.225) self.assertEqual(td.seconds, 0) self.assertEqual(td.microseconds, 225000) td = make_timedelta('PT12.345S') self.assertEqual(td.total_seconds(), 12.345) self.assertEqual(td.seconds, 12) self.assertEqual(td.microseconds, 345000) td = make_timedelta('PT02.5000M') self.assertEqual(td.total_seconds(), 150) td = make_timedelta('PT1H') self.assertEqual(td.total_seconds(), 3600) self.assertEqual(td.seconds, 3600) self.assertEqual(td.microseconds, 0) td = make_timedelta('PT0.5H') self.assertEqual(td.total_seconds(), 1800) self.assertEqual(td.seconds, 1800) self.assertEqual(td.microseconds, 0) td = make_timedelta('PT01H02M03.045S') self.assertEqual(td.total_seconds(), 3600 + 2 * 60 + 3.045) self.assertEqual(td.seconds, 3723) self.assertEqual(td.microseconds, 45000) td = make_timedelta('P1D') self.assertEqual(td.total_seconds(), 24 * 3600) td = make_timedelta('P00.5D') self.assertEqual(td.total_seconds(), 12 * 3600)
def test_bad_iso_to_timedelta(self): with self.assertRaises(ValueError): make_timedelta('PT0.5M0.25S') with self.assertRaises(ValueError): make_timedelta('T0S') with self.assertRaises(ValueError): make_timedelta('PT1') with self.assertRaises(ValueError): make_timedelta('')
def duration(self, value): if value is None or isinstance(value, timedelta): self._duration = value return try: self._duration = make_timedelta(value) except Exception as e: raise Exception( f"Property 'duration' in a 'tincan.{self.__class__.__name__}' object must be set with a " f"datetime.timedelta, str, unicode, int, float or None.\n\n{repr(e)}" )
def duration(self, value): if value is None or isinstance(value, timedelta): self._duration = value return try: self._duration = make_timedelta(value) except Exception as e: e.message = ( "Property 'duration' in a 'tincan.%s' object must be set with a " "datetime.timedelta, str, unicode, int, float or None.\n\n%s" % (self.__class__.__name__, e.message) ) raise e
def duration(self, value): if value is None or isinstance(value, timedelta): self._duration = value return try: self._duration = make_timedelta(value) except Exception as e: e.message = ( "Property 'duration' in a 'tincan.%s' object must be set with a " "datetime.timedelta, str, unicode, int, float or None.\n\n%s" % ( self.__class__.__name__, e.message, )) raise e