View Javadoc
1   /*
2    * Copyright (c) 2002-2017 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    * http://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 com.gargoylesoftware.htmlunit.javascript.host.intl;
16  
17  import static com.gargoylesoftware.htmlunit.BrowserVersionFeatures.JS_DATE_AR_DZ_ASCII_DIGITS;
18  import static com.gargoylesoftware.htmlunit.BrowserVersionFeatures.JS_DATE_WITH_LEFT_TO_RIGHT_MARK;
19  
20  import java.time.ZoneId;
21  import java.time.chrono.Chronology;
22  import java.time.chrono.HijrahChronology;
23  import java.time.chrono.JapaneseChronology;
24  import java.time.chrono.ThaiBuddhistChronology;
25  import java.time.format.DateTimeFormatter;
26  import java.time.format.DecimalStyle;
27  import java.time.temporal.TemporalAccessor;
28  import java.util.Date;
29  
30  import com.gargoylesoftware.htmlunit.BrowserVersion;
31  
32  /**
33   * An implementation of {@code DateTimeFormatter}, supporting Java 8 Date and Time.
34   *
35   * @author Ahmed Ashour
36   */
37  class DefaultDateTimeFormatter implements AbstractDateTimeFormatter {
38  
39      private DateTimeFormatter formatter_;
40      private Chronology chronology_;
41  
42      DefaultDateTimeFormatter(final String locale, final BrowserVersion browserVersion, final String pattern) {
43          formatter_ = DateTimeFormatter.ofPattern(pattern);
44          if (locale.startsWith("ar")
45                  && (!browserVersion.hasFeature(JS_DATE_AR_DZ_ASCII_DIGITS)
46                          || (!"ar-DZ".equals(locale)
47                                  && !"ar-LY".equals(locale)
48                                  && !"ar-MA".equals(locale)
49                                  && !"ar-TN".equals(locale)))) {
50              final DecimalStyle decimalStyle = DecimalStyle.STANDARD.withZeroDigit('\u0660');
51              formatter_ = formatter_.withDecimalStyle(decimalStyle);
52          }
53  
54          switch (locale) {
55              case "ja-JP-u-ca-japanese":
56                  chronology_ = JapaneseChronology.INSTANCE;
57                  break;
58  
59              case "ar":
60                  if (browserVersion.hasFeature(JS_DATE_WITH_LEFT_TO_RIGHT_MARK)) {
61                      chronology_ = HijrahChronology.INSTANCE;
62                  }
63                  break;
64  
65              case "ar-SA":
66                  if (browserVersion.hasFeature(JS_DATE_AR_DZ_ASCII_DIGITS)) {
67                      chronology_ = HijrahChronology.INSTANCE;
68                  }
69                  break;
70  
71              case "ar-SD":
72                  if (browserVersion.hasFeature(JS_DATE_WITH_LEFT_TO_RIGHT_MARK)) {
73                      chronology_ = HijrahChronology.INSTANCE;
74                  }
75                  break;
76  
77              case "th":
78              case "th-TH":
79                  chronology_ = ThaiBuddhistChronology.INSTANCE;
80                  break;
81  
82              default:
83          }
84      }
85  
86      /**
87       * {@inheritDoc}
88       */
89      @Override
90      public String format(final Date date) {
91          TemporalAccessor zonedDateTime = date.toInstant().atZone(ZoneId.systemDefault());
92          if (chronology_ != null) {
93              zonedDateTime = chronology_.date(zonedDateTime);
94          }
95          return formatter_.format(zonedDateTime);
96      }
97  
98  }