2005-07-13 09:25:57 +08:00
|
|
|
"""
|
|
|
|
PHP date() style date formatting
|
|
|
|
See http://www.php.net/date for format strings
|
|
|
|
|
|
|
|
Usage:
|
|
|
|
>>> import datetime
|
|
|
|
>>> d = datetime.datetime.now()
|
|
|
|
>>> df = DateFormat(d)
|
|
|
|
>>> print df.format('jS F Y H:i')
|
|
|
|
7th October 2003 11:39
|
|
|
|
>>>
|
|
|
|
"""
|
|
|
|
|
2009-05-08 21:39:37 +08:00
|
|
|
import re
|
|
|
|
import time
|
|
|
|
import calendar
|
2011-11-18 21:01:06 +08:00
|
|
|
import datetime
|
|
|
|
|
2010-12-13 21:51:28 +08:00
|
|
|
from django.utils.dates import MONTHS, MONTHS_3, MONTHS_ALT, MONTHS_AP, WEEKDAYS, WEEKDAYS_ABBR
|
2005-10-23 05:37:59 +08:00
|
|
|
from django.utils.tzinfo import LocalTimezone
|
2008-07-22 11:24:09 +08:00
|
|
|
from django.utils.translation import ugettext as _
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
from django.utils.encoding import force_unicode
|
2011-11-18 21:01:06 +08:00
|
|
|
from django.utils.timezone import is_aware, is_naive
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2012-02-10 02:57:31 +08:00
|
|
|
re_formatchars = re.compile(r'(?<!\\)([aAbBcdDeEfFgGhHiIjlLmMnNoOPrsStTUuwWyYzZ])')
|
2005-10-20 13:30:11 +08:00
|
|
|
re_escaped = re.compile(r'\\(.)')
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2006-06-08 13:00:13 +08:00
|
|
|
class Formatter(object):
|
2005-10-20 13:30:11 +08:00
|
|
|
def format(self, formatstr):
|
|
|
|
pieces = []
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
for i, piece in enumerate(re_formatchars.split(force_unicode(formatstr))):
|
2005-10-20 13:30:11 +08:00
|
|
|
if i % 2:
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
pieces.append(force_unicode(getattr(self, piece)()))
|
2005-10-20 13:30:11 +08:00
|
|
|
elif piece:
|
|
|
|
pieces.append(re_escaped.sub(r'\1', piece))
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
return u''.join(pieces)
|
2005-10-20 13:30:11 +08:00
|
|
|
|
|
|
|
class TimeFormat(Formatter):
|
|
|
|
def __init__(self, t):
|
|
|
|
self.data = t
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
def a(self):
|
|
|
|
"'a.m.' or 'p.m.'"
|
2005-10-20 13:30:11 +08:00
|
|
|
if self.data.hour > 11:
|
2007-03-11 18:15:35 +08:00
|
|
|
return _('p.m.')
|
|
|
|
return _('a.m.')
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
def A(self):
|
|
|
|
"'AM' or 'PM'"
|
2005-10-23 05:37:59 +08:00
|
|
|
if self.data.hour > 11:
|
2007-03-11 18:15:35 +08:00
|
|
|
return _('PM')
|
|
|
|
return _('AM')
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
def B(self):
|
|
|
|
"Swatch Internet time"
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def f(self):
|
|
|
|
"""
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
Time, in 12-hour hours and minutes, with minutes left off if they're
|
|
|
|
zero.
|
2005-07-13 09:25:57 +08:00
|
|
|
Examples: '1', '1:30', '2:05', '2'
|
|
|
|
Proprietary extension.
|
|
|
|
"""
|
2005-10-20 13:30:11 +08:00
|
|
|
if self.data.minute == 0:
|
2005-07-13 09:25:57 +08:00
|
|
|
return self.g()
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
return u'%s:%s' % (self.g(), self.i())
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
def g(self):
|
|
|
|
"Hour, 12-hour format without leading zeros; i.e. '1' to '12'"
|
2005-10-20 13:30:11 +08:00
|
|
|
if self.data.hour == 0:
|
2005-07-13 09:25:57 +08:00
|
|
|
return 12
|
2005-10-20 13:30:11 +08:00
|
|
|
if self.data.hour > 12:
|
|
|
|
return self.data.hour - 12
|
|
|
|
return self.data.hour
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
def G(self):
|
|
|
|
"Hour, 24-hour format without leading zeros; i.e. '0' to '23'"
|
2005-10-20 13:30:11 +08:00
|
|
|
return self.data.hour
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
def h(self):
|
|
|
|
"Hour, 12-hour format; i.e. '01' to '12'"
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
return u'%02d' % self.g()
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
def H(self):
|
|
|
|
"Hour, 24-hour format; i.e. '00' to '23'"
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
return u'%02d' % self.G()
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
def i(self):
|
|
|
|
"Minutes; i.e. '00' to '59'"
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
return u'%02d' % self.data.minute
|
2005-10-20 13:30:11 +08:00
|
|
|
|
|
|
|
def P(self):
|
|
|
|
"""
|
|
|
|
Time, in 12-hour hours, minutes and 'a.m.'/'p.m.', with minutes left off
|
|
|
|
if they're zero and the strings 'midnight' and 'noon' if appropriate.
|
|
|
|
Examples: '1 a.m.', '1:30 p.m.', 'midnight', 'noon', '12:30 p.m.'
|
|
|
|
Proprietary extension.
|
|
|
|
"""
|
|
|
|
if self.data.minute == 0 and self.data.hour == 0:
|
2007-03-11 18:15:35 +08:00
|
|
|
return _('midnight')
|
2005-10-20 13:30:11 +08:00
|
|
|
if self.data.minute == 0 and self.data.hour == 12:
|
2007-03-11 18:15:35 +08:00
|
|
|
return _('noon')
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
return u'%s %s' % (self.f(), self.a())
|
2005-10-20 13:30:11 +08:00
|
|
|
|
|
|
|
def s(self):
|
|
|
|
"Seconds; i.e. '00' to '59'"
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
return u'%02d' % self.data.second
|
2005-10-20 13:30:11 +08:00
|
|
|
|
2010-01-02 05:39:26 +08:00
|
|
|
def u(self):
|
|
|
|
"Microseconds"
|
|
|
|
return self.data.microsecond
|
|
|
|
|
|
|
|
|
2005-10-20 21:13:55 +08:00
|
|
|
class DateFormat(TimeFormat):
|
2005-10-20 13:30:11 +08:00
|
|
|
year_days = [None, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334]
|
|
|
|
|
2005-10-23 05:37:59 +08:00
|
|
|
def __init__(self, dt):
|
|
|
|
# Accepts either a datetime or date object.
|
|
|
|
self.data = dt
|
2011-11-18 21:01:06 +08:00
|
|
|
self.timezone = None
|
|
|
|
if isinstance(dt, datetime.datetime):
|
|
|
|
if is_naive(dt):
|
|
|
|
self.timezone = LocalTimezone(dt)
|
|
|
|
else:
|
|
|
|
self.timezone = dt.tzinfo
|
2005-10-20 13:30:11 +08:00
|
|
|
|
2007-03-01 00:18:55 +08:00
|
|
|
def b(self):
|
|
|
|
"Month, textual, 3 letters, lowercase; e.g. 'jan'"
|
|
|
|
return MONTHS_3[self.data.month]
|
|
|
|
|
2010-01-02 05:39:26 +08:00
|
|
|
def c(self):
|
|
|
|
"""
|
|
|
|
ISO 8601 Format
|
|
|
|
Example : '2008-01-02T10:30:00.000123'
|
|
|
|
"""
|
2010-05-16 23:54:10 +08:00
|
|
|
return self.data.isoformat()
|
2010-01-02 05:39:26 +08:00
|
|
|
|
2005-10-20 13:30:11 +08:00
|
|
|
def d(self):
|
|
|
|
"Day of the month, 2 digits with leading zeros; i.e. '01' to '31'"
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
return u'%02d' % self.data.day
|
2005-10-20 13:30:11 +08:00
|
|
|
|
|
|
|
def D(self):
|
|
|
|
"Day of the week, textual, 3 letters; e.g. 'Fri'"
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
return WEEKDAYS_ABBR[self.data.weekday()]
|
2005-10-20 13:30:11 +08:00
|
|
|
|
2012-02-10 02:57:31 +08:00
|
|
|
def e(self):
|
|
|
|
"Timezone name if available"
|
|
|
|
try:
|
2012-02-10 09:13:29 +08:00
|
|
|
if hasattr(self.data, 'tzinfo') and self.data.tzinfo:
|
2012-02-10 02:57:31 +08:00
|
|
|
# Have to use tzinfo.tzname and not datetime.tzname
|
|
|
|
# because datatime.tzname does not expect Unicode
|
|
|
|
return self.data.tzinfo.tzname(self.data) or ""
|
|
|
|
except NotImplementedError:
|
|
|
|
pass
|
|
|
|
return ""
|
|
|
|
|
2010-12-13 21:51:28 +08:00
|
|
|
def E(self):
|
|
|
|
"Alternative month names as required by some locales. Proprietary extension."
|
|
|
|
return MONTHS_ALT[self.data.month]
|
|
|
|
|
2005-10-20 13:30:11 +08:00
|
|
|
def F(self):
|
|
|
|
"Month, textual, long; e.g. 'January'"
|
|
|
|
return MONTHS[self.data.month]
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2005-10-23 05:37:59 +08:00
|
|
|
def I(self):
|
|
|
|
"'1' if Daylight Savings Time, '0' otherwise."
|
2009-02-28 12:51:13 +08:00
|
|
|
if self.timezone and self.timezone.dst(self.data):
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
return u'1'
|
2005-10-23 05:37:59 +08:00
|
|
|
else:
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
return u'0'
|
2005-10-23 05:37:59 +08:00
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
def j(self):
|
|
|
|
"Day of the month without leading zeros; i.e. '1' to '31'"
|
2005-10-20 13:30:11 +08:00
|
|
|
return self.data.day
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
def l(self):
|
|
|
|
"Day of the week, textual, long; e.g. 'Friday'"
|
2005-10-20 13:30:11 +08:00
|
|
|
return WEEKDAYS[self.data.weekday()]
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
def L(self):
|
|
|
|
"Boolean for whether it is a leap year; i.e. True or False"
|
2009-05-08 21:39:37 +08:00
|
|
|
return calendar.isleap(self.data.year)
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
def m(self):
|
|
|
|
"Month; i.e. '01' to '12'"
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
return u'%02d' % self.data.month
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
def M(self):
|
|
|
|
"Month, textual, 3 letters; e.g. 'Jan'"
|
2006-12-19 00:16:12 +08:00
|
|
|
return MONTHS_3[self.data.month].title()
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
def n(self):
|
|
|
|
"Month without leading zeros; i.e. '1' to '12'"
|
2005-10-20 13:30:11 +08:00
|
|
|
return self.data.month
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
def N(self):
|
|
|
|
"Month abbreviation in Associated Press style. Proprietary extension."
|
2005-10-20 13:30:11 +08:00
|
|
|
return MONTHS_AP[self.data.month]
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2012-02-10 02:57:31 +08:00
|
|
|
def o(self):
|
|
|
|
"ISO 8601 year number matching the ISO week number (W)"
|
|
|
|
return self.data.isocalendar()[0]
|
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
def O(self):
|
2011-09-26 01:08:31 +08:00
|
|
|
"Difference to Greenwich time in hours; e.g. '+0200', '-0430'"
|
2007-09-16 19:59:56 +08:00
|
|
|
seconds = self.Z()
|
2011-09-26 01:08:31 +08:00
|
|
|
sign = '-' if seconds < 0 else '+'
|
|
|
|
seconds = abs(seconds)
|
|
|
|
return u"%s%02d%02d" % (sign, seconds // 3600, (seconds // 60) % 60)
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
def r(self):
|
2007-11-04 09:14:58 +08:00
|
|
|
"RFC 2822 formatted date; e.g. 'Thu, 21 Dec 2000 16:01:07 +0200'"
|
2005-10-23 05:37:59 +08:00
|
|
|
return self.format('D, j M Y H:i:s O')
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
def S(self):
|
|
|
|
"English ordinal suffix for the day of the month, 2 characters; i.e. 'st', 'nd', 'rd' or 'th'"
|
2005-10-20 13:30:11 +08:00
|
|
|
if self.data.day in (11, 12, 13): # Special case
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
return u'th'
|
2005-10-20 13:30:11 +08:00
|
|
|
last = self.data.day % 10
|
2005-07-13 09:25:57 +08:00
|
|
|
if last == 1:
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
return u'st'
|
2005-07-13 09:25:57 +08:00
|
|
|
if last == 2:
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
return u'nd'
|
2005-07-13 09:25:57 +08:00
|
|
|
if last == 3:
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
return u'rd'
|
|
|
|
return u'th'
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
def t(self):
|
|
|
|
"Number of days in the given month; i.e. '28' to '31'"
|
2009-05-08 21:39:37 +08:00
|
|
|
return u'%02d' % calendar.monthrange(self.data.year, self.data.month)[1]
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
def T(self):
|
|
|
|
"Time zone of this machine; e.g. 'EST' or 'MDT'"
|
2009-02-28 12:51:13 +08:00
|
|
|
name = self.timezone and self.timezone.tzname(self.data) or None
|
2005-10-23 05:37:59 +08:00
|
|
|
if name is None:
|
|
|
|
name = self.format('O')
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
return unicode(name)
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
def U(self):
|
|
|
|
"Seconds since the Unix epoch (January 1 1970 00:00:00 GMT)"
|
2011-11-18 21:01:06 +08:00
|
|
|
if isinstance(self.data, datetime.datetime) and is_aware(self.data):
|
2009-05-08 21:39:37 +08:00
|
|
|
return int(calendar.timegm(self.data.utctimetuple()))
|
|
|
|
else:
|
|
|
|
return int(time.mktime(self.data.timetuple()))
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
def w(self):
|
|
|
|
"Day of the week, numeric, i.e. '0' (Sunday) to '6' (Saturday)"
|
2005-10-20 13:30:11 +08:00
|
|
|
return (self.data.weekday() + 1) % 7
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
def W(self):
|
|
|
|
"ISO-8601 week number of year, weeks starting on Monday"
|
|
|
|
# Algorithm from http://www.personal.ecu.edu/mccartyr/ISOwdALG.txt
|
|
|
|
week_number = None
|
2005-10-20 13:30:11 +08:00
|
|
|
jan1_weekday = self.data.replace(month=1, day=1).weekday() + 1
|
|
|
|
weekday = self.data.weekday() + 1
|
2005-07-13 09:25:57 +08:00
|
|
|
day_of_year = self.z()
|
|
|
|
if day_of_year <= (8 - jan1_weekday) and jan1_weekday > 4:
|
2009-05-08 21:39:37 +08:00
|
|
|
if jan1_weekday == 5 or (jan1_weekday == 6 and calendar.isleap(self.data.year-1)):
|
2005-07-13 09:25:57 +08:00
|
|
|
week_number = 53
|
|
|
|
else:
|
|
|
|
week_number = 52
|
|
|
|
else:
|
2009-05-08 21:39:37 +08:00
|
|
|
if calendar.isleap(self.data.year):
|
2005-07-13 09:25:57 +08:00
|
|
|
i = 366
|
|
|
|
else:
|
|
|
|
i = 365
|
|
|
|
if (i - day_of_year) < (4 - weekday):
|
|
|
|
week_number = 1
|
|
|
|
else:
|
|
|
|
j = day_of_year + (7 - weekday) + (jan1_weekday - 1)
|
2007-07-12 21:55:19 +08:00
|
|
|
week_number = j // 7
|
2005-07-13 09:25:57 +08:00
|
|
|
if jan1_weekday > 4:
|
|
|
|
week_number -= 1
|
|
|
|
return week_number
|
|
|
|
|
|
|
|
def y(self):
|
|
|
|
"Year, 2 digits; e.g. '99'"
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
return unicode(self.data.year)[2:]
|
2005-10-20 13:30:11 +08:00
|
|
|
|
|
|
|
def Y(self):
|
|
|
|
"Year, 4 digits; e.g. '1999'"
|
|
|
|
return self.data.year
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
def z(self):
|
|
|
|
"Day of the year; i.e. '0' to '365'"
|
2005-10-20 13:30:11 +08:00
|
|
|
doy = self.year_days[self.data.month] + self.data.day
|
|
|
|
if self.L() and self.data.month > 2:
|
2005-07-13 09:25:57 +08:00
|
|
|
doy += 1
|
|
|
|
return doy
|
|
|
|
|
|
|
|
def Z(self):
|
2007-09-15 18:36:14 +08:00
|
|
|
"""
|
|
|
|
Time zone offset in seconds (i.e. '-43200' to '43200'). The offset for
|
|
|
|
timezones west of UTC is always negative, and for those east of UTC is
|
|
|
|
always positive.
|
|
|
|
"""
|
2009-02-28 12:51:13 +08:00
|
|
|
if not self.timezone:
|
|
|
|
return 0
|
2007-09-15 18:36:14 +08:00
|
|
|
offset = self.timezone.utcoffset(self.data)
|
2011-09-26 01:08:31 +08:00
|
|
|
# `offset` is a datetime.timedelta. For negative values (to the west of
|
|
|
|
# UTC) only days can be negative (days=-1) and seconds are always
|
|
|
|
# positive. e.g. UTC-1 -> timedelta(days=-1, seconds=82800, microseconds=0)
|
|
|
|
# Positive offsets have days=0
|
2007-09-15 18:36:14 +08:00
|
|
|
return offset.days * 86400 + offset.seconds
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
def format(value, format_string):
|
|
|
|
"Convenience function"
|
|
|
|
df = DateFormat(value)
|
|
|
|
return df.format(format_string)
|
|
|
|
|
|
|
|
def time_format(value, format_string):
|
|
|
|
"Convenience function"
|
|
|
|
tf = TimeFormat(value)
|
|
|
|
return tf.format(format_string)
|