Fix _format_api_dates: parse ISO 8601 datetimes#247
Draft
thodson-usgs wants to merge 1 commit intoDOI-USGS:mainfrom
Draft
Fix _format_api_dates: parse ISO 8601 datetimes#247thodson-usgs wants to merge 1 commit intoDOI-USGS:mainfrom
thodson-usgs wants to merge 1 commit intoDOI-USGS:mainfrom
Conversation
A single ISO 8601 datetime such as "2018-02-12T23:20:50Z" — the format the docstrings of every getter cite as a valid `time` example — was silently dropped to None. The function only tried "%Y-%m-%d %H:%M:%S" and "%Y-%m-%d", neither of which matches a string with a `T` separator and trailing `Z`. `_construct_api_requests` then set `params["time"] = None`, requests dropped the param, and the user's time filter was silently discarded — the API fell back to "most recent year". Replace the cascading try/except with a single helper that walks the supported formats in order: ISO 8601 with offset and/or fractional seconds, plain ISO 8601, the legacy space-separated form, and date-only. Tz-aware inputs convert directly to UTC; naive inputs continue to be interpreted in the runner's local zone for backwards compatibility. Also tighten the duration / interval pass-through. The previous `re.search(r"P", ..., re.IGNORECASE)` matched any string containing a `p` or `P` anywhere — words like "Apr" or "sample" would skip parsing entirely. Anchor the check to `^[Pp]\d`. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This was referenced May 4, 2026
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
A single ISO 8601 datetime such as
\"2018-02-12T23:20:50Z\"— the format the docstrings of every getter cite as a validtimeexample — was silently dropped toNone. The function only tried\"%Y-%m-%d %H:%M:%S\"and\"%Y-%m-%d\", neither of which matches a string with aTseparator and trailingZ._construct_api_requeststhen setparams[\"time\"] = None,requestsdropped the param, and the user's time filter was silently discarded — the API fell back to "most recent year".This PR replaces the cascading try/except with a single helper that walks the supported formats in order: ISO 8601 with offset and/or fractional seconds, plain ISO 8601, the legacy space-separated form, and date-only. Tz-aware inputs convert directly to UTC; naive inputs continue to be interpreted in the runner's local zone for backwards compatibility.
It also tightens the duration / interval pass-through. The previous
re.search(r\"P\", ..., re.IGNORECASE)matched any string containing aporPanywhere — words like\"Apr\"or\"sample\"would skip parsing entirely. The check is now anchored to^[Pp]\\d.Test plan
tests/waterdata_utils_test.pycover: ISO 8601 withZ; with fractional seconds; with numeric offset; pair of ISO 8601 datetimes; passthrough of intervals / durations; words containingpare no longer mis-parsed; legacy space-separated form still works; date-only output.Related PRs
Other open PRs in this bug-review series that touch
dataretrieval/waterdata/utils.py(different functions, no functional conflicts):_format_api_datesaccept ISO 8601._arrange_colsstop mutating caller list._handle_stats_nestingtolerate missing drop columns.