aboutsummaryrefslogtreecommitdiffstats
path: root/hommaexceli_py/parser.py
diff options
context:
space:
mode:
authorJan Tuomi <jans.tuomi@gmail.com>2021-02-12 16:24:45 +0200
committerJan Tuomi <jans.tuomi@gmail.com>2021-02-12 16:24:45 +0200
commit909ce794a8d6d4c207d5b96c0200c8bde326e8be (patch)
treed9b6a10fc430b15c854ba181181d3f11154eee79 /hommaexceli_py/parser.py
parente153bf197a42030dd92fc91c672dbff2d1b0ce6d (diff)
Add date parsing
Diffstat (limited to 'hommaexceli_py/parser.py')
-rw-r--r--hommaexceli_py/parser.py45
1 files changed, 45 insertions, 0 deletions
diff --git a/hommaexceli_py/parser.py b/hommaexceli_py/parser.py
new file mode 100644
index 0000000..b4abb46
--- /dev/null
+++ b/hommaexceli_py/parser.py
@@ -0,0 +1,45 @@
+import datetime
+import logging
+import re
+from sheets_client import SheetsRow
+
+INTERVAL_ABBREVS = {
+ "pv": datetime.timedelta(days=1),
+ "vk": datetime.timedelta(days=7),
+ "kk": datetime.timedelta(days=30),
+ "v": datetime.timedelta(days=365),
+}
+
+
+def _calculate_interval(interval_str: str):
+ match = re.search(r"(\d+)(\w+)", interval_str)
+ number = int(match[1])
+ abbrev = match[2]
+
+ try:
+ interval = INTERVAL_ABBREVS[abbrev]
+ except KeyError:
+ raise Exception(f'"{interval_str}" is an invalid interval string!')
+
+ total_interval = datetime.timedelta(seconds=interval.total_seconds() * number)
+ return total_interval
+
+
+def _timestamp_to_date(timestamp: str):
+ return datetime.datetime.strptime(timestamp, "%Y-%m-%d")
+
+
+def _date_to_timestamp(date):
+ return datetime.datetime.strftime(date, "%Y-%m-%d")
+
+
+def _process_row(row: SheetsRow):
+ interval = _calculate_interval(row.interval)
+ last_done_date = _timestamp_to_date(row.last_done)
+ next_date = last_done_date + interval
+ next_date_str = _date_to_timestamp(next_date)
+ return (row.last_done, row.interval, next_date_str, row.name)
+
+
+def process_rows(rows: list[SheetsRow]):
+ return map(_process_row, rows)