From 702917ab07c86e1700ff50f000dec1e5070c48e1 Mon Sep 17 00:00:00 2001 From: Faerbit Date: Mon, 2 Mar 2020 21:53:19 +0100 Subject: [PATCH] Improve previous next algorithm. --- data.py | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/data.py b/data.py index 1471e1f..3edc58a 100644 --- a/data.py +++ b/data.py @@ -160,6 +160,9 @@ class Report: self._data = data self._date = pdate self._actual_data_len = 0 + self._prev = None + self._next = None + self._update_prev_next() def report(self): tmp = [] @@ -223,18 +226,36 @@ class Report: month[self._date.strftime("%d")] = save_list self._data[self._date.strftime("%Y-%m")] = month + def _update_prev_next(self): + self._prev = None + self._next = None + for i in range(1, 32): + new_date = self._date - timedelta(days=i) + if new_date.strftime("%Y-%m") not in self._data: + break + if new_date.strftime("%d") in self._data[new_date.strftime("%Y-%m")]: + self._prev = new_date + break + for i in range(1, 32): + new_date = self._date + timedelta(days=i) + if new_date > date.today(): + break + if new_date.strftime("%Y-%m") not in self._data: + break + if new_date.strftime("%d") in self._data[new_date.strftime("%Y-%m")]: + self._next = new_date + break + def prev_next_avail(self): - if self._date.strftime("%Y-%m") not in self._data: - return False, False - prev = (self._date - timedelta(days=1)).strftime("%d") in self._data[self._date.strftime("%Y-%m")] - _next = (self._date + timedelta(days=1)).strftime("%d") in self._data[self._date.strftime("%Y-%m")] - return prev, _next + return self._prev is not None, self._next is not None def previous(self): - self._date = self._date - timedelta(days=1) + self._date = self._prev + self._update_prev_next() def next(self): - self._date = self._date + timedelta(days=1) + self._date = self._next + self._update_prev_next() def date(self): return self._date.strftime("%Y-%m-%d")