Restart rework, for real this time ™️

This commit is contained in:
Stijn De Clercq 2021-07-23 21:03:14 +02:00
parent ec30228929
commit 49870d23eb
3 changed files with 63 additions and 144 deletions

58
functions/les_rework.py Normal file
View file

@ -0,0 +1,58 @@
from datetime import datetime, timedelta
from timeFormatters import dateTimeNow, weekdayToInt
from typing import Optional
def find_target_date(arg: Optional[str]) -> datetime:
"""
Find the requested date out of the user's arguments
"""
# Start at current date
day: datetime = dateTimeNow()
# If no offset was provided, check the time
# otherwise the argument overrides it
if arg is None:
# When the command is used after 6 pm, show schedule
# for the next day instead
if day.hour > 18:
day += timedelta(days=1)
elif 0 <= (weekday := weekdayToInt(arg)) <= 4: # Weekday provided
day = forward_to_weekday(day, weekday)
elif arg.lower() == "morgen": # Tomorrow's schedule
day += timedelta(days=1)
elif arg.lower() == "overmorgen": # Day after tomorrow's schedule
day += timedelta(days=2)
# Don't land on a weekend
day = skip_weekends(day)
return day
def skip_weekends(day: datetime) -> datetime:
"""
Increment the current date if it's not a weekday
"""
weekday = day.weekday()
# Friday is weekday 4
if weekday > 4:
return day + timedelta(days=(7 - weekday))
return day
def forward_to_weekday(day: datetime, weekday: int) -> datetime:
"""
Increment a date until the weekday is the same as the one provided
Finds the "next" [weekday]
"""
current = day.weekday()
# This avoids negative numbers below, and shows
# next week in case the days are the same
if weekday >= current:
weekday += 7
return day + timedelta(days=(weekday - current))

View file

@ -134,8 +134,12 @@ def getPlural(amount, unit):
return dic[unit.lower()]["s" if amount == 1 else "p"]
def weekdayToInt(day):
def weekdayToInt(day) -> int:
days = {"maandag": 0, "dinsdag": 1, "woensdag": 2, "donderdag": 3, "vrijdag": 4, "zaterdag": 5, "zondag": 6}
if day.lower() not in days:
return -1
return days[day.lower()]