Improve date format heuristic for bank statement import (#4814)

When dates are ambiguous, use the event's locale to infer whether the
day or month comes first. Since regions follow certain date format
conventions, this helps make more accurate guesses.
This commit is contained in:
Kian Cross
2025-02-10 10:33:14 +00:00
committed by GitHub
parent 7e8ef47537
commit 5bf6980a7b
3 changed files with 35 additions and 3 deletions

View File

@@ -292,11 +292,11 @@ def _handle_transaction(trans: BankTransaction, matches: tuple, event: Event = N
trans.save()
def parse_date(date_str):
def parse_date(date_str, region=None):
try:
return dateutil.parser.parse(
date_str,
dayfirst="." in date_str,
dayfirst="." in date_str or region in ["GB"],
).date()
except (ValueError, OverflowError):
pass
@@ -339,7 +339,7 @@ def _get_unknown_transactions(job: BankImportJob, data: list, event: Event = Non
external_id=row.get('external_id'),
currency=event.currency if event else job.currency)
trans.date_parsed = parse_date(trans.date)
trans.date_parsed = parse_date(trans.date, (event and event.settings.region) or (organizer and organizer.settings.region) or None)
trans.checksum = trans.calculate_checksum()
if trans.checksum not in known_checksums and (not trans.external_id or (trans.external_id, trans.date, trans.amount) not in known_by_external_id):