测试gitnore
This commit is contained in:
@@ -12,27 +12,21 @@ Usage:
|
||||
"""
|
||||
import calendar
|
||||
import datetime
|
||||
import time
|
||||
from email.utils import format_datetime as format_datetime_rfc5322
|
||||
|
||||
from django.utils.dates import (
|
||||
MONTHS,
|
||||
MONTHS_3,
|
||||
MONTHS_ALT,
|
||||
MONTHS_AP,
|
||||
WEEKDAYS,
|
||||
WEEKDAYS_ABBR,
|
||||
MONTHS, MONTHS_3, MONTHS_ALT, MONTHS_AP, WEEKDAYS, WEEKDAYS_ABBR,
|
||||
)
|
||||
from django.utils.regex_helper import _lazy_re_compile
|
||||
from django.utils.timezone import (
|
||||
_datetime_ambiguous_or_imaginary,
|
||||
get_default_timezone,
|
||||
is_naive,
|
||||
_datetime_ambiguous_or_imaginary, get_default_timezone, is_aware, is_naive,
|
||||
make_aware,
|
||||
)
|
||||
from django.utils.translation import gettext as _
|
||||
|
||||
re_formatchars = _lazy_re_compile(r"(?<!\\)([aAbcdDeEfFgGhHiIjlLmMnNoOPrsStTUuwWyYzZ])")
|
||||
re_escaped = _lazy_re_compile(r"\\(.)")
|
||||
re_formatchars = _lazy_re_compile(r'(?<!\\)([aAbcdDeEfFgGhHiIjlLmMnNoOPrsStTUuwWyYzZ])')
|
||||
re_escaped = _lazy_re_compile(r'\\(.)')
|
||||
|
||||
|
||||
class Formatter:
|
||||
@@ -47,11 +41,12 @@ class Formatter:
|
||||
)
|
||||
pieces.append(str(getattr(self, piece)()))
|
||||
elif piece:
|
||||
pieces.append(re_escaped.sub(r"\1", piece))
|
||||
return "".join(pieces)
|
||||
pieces.append(re_escaped.sub(r'\1', piece))
|
||||
return ''.join(pieces)
|
||||
|
||||
|
||||
class TimeFormat(Formatter):
|
||||
|
||||
def __init__(self, obj):
|
||||
self.data = obj
|
||||
self.timezone = None
|
||||
@@ -65,23 +60,17 @@ class TimeFormat(Formatter):
|
||||
else:
|
||||
self.timezone = obj.tzinfo
|
||||
|
||||
@property
|
||||
def _no_timezone_or_datetime_is_ambiguous_or_imaginary(self):
|
||||
return not self.timezone or _datetime_ambiguous_or_imaginary(
|
||||
self.data, self.timezone
|
||||
)
|
||||
|
||||
def a(self):
|
||||
"'a.m.' or 'p.m.'"
|
||||
if self.data.hour > 11:
|
||||
return _("p.m.")
|
||||
return _("a.m.")
|
||||
return _('p.m.')
|
||||
return _('a.m.')
|
||||
|
||||
def A(self):
|
||||
"'AM' or 'PM'"
|
||||
if self.data.hour > 11:
|
||||
return _("PM")
|
||||
return _("AM")
|
||||
return _('PM')
|
||||
return _('AM')
|
||||
|
||||
def e(self):
|
||||
"""
|
||||
@@ -93,8 +82,8 @@ class TimeFormat(Formatter):
|
||||
return ""
|
||||
|
||||
try:
|
||||
if hasattr(self.data, "tzinfo") and self.data.tzinfo:
|
||||
return self.data.tzname() or ""
|
||||
if hasattr(self.data, 'tzinfo') and self.data.tzinfo:
|
||||
return self.data.tzname() or ''
|
||||
except NotImplementedError:
|
||||
pass
|
||||
return ""
|
||||
@@ -106,9 +95,9 @@ class TimeFormat(Formatter):
|
||||
Examples: '1', '1:30', '2:05', '2'
|
||||
Proprietary extension.
|
||||
"""
|
||||
hour = self.data.hour % 12 or 12
|
||||
minute = self.data.minute
|
||||
return "%d:%02d" % (hour, minute) if minute else hour
|
||||
if self.data.minute == 0:
|
||||
return self.g()
|
||||
return '%s:%s' % (self.g(), self.i())
|
||||
|
||||
def g(self):
|
||||
"Hour, 12-hour format without leading zeros; i.e. '1' to '12'"
|
||||
@@ -120,15 +109,15 @@ class TimeFormat(Formatter):
|
||||
|
||||
def h(self):
|
||||
"Hour, 12-hour format; i.e. '01' to '12'"
|
||||
return "%02d" % (self.data.hour % 12 or 12)
|
||||
return '%02d' % self.g()
|
||||
|
||||
def H(self):
|
||||
"Hour, 24-hour format; i.e. '00' to '23'"
|
||||
return "%02d" % self.data.hour
|
||||
return '%02d' % self.G()
|
||||
|
||||
def i(self):
|
||||
"Minutes; i.e. '00' to '59'"
|
||||
return "%02d" % self.data.minute
|
||||
return '%02d' % self.data.minute
|
||||
|
||||
def O(self): # NOQA: E743, E741
|
||||
"""
|
||||
@@ -136,11 +125,13 @@ class TimeFormat(Formatter):
|
||||
|
||||
If timezone information is not available, return an empty string.
|
||||
"""
|
||||
if self._no_timezone_or_datetime_is_ambiguous_or_imaginary:
|
||||
if not self.timezone:
|
||||
return ""
|
||||
|
||||
seconds = self.Z()
|
||||
sign = "-" if seconds < 0 else "+"
|
||||
if seconds == "":
|
||||
return ""
|
||||
sign = '-' if seconds < 0 else '+'
|
||||
seconds = abs(seconds)
|
||||
return "%s%02d%02d" % (sign, seconds // 3600, (seconds // 60) % 60)
|
||||
|
||||
@@ -152,14 +143,14 @@ class TimeFormat(Formatter):
|
||||
Proprietary extension.
|
||||
"""
|
||||
if self.data.minute == 0 and self.data.hour == 0:
|
||||
return _("midnight")
|
||||
return _('midnight')
|
||||
if self.data.minute == 0 and self.data.hour == 12:
|
||||
return _("noon")
|
||||
return "%s %s" % (self.f(), self.a())
|
||||
return _('noon')
|
||||
return '%s %s' % (self.f(), self.a())
|
||||
|
||||
def s(self):
|
||||
"Seconds; i.e. '00' to '59'"
|
||||
return "%02d" % self.data.second
|
||||
return '%02d' % self.data.second
|
||||
|
||||
def T(self):
|
||||
"""
|
||||
@@ -167,14 +158,18 @@ class TimeFormat(Formatter):
|
||||
|
||||
If timezone information is not available, return an empty string.
|
||||
"""
|
||||
if self._no_timezone_or_datetime_is_ambiguous_or_imaginary:
|
||||
if not self.timezone:
|
||||
return ""
|
||||
|
||||
return str(self.timezone.tzname(self.data))
|
||||
if not _datetime_ambiguous_or_imaginary(self.data, self.timezone):
|
||||
name = self.timezone.tzname(self.data)
|
||||
else:
|
||||
name = self.format('O')
|
||||
return str(name)
|
||||
|
||||
def u(self):
|
||||
"Microseconds; i.e. '000000' to '999999'"
|
||||
return "%06d" % self.data.microsecond
|
||||
return '%06d' % self.data.microsecond
|
||||
|
||||
def Z(self):
|
||||
"""
|
||||
@@ -184,7 +179,10 @@ class TimeFormat(Formatter):
|
||||
|
||||
If timezone information is not available, return an empty string.
|
||||
"""
|
||||
if self._no_timezone_or_datetime_is_ambiguous_or_imaginary:
|
||||
if (
|
||||
not self.timezone or
|
||||
_datetime_ambiguous_or_imaginary(self.data, self.timezone)
|
||||
):
|
||||
return ""
|
||||
|
||||
offset = self.timezone.utcoffset(self.data)
|
||||
@@ -210,7 +208,7 @@ class DateFormat(TimeFormat):
|
||||
|
||||
def d(self):
|
||||
"Day of the month, 2 digits with leading zeros; i.e. '01' to '31'"
|
||||
return "%02d" % self.data.day
|
||||
return '%02d' % self.data.day
|
||||
|
||||
def D(self):
|
||||
"Day of the week, textual, 3 letters; e.g. 'Fri'"
|
||||
@@ -225,10 +223,13 @@ class DateFormat(TimeFormat):
|
||||
return MONTHS[self.data.month]
|
||||
|
||||
def I(self): # NOQA: E743, E741
|
||||
"'1' if daylight saving time, '0' otherwise."
|
||||
if self._no_timezone_or_datetime_is_ambiguous_or_imaginary:
|
||||
return ""
|
||||
return "1" if self.timezone.dst(self.data) else "0"
|
||||
"'1' if Daylight Savings Time, '0' otherwise."
|
||||
if (
|
||||
not self.timezone or
|
||||
_datetime_ambiguous_or_imaginary(self.data, self.timezone)
|
||||
):
|
||||
return ''
|
||||
return '1' if self.timezone.dst(self.data) else '0'
|
||||
|
||||
def j(self):
|
||||
"Day of the month without leading zeros; i.e. '1' to '31'"
|
||||
@@ -244,7 +245,7 @@ class DateFormat(TimeFormat):
|
||||
|
||||
def m(self):
|
||||
"Month; i.e. '01' to '12'"
|
||||
return "%02d" % self.data.month
|
||||
return '%02d' % self.data.month
|
||||
|
||||
def M(self):
|
||||
"Month, textual, 3 letters; e.g. 'Jan'"
|
||||
@@ -276,31 +277,28 @@ class DateFormat(TimeFormat):
|
||||
return format_datetime_rfc5322(dt)
|
||||
|
||||
def S(self):
|
||||
"""
|
||||
English ordinal suffix for the day of the month, 2 characters; i.e.
|
||||
'st', 'nd', 'rd' or 'th'.
|
||||
"""
|
||||
"English ordinal suffix for the day of the month, 2 characters; i.e. 'st', 'nd', 'rd' or 'th'"
|
||||
if self.data.day in (11, 12, 13): # Special case
|
||||
return "th"
|
||||
return 'th'
|
||||
last = self.data.day % 10
|
||||
if last == 1:
|
||||
return "st"
|
||||
return 'st'
|
||||
if last == 2:
|
||||
return "nd"
|
||||
return 'nd'
|
||||
if last == 3:
|
||||
return "rd"
|
||||
return "th"
|
||||
return 'rd'
|
||||
return 'th'
|
||||
|
||||
def t(self):
|
||||
"Number of days in the given month; i.e. '28' to '31'"
|
||||
return "%02d" % calendar.monthrange(self.data.year, self.data.month)[1]
|
||||
return '%02d' % calendar.monthrange(self.data.year, self.data.month)[1]
|
||||
|
||||
def U(self):
|
||||
"Seconds since the Unix epoch (January 1 1970 00:00:00 GMT)"
|
||||
value = self.data
|
||||
if not isinstance(value, datetime.datetime):
|
||||
value = datetime.datetime.combine(value, datetime.time.min)
|
||||
return int(value.timestamp())
|
||||
if isinstance(self.data, datetime.datetime) and is_aware(self.data):
|
||||
return int(calendar.timegm(self.data.utctimetuple()))
|
||||
else:
|
||||
return int(time.mktime(self.data.timetuple()))
|
||||
|
||||
def w(self):
|
||||
"Day of the week, numeric, i.e. '0' (Sunday) to '6' (Saturday)"
|
||||
@@ -312,11 +310,11 @@ class DateFormat(TimeFormat):
|
||||
|
||||
def y(self):
|
||||
"""Year, 2 digits with leading zeros; e.g. '99'."""
|
||||
return "%02d" % (self.data.year % 100)
|
||||
return '%02d' % (self.data.year % 100)
|
||||
|
||||
def Y(self):
|
||||
"""Year, 4 digits with leading zeros; e.g. '1999'."""
|
||||
return "%04d" % self.data.year
|
||||
"Year, 4 digits; e.g. '1999'"
|
||||
return self.data.year
|
||||
|
||||
def z(self):
|
||||
"""Day of the year, i.e. 1 to 366."""
|
||||
|
||||
Reference in New Issue
Block a user