Skip to content
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 @@ -80,6 +80,47 @@ public void testDateTimeFormat() {
}
}

@Test
public void testDateTimeFormatMidnightHourCycles() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
return;
}

try (JSRuntime rt = JSRuntime.makeHermesRuntime()) {
rt.evaluateJavaScript(
"var midnight = new Date('2026-05-29T00:54:00Z');\n"
+ "function hourAtMidnight(locale, options) {\n"
+ " var formatter = new Intl.DateTimeFormat(locale, Object.assign({\n"
+ " timeStyle: 'short', timeZone: 'UTC'\n"
+ " }, options));\n"
+ " var parts = formatter.formatToParts(midnight);\n"
+ " var hour = parts.filter(function(part) {\n"
+ " return part.type === 'hour';\n"
+ " })[0].value;\n"
+ " var dayPeriod = parts.some(function(part) {\n"
+ " return part.type === 'dayPeriod';\n"
+ " });\n"
+ " return formatter.resolvedOptions().hourCycle + ':' + Number(hour)\n"
+ " + ':' + dayPeriod;\n"
+ "}\n"
+ "var midnightResults = [\n"
+ " hourAtMidnight('en-GB', {hourCycle: 'h11'}),\n"
+ " hourAtMidnight('en-GB', {hourCycle: 'h12'}),\n"
+ " hourAtMidnight('en-US', {hourCycle: 'h23'}),\n"
+ " hourAtMidnight('en-US', {hourCycle: 'h24'}),\n"
+ " hourAtMidnight('en-GB', {hour12: true}),\n"
+ " hourAtMidnight('en-US', {hour12: false}),\n"
+ " hourAtMidnight('en-GB-u-hc-h24', {hour12: true}),\n"
+ " hourAtMidnight('ja-JP', {hour12: true})\n"
+ "].join(',');");

assertThat(rt.getGlobalStringProperty("midnightResults"))
.isEqualTo(
"h11:0:true,h12:12:true,h23:0:false,h24:24:false,"
+ "h12:12:true,h23:0:false,h12:12:true,h11:0:true");
}
}

@Test
public void testDateTimeFormatCaseInsensitivity() {
try (JSRuntime rt = JSRuntime.makeHermesRuntime()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -409,17 +409,9 @@ private void initializeDateTimeFormat(List<String> locales, Map<String, Object>
}

if (!JSObjects.isUndefined(hour12)) {
if (JSObjects.getJavaBoolean(hour12)) { // true
if (hcDefault == IPlatformDateTimeFormatter.HourCycle.H11
|| hcDefault == IPlatformDateTimeFormatter.HourCycle.H23)
hc = IPlatformDateTimeFormatter.HourCycle.H11;
else hc = IPlatformDateTimeFormatter.HourCycle.H12;
} else {
if (hcDefault == IPlatformDateTimeFormatter.HourCycle.H11
|| hcDefault == IPlatformDateTimeFormatter.HourCycle.H23)
hc = IPlatformDateTimeFormatter.HourCycle.H23;
else hc = IPlatformDateTimeFormatter.HourCycle.H24;
}
hc =
mPlatformDateTimeFormatter.getPreferredHourCycle(
mResolvedLocaleObject, JSObjects.getJavaBoolean(hour12));
}
mHourCycle = hc;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,9 @@ String getDefaultCalendarName(ILocaleObject<?> mResolvedLocaleObject)

HourCycle getDefaultHourCycle(ILocaleObject<?> localeObject) throws JSRangeErrorException;

HourCycle getPreferredHourCycle(ILocaleObject<?> localeObject, boolean hour12)
throws JSRangeErrorException;

String getDefaultTimeZone(ILocaleObject<?> localeObject) throws JSRangeErrorException;

String getDefaultNumberingSystem(ILocaleObject<?> localeObject) throws JSRangeErrorException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,20 @@ else if (dateFormatPatternWithoutLiterals.contains(String.valueOf('H')))
return hourCycle;
}

@Override
public HourCycle getPreferredHourCycle(ILocaleObject<?> localeObject, boolean hour12)
throws JSRangeErrorException {
HourCycle defaultCycle = getDefaultHourCycle(localeObject);
if (hour12) {
return defaultCycle == HourCycle.H11 || defaultCycle == HourCycle.H23
? HourCycle.H11
: HourCycle.H12;
}
return defaultCycle == HourCycle.H11 || defaultCycle == HourCycle.H23
? HourCycle.H23
: HourCycle.H24;
}

@Override
public String getDefaultTimeZone(ILocaleObject<?> localeObject) throws JSRangeErrorException {
return Calendar.getInstance((Locale) localeObject.getLocale()).getTimeZone().getID();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import static com.facebook.hermes.intl.IPlatformDateTimeFormatter.DateStyle.UNDEFINED;

import android.icu.text.DateFormat;
import android.icu.text.DateTimePatternGenerator;
import android.icu.text.NumberingSystem;
import android.icu.text.SimpleDateFormat;
import android.icu.util.Calendar;
Expand Down Expand Up @@ -127,6 +128,64 @@ public static String getPatternWithoutLiterals(String pattern) {

return segment.toString();
}

public static String withHourCycle(String pattern, HourCycle hourCycle) {
char hourSymbol;
switch (hourCycle) {
case H11:
hourSymbol = 'K';
break;
case H12:
hourSymbol = 'h';
break;
case H23:
hourSymbol = 'H';
break;
case H24:
hourSymbol = 'k';
break;
default:
return pattern;
}

StringBuilder result = new StringBuilder(pattern.length());
boolean inLiteral = false;
for (int i = 0; i < pattern.length(); i++) {
char c = pattern.charAt(i);
if (c == '\'') {
result.append(c);
if (i + 1 < pattern.length() && pattern.charAt(i + 1) == '\'') {
result.append(pattern.charAt(++i));
} else {
inLiteral = !inLiteral;
}
} else if (!inLiteral && (c == 'h' || c == 'H' || c == 'K' || c == 'k')) {
result.append(hourSymbol);
} else {
result.append(c);
}
}
return result.toString();
}

public static String withoutDayPeriod(String pattern) {
StringBuilder result = new StringBuilder(pattern.length());
boolean inLiteral = false;
for (int i = 0; i < pattern.length(); i++) {
char c = pattern.charAt(i);
if (c == '\'') {
result.append(c);
if (i + 1 < pattern.length() && pattern.charAt(i + 1) == '\'') {
result.append(pattern.charAt(++i));
} else {
inLiteral = !inLiteral;
}
} else if (inLiteral || (c != 'a' && c != 'b' && c != 'B')) {
result.append(c);
}
}
return result.toString();
}
}

@RequiresApi(api = Build.VERSION_CODES.N)
Expand Down Expand Up @@ -154,6 +213,21 @@ else if (dateFormatPatternWithoutLiterals.contains(String.valueOf('H')))
return hourCycle;
}

@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public HourCycle getPreferredHourCycle(ILocaleObject<?> localeObject, boolean hour12)
throws JSRangeErrorException {
ULocale locale = (ULocale) localeObject.getLocaleWithoutExtensions();
String pattern =
DateTimePatternGenerator.getInstance(locale).getBestPattern(hour12 ? "hm" : "Hm");
String fields = PatternUtils.getPatternWithoutLiterals(pattern);
if (fields.indexOf('K') >= 0) return HourCycle.H11;
if (fields.indexOf('h') >= 0) return HourCycle.H12;
if (fields.indexOf('H') >= 0) return HourCycle.H23;
if (fields.indexOf('k') >= 0) return HourCycle.H24;
throw new JSRangeErrorException("No hour field in locale time pattern");
}

@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public String getDefaultTimeZone(ILocaleObject<?> localeObject) throws JSRangeErrorException {
Expand Down Expand Up @@ -293,6 +367,10 @@ private static String getSkeleton(
replacePatternChars(skeletonBuffer, new char[] {'h', 'H', 'K'}, 'k');
}
}

if (hourCycle == HourCycle.H23 || hourCycle == HourCycle.H24) {
skeletonBuffer = new StringBuilder(PatternUtils.withoutDayPeriod(skeletonBuffer.toString()));
}
} else {
skeletonBuffer.append(weekDay.getSkeleonSymbol());
skeletonBuffer.append(era.getSkeleonSymbol());
Expand Down Expand Up @@ -388,6 +466,11 @@ public void configure(
mDateFormat =
DateFormat.getPatternInstance(skeleton, (ULocale) resolvedLocaleObject.getLocale());

if (hourCycle != HourCycle.UNDEFINED) {
SimpleDateFormat formatter = (SimpleDateFormat) mDateFormat;
formatter.applyPattern(PatternUtils.withHourCycle(formatter.toPattern(), hourCycle));
}

if (!JSObjects.isUndefined(timeZone) && !JSObjects.isNull(timeZone)) {
TimeZone timeZoneObject = TimeZone.getTimeZone(JSObjects.getJavaString(timeZone));
mDateFormat.setTimeZone(timeZoneObject);
Expand Down