From 7897fc057b78ee5344ae2d591566c67175dafa41 Mon Sep 17 00:00:00 2001 From: Valentin Deniaud Date: Thu, 20 Jan 2022 11:29:48 +0100 Subject: [PATCH 1/5] utils: add function to get weekday index from date (#45159) --- chrono/utils/date.py | 2 ++ tests/test_utils.py | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 chrono/utils/date.py create mode 100644 tests/test_utils.py diff --git a/chrono/utils/date.py b/chrono/utils/date.py new file mode 100644 index 00000000..2ef19c49 --- /dev/null +++ b/chrono/utils/date.py @@ -0,0 +1,2 @@ +def get_weekday_index(datetime): + return (datetime.day - 1) // 7 + 1 diff --git a/tests/test_utils.py b/tests/test_utils.py new file mode 100644 index 00000000..1091c0be --- /dev/null +++ b/tests/test_utils.py @@ -0,0 +1,23 @@ +import datetime + +from chrono.utils.date import get_weekday_index + + +def test_get_weekday_index(): + for date in ( + datetime.date(2021, 11, 1), # month starting a Monday + datetime.date(2021, 12, 1), # month starting a Wednesday + datetime.date(2021, 5, 1), # month starting a Sunday + ): + assert get_weekday_index(date) == 1 + assert get_weekday_index(date.replace(day=3)) == 1 + assert get_weekday_index(date.replace(day=7)) == 1 + assert get_weekday_index(date.replace(day=8)) == 2 + assert get_weekday_index(date.replace(day=10)) == 2 + assert get_weekday_index(date.replace(day=14)) == 2 + assert get_weekday_index(date.replace(day=15)) == 3 + assert get_weekday_index(date.replace(day=21)) == 3 + assert get_weekday_index(date.replace(day=22)) == 4 + assert get_weekday_index(date.replace(day=28)) == 4 + assert get_weekday_index(date.replace(day=29)) == 5 + assert get_weekday_index(date.replace(day=30)) == 5 -- 2.30.2