1
0
mirror of https://github.com/django/django.git synced 2024-12-22 17:16:24 +00:00

Simplified django.utils.dateformat.DateFormat.O()/t()/e() a bit.

O() - we should try to avoid calling specifier methods from each other
to avoid extra function call overhead. In addition we end up, in this
case, duplicating the ambiguous/imaginary datetime checks. We're also
going to be looking at simplifying things by having all of these
specifier methods return strings and not an random mix of types.

t() - the value can only be one of 28, 29, 30, or 31. As such, there is
no need to zero-pad to a width of two.
This commit is contained in:
Nick Pope 2022-10-12 23:20:32 +01:00 committed by Mariusz Felisiak
parent d7a8ab3513
commit 41ca2afd1c

View File

@ -93,7 +93,7 @@ class TimeFormat(Formatter):
return ""
try:
if hasattr(self.data, "tzinfo") and self.data.tzinfo:
if getattr(self.data, "tzinfo", None):
return self.data.tzname() or ""
except NotImplementedError:
pass
@ -139,7 +139,8 @@ class TimeFormat(Formatter):
if self._no_timezone_or_datetime_is_ambiguous_or_imaginary:
return ""
seconds = self.Z()
offset = self.timezone.utcoffset(self.data)
seconds = offset.days * 86400 + offset.seconds
sign = "-" if seconds < 0 else "+"
seconds = abs(seconds)
return "%s%02d%02d" % (sign, seconds // 3600, (seconds // 60) % 60)
@ -293,7 +294,7 @@ class DateFormat(TimeFormat):
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 calendar.monthrange(self.data.year, self.data.month)[1]
def U(self):
"Seconds since the Unix epoch (January 1 1970 00:00:00 GMT)"