From 3c78ab79ca14d71eb545fdb4e5aa641ad70d9786 Mon Sep 17 00:00:00 2001 From: Philip Sargent Date: Tue, 25 Jul 2023 01:33:58 +0300 Subject: [PATCH] better fix for variant date formats --- core/models/wallets.py | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/core/models/wallets.py b/core/models/wallets.py index 29ca9e2..c0a8664 100644 --- a/core/models/wallets.py +++ b/core/models/wallets.py @@ -21,6 +21,7 @@ def make_valid_date(date): datestr = date.replace(".", "-") try: samedate = datetime.date.fromisoformat(datestr) + return samedate except ValueError: # Could be in std euro format e.g. 14/07/2023 match = re.search(r'(\d{1,2})/(\d{1,2})/(\d{2,4})', datestr) @@ -37,19 +38,22 @@ def make_valid_date(date): except: print(f"! - Fail, tried to decompose date in dd/mm/yyyy format but failed: {datestr=} ") return None - # probably a single digit day number. HACKUS MAXIMUS. - datestr = datestr[:-1] + "0" + datestr[-1] - # datestr = f"{datestr:02d}" - print(f"! - ValueError, trying.. {datestr=} ") - try: - samedate = datetime.date.fromisoformat(datestr) - except: + # probably a single digit day number or month number + match = re.search(r'(\d{4})-(\d{1,2})-(\d{1,2})', datestr) + if match: + y = int(match.group(1)) + m = int(match.group(2)) + d = int(match.group(3)) try: - samedate = datetime.date.fromisoformat(datestr[:10]) + samedate = datetime.date(y, m, d) + print(f"- - Warning, 1 digit only for month or day '{datestr=}' but we coped: {samedate.isoformat()} ") + return samedate except: - print(f"! - ValueError, FAILED {datestr=} ") - samedate = None - return samedate + print(f"! - Fail, tried to decompose date in yyyy-mm-d or yyy-m-dd format but failed: {datestr=} ") + return None + + print(f"! - Failed to understand date, none of our tricks worked {datestr=} ") + return None class Wallet(models.Model): """We do not keep the JSON values in the database, we query them afresh each time,