Skip to content

Commit

Permalink
Fix standalone days of week
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 669223129
  • Loading branch information
clementbera authored and Javac Team committed Aug 30, 2024
1 parent 889f6a3 commit d34381b
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -398,8 +398,8 @@ private Object createStore(TemporalField field, Locale locale) {
// }
DateFormatSymbols symbols = DateFormatSymbols.getInstance(locale);

// For desugar: Include standalone formats.
DesugarDateTimeTextProviderHelper.fillWithStandaloneStyleMap(styleMap, symbols, locale);
// For desugar: Include standalone formats.
DesugarDateTimeTextProviderHelper.fillWithStandaloneMonthStyleMap(styleMap, symbols, locale);

Map<Long, String> longMap = new HashMap<>();
Map<Long, String> narrowMap = new HashMap<>();
Expand Down Expand Up @@ -458,6 +458,11 @@ private Object createStore(TemporalField field, Locale locale) {
// }
// }
DateFormatSymbols symbols = DateFormatSymbols.getInstance(locale);

// For desugar: Include standalone formats.
DesugarDateTimeTextProviderHelper.fillWithStandaloneDayOfWeekStyleMap(
styleMap, symbols, locale);

Map<Long, String> longMap = new HashMap<>();
String[] longSymbols = symbols.getWeekdays();
// per getWeekdays() javadoc Calendar.MONDAY etc. are supposed to be present
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
/** Utilities for {@link DateTimeTextProvider}. */
public class DesugarDateTimeTextProviderHelper {

public static void fillWithStandaloneStyleMap(
public static void fillWithStandaloneMonthStyleMap(
Map<TextStyle, Map<Long, String>> styleMapMap,
DateFormatSymbols dateFormatSymbols,
Locale loc) {
Expand Down Expand Up @@ -77,5 +77,47 @@ private static String firstCodePoint(String string) {
return string.substring(0, Character.charCount(string.codePointAt(0)));
}

public static void fillWithStandaloneDayOfWeekStyleMap(
Map<TextStyle, Map<Long, String>> styleMapMap,
DateFormatSymbols dateFormatSymbols,
Locale loc) {

int numDaysOfWeek = dateFormatSymbols.getWeekdays().length;

Map<Long, String> longStandAloneMap = new LinkedHashMap<>();
Map<Long, String> narrowStandAloneMap = new LinkedHashMap<>();
Map<Long, String> shortStandAloneMap = new LinkedHashMap<>();

String longDay = "cccc";
String shortDay = "ccc";

for (int i = 1; i <= numDaysOfWeek; i++) {
String longName = computeStandaloneDayOfWeekName(i, longDay, loc);
longStandAloneMap.put((long) i, longName);
narrowStandAloneMap.put((long) i, firstCodePoint(longName));
String shortName = computeStandaloneDayOfWeekName(i, shortDay, loc);
shortStandAloneMap.put((long) i, shortName);
}

if (numDaysOfWeek > 0) {
styleMapMap.put(TextStyle.FULL_STANDALONE, longStandAloneMap);
styleMapMap.put(TextStyle.NARROW_STANDALONE, narrowStandAloneMap);
styleMapMap.put(TextStyle.SHORT_STANDALONE, shortStandAloneMap);
}
}

private static String computeStandaloneDayOfWeekName(
int id, String standalonePattern, Locale loc) {
TimeZone legacyUtc = TimeZone.getTimeZone("UTC");
SimpleDateFormat writer = new SimpleDateFormat(standalonePattern, loc);
writer.setTimeZone(legacyUtc);
Calendar calendar = Calendar.getInstance();
calendar.setTimeZone(legacyUtc);
// Jan 1st 2016 is a Monday.
calendar.set(2016, 1, id, 0, 0, 0);
Date legacy = calendar.getTime();
return writer.format(legacy);
}

private DesugarDateTimeTextProviderHelper() {}
}

0 comments on commit d34381b

Please sign in to comment.