def to_python(self, value): """ Converts the input value into the timedelta Python data type, raising django.core.exceptions.ValidationError if the data can't be converted. Returns the converted value as a timedelta. """ # Note that value may be coming from the database column or a serializer so we should # handle a timedelta, string or an integer if value is None: return value if isinstance(value, timedelta): return value if isinstance(value, six.integer_types): return timedelta(microseconds=value) # Try to parse the value str_val = smart_text(value) if isinstance(str_val, six.string_types): try: return str_to_timedelta(str_val) except ValueError: raise exceptions.ValidationError( self.default_error_messages['invalid']) raise exceptions.ValidationError( self.default_error_messages['unknown_type'])
def to_python(self, value): """ Converts the input value into the timedelta Python data type, raising django.core.exceptions.ValidationError if the data can't be converted. Returns the converted value as a timedelta. """ # Note that value may be coming from the database column or a serializer so we should # handle a timedelta, string or an integer if value is None: return value if isinstance(value, timedelta): return value if isinstance(value, six.integer_types): return timedelta(microseconds=value) # As of Django 1.10, it appears that during some aggregation operations # we may receive decimal.Decimal instances via from_db_value. # WARNING: potentially lossy conversion if isinstance(value, decimal.Decimal): return timedelta(microseconds=float(value)) # Try to parse the value str_val = smart_text(value) if isinstance(str_val, six.string_types): try: return str_to_timedelta(str_val) except ValueError: raise exceptions.ValidationError(self.default_error_messages['invalid']) raise exceptions.ValidationError(self.default_error_messages['unknown_type'])
def testInputAll(self): delta = timestring.str_to_timedelta( "1 year, 10 months, 3 weeks, 2 days, 3:40:50") days = ((1 * 365) + (10 * 30) + (3 * 7) + 2) seconds = ((3 * 60 * 60) + (40 * 60) + 50) self.assertEqual(days, delta.days) self.assertEqual(seconds, delta.seconds)
def to_python(self, value): """ Converts the input value into the timedelta Python data type, raising django.core.exceptions.ValidationError if the data can't be converted. Returns the converted value as a timedelta. """ # Note that value may be coming from the database column or a serializer so we should # handle a timedelta, string or an integer if value is None: return value if isinstance(value, timedelta): return value if isinstance(value, int) or isinstance(value, long): return timedelta(seconds=value) # Try to parse the value str_val = smart_str(value) if isinstance(str_val, basestring): try: return str_to_timedelta(str_val) except ValueError: raise exceptions.ValidationError(self.default_error_messages['invalid']) raise exceptions.ValidationError(self.default_error_messages['unknown_type'])
def clean(self, value): """ Returns a datetime.timedelta object. """ super(DurationField, self).clean(value) try: return str_to_timedelta(value) except ValueError, e: raise ValidationError(self.default_error_messages['invalid'])
def clean(self, value): """ Returns a datetime.timedelta object. """ super(DurationField, self).clean(value) try: return str_to_timedelta(value) except ValueError: raise ValidationError(self.default_error_messages['invalid'])
def testInputAll(self): delta = timestring.str_to_timedelta("1Y 10M 3w 2d 3m") days = ( (1 * 365) + (10 * 30) + (3 * 7) + 2 ) seconds = ( (3 * 60) ) self.assertEqual( days, delta.days ) self.assertEqual( seconds, delta.seconds )
def testInputAllAbbreviated(self): delta = timestring.str_to_timedelta("2Y 9M 1w 20d 0m") days = ( (2 * 365) + (9 * 30) + (1 * 7) + 20 ) seconds = ( (0 * 60 * 60) ) self.assertEqual( days, delta.days ) self.assertEqual( seconds, delta.seconds )
def testInputAll(self): delta = timestring.str_to_timedelta("1 year, 10 months, 3 weeks, 2 days, 3:40:50") days = ( (1 * 365) + (10 * 30) + (3 * 7) + 2 ) seconds = ( (3 * 60 * 60) + (40 * 60) + 50 ) self.assertEqual( days, delta.days ) self.assertEqual( seconds, delta.seconds )
def testInputAllAbbreviated(self): delta = timestring.str_to_timedelta("2y 9m 1w 20d 0:10:39") days = ( (2 * 365) + (9 * 30) + (1 * 7) + 20 ) seconds = ( (0 * 60 * 60) + (10 * 60) + 39 ) self.assertEquals( days, delta.days ) self.assertEquals( seconds, delta.seconds )
def testInputTimeSecondsMicroseconds(self): delta = timestring.str_to_timedelta("11:20:22.000098") seconds = (11 * 60 * 60) + (20 * 60) + 22 self.assertEqual(seconds, delta.seconds) self.assertEqual(98, delta.microseconds)
def testInputTimeSeonds(self): delta = timestring.str_to_timedelta("12:21:24") seconds = (12 * 60 * 60) + (21 * 60) + 24 self.assertEqual(seconds, delta.seconds)
def testInputTime(self): delta = timestring.str_to_timedelta("10:23") seconds = (10 * 60 * 60) + (23 * 60) self.assertEqual(seconds, delta.seconds)
def to_python(self, value): try: return str_to_timedelta(value) except ValueError: raise ValidationError(self.default_error_messages['invalid'])
def testInputTimeSecondsMicroseconds(self): delta = timestring.str_to_timedelta("11m") seconds = (11 * 60) self.assertEqual(seconds, delta.seconds)
def testInputTimeSeonds(self): delta = timestring.str_to_timedelta("12h 21m") seconds = (12 * 60 * 60) + (21 * 60) self.assertEqual(seconds, delta.seconds)
def testInputTimeMicrosecondsBothPadZeros(self): delta = timestring.str_to_timedelta("11:20:22.0160") self.assertEqual(16000, delta.microseconds)
def testTimedeltaStrRoundtrip(self): for td in self.test_tds: td_str = timedelta_to_string(td) td_from_str = timestring.str_to_timedelta(td_str) self.assertEqual(td_from_str, td)
def testInputDaysOnly(self): delta = timestring.str_to_timedelta("24 days") self.assertEqual( 24, delta.days )
def testInputAll(self): delta = timestring.str_to_timedelta("1Y 10M 3w 2d 3m") days = ((1 * 365) + (10 * 30) + (3 * 7) + 2) seconds = ((3 * 60)) self.assertEqual(days, delta.days) self.assertEqual(seconds, delta.seconds)
def to_python(self, value): try: return str_to_timedelta(value) except ValueError, e: raise ValidationError(self.default_error_messages['invalid'])
def testInputAllAbbreviated(self): delta = timestring.str_to_timedelta("2Y 9M 1w 20d 0m") days = ((2 * 365) + (9 * 30) + (1 * 7) + 20) seconds = ((0 * 60 * 60)) self.assertEqual(days, delta.days) self.assertEqual(seconds, delta.seconds)
def testInputTime(self): delta = timestring.str_to_timedelta("10h 23m") seconds = (10 * 60 * 60) + (23 * 60) self.assertEqual(seconds, delta.seconds)
def testTimedeltaStrRoundtrip(self): for td in self.test_tds: td_str = str(td) td_from_str = timestring.str_to_timedelta(td_str) self.assertEquals(td_from_str, td)
def testInputDaysOnly(self): delta = timestring.str_to_timedelta("24 days") self.assertEqual(24, delta.days)