Home | History | Annotate | Download | only in util
      1 //  2016 and later: Unicode, Inc. and others.
      2 // License & terms of use: http://www.unicode.org/copyright.html#License
      3 /*
      4  *******************************************************************************
      5  * Copyright (C) 2008-2012, International Business Machines Corporation and    *
      6  * others. All Rights Reserved.                                                *
      7  *******************************************************************************
      8  */
      9 package com.ibm.icu.impl.javaspi.util;
     10 
     11 import java.util.Locale;
     12 import java.util.TimeZone;
     13 
     14 import com.ibm.icu.impl.javaspi.ICULocaleServiceProvider;
     15 import com.ibm.icu.text.TimeZoneNames;
     16 import com.ibm.icu.text.TimeZoneNames.NameType;
     17 
     18 public class TimeZoneNameProviderICU extends java.util.spi.TimeZoneNameProvider {
     19 
     20     @Override
     21     public String getDisplayName(String ID, boolean daylight, int style, Locale locale) {
     22         String dispName = null;
     23         boolean[] isSystemID = new boolean[1];
     24         String canonicalID = com.ibm.icu.util.TimeZone.getCanonicalID(ID, isSystemID);
     25         if (isSystemID[0]) {
     26             long date = System.currentTimeMillis();
     27             TimeZoneNames tznames = TimeZoneNames.getInstance(ICULocaleServiceProvider.toULocaleNoSpecialVariant(locale));
     28 
     29             {
     30                 // Workaround for Java bug. Java needs all 4 names available at the same time.
     31                 // 2012-10-09 yoshito
     32                 String lstd = tznames.getDisplayName(canonicalID, NameType.LONG_STANDARD, date);
     33                 String ldst = tznames.getDisplayName(canonicalID, NameType.LONG_DAYLIGHT, date);
     34                 String sstd = tznames.getDisplayName(canonicalID, NameType.SHORT_STANDARD, date);
     35                 String sdst = tznames.getDisplayName(canonicalID, NameType.SHORT_DAYLIGHT, date);
     36 
     37                 if (lstd != null && ldst != null && sstd != null && sdst != null) {
     38                     switch (style) {
     39                     case TimeZone.LONG:
     40                         dispName = daylight ? ldst : lstd;
     41                         break;
     42                     case TimeZone.SHORT:
     43                         dispName = daylight ? sdst : sstd;
     44                         break;
     45                     }
     46                 }
     47             }
     48 
     49 //            {
     50 //                switch (style) {
     51 //                case TimeZone.LONG:
     52 //                    dispName = daylight ?
     53 //                            tznames.getDisplayName(canonicalID, NameType.LONG_DAYLIGHT, date) :
     54 //                            tznames.getDisplayName(canonicalID, NameType.LONG_STANDARD, date);
     55 //                    break;
     56 //                case TimeZone.SHORT:
     57 //                    dispName = daylight ?
     58 //                            tznames.getDisplayName(canonicalID, NameType.SHORT_DAYLIGHT, date) :
     59 //                            tznames.getDisplayName(canonicalID, NameType.SHORT_STANDARD, date);
     60 //                    break;
     61 //                }
     62 //            }
     63         }
     64         return dispName;
     65     }
     66 
     67     @Override
     68     public Locale[] getAvailableLocales() {
     69         return ICULocaleServiceProvider.getAvailableLocales();
     70     }
     71 
     72 }
     73