From: Christian Heller Date: Thu, 30 May 2024 06:17:54 +0000 (+0200) Subject: For date range inputs, allow "yesterday" and "tomorrow" next to "today". X-Git-Url: https://plomlompom.com/repos/%7B%7Bdb.prefix%7D%7D/%22https:/validator.w3.org/task?a=commitdiff_plain;h=8945aa6762977c04538c49c320417311b38232be;p=plomtask For date range inputs, allow "yesterday" and "tomorrow" next to "today". --- diff --git a/plomtask/dating.py b/plomtask/dating.py index e143fb6..711da95 100644 --- a/plomtask/dating.py +++ b/plomtask/dating.py @@ -1,5 +1,5 @@ """Various utilities for handling dates.""" -from datetime import datetime +from datetime import datetime, timedelta from plomtask.exceptions import BadFormatException DATE_FORMAT = '%Y-%m-%d' @@ -8,9 +8,18 @@ MAX_RANGE_DATE = '2030-12-31' def valid_date(date_str: str) -> str: - """Validate date against DATE_FORMAT or 'today', return in DATE_FORMAT.""" + """Validate date against DATE_FORMAT or 'today'/'yesterday'/'tomorrow. + + In any case, returns in DATE_FORMAT. + """ if date_str == 'today': date_str = todays_date() + elif date_str == 'yesterday': + date = datetime.now() - timedelta(days=1) + date_str = date.strftime(DATE_FORMAT) + elif date_str == 'tomorrow': + date = datetime.now() + timedelta(days=1) + date_str = date.strftime(DATE_FORMAT) try: dt = datetime.strptime(date_str, DATE_FORMAT) except (ValueError, TypeError) as e: