Django

Code

Changeset 992

Show
Ignore:
Timestamp:
10/22/05 16:37:59 (3 years ago)
Author:
adrian
Message:

Fixed #479 -- Implemented time-zone formats in dateformat. Thanks, Sune

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/utils/dateformat.py

    r976 r992  
    1313 
    1414from django.utils.dates import MONTHS, MONTHS_AP, WEEKDAYS 
     15from django.utils.tzinfo import LocalTimezone 
    1516from calendar import isleap 
    16 import re 
     17import re, time 
    1718 
    1819re_formatchars = re.compile(r'(?<!\\)([aABdDfFgGhHiIjlLmMnNOPrsStTUwWyYzZ])') 
     
    4142    def A(self): 
    4243        "'AM' or 'PM'" 
    43         return self.a().upper() 
     44        if self.data.hour > 11: 
     45            return 'PM' 
     46        return 'AM' 
    4447 
    4548    def B(self): 
     
    101104    year_days = [None, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334] 
    102105 
    103     def __init__(self, d): 
    104         self.data = d 
     106    def __init__(self, dt): 
     107        # Accepts either a datetime or date object. 
     108        self.data = dt 
     109        self.timezone = getattr(dt, 'tzinfo', None) 
     110        if hasattr(self.data, 'hour') and not self.timezone: 
     111            self.timezone = LocalTimezone(dt) 
    105112 
    106113    def d(self): 
     
    120127        raise NotImplementedError 
    121128 
     129    def I(self): 
     130        "'1' if Daylight Savings Time, '0' otherwise." 
     131        if self.timezone.dst(self.data): 
     132            return '1' 
     133        else: 
     134            return '0' 
     135 
    122136    def j(self): 
    123137        "Day of the month without leading zeros; i.e. '1' to '31'" 
     
    150164    def O(self): 
    151165        "Difference to Greenwich time in hours; e.g. '+0200'" 
    152         raise NotImplementedError 
     166        tz = self.timezone.utcoffset(self.data) 
     167        return "%+03d%02d" % (tz.seconds // 3600, (tz.seconds // 60) % 60) 
    153168 
    154169    def r(self): 
    155170        "RFC 822 formatted date; e.g. 'Thu, 21 Dec 2000 16:01:07 +0200'" 
    156         raise NotImplementedError 
     171        return self.format('D, j M Y H:i:s O') 
    157172 
    158173    def S(self): 
     
    175190    def T(self): 
    176191        "Time zone of this machine; e.g. 'EST' or 'MDT'" 
    177         raise NotImplementedError 
     192        name = self.timezone.tzname(self.data) 
     193        if name is None: 
     194            name = self.format('O') 
     195        return name 
    178196 
    179197    def U(self): 
    180198        "Seconds since the Unix epoch (January 1 1970 00:00:00 GMT)" 
    181         raise NotImplementedError 
     199        off = self.timezone.utcoffset(self.data) 
     200        return int(time.mktime(self.data.timetuple())) + off.seconds * 60 
    182201 
    183202    def w(self): 
     
    230249        for timezones west of UTC is always negative, and for those east of UTC 
    231250        is always positive.""" 
    232         raise NotImplementedError 
     251        return self.timezone.utcoffset(self.data).seconds 
    233252 
    234253def format(value, format_string): 
  • django/trunk/django/utils/timesince.py

    r3 r992  
    1 import time, math, datetime 
     1import datetime, math, time 
     2from django.utils.tzinfo import LocalTimezone 
    23 
    34def timesince(d, now=None): 
     
    78    Adapted from http://blog.natbat.co.uk/archive/2003/Jun/14/time_since 
    89    """ 
    9     original = time.mktime(d.timetuple()) 
    1010    chunks = ( 
    1111      (60 * 60 * 24 * 365, 'year'), 
     
    1515      (60, 'minute') 
    1616    ) 
    17     if not now: 
    18         now = time.time() 
    19     since = now - original 
     17    if now: 
     18        t = time.mktime(now) 
     19    else: 
     20        t = time.localtime() 
     21    if d.tzinfo: 
     22        tz = LocalTimezone() 
     23    else: 
     24        tz = None 
     25    now = datetime.datetime(t[0], t[1], t[2], t[3], t[4], t[5], tzinfo=tz) 
     26    delta = now - d 
     27    since = delta.days * 24 * 60 * 60 + delta.seconds 
    2028    # Crazy iteration syntax because we need i to be current index 
    2129    for i, (seconds, name) in zip(range(len(chunks)), chunks): 
  • django/trunk/docs/templates.txt

    r983 r992  
    518518    N                 Month abbreviation in Associated Press  ``'Jan.'``, ``'Feb.'``, ``'March'``, ``'May'`` 
    519519                      style. Proprietary extension. 
    520     O                 Not implemented. 
     520    O                 Difference to Greenwich time in hours.  ``'+0200'`` 
    521521    P                 Time, in 12-hour hours, minutes and     ``'1 a.m.'``, ``'1:30 p.m.'``, ``'midnight'``, ``'noon'``, ``'12:30 p.m.'`` 
    522522                      'a.m.'/'p.m.', with minutes left off 
     
    524524                      strings 'midnight' and 'noon' if 
    525525                      appropriate. Proprietary extension. 
    526     r                 Not implemented. 
     526    r                 RFC 822 formatted date.                 ``'Thu, 21 Dec 2000 16:01:07 +0200'`` 
    527527    s                 Seconds, 2 digits with leading zeros.   ``'00'`` to ``'59'`` 
    528528    S                 English ordinal suffix for day of the   ``'st'``, ``'nd'``, ``'rd'`` or ``'th'`` 
    529529                      month, 2 characters. 
    530530    t                 Not implemented. 
    531     T                 Not implemented. 
     531    T                 Time zone of this machine.              ``'EST'``, ``'MDT'`` 
    532532    U                 Not implemented. 
    533533    w                 Day of the week, digits without         ``'0'`` (Sunday) to ``'6'`` (Saturday) 
     
    538538    Y                 Year, 4 digits.                         ``'1999'`` 
    539539    z                 Day of the year.                        ``0`` to ``365`` 
    540     Z                 Not implemented. 
     540    Z                 Time zone offset in seconds. The        ``-43200`` to ``43200`` 
     541                      offset for timezones west of UTC is 
     542                      always negative, and for those east of 
     543                      UTC is always positive. 
    541544    ================  ======================================  ===================== 
    542545