From 5e2839f3207a02c05b96d761b2441d1ef4d920d4 Mon Sep 17 00:00:00 2001 From: Baptiste Mispelon Date: Fri, 15 Nov 2019 17:30:22 +0100 Subject: [PATCH] Simplified DateFormat.W() and z(). --- django/utils/dateformat.py | 30 ++---------------------------- 1 file changed, 2 insertions(+), 28 deletions(-) diff --git a/django/utils/dateformat.py b/django/utils/dateformat.py index af46a81ce6c..82cb444691d 100644 --- a/django/utils/dateformat.py +++ b/django/utils/dateformat.py @@ -208,8 +208,6 @@ class TimeFormat(Formatter): class DateFormat(TimeFormat): - year_days = [None, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334] - def b(self): "Month, textual, 3 letters, lowercase; e.g. 'jan'" return MONTHS_3[self.data.month] @@ -316,28 +314,7 @@ class DateFormat(TimeFormat): def W(self): "ISO-8601 week number of year, weeks starting on Monday" - # Algorithm from http://www.personal.ecu.edu/mccartyr/ISOwdALG.txt - jan1_weekday = self.data.replace(month=1, day=1).weekday() + 1 - weekday = self.data.weekday() + 1 - day_of_year = self.z() - if day_of_year <= (8 - jan1_weekday) and jan1_weekday > 4: - if jan1_weekday == 5 or (jan1_weekday == 6 and calendar.isleap(self.data.year - 1)): - week_number = 53 - else: - week_number = 52 - else: - if calendar.isleap(self.data.year): - 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) - week_number = j // 7 - if jan1_weekday > 4: - week_number -= 1 - return week_number + return self.data.isocalendar()[1] def y(self): "Year, 2 digits; e.g. '99'" @@ -349,10 +326,7 @@ class DateFormat(TimeFormat): def z(self): """Day of the year, i.e. 1 to 366.""" - doy = self.year_days[self.data.month] + self.data.day - if self.L() and self.data.month > 2: - doy += 1 - return doy + return self.data.timetuple().tm_yday def format(value, format_string):