2005-07-13 09:25:57 +08:00
|
|
|
"""
|
|
|
|
PHP date() style date formatting
|
2021-04-05 07:29:58 +08:00
|
|
|
See https://www.php.net/date for format strings
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
Usage:
|
|
|
|
>>> import datetime
|
|
|
|
>>> d = datetime.datetime.now()
|
|
|
|
>>> df = DateFormat(d)
|
2012-04-29 00:02:01 +08:00
|
|
|
>>> print(df.format('jS F Y H:i'))
|
2005-07-13 09:25:57 +08:00
|
|
|
7th October 2003 11:39
|
|
|
|
>>>
|
|
|
|
"""
|
2009-05-08 21:39:37 +08:00
|
|
|
import calendar
|
2011-11-18 21:01:06 +08:00
|
|
|
import datetime
|
2019-11-22 03:34:58 +08:00
|
|
|
from email.utils import format_datetime as format_datetime_rfc5322
|
2011-11-18 21:01:06 +08:00
|
|
|
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.utils.dates import (
|
|
|
|
MONTHS,
|
|
|
|
MONTHS_3,
|
|
|
|
MONTHS_ALT,
|
|
|
|
MONTHS_AP,
|
|
|
|
WEEKDAYS,
|
|
|
|
WEEKDAYS_ABBR,
|
|
|
|
)
|
2019-10-26 22:42:32 +08:00
|
|
|
from django.utils.regex_helper import _lazy_re_compile
|
2019-11-22 03:34:58 +08:00
|
|
|
from django.utils.timezone import (
|
2021-05-07 17:42:59 +08:00
|
|
|
_datetime_ambiguous_or_imaginary,
|
|
|
|
get_default_timezone,
|
|
|
|
is_naive,
|
2021-01-19 18:16:01 +08:00
|
|
|
make_aware,
|
2019-11-22 03:34:58 +08:00
|
|
|
)
|
2017-01-27 03:58:33 +08:00
|
|
|
from django.utils.translation import gettext as _
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2019-11-18 19:50:41 +08:00
|
|
|
re_formatchars = _lazy_re_compile(r"(?<!\\)([aAbcdDeEfFgGhHiIjlLmMnNoOPrsStTUuwWyYzZ])")
|
2019-10-26 22:42:32 +08:00
|
|
|
re_escaped = _lazy_re_compile(r"\\(.)")
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2013-11-03 07:53:29 +08:00
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class Formatter:
|
2005-10-20 13:30:11 +08:00
|
|
|
def format(self, formatstr):
|
|
|
|
pieces = []
|
2017-04-22 01:52:26 +08:00
|
|
|
for i, piece in enumerate(re_formatchars.split(str(formatstr))):
|
2005-10-20 13:30:11 +08:00
|
|
|
if i % 2:
|
2016-04-04 01:31:35 +08:00
|
|
|
if type(self.data) is datetime.date and hasattr(TimeFormat, piece):
|
|
|
|
raise TypeError(
|
|
|
|
"The format for date objects may not contain "
|
|
|
|
"time-related format specifiers (found '%s')." % piece
|
|
|
|
)
|
2017-04-22 01:52:26 +08:00
|
|
|
pieces.append(str(getattr(self, piece)()))
|
2005-10-20 13:30:11 +08:00
|
|
|
elif piece:
|
|
|
|
pieces.append(re_escaped.sub(r"\1", piece))
|
2012-06-08 00:08:47 +08:00
|
|
|
return "".join(pieces)
|
2005-10-20 13:30:11 +08:00
|
|
|
|
2013-11-03 07:53:29 +08:00
|
|
|
|
2005-10-20 13:30:11 +08:00
|
|
|
class TimeFormat(Formatter):
|
2013-07-03 06:19:56 +08:00
|
|
|
def __init__(self, obj):
|
|
|
|
self.data = obj
|
|
|
|
self.timezone = None
|
|
|
|
|
|
|
|
# We only support timezone when formatting datetime objects,
|
|
|
|
# not date objects (timezone information not appropriate),
|
|
|
|
# or time objects (against established django policy).
|
|
|
|
if isinstance(obj, datetime.datetime):
|
|
|
|
if is_naive(obj):
|
2013-09-08 16:43:33 +08:00
|
|
|
self.timezone = get_default_timezone()
|
2013-07-03 06:19:56 +08:00
|
|
|
else:
|
|
|
|
self.timezone = obj.tzinfo
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2021-05-13 20:33:19 +08:00
|
|
|
@property
|
|
|
|
def _no_timezone_or_datetime_is_ambiguous_or_imaginary(self):
|
|
|
|
return not self.timezone or _datetime_ambiguous_or_imaginary(
|
|
|
|
self.data, self.timezone
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
2013-07-03 06:19:56 +08:00
|
|
|
def e(self):
|
|
|
|
"""
|
|
|
|
Timezone name.
|
|
|
|
|
2017-01-25 04:32:33 +08:00
|
|
|
If timezone information is not available, return an empty string.
|
2013-07-03 06:19:56 +08:00
|
|
|
"""
|
|
|
|
if not self.timezone:
|
|
|
|
return ""
|
|
|
|
|
2017-09-07 20:16:21 +08:00
|
|
|
try:
|
2013-07-03 06:19:56 +08:00
|
|
|
if hasattr(self.data, "tzinfo") and self.data.tzinfo:
|
2017-01-21 05:04:05 +08:00
|
|
|
return self.data.tzname() or ""
|
2017-09-07 20:16:21 +08:00
|
|
|
except NotImplementedError:
|
|
|
|
pass
|
2013-07-03 06:19:56 +08:00
|
|
|
return ""
|
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
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.
|
|
|
|
"""
|
2021-05-13 20:33:19 +08:00
|
|
|
hour = self.data.hour % 12 or 12
|
|
|
|
minute = self.data.minute
|
|
|
|
return "%d:%02d" % (hour, minute) if minute else hour
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
def g(self):
|
|
|
|
"Hour, 12-hour format without leading zeros; i.e. '1' to '12'"
|
2020-11-12 22:19:17 +08:00
|
|
|
return self.data.hour % 12 or 12
|
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'"
|
2021-05-13 20:33:19 +08:00
|
|
|
return "%02d" % (self.data.hour % 12 or 12)
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
def H(self):
|
|
|
|
"Hour, 24-hour format; i.e. '00' to '23'"
|
2021-05-13 20:33:19 +08:00
|
|
|
return "%02d" % self.data.hour
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
def i(self):
|
|
|
|
"Minutes; i.e. '00' to '59'"
|
2012-06-08 00:08:47 +08:00
|
|
|
return "%02d" % self.data.minute
|
2005-10-20 13:30:11 +08:00
|
|
|
|
2020-05-12 14:52:23 +08:00
|
|
|
def O(self): # NOQA: E743, E741
|
2013-07-03 06:19:56 +08:00
|
|
|
"""
|
|
|
|
Difference to Greenwich time in hours; e.g. '+0200', '-0430'.
|
|
|
|
|
2017-01-25 04:32:33 +08:00
|
|
|
If timezone information is not available, return an empty string.
|
2013-07-03 06:19:56 +08:00
|
|
|
"""
|
2021-05-13 20:33:19 +08:00
|
|
|
if self._no_timezone_or_datetime_is_ambiguous_or_imaginary:
|
2013-07-03 06:19:56 +08:00
|
|
|
return ""
|
|
|
|
|
|
|
|
seconds = self.Z()
|
|
|
|
sign = "-" if seconds < 0 else "+"
|
|
|
|
seconds = abs(seconds)
|
|
|
|
return "%s%02d%02d" % (sign, seconds // 3600, (seconds // 60) % 60)
|
|
|
|
|
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")
|
2012-06-08 00:08:47 +08:00
|
|
|
return "%s %s" % (self.f(), self.a())
|
2005-10-20 13:30:11 +08:00
|
|
|
|
|
|
|
def s(self):
|
|
|
|
"Seconds; i.e. '00' to '59'"
|
2012-06-08 00:08:47 +08:00
|
|
|
return "%02d" % self.data.second
|
2005-10-20 13:30:11 +08:00
|
|
|
|
2013-07-03 06:19:56 +08:00
|
|
|
def T(self):
|
|
|
|
"""
|
|
|
|
Time zone of this machine; e.g. 'EST' or 'MDT'.
|
|
|
|
|
2017-01-25 04:32:33 +08:00
|
|
|
If timezone information is not available, return an empty string.
|
2013-07-03 06:19:56 +08:00
|
|
|
"""
|
2021-05-13 20:33:19 +08:00
|
|
|
if self._no_timezone_or_datetime_is_ambiguous_or_imaginary:
|
2013-07-03 06:19:56 +08:00
|
|
|
return ""
|
|
|
|
|
2021-05-13 20:33:19 +08:00
|
|
|
return str(self.timezone.tzname(self.data))
|
2013-07-03 06:19:56 +08:00
|
|
|
|
2010-01-02 05:39:26 +08:00
|
|
|
def u(self):
|
2012-09-22 18:02:21 +08:00
|
|
|
"Microseconds; i.e. '000000' to '999999'"
|
2013-10-23 18:09:29 +08:00
|
|
|
return "%06d" % self.data.microsecond
|
2010-01-02 05:39:26 +08:00
|
|
|
|
2013-07-03 06:19:56 +08:00
|
|
|
def Z(self):
|
|
|
|
"""
|
|
|
|
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.
|
|
|
|
|
2017-01-25 04:32:33 +08:00
|
|
|
If timezone information is not available, return an empty string.
|
2013-07-03 06:19:56 +08:00
|
|
|
"""
|
2021-05-13 20:33:19 +08:00
|
|
|
if self._no_timezone_or_datetime_is_ambiguous_or_imaginary:
|
2013-07-03 06:19:56 +08:00
|
|
|
return ""
|
|
|
|
|
2021-01-19 18:16:01 +08:00
|
|
|
offset = self.timezone.utcoffset(self.data)
|
2015-11-07 23:12:13 +08:00
|
|
|
|
2013-07-03 06:19:56 +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
|
|
|
|
return offset.days * 86400 + offset.seconds
|
|
|
|
|
2010-01-02 05:39:26 +08:00
|
|
|
|
2005-10-20 21:13:55 +08:00
|
|
|
class DateFormat(TimeFormat):
|
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'"
|
2012-06-08 00:08:47 +08:00
|
|
|
return "%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
|
|
|
|
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
|
|
|
|
2020-05-12 14:52:23 +08:00
|
|
|
def I(self): # NOQA: E743, E741
|
2021-07-30 20:41:35 +08:00
|
|
|
"'1' if daylight saving time, '0' otherwise."
|
2021-05-13 20:33:19 +08:00
|
|
|
if self._no_timezone_or_datetime_is_ambiguous_or_imaginary:
|
2015-11-07 23:12:13 +08:00
|
|
|
return ""
|
2021-01-19 18:16:01 +08:00
|
|
|
return "1" if self.timezone.dst(self.data) else "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
|
|
|
|
2020-05-12 14:52:23 +08:00
|
|
|
def l(self): # NOQA: E743, E741
|
2005-07-13 09:25:57 +08:00
|
|
|
"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'"
|
2012-06-08 00:08:47 +08:00
|
|
|
return "%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 r(self):
|
2016-05-04 18:45:31 +08:00
|
|
|
"RFC 5322 formatted date; e.g. 'Thu, 21 Dec 2000 16:01:07 +0200'"
|
2019-11-22 16:40:49 +08:00
|
|
|
if type(self.data) is datetime.date:
|
|
|
|
raise TypeError(
|
|
|
|
"The format for date objects may not contain time-related "
|
|
|
|
"format specifiers (found 'r')."
|
|
|
|
)
|
2019-11-22 03:34:58 +08:00
|
|
|
if is_naive(self.data):
|
|
|
|
dt = make_aware(self.data, timezone=self.timezone)
|
|
|
|
else:
|
|
|
|
dt = self.data
|
|
|
|
return format_datetime_rfc5322(dt)
|
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'.
|
2022-02-04 15:08:27 +08:00
|
|
|
"""
|
2013-11-03 03:27:47 +08:00
|
|
|
if self.data.day in (11, 12, 13): # Special case
|
2012-06-08 00:08:47 +08:00
|
|
|
return "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:
|
2012-06-08 00:08:47 +08:00
|
|
|
return "st"
|
2005-07-13 09:25:57 +08:00
|
|
|
if last == 2:
|
2012-06-08 00:08:47 +08:00
|
|
|
return "nd"
|
2005-07-13 09:25:57 +08:00
|
|
|
if last == 3:
|
2012-06-08 00:08:47 +08:00
|
|
|
return "rd"
|
|
|
|
return "th"
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
def t(self):
|
|
|
|
"Number of days in the given month; i.e. '28' to '31'"
|
2012-06-08 00:08:47 +08:00
|
|
|
return "%02d" % calendar.monthrange(self.data.year, self.data.month)[1]
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
def U(self):
|
|
|
|
"Seconds since the Unix epoch (January 1 1970 00:00:00 GMT)"
|
2021-05-07 17:42:59 +08:00
|
|
|
value = self.data
|
|
|
|
if not isinstance(value, datetime.datetime):
|
|
|
|
value = datetime.datetime.combine(value, datetime.time.min)
|
|
|
|
return int(value.timestamp())
|
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"
|
2019-11-16 00:30:22 +08:00
|
|
|
return self.data.isocalendar()[1]
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
def y(self):
|
2020-10-27 12:12:14 +08:00
|
|
|
"""Year, 2 digits with leading zeros; e.g. '99'."""
|
|
|
|
return "%02d" % (self.data.year % 100)
|
2005-10-20 13:30:11 +08:00
|
|
|
|
|
|
|
def Y(self):
|
2021-05-10 22:33:48 +08:00
|
|
|
"""Year, 4 digits with leading zeros; e.g. '1999'."""
|
|
|
|
return "%04d" % self.data.year
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
def z(self):
|
2019-11-16 00:28:48 +08:00
|
|
|
"""Day of the year, i.e. 1 to 366."""
|
2019-11-16 00:30:22 +08:00
|
|
|
return self.data.timetuple().tm_yday
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
|
|
|
|
def format(value, format_string):
|
|
|
|
"Convenience function"
|
|
|
|
df = DateFormat(value)
|
|
|
|
return df.format(format_string)
|
|
|
|
|
2013-07-03 06:19:56 +08:00
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
def time_format(value, format_string):
|
|
|
|
"Convenience function"
|
|
|
|
tf = TimeFormat(value)
|
|
|
|
return tf.format(format_string)
|