Repository navigation
ZmanimFormatter.formatInstant: format in the ZoneId passed in - #302
Merged
Merged
Conversation
The DateTimeFormatter created by the constructor has the formatter's zone as its override zone, and an override zone converts the ZonedDateTime built from the zoneId argument back to the formatter's zone, so the argument was ignored (a UTC formatter returned the UTC time for every zoneId). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015Er4Sx7LnKzzCbgDEatxkU
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.
The issue
ZmanimFormatter.formatInstant(Instant instant, ZoneId zoneId)ignores itszoneIdargument and always formats in the formatter's own zone.The edge case
Any call where
zoneIdis different from the zone theZmanimFormatterwas created with. One formatter is often reused to show times for several locations, or created with UTC.What happens
The constructor stores
dateTimeFormatter.withZone(zoneId), so the formatter has an override zone.formatInstantbuildsinstant.atZone(zoneId)and formats it, but aDateTimeFormatterwith an override zone converts aZonedDateTimeto the override zone before formatting. So the requested zone is silently replaced:The time shown is off by the difference between the two zones, and nothing indicates an error. With a formatter whose zone differs from the location's, every zman displayed through
formatInstantis wrong.The fix
Format with
getDateTimeFormatter().withZone(zoneId), so the requested zone becomes the override zone. A formatter set throughsetDateTimeFormatterwithout a zone also works, and anullzoneIdstill throws as before (frominstant.atZone).Tests
ZmanimFormatterFormatInstantTest:formatsInTheRequestedZone: A UTC formatter formats in UTC,America/New_YorkandPacific/Kiritimati. This fails without the fix.worksWithAFormatterSetWithoutAZone: A formatter set withsetDateTimeFormatterand no zone.🤖 Generated with Claude Code
https://claude.ai/code/session_015Er4Sx7LnKzzCbgDEatxkU