From a14dd87891f83c73eba8f047cedd80dd0d6491eb Mon Sep 17 00:00:00 2001 From: Prince Roshan Date: Tue, 25 Apr 2023 10:44:19 +0530 Subject: [PATCH 1/3] gh-103822: change return value to enums for days and month API in calendar module --- Lib/calendar.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/Lib/calendar.py b/Lib/calendar.py index c219f0c218d9fa..897e68f7118660 100644 --- a/Lib/calendar.py +++ b/Lib/calendar.py @@ -70,6 +70,13 @@ class Day(IntEnum): SUNDAY = 6 +def _get_enum_attribute(value, enum_type): + """Returns the value of the specified attribute of a Day or Month Enum object based on its integer value.""" + if enum_type == "day": + return Day(value) + else: + return Month(value) + # Number of days per month (except for February in leap years) mdays = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] @@ -143,7 +150,7 @@ def weekday(year, month, day): """Return weekday (0-6 ~ Mon-Sun) for year, month (1-12), day (1-31).""" if not datetime.MINYEAR <= year <= datetime.MAXYEAR: year = 2000 + year % 400 - return datetime.date(year, month, day).weekday() + return _get_enum_attribute(datetime.date(year, month, day).weekday(),'day') def monthrange(year, month): From ced565316b7797cd050838a4b1eb099943b74094 Mon Sep 17 00:00:00 2001 From: Prince Roshan Date: Tue, 2 May 2023 10:13:47 +0530 Subject: [PATCH 2/3] remove _get_enum_attribute --- Lib/calendar.py | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/Lib/calendar.py b/Lib/calendar.py index 897e68f7118660..042195f4ae6aef 100644 --- a/Lib/calendar.py +++ b/Lib/calendar.py @@ -70,14 +70,6 @@ class Day(IntEnum): SUNDAY = 6 -def _get_enum_attribute(value, enum_type): - """Returns the value of the specified attribute of a Day or Month Enum object based on its integer value.""" - if enum_type == "day": - return Day(value) - else: - return Month(value) - - # Number of days per month (except for February in leap years) mdays = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] @@ -150,7 +142,7 @@ def weekday(year, month, day): """Return weekday (0-6 ~ Mon-Sun) for year, month (1-12), day (1-31).""" if not datetime.MINYEAR <= year <= datetime.MAXYEAR: year = 2000 + year % 400 - return _get_enum_attribute(datetime.date(year, month, day).weekday(),'day') + return Day(datetime.date(year, month, day).weekday()) def monthrange(year, month): From 950e31f8f055409ada5888dec20fd1253d290d48 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Tue, 2 May 2023 04:49:46 +0000 Subject: [PATCH 3/3] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2023-05-02-04-49-45.gh-issue-103822.m0QdAO.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2023-05-02-04-49-45.gh-issue-103822.m0QdAO.rst diff --git a/Misc/NEWS.d/next/Library/2023-05-02-04-49-45.gh-issue-103822.m0QdAO.rst b/Misc/NEWS.d/next/Library/2023-05-02-04-49-45.gh-issue-103822.m0QdAO.rst new file mode 100644 index 00000000000000..3daf9cc093807b --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-05-02-04-49-45.gh-issue-103822.m0QdAO.rst @@ -0,0 +1 @@ +Update the return type of ``weekday`` to the newly added Day attribute