Ticket #10825: 10825.diff

File 10825.diff, 4.5 KB (added by mir, 6 years ago)

patch inluding fix of testcases, replaces older patch

  • django/utils/dateformat.py

    diff --git a/django/utils/dateformat.py b/django/utils/dateformat.py
    index 2751d82..f5edf42 100644
    a b from django.utils.tzinfo import LocalTimezone 
    1616from django.utils.translation import ugettext as _
    1717from django.utils.encoding import force_unicode
    1818from calendar import isleap, monthrange
    19 import re, time
     19import re
     20from calendar import timegm
     21from time import mktime
    2022
    2123re_formatchars = re.compile(r'(?<!\\)([aAbBdDfFgGhHiIjlLmMnNOPrsStTUwWyYzZ])')
    2224re_escaped = re.compile(r'\\(.)')
    class DateFormat(TimeFormat): 
    199201
    200202    def U(self):
    201203        "Seconds since the Unix epoch (January 1 1970 00:00:00 GMT)"
    202         off = self.timezone and self.timezone.utcoffset(self.data) or 0
    203         return int(time.mktime(self.data.timetuple())) + off.seconds * 60
     204        if getattr(self.data, 'tzinfo', None):
     205            return int(timegm(self.data.utctimetuple()))
     206        else:
     207            return int(mktime(self.data.timetuple()))
    204208
    205209    def w(self):
    206210        "Day of the week, numeric, i.e. '0' (Sunday) to '6' (Saturday)"
  • docs/ref/templates/builtins.txt

    diff --git a/docs/ref/templates/builtins.txt b/docs/ref/templates/builtins.txt
    index f4e49a9..e06d28d 100644
    a b Available format strings: 
    506506                      month, 2 characters.
    507507    t                 Number of days in the given month.        ``28`` to ``31``
    508508    T                 Time zone of this machine.                ``'EST'``, ``'MDT'``
    509     U                 Not implemented.
     509    U                 Seconds since the Unix Epoch
     510                      (January 1 1970 00:00:00 UTC).
    510511    w                 Day of the week, digits without           ``'0'`` (Sunday) to ``'6'`` (Saturday)
    511512                      leading zeros.
    512513    W                 ISO-8601 week number of year, with        ``1``, ``53``
  • tests/regressiontests/dateformat/tests.py

    diff --git a/tests/regressiontests/dateformat/tests.py b/tests/regressiontests/dateformat/tests.py
    index 481e36a..d649d47 100644
    a b u'th' 
    3737u'31'
    3838>>> no_tz or format(my_birthday, 'T') == 'CET'
    3939True
    40 >>> no_tz or format(my_birthday, 'U') == '300531600'
     40>>> no_tz or format(my_birthday, 'U') == '300315600'
    4141True
    4242>>> format(my_birthday, 'w')
    4343u'0'
  • new file tests/regressiontests/utils/dateformat.py

    diff --git a/tests/regressiontests/utils/dateformat.py b/tests/regressiontests/utils/dateformat.py
    new file mode 100644
    index 0000000..63b8201
    - +  
     1"""
     2>>> from datetime import datetime, date
     3>>> from django.utils.dateformat import format
     4>>> from django.utils.tzinfo import FixedOffset, LocalTimezone
     5
     6# date
     7>>> d = date(2009, 5, 16)
     8>>> date.fromtimestamp(int(format(d, 'U'))) == d
     9True
     10
     11# Naive datetime
     12>>> dt = datetime(2009, 5, 16, 5, 30, 30)
     13>>> datetime.fromtimestamp(int(format(dt, 'U'))) == dt
     14True
     15
     16# datetime with local tzinfo
     17>>> ltz = LocalTimezone(datetime.now())
     18>>> dt = datetime(2009, 5, 16, 5, 30, 30, tzinfo=ltz)
     19>>> datetime.fromtimestamp(int(format(dt, 'U')), ltz) == dt
     20True
     21>>> datetime.fromtimestamp(int(format(dt, 'U'))) == dt.replace(tzinfo=None)
     22True
     23
     24# datetime with arbitrary tzinfo
     25>>> tz = FixedOffset(-510)
     26>>> ltz = LocalTimezone(datetime.now())
     27>>> dt = datetime(2009, 5, 16, 5, 30, 30, tzinfo=tz)
     28>>> datetime.fromtimestamp(int(format(dt, 'U')), tz) == dt
     29True
     30>>> datetime.fromtimestamp(int(format(dt, 'U')), ltz) == dt
     31True
     32>>> datetime.fromtimestamp(int(format(dt, 'U'))) == dt.astimezone(ltz).replace(tzinfo=None)
     33True
     34>>> datetime.fromtimestamp(int(format(dt, 'U')), tz).utctimetuple() == dt.utctimetuple()
     35True
     36>>> datetime.fromtimestamp(int(format(dt, 'U')), ltz).utctimetuple() == dt.utctimetuple()
     37True
     38
     39# Epoch
     40>>> utc = FixedOffset(0)
     41>>> udt = datetime(1970, 1, 1, tzinfo=utc)
     42>>> format(udt, 'U')
     43u'0'
     44"""
     45
     46if __name__ == "__main__":
     47    import doctest
     48    doctest.testmod()
  • tests/regressiontests/utils/tests.py

    diff --git a/tests/regressiontests/utils/tests.py b/tests/regressiontests/utils/tests.py
    index 485c5fa..6d345d9 100644
    a b from django.utils import html, checksums 
    88
    99import timesince
    1010import datastructures
     11import dateformat
    1112import itercompat
    1213from decorators import DecoratorFromMiddlewareTests
    1314
    except NameError: 
    2223__test__ = {
    2324    'timesince': timesince,
    2425    'datastructures': datastructures,
     26    'dateformat': dateformat,
    2527    'itercompat': itercompat,
    2628}
    2729
Back to Top