Skip to content
All projects
Core engineering libraryData Manager

date_utils.py — Date Engine and Power BI Calendar

BCHPR · all projects · 2023 – present

5,220 lines handling the dates that actually arrive: French month names from Francophone Cameroon, Excel serials, paper forms re-entered at the wrong precision, and REDCap fields whose _dmy suffix controls display only — the API returns ISO regardless, so a hand-exported Excel file parses differently from an API pull.

Highlights

  • 47 parse formats across a priority / secondary / tertiary fallback chain, with an LRU cache on the scalar path.
  • French months and ordinals (janvier, 1er) mapped to English before parsing, so a date resolves identically on a fr_FR workstation and a C-locale cron host.
  • Excel's phantom 1900 leap day handled explicitly rather than absorbed as an off-by-one.
  • A documented inconsistency closed: parse_date defaulted month-first while safe_to_datetime defaulted day-first, so the same ambiguous string returned two different dates depending which function you called.
  • Ordered fallback profiles for REDCap precision mismatches — a date-only value in a datetime field, or seconds data in a minute field — deliberately disabled for time-only types, where retrying as a date produces silent garbage.
  • A 93-column Power BI date dimension with fiscal periods, Cameroon holidays, and tropical dry / rainy seasons.
  • ISO week labels carry a two-digit year because Power BI's sort-by-column rejects a label that maps to two sort values — exactly what 29–31 December does.