"""Collecting Day and date-related items."""
from datetime import datetime
+from plomtask.misc import HandledException
DATE_FORMAT = '%Y-%m-%d'
+def date_valid(date: str):
+ """Validate date against DATE_FORMAT, return Datetime or None."""
+ try:
+ result = datetime.strptime(date, DATE_FORMAT)
+ except (ValueError, TypeError):
+ return None
+ return result
+
+
class Day:
"""Individual days defined by their dates."""
def __init__(self, date: str):
self.date = date
- self.datetime = datetime.strptime(date, DATE_FORMAT)
+ self.datetime = date_valid(self.date)
+ if not self.datetime:
+ raise HandledException(f'Given date of wrong format: {self.date}')
@property
def weekday(self):
"""Test days module."""
from unittest import TestCase
from plomtask.days import Day
+from plomtask.misc import HandledException
class DayTests(TestCase):
"""Tests for testing the days module."""
+ def test_Day_dates(self):
+ """Test Day's date format.."""
+ with self.assertRaises(HandledException):
+ Day('foo')
+ Day(None)
+ Day(3)
+ Day('2024-02-30')
+
def test_Day_weekday(self):
"""Test Day.weekday."""
self.assertEqual(Day('2024-03-17').weekday, 'Sunday')