Skip to content

fix: replace fragile datetime string slicing with robust parser#20

Merged
dtsong merged 4 commits intomasterfrom
fix/datetime-parsing
Mar 2, 2026
Merged

fix: replace fragile datetime string slicing with robust parser#20
dtsong merged 4 commits intomasterfrom
fix/datetime-parsing

Conversation

@dtsong
Copy link
Owner

@dtsong dtsong commented Mar 2, 2026

Summary

  • Replace hardcoded res[:23] truncation in base.py with a _parse_datetime() helper that truncates sub-microsecond precision to 6 digits before calling fromisoformat()
  • Preserves microsecond precision (previously truncated to milliseconds) and handles variable-length fractional seconds, timezone suffixes, and trailing whitespace
  • Add unit tests covering standard, millisecond, no-fractional, nanosecond, whitespace, timezone, and invalid input cases

Closes #6

Test plan

  • uv run python -m pytest tests/test_datetime_parsing.py -v — 7/7 new tests pass
  • uv run python -m pytest tests/test_query.py -v — 16/16 existing tests pass
  • No remaining res[:23] in base.py

🤖 Generated with Claude Code

Replace hardcoded `res[:23]` truncation with `_parse_datetime()` helper
that preserves microsecond precision and handles variable-length
fractional seconds, timezone suffixes, and trailing whitespace.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 2d81900931

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

frac_digits = frac_end - dot - 1
if frac_digits > 6:
s = s[: dot + 7] + s[frac_end:]
return datetime.fromisoformat(s)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Normalize trailing Z suffix before parsing datetime

pyproject.toml supports Python 3.10, but datetime.fromisoformat() on 3.10 rejects RFC3339 UTC strings ending in Z. _parse_datetime() now keeps timezone suffixes after truncating fractional digits, so inputs like 2022-06-03T12:24:35.123456789Z raise ValueError in supported runtimes when a driver returns UTC timestamps in Z form; the previous res[:23] path dropped that suffix and parsed successfully. This turns previously accepted query results into runtime failures for query(..., datetime).

Useful? React with 👍 / 👎.

dtsong and others added 3 commits March 2, 2026 00:38
- Normalize Z suffix to +00:00 for Python 3.10 compatibility
- Add error logging at call site before ValueError propagates
- Fix incorrect skipif guard on timezone test (+HH:MM works on 3.7+)
- Add tests: Z suffix, 7-digit boundary, leading whitespace,
  nanoseconds with timezone, empty string

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- Remove skipif on test_nanosecond_precision_with_timezone — after
  truncation the string is valid on Python 3.10
- Add test_nanosecond_precision_with_z_suffix (combined Z + truncation)
- Add test_trailing_dot_raises (malformed fractional seconds)
- Remove unused sys import

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- Expand docstring to document all normalizations (whitespace, Z suffix,
  truncation)
- Add defensive guards for empty/missing datetime results, matching the
  existing int path
- Enrich error log with database name and SQL context
- Add tests: all-nines truncation boundary, negative timezone offset

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@dtsong dtsong merged commit 33f22a9 into master Mar 2, 2026
6 checks passed
@dtsong dtsong deleted the fix/datetime-parsing branch March 2, 2026 08:48
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.

Fragile datetime parsing via string slicing

1 participant