View Javadoc
1   /*
2    * Copyright (c) 2002-2026 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.htmlunit.javascript.host.intl;
16  
17  import java.time.ZoneId;
18  import java.time.chrono.Chronology;
19  import java.time.chrono.JapaneseChronology;
20  import java.time.chrono.ThaiBuddhistChronology;
21  import java.time.format.DateTimeFormatter;
22  import java.time.format.DecimalStyle;
23  import java.time.temporal.TemporalAccessor;
24  import java.util.Date;
25  import java.util.HashMap;
26  import java.util.Map;
27  import java.util.concurrent.ConcurrentHashMap;
28  
29  import org.htmlunit.BrowserVersion;
30  import org.htmlunit.corejs.javascript.Context;
31  import org.htmlunit.corejs.javascript.Function;
32  import org.htmlunit.corejs.javascript.FunctionObject;
33  import org.htmlunit.corejs.javascript.NativeArray;
34  import org.htmlunit.corejs.javascript.Scriptable;
35  import org.htmlunit.corejs.javascript.VarScope;
36  import org.htmlunit.javascript.HtmlUnitScriptable;
37  import org.htmlunit.javascript.JavaScriptEngine;
38  import org.htmlunit.javascript.configuration.JsxClass;
39  import org.htmlunit.javascript.configuration.JsxConstructor;
40  import org.htmlunit.javascript.configuration.JsxFunction;
41  import org.htmlunit.javascript.configuration.JsxStaticFunction;
42  import org.htmlunit.javascript.configuration.JsxSymbolConstant;
43  import org.htmlunit.javascript.host.Window;
44  import org.htmlunit.util.StringUtils;
45  
46  /**
47   * A JavaScript object for Intl.DateTimeFormat.
48   *
49   * @author Ahmed Ashour
50   * @author Ronald Brill
51   * @author Lai Quang Duong
52   */
53  @JsxClass
54  public class DateTimeFormat extends HtmlUnitScriptable {
55  
56      /** Symbol.toStringTag support. */
57      @JsxSymbolConstant
58      public static final String TO_STRING_TAG = "Intl.DateTimeFormat";
59  
60      private static final ConcurrentHashMap<String, String> CHROME_FORMATS_ = new ConcurrentHashMap<>();
61      private static final ConcurrentHashMap<String, String> EDGE_FORMATS_ = new ConcurrentHashMap<>();
62      private static final ConcurrentHashMap<String, String> FF_FORMATS_ = new ConcurrentHashMap<>();
63      private static final ConcurrentHashMap<String, String> FF_ESR_FORMATS_ = new ConcurrentHashMap<>();
64  
65      private static final ConcurrentHashMap<String, Chronology> CHROME_CHRONOLOGIES_ = new ConcurrentHashMap<>();
66      private static final ConcurrentHashMap<String, Chronology> EDGE_CHRONOLOGIES_ = new ConcurrentHashMap<>();
67      private static final ConcurrentHashMap<String, Chronology> FF_CHRONOLOGIES_ = new ConcurrentHashMap<>();
68      private static final ConcurrentHashMap<String, Chronology> FF_ESR_CHRONOLOGIES_ = new ConcurrentHashMap<>();
69  
70      private transient DateTimeFormatHelper formatter_;
71  
72      static {
73          final String ddSlash = "\u200Edd\u200E/\u200EMM\u200E/\u200EYYYY";
74          final String ddDash = "\u200Edd\u200E-\u200EMM\u200E-\u200EYYYY";
75          final String ddDot = "\u200Edd\u200E.\u200EMM\u200E.\u200EYYYY";
76          final String ddDotDot = "\u200Edd\u200E.\u200EMM\u200E.\u200EYYYY\u200E.";
77          final String ddDotBlank = "\u200Edd\u200E. \u200EMM\u200E. \u200EYYYY";
78          final String ddDotBlankDot = "\u200Edd\u200E. \u200EMM\u200E. \u200EYYYY.";
79          final String mmSlash = "\u200EMM\u200E/\u200Edd\u200E/\u200EYYYY";
80          final String yyyySlash = "\u200EYYYY\u200E/\u200EMM\u200E/\u200Edd";
81          final String yyyyDash = "\u200EYYYY\u200E-\u200EMM\u200E-\u200Edd";
82          final String yyyyDotBlankDot = "\u200EYYYY\u200E. \u200EMM\u200E. \u200Edd.";
83  
84          final Map<String, String> commonFormats = new HashMap<>();
85          commonFormats.put("", ddDot);
86          commonFormats.put("ar", "dd\u200F/MM\u200F/YYYY");
87          commonFormats.put("ban", mmSlash);
88          commonFormats.put("be", ddDot);
89          commonFormats.put("bg", ddDot + "\u200E \u0433.");
90          commonFormats.put("ca", ddSlash);
91          commonFormats.put("cs", ddDotBlank);
92          commonFormats.put("da", ddDot);
93          commonFormats.put("de", ddDot);
94          commonFormats.put("el", ddSlash);
95          commonFormats.put("en", mmSlash);
96          commonFormats.put("en-CA", yyyyDash);
97          commonFormats.put("en-NZ", ddSlash);
98          commonFormats.put("en-PA", ddSlash);
99          commonFormats.put("en-PR", ddSlash);
100         commonFormats.put("en-PH", mmSlash);
101         commonFormats.put("en-AU", ddSlash);
102         commonFormats.put("en-GB", ddSlash);
103         commonFormats.put("en-IE", ddSlash);
104         commonFormats.put("en-IN", ddSlash);
105         commonFormats.put("en-MT", ddSlash);
106         commonFormats.put("en-SG", ddSlash);
107         commonFormats.put("en-ZA", yyyySlash);
108         commonFormats.put("es", ddSlash);
109         commonFormats.put("es-CL", ddDash);
110         commonFormats.put("es-PA", mmSlash);
111         commonFormats.put("es-PR", mmSlash);
112         commonFormats.put("es-US", ddSlash);
113         commonFormats.put("et", ddDot);
114         commonFormats.put("fi", ddDot);
115         commonFormats.put("fr", ddSlash);
116         commonFormats.put("fr-CA", yyyyDash);
117         commonFormats.put("ga", ddSlash);
118         commonFormats.put("hi", ddSlash);
119         commonFormats.put("hr", ddDotBlankDot);
120         commonFormats.put("hu", yyyyDotBlankDot);
121         commonFormats.put("id", ddSlash);
122         commonFormats.put("in", ddSlash);
123         commonFormats.put("is", ddDot);
124         commonFormats.put("it", ddSlash);
125         commonFormats.put("iw", ddDot);
126         commonFormats.put("ja", yyyySlash);
127         commonFormats.put("ja-JP-u-ca-japanese", "'H'yy/MM/dd");
128         commonFormats.put("ko", yyyyDotBlankDot);
129         commonFormats.put("lt", yyyyDash);
130         commonFormats.put("lv", ddDotDot);
131         commonFormats.put("mk", ddDot + "\u200E \u0433.");
132         commonFormats.put("ms", ddSlash);
133         commonFormats.put("mt", mmSlash);
134         commonFormats.put("nl", ddDash);
135         commonFormats.put("nl-BE", ddSlash);
136         commonFormats.put("pl", ddDot);
137         commonFormats.put("pt", ddSlash);
138         commonFormats.put("ro", ddDot);
139         commonFormats.put("ru", ddDot);
140         commonFormats.put("sk", ddDotBlank);
141         commonFormats.put("sl", ddDotBlank);
142         commonFormats.put("sq", ddDot);
143         commonFormats.put("sr", ddDotBlankDot);
144         commonFormats.put("sv", yyyyDash);
145         commonFormats.put("th", ddSlash);
146         commonFormats.put("tr", ddDot);
147         commonFormats.put("uk", ddDot);
148         commonFormats.put("vi", ddSlash);
149         commonFormats.put("zh", yyyySlash);
150         commonFormats.put("zh-HK", ddSlash);
151         commonFormats.put("zh-SG", "\u200EYYYY\u200E\u5E74\u200EMM\u200E\u6708\u200Edd\u200E\u65E5");
152         commonFormats.put("fr-CH", ddDot);
153 
154         FF_FORMATS_.putAll(commonFormats);
155         FF_ESR_FORMATS_.putAll(commonFormats);
156 
157         commonFormats.put("be", mmSlash);
158         commonFormats.put("ga", mmSlash);
159         commonFormats.put("is", mmSlash);
160         commonFormats.put("mk", mmSlash);
161 
162         EDGE_FORMATS_.putAll(commonFormats);
163 
164         CHROME_FORMATS_.putAll(commonFormats);
165         CHROME_FORMATS_.put("sq", mmSlash);
166 
167         final Map<String, Chronology> commonChronologies = new HashMap<>();
168         commonChronologies.put("ja-JP-u-ca-japanese", JapaneseChronology.INSTANCE);
169         commonChronologies.put("th", ThaiBuddhistChronology.INSTANCE);
170         commonChronologies.put("th-TH", ThaiBuddhistChronology.INSTANCE);
171 
172         FF_CHRONOLOGIES_.putAll(commonChronologies);
173         FF_ESR_CHRONOLOGIES_.putAll(commonChronologies);
174         CHROME_CHRONOLOGIES_.putAll(commonChronologies);
175         EDGE_CHRONOLOGIES_.putAll(commonChronologies);
176     }
177 
178     /**
179      * Default constructor.
180      */
181     public DateTimeFormat() {
182         super();
183     }
184 
185     private DateTimeFormat(final String[] locales, final BrowserVersion browserVersion) {
186         super();
187 
188         final Map<String, String> formats;
189         final Map<String, Chronology> chronologies;
190         if (browserVersion.isChrome()) {
191             formats = CHROME_FORMATS_;
192             chronologies = CHROME_CHRONOLOGIES_;
193         }
194         else if (browserVersion.isEdge()) {
195             formats = EDGE_FORMATS_;
196             chronologies = EDGE_CHRONOLOGIES_;
197         }
198         else if (browserVersion.isFirefoxESR()) {
199             formats = FF_ESR_FORMATS_;
200             chronologies = FF_ESR_CHRONOLOGIES_;
201         }
202         else {
203             formats = FF_FORMATS_;
204             chronologies = FF_CHRONOLOGIES_;
205         }
206 
207         String locale = browserVersion.getBrowserLocale().toLanguageTag();
208         String pattern = getPattern(formats, locale);
209 
210         for (final String l : locales) {
211             pattern = getPattern(formats, l);
212             if (pattern != null) {
213                 locale = l;
214             }
215         }
216 
217         if (pattern == null) {
218             pattern = formats.get("");
219         }
220 
221         if (!locale.startsWith("ar")) {
222             pattern = pattern.replace("\u200E", "");
223         }
224 
225         final Chronology chronology = getChronology(chronologies, locale);
226 
227         formatter_ = new DateTimeFormatHelper(locale, chronology, pattern);
228     }
229 
230     private static String getPattern(final Map<String, String> formats, String locale) {
231         if ("no-NO-NY".equals(locale)) {
232             throw JavaScriptEngine.rangeError("Invalid language tag: " + locale);
233         }
234         String pattern = formats.get(locale);
235         while (pattern == null && locale.indexOf('-') != -1) {
236             locale = locale.substring(0, locale.lastIndexOf('-'));
237             pattern = formats.get(locale);
238         }
239         return pattern;
240     }
241 
242     private static Chronology getChronology(final Map<String, Chronology> chronologies, String locale) {
243         Chronology chronology = chronologies.get(locale);
244         while (chronology == null && locale.indexOf('-') != -1) {
245             locale = locale.substring(0, locale.lastIndexOf('-'));
246             chronology = chronologies.get(locale);
247         }
248         return chronology;
249     }
250 
251     /**
252      * JavaScript constructor.
253      * @param cx the current context
254      * @param scope the scope
255      * @param args the arguments to the WebSocket constructor
256      * @param ctorObj the function object
257      * @param inNewExpr Is new or not
258      * @return the java object to allow JavaScript to access
259      */
260     @JsxConstructor
261     public static Scriptable jsConstructor(final Context cx, final VarScope scope,
262             final Object[] args, final Function ctorObj, final boolean inNewExpr) {
263         final String[] locales;
264         if (args.length != 0) {
265             if (args[0] instanceof NativeArray array) {
266                 locales = new String[(int) array.getLength()];
267                 for (int i = 0; i < locales.length; i++) {
268                     locales[i] = JavaScriptEngine.toString(array.get(i));
269                 }
270             }
271             else {
272                 locales = new String[] {JavaScriptEngine.toString(args[0])};
273             }
274         }
275         else {
276             locales = new String[0];
277         }
278 
279         final Window window = getWindow(ctorObj);
280         final DateTimeFormat format = new DateTimeFormat(locales, window.getBrowserVersion());
281         format.setParentScope(getTopLevelScope(scope));
282         format.setPrototype(((FunctionObject) ctorObj).getClassPrototype());
283         return format;
284     }
285 
286     /**
287      * Formats a date according to the locale and formatting options of this {@code DateTimeFormat} object.
288      * @param object the JavaScript object to convert
289      * @return the dated formated
290      */
291     @JsxFunction
292     public String format(final Object object) {
293         final Date date = (Date) Context.jsToJava(object, Date.class);
294         return formatter_.format(date, Context.getCurrentContext().getTimeZone().toZoneId());
295     }
296 
297     /**
298      * @return A new object with properties reflecting the locale and date and time formatting options
299      *         computed during the initialization of the given {@code DateTimeFormat} object.
300      */
301     @JsxFunction
302     public Scriptable resolvedOptions() {
303         final Context cx = Context.getCurrentContext();
304         final Scriptable options = JavaScriptEngine.newObject(getParentScope());
305         options.put("timeZone", options, cx.getTimeZone().getID());
306 
307         if (StringUtils.isEmptyOrNull(formatter_.locale_)) {
308             options.put("locale", options, cx.getLocale().toLanguageTag());
309         }
310         else {
311             options.put("locale", options, formatter_.locale_);
312         }
313         return options;
314     }
315 
316     /**
317      * Returns an array containing those of the provided locales that are supported
318      * without having to fall back to the default locale.
319      * @param localesArgument A string with a BCP 47 language tag, or an array of such strings
320      * @param options unused
321      * @return an array containing supported locales
322      */
323     @JsxStaticFunction
324     public static Scriptable supportedLocalesOf(final Scriptable localesArgument, final Scriptable options) {
325         return Intl.supportedLocalesOf(localesArgument);
326     }
327 
328     @Override
329     public Object getDefaultValue(final Class<?> hint) {
330         if (String.class.equals(hint) || hint == null) {
331             return "[object Intl.DateTimeFormat]";
332         }
333         return super.getDefaultValue(hint);
334     }
335 
336     /**
337      * Helper.
338      */
339     static final class DateTimeFormatHelper {
340 
341         private final DateTimeFormatter formatter_;
342         private final Chronology chronology_;
343         private final String locale_;
344 
345         DateTimeFormatHelper(final String locale, final Chronology chronology, final String pattern) {
346             locale_ = locale;
347             chronology_ = chronology;
348 
349             if (locale.startsWith("ar")
350                     && !"ar-DZ".equals(locale)
351                     && !"ar-LY".equals(locale)
352                     && !"ar-MA".equals(locale)
353                     && !"ar-TN".equals(locale)) {
354                 final DecimalStyle decimalStyle = DecimalStyle.STANDARD.withZeroDigit('\u0660');
355                 formatter_ = DateTimeFormatter.ofPattern(pattern).withDecimalStyle(decimalStyle);
356             }
357             else {
358                 formatter_ = DateTimeFormatter.ofPattern(pattern);
359             }
360         }
361 
362         /**
363          * Formats a date according to the locale and formatting options of this {@code DateTimeFormat} object.
364          * @param date the JavaScript object to convert
365          * @param zoneId the current time zone id
366          * @return the dated formated
367          */
368         String format(final Date date, final ZoneId zoneId) {
369             TemporalAccessor zonedDateTime = date.toInstant().atZone(zoneId);
370             if (chronology_ != null) {
371                 zonedDateTime = chronology_.date(zonedDateTime);
372             }
373             return formatter_.format(zonedDateTime);
374         }
375     }
376 }