Ticket #10825: utils_dateformat-r10651.patch

File utils_dateformat-r10651.patch, 4.2 KB (added by George Song, 16 years ago)

'U' implementation, tests, and documentation

  • django/utils/dateformat.py

     
    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'\\(.)')
     
    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 self.timezone and 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

     
    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/utils/dateformat.py

     
     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

    Property changes on: tests\regressiontests\utils\dateformat.py
    ___________________________________________________________________
    Added: svn:eol-style
       + native
    
     
    88
    99import timesince
    1010import datastructures
     11import dateformat
    1112import itercompat
    1213from decorators import DecoratorFromMiddlewareTests
    1314
     
    2223__test__ = {
    2324    'timesince': timesince,
    2425    'datastructures': datastructures,
     26    'dateformat': dateformat,
    2527    'itercompat': itercompat,
    2628}
    2729
Back to Top