~ubuntu-branches/ubuntu/jaunty/python-django/jaunty

« back to all changes in this revision

Viewing changes to django/utils/dateformat.py

  • Committer: Bazaar Package Importer
  • Author(s): Scott James Remnant, Eddy Mulyono
  • Date: 2008-09-16 12:18:47 UTC
  • mfrom: (1.1.5 upstream) (4.1.1 lenny)
  • Revision ID: james.westby@ubuntu.com-20080916121847-mg225rg5mnsdqzr0
Tags: 1.0-1ubuntu1
* Merge from Debian (LP: #264191), remaining changes:
  - Run test suite on build.

[Eddy Mulyono]
* Update patch to workaround network test case failures.

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
>>>
12
12
"""
13
13
 
14
 
from django.utils.dates import MONTHS, MONTHS_3, MONTHS_AP, WEEKDAYS
 
14
from django.utils.dates import MONTHS, MONTHS_3, MONTHS_AP, WEEKDAYS, WEEKDAYS_ABBR
15
15
from django.utils.tzinfo import LocalTimezone
16
 
from django.utils.translation import gettext as _
 
16
from django.utils.translation import ugettext as _
 
17
from django.utils.encoding import force_unicode
17
18
from calendar import isleap, monthrange
18
19
import re, time
19
20
 
23
24
class Formatter(object):
24
25
    def format(self, formatstr):
25
26
        pieces = []
26
 
        for i, piece in enumerate(re_formatchars.split(formatstr)):
 
27
        for i, piece in enumerate(re_formatchars.split(force_unicode(formatstr))):
27
28
            if i % 2:
28
 
                pieces.append(str(getattr(self, piece)()))
 
29
                pieces.append(force_unicode(getattr(self, piece)()))
29
30
            elif piece:
30
31
                pieces.append(re_escaped.sub(r'\1', piece))
31
 
        return ''.join(pieces)
 
32
        return u''.join(pieces)
32
33
 
33
34
class TimeFormat(Formatter):
34
35
    def __init__(self, t):
52
53
 
53
54
    def f(self):
54
55
        """
55
 
        Time, in 12-hour hours and minutes, with minutes left off if they're zero.
 
56
        Time, in 12-hour hours and minutes, with minutes left off if they're
 
57
        zero.
56
58
        Examples: '1', '1:30', '2:05', '2'
57
59
        Proprietary extension.
58
60
        """
59
61
        if self.data.minute == 0:
60
62
            return self.g()
61
 
        return '%s:%s' % (self.g(), self.i())
 
63
        return u'%s:%s' % (self.g(), self.i())
62
64
 
63
65
    def g(self):
64
66
        "Hour, 12-hour format without leading zeros; i.e. '1' to '12'"
74
76
 
75
77
    def h(self):
76
78
        "Hour, 12-hour format; i.e. '01' to '12'"
77
 
        return '%02d' % self.g()
 
79
        return u'%02d' % self.g()
78
80
 
79
81
    def H(self):
80
82
        "Hour, 24-hour format; i.e. '00' to '23'"
81
 
        return '%02d' % self.G()
 
83
        return u'%02d' % self.G()
82
84
 
83
85
    def i(self):
84
86
        "Minutes; i.e. '00' to '59'"
85
 
        return '%02d' % self.data.minute
 
87
        return u'%02d' % self.data.minute
86
88
 
87
89
    def P(self):
88
90
        """
95
97
            return _('midnight')
96
98
        if self.data.minute == 0 and self.data.hour == 12:
97
99
            return _('noon')
98
 
        return '%s %s' % (self.f(), self.a())
 
100
        return u'%s %s' % (self.f(), self.a())
99
101
 
100
102
    def s(self):
101
103
        "Seconds; i.e. '00' to '59'"
102
 
        return '%02d' % self.data.second
 
104
        return u'%02d' % self.data.second
103
105
 
104
106
class DateFormat(TimeFormat):
105
107
    year_days = [None, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334]
117
119
 
118
120
    def d(self):
119
121
        "Day of the month, 2 digits with leading zeros; i.e. '01' to '31'"
120
 
        return '%02d' % self.data.day
 
122
        return u'%02d' % self.data.day
121
123
 
122
124
    def D(self):
123
125
        "Day of the week, textual, 3 letters; e.g. 'Fri'"
124
 
        return WEEKDAYS[self.data.weekday()][0:3]
 
126
        return WEEKDAYS_ABBR[self.data.weekday()]
125
127
 
126
128
    def F(self):
127
129
        "Month, textual, long; e.g. 'January'"
130
132
    def I(self):
131
133
        "'1' if Daylight Savings Time, '0' otherwise."
132
134
        if self.timezone.dst(self.data):
133
 
            return '1'
 
135
            return u'1'
134
136
        else:
135
 
            return '0'
 
137
            return u'0'
136
138
 
137
139
    def j(self):
138
140
        "Day of the month without leading zeros; i.e. '1' to '31'"
148
150
 
149
151
    def m(self):
150
152
        "Month; i.e. '01' to '12'"
151
 
        return '%02d' % self.data.month
 
153
        return u'%02d' % self.data.month
152
154
 
153
155
    def M(self):
154
156
        "Month, textual, 3 letters; e.g. 'Jan'"
164
166
 
165
167
    def O(self):
166
168
        "Difference to Greenwich time in hours; e.g. '+0200'"
167
 
        tz = self.timezone.utcoffset(self.data)
168
 
        return "%+03d%02d" % (tz.seconds // 3600, (tz.seconds // 60) % 60)
 
169
        seconds = self.Z()
 
170
        return u"%+03d%02d" % (seconds // 3600, (seconds // 60) % 60)
169
171
 
170
172
    def r(self):
171
 
        "RFC 822 formatted date; e.g. 'Thu, 21 Dec 2000 16:01:07 +0200'"
 
173
        "RFC 2822 formatted date; e.g. 'Thu, 21 Dec 2000 16:01:07 +0200'"
172
174
        return self.format('D, j M Y H:i:s O')
173
175
 
174
176
    def S(self):
175
177
        "English ordinal suffix for the day of the month, 2 characters; i.e. 'st', 'nd', 'rd' or 'th'"
176
178
        if self.data.day in (11, 12, 13): # Special case
177
 
            return 'th'
 
179
            return u'th'
178
180
        last = self.data.day % 10
179
181
        if last == 1:
180
 
            return 'st'
 
182
            return u'st'
181
183
        if last == 2:
182
 
            return 'nd'
 
184
            return u'nd'
183
185
        if last == 3:
184
 
            return 'rd'
185
 
        return 'th'
 
186
            return u'rd'
 
187
        return u'th'
186
188
 
187
189
    def t(self):
188
190
        "Number of days in the given month; i.e. '28' to '31'"
189
 
        return '%02d' % monthrange(self.data.year, self.data.month)[1]
 
191
        return u'%02d' % monthrange(self.data.year, self.data.month)[1]
190
192
 
191
193
    def T(self):
192
194
        "Time zone of this machine; e.g. 'EST' or 'MDT'"
193
195
        name = self.timezone.tzname(self.data)
194
196
        if name is None:
195
197
            name = self.format('O')
196
 
        return name
 
198
        return unicode(name)
197
199
 
198
200
    def U(self):
199
201
        "Seconds since the Unix epoch (January 1 1970 00:00:00 GMT)"
225
227
                week_number = 1
226
228
            else:
227
229
                j = day_of_year + (7 - weekday) + (jan1_weekday - 1)
228
 
                week_number = j / 7
 
230
                week_number = j // 7
229
231
                if jan1_weekday > 4:
230
232
                    week_number -= 1
231
233
        return week_number
232
234
 
233
235
    def y(self):
234
236
        "Year, 2 digits; e.g. '99'"
235
 
        return str(self.data.year)[2:]
 
237
        return unicode(self.data.year)[2:]
236
238
 
237
239
    def Y(self):
238
240
        "Year, 4 digits; e.g. '1999'"
246
248
        return doy
247
249
 
248
250
    def Z(self):
249
 
        """Time zone offset in seconds (i.e. '-43200' to '43200'). The offset
250
 
        for timezones west of UTC is always negative, and for those east of UTC
251
 
        is always positive."""
252
 
        return self.timezone.utcoffset(self.data).seconds
 
251
        """
 
252
        Time zone offset in seconds (i.e. '-43200' to '43200'). The offset for
 
253
        timezones west of UTC is always negative, and for those east of UTC is
 
254
        always positive.
 
255
        """
 
256
        offset = self.timezone.utcoffset(self.data)
 
257
        # Only days can be negative, so negative offsets have days=-1 and
 
258
        # seconds positive. Positive offsets have days=0
 
259
        return offset.days * 86400 + offset.seconds
253
260
 
254
261
def format(value, format_string):
255
262
    "Convenience function"