Skip to content
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

Explicitly use ISO weekfields for weekyear date formats #113787

Open
wants to merge 2 commits into
base: 8.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ Function<String, ZonedDateTime> getFunction(String format, ZoneId zoneId, Locale
// fill the rest of the date up with the parsed date
if (accessor.isSupported(ChronoField.YEAR) == false
&& accessor.isSupported(ChronoField.YEAR_OF_ERA) == false
&& accessor.isSupported(WeekFields.ISO.weekBasedYear()) == false
Copy link
Member Author

Choose a reason for hiding this comment

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

These additions, and similar, are because ISO.weekBasedYear is a different thing to of(locale).weekBasedYear, even though they probably represent the same thing

&& accessor.isSupported(WeekFields.of(locale).weekBasedYear()) == false
&& accessor.isSupported(ChronoField.INSTANT_SECONDS) == false) {
int year = LocalDate.now(ZoneOffset.UTC).getYear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ private static DateFormatter newDateFormatter(String format, DateTimeFormatter p
);
}

public static final WeekFields WEEK_FIELDS_ROOT = WeekFields.of(Locale.ROOT);
public static final WeekFields WEEK_FIELDS_ROOT = WeekFields.ISO;

private static final DateTimeFormatter TIME_ZONE_FORMATTER_NO_COLON = new DateTimeFormatterBuilder().appendOffset("+HHmm", "Z")
.toFormatter(Locale.ROOT)
Expand Down Expand Up @@ -2391,6 +2391,7 @@ public static ZonedDateTime from(TemporalAccessor accessor, Locale locale, ZoneI
boolean isLocalTimeSet = localTime != null;

// the first two cases are the most common, so this allows us to exit early when parsing dates
WeekFields localeWeekFields;
if (isLocalDateSet && isLocalTimeSet) {
return of(localDate, localTime, zoneId);
} else if (accessor.isSupported(ChronoField.INSTANT_SECONDS) && accessor.isSupported(NANO_OF_SECOND)) {
Expand All @@ -2409,17 +2410,18 @@ public static ZonedDateTime from(TemporalAccessor accessor, Locale locale, ZoneI
} else if (accessor.isSupported(MONTH_OF_YEAR)) {
// missing year, falling back to the epoch and then filling
return getLocalDate(accessor, locale).atStartOfDay(zoneId);
} else if (accessor.isSupported(WeekFields.of(locale).weekBasedYear())) {
return localDateFromWeekBasedDate(accessor, locale).atStartOfDay(zoneId);
} else if (accessor.isSupported(WeekFields.ISO.weekBasedYear())) {
return localDateFromWeekBasedDate(accessor, locale, WeekFields.ISO).atStartOfDay(zoneId);
} else if (accessor.isSupported((localeWeekFields = WeekFields.of(locale)).weekBasedYear())) {
return localDateFromWeekBasedDate(accessor, locale, localeWeekFields).atStartOfDay(zoneId);
}

// we should not reach this piece of code, everything being parsed we should be able to
// convert to a zoned date time! If not, we have to extend the above methods
throw new IllegalArgumentException("temporal accessor [" + accessor + "] cannot be converted to zoned date time");
}

private static LocalDate localDateFromWeekBasedDate(TemporalAccessor accessor, Locale locale) {
WeekFields weekFields = WeekFields.of(locale);
private static LocalDate localDateFromWeekBasedDate(TemporalAccessor accessor, Locale locale, WeekFields weekFields) {
if (accessor.isSupported(weekFields.weekOfWeekBasedYear())) {
return LocalDate.ofEpochDay(0)
.with(weekFields.weekBasedYear(), accessor.get(weekFields.weekBasedYear()))
Expand Down Expand Up @@ -2461,8 +2463,11 @@ public String toString() {
};

private static LocalDate getLocalDate(TemporalAccessor accessor, Locale locale) {
if (accessor.isSupported(WeekFields.of(locale).weekBasedYear())) {
return localDateFromWeekBasedDate(accessor, locale);
WeekFields localeWeekFields;
if (accessor.isSupported(WeekFields.ISO.weekBasedYear())) {
return localDateFromWeekBasedDate(accessor, locale, WeekFields.ISO);
} else if (accessor.isSupported((localeWeekFields = WeekFields.of(locale)).weekBasedYear())) {
return localDateFromWeekBasedDate(accessor, locale, localeWeekFields);
} else if (accessor.isSupported(MONTH_OF_YEAR)) {
int year = getYear(accessor);
if (accessor.isSupported(DAY_OF_MONTH)) {
Expand Down