From 03924929ba212368240bd7a26b14208fba965cb9 Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Sat, 13 Feb 2010 14:02:32 +0000 Subject: [PATCH] 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 --- django/utils/datetime_safe.py | 4 ++-- tests/modeltests/serializers/models.py | 15 +++++++++++++++ tests/regressiontests/datetime_safe/__init__.py | 0 tests/regressiontests/datetime_safe/models.py | 0 .../tests.py => utils/datetime_safe.py} | 9 ++++++++- tests/regressiontests/utils/tests.py | 2 ++ 6 files changed, 27 insertions(+), 3 deletions(-) delete mode 100644 tests/regressiontests/datetime_safe/__init__.py delete mode 100644 tests/regressiontests/datetime_safe/models.py rename tests/regressiontests/{datetime_safe/tests.py => utils/datetime_safe.py} (83%) diff --git a/django/utils/datetime_safe.py b/django/utils/datetime_safe.py index a048ecd066f..b634888092f 100644 --- a/django/utils/datetime_safe.py +++ b/django/utils/datetime_safe.py @@ -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 diff --git a/tests/modeltests/serializers/models.py b/tests/modeltests/serializers/models.py index 45d57e58d48..a3ff87353ee 100644 --- a/tests/modeltests/serializers/models.py +++ b/tests/modeltests/serializers/models.py @@ -277,6 +277,21 @@ None >>> print obj +# 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: diff --git a/tests/regressiontests/datetime_safe/__init__.py b/tests/regressiontests/datetime_safe/__init__.py deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/tests/regressiontests/datetime_safe/models.py b/tests/regressiontests/datetime_safe/models.py deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/tests/regressiontests/datetime_safe/tests.py b/tests/regressiontests/utils/datetime_safe.py similarity index 83% rename from tests/regressiontests/datetime_safe/tests.py rename to tests/regressiontests/utils/datetime_safe.py index e1fe2b0f0e6..6c3e7df3595 100644 --- a/tests/regressiontests/datetime_safe/tests.py +++ b/tests/regressiontests/utils/datetime_safe.py @@ -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' """ diff --git a/tests/regressiontests/utils/tests.py b/tests/regressiontests/utils/tests.py index 56d41b9ae61..96b7b4d2985 100644 --- a/tests/regressiontests/utils/tests.py +++ b/tests/regressiontests/utils/tests.py @@ -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, }