diff options
Diffstat (limited to 'ppad/lib')
-rw-r--r-- | ppad/lib/__init__.py | 0 | ||||
-rw-r--r-- | ppad/lib/util.py | 45 |
2 files changed, 45 insertions, 0 deletions
diff --git a/ppad/lib/__init__.py b/ppad/lib/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/ppad/lib/__init__.py diff --git a/ppad/lib/util.py b/ppad/lib/util.py new file mode 100644 index 0000000..0a97141 --- /dev/null +++ b/ppad/lib/util.py @@ -0,0 +1,45 @@ +import datetime +from dateutil import parser +import pytz +import time + + +def parse_date(date: str) -> datetime: + dt = parser.isoparse(date) + + if dt.tzinfo is not None: + return dt + + default_tz = pytz.timezone(time.tzname[time.daylight]) + return default_tz.localize(dt) + + +def parse_argv(argv: list[str]) -> tuple[datetime, datetime]: + if len(argv) == 1: + return None, None + + date_to: datetime = None + date_from: datetime = None + from_str: str + to_str: str + + span = argv[1].split('~') + if len(span) == 1: + from_str = to_str = span[0] + else: + [from_str, to_str, *_] = span + + if from_str: + date_from = parse_date(from_str) + + if to_str: + date_to = parse_date(to_str) + + if date_from is None and date_to is None: + # probably `argv` would be only character '~' + return None, None + + if date_from == date_to: + date_to = date_to + datetime.timedelta(days=1) + + return date_from, date_to |