Improve previous next algorithm.

This commit is contained in:
Faerbit 2020-03-02 21:53:19 +01:00
parent 6f3b74099e
commit 702917ab07

35
data.py
View File

@ -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")