-
Notifications
You must be signed in to change notification settings - Fork 25.6k
RFC 9557 : Add support for parsing ISO date time with zone-id #130054
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| pr: 130054 | ||
| summary: Add support for parsing ISO date time with zone-id (RFC 9557) | ||
| area: Mapping | ||
| type: enhancement | ||
| issues: [] |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -450,7 +450,7 @@ private ZoneId parseZoneId(CharSequence str, int pos) { | |
|
|
||
| boolean positive; | ||
| switch (first) { | ||
| case '+' -> positive = true; | ||
| case '+', 'Z' -> positive = true; | ||
| case '-' -> positive = false; | ||
| default -> { | ||
| // non-trivial zone offset, fallback on the built-in java zoneid parser | ||
|
|
@@ -462,6 +462,9 @@ private ZoneId parseZoneId(CharSequence str, int pos) { | |
| } | ||
| } | ||
| pos++; // read the + or - | ||
| if (str.charAt(pos) == '[' && str.charAt(len - 1) == ']') { | ||
| return parseRawZoneId(str, pos); | ||
| } | ||
|
|
||
| Integer hours = parseInt(str, pos, pos += 2); | ||
| if (hours == null || hours > 23) return null; | ||
|
|
@@ -477,6 +480,10 @@ private ZoneId parseZoneId(CharSequence str, int pos) { | |
| if (minutes == null || minutes > 59) return null; | ||
| if (len == pos) return ofHoursMinutesSeconds(hours, minutes, 0, positive); | ||
|
|
||
| if (str.charAt(pos) == '[' && str.charAt(len - 1) == ']') { | ||
| return parseRawZoneId(str, pos); | ||
| } | ||
|
|
||
| // either both dividers have a colon, or neither do | ||
| if ((str.charAt(pos) == ':') != hasColon) return null; | ||
| if (hasColon) { | ||
|
|
@@ -487,10 +494,32 @@ private ZoneId parseZoneId(CharSequence str, int pos) { | |
| if (seconds == null || seconds > 59) return null; | ||
| if (len == pos) return ofHoursMinutesSeconds(hours, minutes, seconds, positive); | ||
|
|
||
| if (str.charAt(pos) == '[' && str.charAt(len - 1) == ']') { | ||
| return parseRawZoneId(str, pos); | ||
| } | ||
|
|
||
| // there's some text left over... | ||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * Parses a raw zone id, which is a string of the form [zoneId] (eg [Europe/Paris]). | ||
| * | ||
| * @param str The string to parse | ||
| * @param pos The position in the string where the zone id starts (the first character after the opening [) | ||
| * @return The parsed zone id, or {@code null} if the string is not a valid zone id. | ||
| */ | ||
| private ZoneId parseRawZoneId(CharSequence str, int pos) { | ||
| try { | ||
| String zoneId = str.subSequence(pos + 1, str.length() - 1).toString(); | ||
| // Try to resolve short zone ids to offsets. | ||
| zoneId = ZoneId.SHORT_IDS.getOrDefault(zoneId, zoneId); | ||
| return ZoneId.of(zoneId); | ||
| } catch (DateTimeException e) { | ||
| return null; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about unknown suffixes, or suffixes representing features like e.g. calendars? (for example, |
||
| } | ||
| } | ||
|
|
||
| /* | ||
| * ZoneOffset.ofTotalSeconds has a ConcurrentHashMap cache of offsets. This is fine, | ||
| * but it does mean there's an expensive map lookup every time we call ofTotalSeconds. | ||
|
|
||
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this could lead to some malformed strings being parsed, like
2022-07-08T00:14:07+[Europe/London]