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 java.text.DateFormat;
18  import java.text.DecimalFormat;
19  import java.text.DecimalFormatSymbols;
20  import java.text.SimpleDateFormat;
21  import java.util.Date;
22  
23  import com.gargoylesoftware.htmlunit.BrowserVersion;
24  
25  /**
26   * An implementation of {@code DateTimeFormatter}, supporting Google App Engine.
27   *
28   * @author Ahmed Ashour
29   */
30  class GAEDateTimeFormatter implements AbstractDateTimeFormatter {
31  
32      private DateFormat format_;
33  
34      /**
35       * Default constructor.
36       */
37      GAEDateTimeFormatter(final String locale, final BrowserVersion browserVersion, final String pattern) {
38          format_ = new SimpleDateFormat(pattern);
39          if (locale.startsWith("ar")
40                  && (!"ar-DZ".equals(locale)
41                          && !"ar-LY".equals(locale)
42                          && !"ar-MA".equals(locale)
43                          && !"ar-TN".equals(locale))) {
44              setZeroDigit('\u0660');
45          }
46      }
47  
48      private void setZeroDigit(final char zeroDigit) {
49          final DecimalFormat df = (DecimalFormat) format_.getNumberFormat();
50          final DecimalFormatSymbols dfs = df.getDecimalFormatSymbols();
51          dfs.setZeroDigit(zeroDigit);
52          df.setDecimalFormatSymbols(dfs);
53      }
54  
55      /**
56       * {@inheritDoc}
57       */
58      @Override
59      public String format(final Date date) {
60          return format_.format(date);
61      }
62  
63  }