home
·
contact
·
privacy
projects
/
plomtask
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Add date validation for Days.
[plomtask]
/
plomtask
/
days.py
diff --git
a/plomtask/days.py
b/plomtask/days.py
index 8db9f15b7362ede61c70265e4d5eb8a4c2c0c626..4e0c4747809bc1e352e492cf619fafb41f3f0bbb 100644
(file)
--- a/
plomtask/days.py
+++ b/
plomtask/days.py
@@
-1,16
+1,27
@@
-#!/usr/bin/env python3
"""Collecting Day and date-related items."""
from datetime import datetime
"""Collecting Day and date-related items."""
from datetime import datetime
+from plomtask.misc import HandledException
DATE_FORMAT = '%Y-%m-%d'
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
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):
@property
def weekday(self):