Changeset 992
- Timestamp:
- 10/22/05 16:37:59 (3 years ago)
- Files:
-
- django/trunk/django/utils/dateformat.py (modified) (7 diffs)
- django/trunk/django/utils/timesince.py (modified) (3 diffs)
- django/trunk/django/utils/tzinfo.py (added)
- django/trunk/docs/templates.txt (modified) (3 diffs)
- django/trunk/tests/othertests/dateformat.py (added)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/django/utils/dateformat.py
r976 r992 13 13 14 14 from django.utils.dates import MONTHS, MONTHS_AP, WEEKDAYS 15 from django.utils.tzinfo import LocalTimezone 15 16 from calendar import isleap 16 import re 17 import re, time 17 18 18 19 re_formatchars = re.compile(r'(?<!\\)([aABdDfFgGhHiIjlLmMnNOPrsStTUwWyYzZ])') … … 41 42 def A(self): 42 43 "'AM' or 'PM'" 43 return self.a().upper() 44 if self.data.hour > 11: 45 return 'PM' 46 return 'AM' 44 47 45 48 def B(self): … … 101 104 year_days = [None, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334] 102 105 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) 105 112 106 113 def d(self): … … 120 127 raise NotImplementedError 121 128 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 122 136 def j(self): 123 137 "Day of the month without leading zeros; i.e. '1' to '31'" … … 150 164 def O(self): 151 165 "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) 153 168 154 169 def r(self): 155 170 "RFC 822 formatted date; e.g. 'Thu, 21 Dec 2000 16:01:07 +0200'" 156 r aise NotImplementedError171 return self.format('D, j M Y H:i:s O') 157 172 158 173 def S(self): … … 175 190 def T(self): 176 191 "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 178 196 179 197 def U(self): 180 198 "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 182 201 183 202 def w(self): … … 230 249 for timezones west of UTC is always negative, and for those east of UTC 231 250 is always positive.""" 232 r aise NotImplementedError251 return self.timezone.utcoffset(self.data).seconds 233 252 234 253 def format(value, format_string): django/trunk/django/utils/timesince.py
r3 r992 1 import time, math, datetime 1 import datetime, math, time 2 from django.utils.tzinfo import LocalTimezone 2 3 3 4 def timesince(d, now=None): … … 7 8 Adapted from http://blog.natbat.co.uk/archive/2003/Jun/14/time_since 8 9 """ 9 original = time.mktime(d.timetuple())10 10 chunks = ( 11 11 (60 * 60 * 24 * 365, 'year'), … … 15 15 (60, 'minute') 16 16 ) 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 20 28 # Crazy iteration syntax because we need i to be current index 21 29 for i, (seconds, name) in zip(range(len(chunks)), chunks): django/trunk/docs/templates.txt
r983 r992 518 518 N Month abbreviation in Associated Press ``'Jan.'``, ``'Feb.'``, ``'March'``, ``'May'`` 519 519 style. Proprietary extension. 520 O Not implemented.520 O Difference to Greenwich time in hours. ``'+0200'`` 521 521 P Time, in 12-hour hours, minutes and ``'1 a.m.'``, ``'1:30 p.m.'``, ``'midnight'``, ``'noon'``, ``'12:30 p.m.'`` 522 522 'a.m.'/'p.m.', with minutes left off … … 524 524 strings 'midnight' and 'noon' if 525 525 appropriate. Proprietary extension. 526 r Not implemented.526 r RFC 822 formatted date. ``'Thu, 21 Dec 2000 16:01:07 +0200'`` 527 527 s Seconds, 2 digits with leading zeros. ``'00'`` to ``'59'`` 528 528 S English ordinal suffix for day of the ``'st'``, ``'nd'``, ``'rd'`` or ``'th'`` 529 529 month, 2 characters. 530 530 t Not implemented. 531 T Not implemented.531 T Time zone of this machine. ``'EST'``, ``'MDT'`` 532 532 U Not implemented. 533 533 w Day of the week, digits without ``'0'`` (Sunday) to ``'6'`` (Saturday) … … 538 538 Y Year, 4 digits. ``'1999'`` 539 539 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. 541 544 ================ ====================================== ===================== 542 545
