home · contact · privacy
For date range inputs, allow "yesterday" and "tomorrow" next to "today".
authorChristian Heller <c.heller@plomlompom.de>
Thu, 30 May 2024 06:17:54 +0000 (08:17 +0200)
committerChristian Heller <c.heller@plomlompom.de>
Thu, 30 May 2024 06:17:54 +0000 (08:17 +0200)
plomtask/dating.py

index e143fb652991780b994ef788b32a23c66b849120..711da95efabfc97153cca3c5d85eb63348244397 100644 (file)
@@ -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: