Skip to content

Improve datetime_from_string robustness and coverage#158

Merged
jinskeep-morpc merged 2 commits into
mainfrom
improve-datetime-parsing
Jul 7, 2026
Merged

Improve datetime_from_string robustness and coverage#158
jinskeep-morpc merged 2 commits into
mainfrom
improve-datetime-parsing

Conversation

@jinskeep-morpc

Copy link
Copy Markdown
Collaborator

Summary

Reworks morpc.utils.datetime_from_string to catch a wider range of date/datetime representations and fixes several correctness bugs. Scope is limited to that one function plus the new dateparser dependency.

Bugs fixed

  • Float epoch bug: floats were always treated as Unix seconds (so 20210310.0 → 1970). Int and float now share one digit-magnitude dispatch (19=ns, 13=ms, 10=s, 8=YYYYMMDD, 6=YYYYMM).
  • int/str inconsistency: 20210310 (int) used to raise while '20210310' (str) parsed. They now resolve identically.
  • Mixed tz-awareness: ISO-with-offset returned tz-aware while everything else was naive. All output is now tz-naive (wall-clock preserved).

Coverage / cleanup

  • Removed the redundant, overly strict hand-rolled ISO regex — pandas validates/parses ISO 8601 directly.
  • Ambiguous dates (10/2/2020) parse month-first (US) explicitly via dateutil.
  • Added dateparser as a graceful optional fallback for natural-language / relative / localized strings (skipped if not installed).
  • Added a docstring documenting supported formats and behavior.
  • Declared dateparser in pyproject.toml.

Behavior change to note

Timezone-aware inputs (e.g. ISO strings with an offset) previously returned tz-aware datetimes; they now return naive (dropping the offset, preserving the written wall-clock). This is the backward-compatible choice for naive pipelines but does change ISO-offset output — flagging in case UTC-normalization is preferred instead.

Testing

Not execution-tested in the dev container (pandas/dateparser not installed there). Pure-Python digit/regex logic verified and the file byte-compiles. Please run pytest in a full env to confirm the parser paths.

🤖 Generated with Claude Code

jinskeep-morpc and others added 2 commits July 7, 2026 09:19
Rework the parser to catch a wider range of string/numeric date
representations and fix correctness bugs:

- Handle float inputs by digit-magnitude (19=ns,13=ms,10=s,8=YYYYMMDD,
  6=YYYYMM) instead of always assuming Unix seconds; unify int/float/str
  numeric handling so 20210310 and '20210310' resolve identically.
- Drop the redundant/overly strict hand-rolled ISO regex; let pandas
  validate and parse ISO 8601.
- Parse ambiguous dates month-first (US) explicitly via dateutil.
- Add dateparser as an optional final fallback for natural-language,
  relative, and localized strings (skipped gracefully if not installed).
- Normalize all output to tz-naive (preserving wall-clock) so results
  never mix aware and naive datetimes.
- Add a docstring documenting supported formats and behavior.

Declare dateparser in pyproject dependencies.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@jinskeep-morpc jinskeep-morpc merged commit 24d7743 into main Jul 7, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant