def test_simple_parse_timedelta(self): self.assertStrictEqual(relativedelta(microseconds=300000.0), parse_relativedelta(timedelta(seconds=0.3))) self.assertStrictEqual(relativedelta( seconds=2, microseconds=300000.0), parse_relativedelta(timedelta(seconds=2.3)) ) self.assertStrictEqual( relativedelta(days=11, hours=18, minutes=11, seconds=50, microseconds=100010), parse_relativedelta(timedelta(weeks=1, days=4.5, hours=5, minutes=70.5, seconds=80.100005, microseconds=5)) )
def get_db_prep_save(self, value, connection): if value is None: return None if connection.vendor == 'postgresql': return super().get_db_prep_save(value, connection) else: if isinstance(value, str): # we need to convert it to the non-postgres format return relativedelta_as_csv(parse_relativedelta(value)) return relativedelta_as_csv(value)
def __get__(self, obj, objtype=None): if obj is None: return None value = obj.__dict__.get(self.field.name) if value is None: return None try: return parse_relativedelta(value) except ValueError as e: raise ValidationError({self.field.name: e})
def to_python(self, value): if value is None: return value try: return parse_relativedelta(value) except (ValueError, TypeError): raise ValidationError( self.error_messages['invalid'], code='invalid', params={'value': value}, )
def test_csv_conversions(self): self.assertEqual( relativedelta_as_csv(relativedelta(years=1925, months=9, days=-4, hours=-12, minutes=27, seconds=54, microseconds=-123456)), '01925/009/-04 -12:027:054.-123456') self.assertEqual( parse_relativedelta('01925/009/-04 -12:027:054.-123456'), relativedelta(years=1925, months=9, days=-4, hours=-12, minutes=27, seconds=54, microseconds=-123456) ) self.assertEqual( relativedelta_as_csv(relativedelta(years=-1925, months=9, days=-4, hours=-12, minutes=27, seconds=-54, microseconds=123456)), '-1925/009/-04 -12:027:-54.0123456') self.assertEqual( parse_relativedelta('-1925/009/-04 -12:027:-54.0123456'), relativedelta(years=-1925, months=9, days=-4, hours=-12, minutes=27, seconds=-54, microseconds=123456) )
def from_db_value(self, value, expression, connection, context=None): if value is not None: return parse_relativedelta(value)
def test_parse_iso8601csv(self): """Test parsing the internal comma-separated-value representation""" self.assertStrictEqual( relativedelta(years=1925, months=9, days=-14, hours=-12, minutes=27, seconds=54, microseconds=-123456), parse_relativedelta('01925/009/-14 -12:027:054.-123456') )
def test_normalize_timedelta(self): self.assertStrictEqual( relativedelta(seconds=21, microseconds=600000.0), parse_relativedelta(timedelta(seconds=2.3, microseconds=19.3e6)) )
def clean(self, value): try: return parse_relativedelta(value) except Exception: raise ValidationError('Not a valid (extended) ISO8601 interval specification', code='format')
def to_python(self, value): return parse_relativedelta(value)