Tidy date handling.

This commit is contained in:
Chris Berkhout 2021-05-28 22:11:45 +02:00
parent 0eac8abea3
commit 569935a5e6
2 changed files with 6 additions and 8 deletions

View file

@ -86,17 +86,17 @@ def build_parser():
raise argparse.ArgumentTypeError(msg) raise argparse.ArgumentTypeError(msg)
def previous_valid_date(s): def previous_valid_date(s):
return str( return (
datetime.strptime(valid_date(s), "%Y-%m-%d").date() - timedelta(days=1) datetime.strptime(valid_date(s), "%Y-%m-%d").date() - timedelta(days=1)
) ).isoformat()
def following_valid_date(s): def following_valid_date(s):
return str( return (
datetime.strptime(valid_date(s), "%Y-%m-%d").date() + timedelta(days=1) datetime.strptime(valid_date(s), "%Y-%m-%d").date() + timedelta(days=1)
) ).isoformat()
def today(): def today():
return str(datetime.now().date()) return datetime.now().date().isoformat()
def formatter(prog): def formatter(prog):
return argparse.HelpFormatter(prog, max_help_position=50) return argparse.HelpFormatter(prog, max_help_position=50)

View file

@ -41,14 +41,12 @@ class ECB(BaseSource):
return [(f"EUR/{c}", f"Euro against {iso[c].name}") for c in currencies] return [(f"EUR/{c}", f"Euro against {iso[c].name}") for c in currencies]
def fetch(self, series): def fetch(self, series):
almost_90_days_ago = str(datetime.now().date() - timedelta(days=85)) almost_90_days_ago = (datetime.now().date() - timedelta(days=85)).isoformat()
root = self._data(series.start < almost_90_days_ago) root = self._data(series.start < almost_90_days_ago)
all_rows = [] all_rows = []
for day in root.cssselect("[time]"): for day in root.cssselect("[time]"):
date = day.attrib["time"] date = day.attrib["time"]
# TODO what if it's not found for that day?
# (some quotes aren't in the earliest data)
for row in day.cssselect(f"[currency='{series.quote}']"): for row in day.cssselect(f"[currency='{series.quote}']"):
rate = Decimal(row.attrib["rate"]) rate = Decimal(row.attrib["rate"])
all_rows.insert(0, (date, rate)) all_rows.insert(0, (date, rate))