From 8945aa6762977c04538c49c320417311b38232be Mon Sep 17 00:00:00 2001 From: Christian Heller Date: Thu, 30 May 2024 08:17:54 +0200 Subject: [PATCH] For date range inputs, allow "yesterday" and "tomorrow" next to "today". --- plomtask/dating.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) 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: -- 2.30.2