Fixed #12524 -- Clarified handling of pre-1000AD dates in datetime_safe (and thus, the serializers). Patch includes moving the datetime_safe tests into the utils regressiontests module. Thanks to gsf for the report and initial patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@12423 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Russell Keith-Magee 2010-02-13 14:02:32 +00:00
parent b794441951
commit 03924929ba
6 changed files with 27 additions and 3 deletions

View File

@ -4,7 +4,7 @@
#
# Based on code submitted to comp.lang.python by Andrew Dalke
#
# >>> datetime_safe.date(1850, 8, 2).strftime("%Y/%M/%d was a %A")
# >>> datetime_safe.date(1850, 8, 2).strftime("%Y/%m/%d was a %A")
# '1850/08/02 was a Friday'
from datetime import date as real_date, datetime as real_datetime
@ -83,7 +83,7 @@ def strftime(dt, fmt):
sites.append(site)
s = s1
syear = "%4d" % (dt.year,)
syear = "%04d" % (dt.year,)
for site in sites:
s = s[:site] + syear + s[site+4:]
return s

View File

@ -277,6 +277,21 @@ None
>>> print obj
<DeserializedObject: serializers.Player(pk=1)>
# Regression for #12524 -- dates before 1000AD get prefixed 0's on the year
>>> a = Article.objects.create(
... pk=4,
... author = jane,
... headline = "Nobody remembers the early years",
... pub_date = datetime(1, 2, 3, 4, 5, 6))
>>> serialized = serializers.serialize("json", [a])
>>> print serialized
[{"pk": 4, "model": "serializers.article", "fields": {"headline": "Nobody remembers the early years", "pub_date": "0001-02-03 04:05:06", "categories": [], "author": 2}}]
>>> obj = list(serializers.deserialize("json", serialized))[0]
>>> print obj.object.pub_date
0001-02-03 04:05:06
"""}
try:

View File

@ -1,4 +1,4 @@
r"""
"""
>>> from datetime import date as original_date, datetime as original_datetime
>>> from django.utils.datetime_safe import date, datetime
>>> just_safe = (1900, 1, 1)
@ -34,4 +34,11 @@ True
'00'
>>> datetime(*just_safe).strftime('%y')
'00'
>>> date(1850, 8, 2).strftime("%Y/%m/%d was a %A")
'1850/08/02 was a Friday'
# Regression for #12524 -- Check that pre-1000AD dates are padded with zeros if necessary
>>> date(1, 1, 1).strftime("%Y/%m/%d was a %A")
'0001/01/01 was a Monday'
"""

View File

@ -9,6 +9,7 @@ from django.utils.functional import SimpleLazyObject
import timesince
import datastructures
import datetime_safe
import itercompat
import tzinfo
@ -26,6 +27,7 @@ except NameError:
__test__ = {
'timesince': timesince,
'datastructures': datastructures,
'datetime_safe': datetime_safe,
'itercompat': itercompat,
'tzinfo': tzinfo,
}