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

improved date localization #1491

Closed
wants to merge 1 commit into from
Closed
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
89 changes: 67 additions & 22 deletions rhino-runtime/src/main/java/org/mozilla/javascript/NativeDate.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@

package org.mozilla.javascript;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
* This class implements the Date native object. See ECMA 15.9.
Expand All @@ -21,7 +27,6 @@ final class NativeDate extends IdScriptableObject {
private static final long serialVersionUID = -8307438915861678966L;

private static final Object DATE_TAG = "Date";

private static final String js_NaN_date_str = "Invalid Date";

static void init(Scriptable scope, boolean sealed) {
Expand Down Expand Up @@ -336,7 +341,7 @@ public Object execIdCall(
case Id_toLocaleTimeString:
case Id_toLocaleDateString:
if (!Double.isNaN(t)) {
return toLocale_helper(t, id);
return toLocale_helper(cx, t, id, args);
}
return js_NaN_date_str;

Expand Down Expand Up @@ -1348,10 +1353,8 @@ private static String date_format(Context cx, double t, int methodId) {
t = MakeDate(day, TimeWithinDay(t));
}
result.append(" (");
Date date = new Date((long) t);
synchronized (timeZoneFormatter) {
result.append(timeZoneFormatter.format(date));
}
final ZoneId zoneid = cx.getTimeZone().toZoneId();
result.append(timeZoneFormatter.format(Instant.ofEpochMilli((long) t).atZone(zoneid)));
result.append(')');
}
return result.toString();
Expand Down Expand Up @@ -1399,25 +1402,57 @@ private static Object jsConstructor(Context cx, Object[] args) {
return obj;
}

private static String toLocale_helper(double t, int methodId) {
DateFormat formatter;
private static String toLocale_helper(Context cx, double t, int methodId, Object[] args) {
DateTimeFormatter formatter;
switch (methodId) {
case Id_toLocaleString:
formatter = localeDateTimeFormatter;
formatter =
cx.getLanguageVersion() >= Context.VERSION_ES6
? localeDateTimeFormatterES6
: localeDateTimeFormatter;
break;
case Id_toLocaleTimeString:
formatter = localeTimeFormatter;
formatter =
cx.getLanguageVersion() >= Context.VERSION_ES6
? localeTimeFormatterES6
: localeTimeFormatter;
break;
case Id_toLocaleDateString:
formatter = localeDateFormatter;
formatter =
cx.getLanguageVersion() >= Context.VERSION_ES6
? localeDateFormatterES6
: localeDateFormatter;
break;
default:
throw new AssertionError(); // unreachable
}

synchronized (formatter) {
return formatter.format(new Date((long) t));
final List<String> languageTags = new ArrayList<>();
if (args.length != 0) {
// we use the 'locales' argument but ignore the second 'options' argument as per spec of
// an
// implementation that has no Intl.DateTimeFormat support
if (args[0] instanceof NativeArray) {
final NativeArray array = (NativeArray) args[0];
for (Object languageTag : array) {
languageTags.add(Context.toString(languageTag));
}
} else {
languageTags.add(Context.toString(args[0]));
}
}

final List<Locale> availableLocales = Arrays.asList(Locale.getAvailableLocales());
for (String languageTag : languageTags) {
Locale locale = Locale.forLanguageTag(languageTag);
if (availableLocales.contains(locale)) {
formatter = formatter.withLocale(locale);
break;
}
}

final ZoneId zoneid = cx.getTimeZone().toZoneId();
return formatter.format(Instant.ofEpochMilli((long) t).atZone(zoneid));
}

private static String js_toUTCString(double date) {
Expand Down Expand Up @@ -1916,12 +1951,22 @@ protected int findPrototypeId(String s) {

private static final int Id_toGMTString = Id_toUTCString; // Alias, see Ecma B.2.6

// not thread safe
private static final DateFormat timeZoneFormatter = new SimpleDateFormat("zzz");
private static final DateFormat localeDateTimeFormatter =
new SimpleDateFormat("MMMM d, yyyy h:mm:ss a z");
private static final DateFormat localeDateFormatter = new SimpleDateFormat("MMMM d, yyyy");
private static final DateFormat localeTimeFormatter = new SimpleDateFormat("h:mm:ss a z");

private static final DateTimeFormatter timeZoneFormatter = DateTimeFormatter.ofPattern("zzz");

private static final DateTimeFormatter localeDateTimeFormatter =
DateTimeFormatter.ofPattern("MMMM d, yyyy h:mm:ss a z");
private static final DateTimeFormatter localeDateFormatter =
DateTimeFormatter.ofPattern("MMMM d, yyyy");
private static final DateTimeFormatter localeTimeFormatter =
DateTimeFormatter.ofPattern("h:mm:ss a z");

// use FormatStyle.SHORT for these as per spec of an implementation that has no
// Intl.DateTimeFormat support
private static final DateTimeFormatter localeDateTimeFormatterES6 =
DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT);
private static final DateTimeFormatter localeDateFormatterES6 =
DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT);
private static final DateTimeFormatter localeTimeFormatterES6 =
DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT);
private double date;
}
Loading
Loading