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