Ticket #1443: date1900.diff

File date1900.diff, 4.2 KB (added by jbronn, 17 years ago)

datetime patch from Chris Green

  • django/db/models/fields/__init__.py

     
    544544        # Casts dates into string format for entry into database.
    545545        if value is not None:
    546546            try:
    547                 value = value.strftime('%Y-%m-%d')
     547                value = "%04.d-%02.d-%02.d" % (value.year,value.month,value.day) # "%Y-%m-%d
    548548            except AttributeError:
    549549                # If value is already a string it won't have a strftime method,
    550550                # so we'll just let it pass through.
     
    556556
    557557    def flatten_data(self, follow, obj=None):
    558558        val = self._get_val_from_obj(obj)
    559         return {self.attname: (val is not None and val.strftime("%Y-%m-%d") or '')}
     559        return {self.attname: (val is not None and ("%04.d-%02.d-%02.d" % (val.year,val.month,val.day)) or '')}
    560560
    561561    def formfield(self, **kwargs):
    562562        defaults = {'form_class': forms.DateField}
  • django/core/serializers/json.py

     
    4747    JSONEncoder subclass that knows how to encode date/time and decimal types.
    4848    """
    4949
    50     DATE_FORMAT = "%Y-%m-%d"
    51     TIME_FORMAT = "%H:%M:%S"
     50    DATE_FORMAT = "%04.d-%02.d-%02.d" # %Y-%m-%d
     51    TIME_FORMAT = "%02.d:%02.d:%02.d" # %H:%M:%S
    5252
    5353    def default(self, o):
    5454        if isinstance(o, datetime.datetime):
    55             return o.strftime("%s %s" % (self.DATE_FORMAT, self.TIME_FORMAT))
     55            fmt = "%s %s" % (self.DATE_FORMAT, self.TIME_FORMAT)
     56            return fmt % (o.year, o.month, o.day, o.hour, o.minute, o.second)
    5657        elif isinstance(o, datetime.date):
    57             return o.strftime(self.DATE_FORMAT)
     58            return self.DATE_FORMAT % (o.year, o.month, o.day)
    5859        elif isinstance(o, datetime.time):
    59             return o.strftime(self.TIME_FORMAT)
     60            return self.TIME_FORMAT % (o.hour, o.minute, o.second)
    6061        elif isinstance(o, decimal.Decimal):
    6162            return str(o)
    6263        else:
  • django/core/serializers/base.py

     
    5959        Convert a field's value to a string.
    6060        """
    6161        if isinstance(field, models.DateTimeField):
    62             value = getattr(obj, field.name).strftime("%Y-%m-%d %H:%M:%S")
     62            dt = getattr(obj, field.name)
     63            value =  "%04.d-%02.d-%02.d %02.d:%02.d:%02.d" % (dt.year,dt.month,dt.day,dt.hour, dt.minute, dt.second)
    6364        elif isinstance(field, models.FileField):
    6465            value = getattr(obj, "get_%s_url" % field.name, lambda: None)()
    6566        else:
  • django/core/validators.py

     
    141141    # Could use time.strptime here and catch errors, but datetime.date below
    142142    # produces much friendlier error messages.
    143143    year, month, day = map(int, date_string.split('-'))
    144     # This check is needed because strftime is used when saving the date
    145     # value to the database, and strftime requires that the year be >=1900.
    146     if year < 1900:
    147         raise ValidationError, _('Year must be 1900 or later.')
     144
    148145    try:
    149146        date(year, month, day)
    150147    except ValueError, e:
  • tests/regressiontests/serializers_regress/tests.py

     
    255255
    256256    (data_obj, 800, AutoNowDateTimeData, datetime.datetime(2006,6,16,10,42,37)),
    257257    (data_obj, 810, ModifyingSaveData, 42),
     258
     259    (data_obj, 900, DateTimeData, datetime.datetime(1,1,1,0,0,0)),
     260    (data_obj, 901, DateData, datetime.date(2,2,2)),
    258261]
    259262
    260263# Because Oracle treats the empty string as NULL, Oracle is expected to fail
Back to Top