From 41ca2afd1ce49949d509177987c3b4b7c8ba3fa1 Mon Sep 17 00:00:00 2001 From: Nick Pope Date: Wed, 12 Oct 2022 23:20:32 +0100 Subject: [PATCH] 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. --- django/utils/dateformat.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/django/utils/dateformat.py b/django/utils/dateformat.py index 4b733733f3..adf748b21c 100644 --- a/django/utils/dateformat.py +++ b/django/utils/dateformat.py @@ -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)"