Ticket #9459: datetimefield-microseconds.diff

File datetimefield-microseconds.diff, 1.6 KB (added by Thomas Güttler, 16 years ago)
  • tests/regressiontests/forms/fields.py

     
    560560datetime.datetime(2006, 10, 25, 14, 30, 59, 200)
    561561>>> f.clean('2006-10-25 14:30:45')
    562562datetime.datetime(2006, 10, 25, 14, 30, 45)
     563>>> f.clean('2006-10-25 14:30:45.123')
     564datetime.datetime(2006, 10, 25, 14, 30, 45, 123)
    563565>>> f.clean('2006-10-25 14:30:00')
    564566datetime.datetime(2006, 10, 25, 14, 30)
    565567>>> f.clean('2006-10-25 14:30')
  • django/forms/fields.py

     
    382382            if len(value) != 2:
    383383                raise ValidationError(self.error_messages['invalid'])
    384384            value = '%s %s' % tuple(value)
     385
     386        # To support DateTimeField in HiddenInput (incl. microseconds)
     387        # YYYY-MM-DD HH:MM:SS.sss
     388        if '.' in value:
     389            try:
     390                value, usecs = value.split('.')
     391                usecs = int(usecs)
     392            except ValueError:
     393                raise ValidationError(self.error_messages['invalid'])
     394        else:
     395            usecs = 0
     396
    385397        for format in self.input_formats:
    386398            try:
    387                 return datetime.datetime(*time.strptime(value, format)[:6])
     399                return datetime.datetime(*time.strptime(value, format)[:6], **{'microsecond': usecs})
    388400            except ValueError:
    389401                continue
    390402        raise ValidationError(self.error_messages['invalid'])
Back to Top