Improve datetime_from_string robustness and coverage#158
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Reworks
morpc.utils.datetime_from_stringto catch a wider range of date/datetime representations and fixes several correctness bugs. Scope is limited to that one function plus the newdateparserdependency.Bugs fixed
20210310.0→ 1970). Int and float now share one digit-magnitude dispatch (19=ns, 13=ms, 10=s, 8=YYYYMMDD, 6=YYYYMM).20210310(int) used to raise while'20210310'(str) parsed. They now resolve identically.Coverage / cleanup
10/2/2020) parse month-first (US) explicitly viadateutil.dateparseras a graceful optional fallback for natural-language / relative / localized strings (skipped if not installed).dateparserinpyproject.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
pytestin a full env to confirm the parser paths.🤖 Generated with Claude Code