utils: add function to get weekday index from date (#45159)

This commit is contained in:
Valentin Deniaud 2022-01-20 11:29:48 +01:00
parent d1df43fd7e
commit 839a578bd0
2 changed files with 25 additions and 0 deletions

2
chrono/utils/date.py Normal file
View File

@ -0,0 +1,2 @@
def get_weekday_index(datetime):
return (datetime.day - 1) // 7 + 1

23
tests/test_utils.py Normal file
View File

@ -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