Simplified DateFormat.W() and z().

This commit is contained in:
Baptiste Mispelon 2019-11-15 17:30:22 +01:00 committed by Mariusz Felisiak
parent 1185c6172b
commit 5e2839f320
1 changed files with 2 additions and 28 deletions

View File

@ -208,8 +208,6 @@ class TimeFormat(Formatter):
class DateFormat(TimeFormat): class DateFormat(TimeFormat):
year_days = [None, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334]
def b(self): def b(self):
"Month, textual, 3 letters, lowercase; e.g. 'jan'" "Month, textual, 3 letters, lowercase; e.g. 'jan'"
return MONTHS_3[self.data.month] return MONTHS_3[self.data.month]
@ -316,28 +314,7 @@ class DateFormat(TimeFormat):
def W(self): def W(self):
"ISO-8601 week number of year, weeks starting on Monday" "ISO-8601 week number of year, weeks starting on Monday"
# Algorithm from http://www.personal.ecu.edu/mccartyr/ISOwdALG.txt return self.data.isocalendar()[1]
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
def y(self): def y(self):
"Year, 2 digits; e.g. '99'" "Year, 2 digits; e.g. '99'"
@ -349,10 +326,7 @@ class DateFormat(TimeFormat):
def z(self): def z(self):
"""Day of the year, i.e. 1 to 366.""" """Day of the year, i.e. 1 to 366."""
doy = self.year_days[self.data.month] + self.data.day return self.data.timetuple().tm_yday
if self.L() and self.data.month > 2:
doy += 1
return doy
def format(value, format_string): def format(value, format_string):