Repository navigation
Convert tz-aware time values in format_time instead of relabeling them - #1522
Open
StrikeRougeAgent wants to merge 1 commit into
Open
StrikeRougeAgent wants to merge 1 commit into
StrikeRougeAgent wants to merge 1 commit into
Conversation
format_time() replaced the tzinfo of a timezone-aware datetime.time without converting the wall time, so 12:00+02:00 formatted as "12:00 UTC" while the equivalent datetime formatted as "10:00". Anchor the wall time to today's date and convert it, mirroring the datetime code path. Naive times keep the existing label-only behaviour. Refs python-babel#545
This branch has not been deployed
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.
What this fixes
format_time()throws away the UTC offset of a timezone-awaredatetime.timeand replaces it with the
tzinfoargument without adjusting the clock:The same value passed as a
datetimealready converts correctly(
format_time(datetime(2020, 1, 1, 12, 0, tzinfo=+02:00), ...)→"10:00"), sothe two accepted input types disagreed about what
tzinfomeans.Fixes #545.
Root cause
babel/dates.py::_get_time():datetime.timehas noastimezone(), so the original implementation avoidedconversion altogether. For a naive time that
replace()is the only thing itcan be (there is no offset to apply), but for an aware time it is not a no-op:
it discards the input's offset and prints the wall time under a different zone
name.
Why this approach
timecarries nodate, and the offset of a DST zone depends on one, so a reference date is
unavoidable.
datetime.date.today()is what the reporter proposed in theissue thread and what
format_datetimeeffectively does when it converts adatetime; it is also deterministic under the repo'sfreeze_time()helper,so the tests pin it.
tzinfofor aware times / keep relabeling. That preserves theliteral wording of the old docstring, but it keeps a silent data loss: the
caller asked for a zone, the value states a different one, and the output
shows the wrong instant. I updated the docstring instead of enshrining it.
utcoffset(). Forzoneinfoandpytza baretimehas no date to resolve DST, soutcoffset()/astimezoneon the timealone cannot be used; the anchored datetime is the smallest correct unit.
times today, and the issue asks for the conversion rather than an error.
Naive times are untouched: they are still labeled with
tzinfo, not converted.The documented examples (all naive) and the existing
test_format_timecaseskeep passing unchanged.
Changes
babel/dates.py_get_time(): remember whether the input was already tz-aware; convert viaan anchored datetime (with
pytz.normalize()when available, mirroring thedatetimebranch); keepreplace()for naive inputs.format_time()docstring: the old paragraph said timezone calculations arenever performed for
timevalues; it now documents the reference-datebehaviour for aware values and the unchanged handling of naive ones.
tests/test_dates.pytest_format_time_with_tz_aware_time— fixed offsets, always runnablewithout a system tz database.
test_format_time_with_tz_aware_time_and_dst—pytzwinter conversionunder
freeze_time("2023-01-01"), so the reference date is pinned.Validation
pytest tests/test_dates.py -k tz_aware_time→ 2 passed.pytest -q→ 8277 passed, 9 failed, 9 errors (with the patch) vs 8275 passed, 9 failed, 9 errors (unmodified tree, same 9zoneinfocases). (identical failure set; see below).zoneinfocases: thecontainer has no system tz database, so
ZoneInfo("Europe/Paris")raisesZoneInfoNotFoundErroron the unmodified tree as well (verified withgit stash).Limitations
converting a value that has no date. That is now stated in the docstring.
CHANGES.rstis written at release time in this repo.action_requireduntil amaintainer approves the run.
Prepared and validated by
StrikeRougeAgent(autonomous agent); every commandand result above comes from a local run on this branch.