Commit 06d9b82a authored by Aymeric Augustin's avatar Aymeric Augustin
Browse files

Fixed #16906 -- Format datetimes with str/unicode instead of strftime where...

Fixed #16906 -- Format datetimes with str/unicode instead of strftime where possible: it's faster and it works for all dates.

Also ensured that datetime_safe is used wherever strftime is called on dates/datetimes that may be before 1900.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@16978 bcc190cf-cafb-0310-a4f2-bffc1f526a37
parent b7845768
Loading
Loading
Loading
Loading
+4 −4
Original line number Diff line number Diff line
@@ -281,9 +281,9 @@ class DateFieldListFilter(FieldListFilter):

        today = datetime.date.today()
        one_week_ago = today - datetime.timedelta(days=7)
        today_str = (isinstance(self.field, models.DateTimeField)
                        and today.strftime('%Y-%m-%d 23:59:59')
                        or today.strftime('%Y-%m-%d'))
        today_str = str(today)
        if isinstance(self.field, models.DateTimeField):
            today_str += ' 23:59:59'

        self.lookup_kwarg_year = '%s__year' % self.field_path
        self.lookup_kwarg_month = '%s__month' % self.field_path
@@ -299,7 +299,7 @@ class DateFieldListFilter(FieldListFilter):
                self.lookup_kwarg_day: str(today.day),
            }),
            (_('Past 7 days'), {
                self.lookup_kwarg_past_7_days_gte: one_week_ago.strftime('%Y-%m-%d'),
                self.lookup_kwarg_past_7_days_gte: str(one_week_ago),
                self.lookup_kwarg_past_7_days_lte: today_str,
            }),
            (_('This month'), {
+3 −4
Original line number Diff line number Diff line
@@ -52,9 +52,8 @@ class PasswordResetTokenGenerator(object):
        # invalid as soon as it is used.
        # We limit the hash to 20 chars to keep URL short
        key_salt = "django.contrib.auth.tokens.PasswordResetTokenGenerator"
        value = unicode(user.id) + \
            user.password + user.last_login.strftime('%Y-%m-%d %H:%M:%S') + \
            unicode(timestamp)
        value = (unicode(user.id) + user.password +
                unicode(user.last_login) + unicode(timestamp))
        hash = salted_hmac(key_salt, value).hexdigest()[::2]
        return "%s-%s" % (ts_b36, hash)

+4 −4
Original line number Diff line number Diff line
@@ -67,7 +67,7 @@ class SitemapTests(TestCase):
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url><loc>%s/location/</loc><lastmod>%s</lastmod><changefreq>never</changefreq><priority>0.5</priority></url>
</urlset>
""" % (self.base_url, date.today().strftime('%Y-%m-%d')))
""" % (self.base_url, date.today()))

    def test_simple_custom_sitemap(self):
        "A simple sitemap can be rendered with a custom template"
@@ -79,7 +79,7 @@ class SitemapTests(TestCase):
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url><loc>%s/location/</loc><lastmod>%s</lastmod><changefreq>never</changefreq><priority>0.5</priority></url>
</urlset>
""" % (self.base_url, date.today().strftime('%Y-%m-%d')))
""" % (self.base_url, date.today()))

    @skipUnless(settings.USE_I18N, "Internationalization is not enabled")
    def test_localized_priority(self):
@@ -93,7 +93,7 @@ class SitemapTests(TestCase):
        # haven't been rendered in localized format
        response = self.client.get('/simple/sitemap.xml')
        self.assertContains(response, '<priority>0.5</priority>')
        self.assertContains(response, '<lastmod>%s</lastmod>' % date.today().strftime('%Y-%m-%d'))
        self.assertContains(response, '<lastmod>%s</lastmod>' % date.today())
        deactivate()

    def test_generic_sitemap(self):
@@ -152,7 +152,7 @@ class SitemapTests(TestCase):
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url><loc>http://testserver/location/</loc><lastmod>%s</lastmod><changefreq>never</changefreq><priority>0.5</priority></url>
</urlset>
""" % date.today().strftime('%Y-%m-%d'))
""" % date.today())

    @skipUnless("django.contrib.sites" in settings.INSTALLED_APPS, "django.contrib.sites app not installed.")
    def test_sitemap_get_urls_no_site_1(self):
+2 −3
Original line number Diff line number Diff line
@@ -9,7 +9,6 @@ from django.conf import settings
from django.db import DEFAULT_DB_ALIAS
from django.db.backends import util
from django.db.transaction import TransactionManagementError
from django.utils import datetime_safe
from django.utils.importlib import import_module


@@ -718,7 +717,7 @@ class BaseDatabaseOperations(object):
        """
        if value is None:
            return None
        return datetime_safe.new_date(value).strftime('%Y-%m-%d')
        return unicode(value)

    def value_to_db_datetime(self, value):
        """
@@ -731,7 +730,7 @@ class BaseDatabaseOperations(object):

    def value_to_db_time(self, value):
        """
        Transform a datetime value to an object compatible with what is expected
        Transform a time value to an object compatible with what is expected
        by the backend driver for time columns.
        """
        if value is None:
+3 −8
Original line number Diff line number Diff line
@@ -320,14 +320,9 @@ def _sqlite_format_dtdelta(dt, conn, days, secs, usecs):
            dt = dt - delta
    except (ValueError, TypeError):
        return None

    if isinstance(dt, datetime.datetime):
        rv = dt.strftime("%Y-%m-%d %H:%M:%S")
        if dt.microsecond:
            rv = "%s.%0.6d" % (rv, dt.microsecond)
    else:
        rv = dt.strftime("%Y-%m-%d")
    return rv
    # typecast_timestamp returns a date or a datetime without timezone.
    # It will be formatted as "%Y-%m-%d" or "%Y-%m-%d %H:%M:%S[.%f]"
    return str(dt)

def _sqlite_regexp(re_pattern, re_string):
    try:
Loading