You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by to...@apache.org on 2008/02/03 08:33:05 UTC

svn commit: r617962 - in /harmony/enhanced/classlib/trunk/modules/luni/src: main/java/java/util/TimeZone.java test/api/common/org/apache/harmony/luni/tests/java/util/TimeZoneTest.java

Author: tonywu
Date: Sat Feb  2 23:33:04 2008
New Revision: 617962

URL: http://svn.apache.org/viewvc?rev=617962&view=rev
Log:
Fix a bug, exposed in HARMONY-5448 (TimeZone.getDisplayName() returns empty value in some locale)

Modified:
    harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/TimeZone.java
    harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/TimeZoneTest.java

Modified: harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/TimeZone.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/TimeZone.java?rev=617962&r1=617961&r2=617962&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/TimeZone.java (original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/TimeZone.java Sat Feb  2 23:33:04 2008
@@ -20,7 +20,6 @@
 import java.io.Serializable;
 import java.security.AccessController;
 import java.security.PrivilegedAction;
-import java.text.DateFormatSymbols;
 
 import org.apache.harmony.luni.util.PriviAction;
 
@@ -61,6 +60,8 @@
     static TimeZone GMT = new SimpleTimeZone(0, "GMT"); // Greenwich Mean Time
 
     private String ID;
+    
+    private com.ibm.icu.util.TimeZone icuTimeZone = null;
 
     private static void initializeAvailable() {
         TimeZone[] zones = TimeZones.getTimeZones();
@@ -79,16 +80,6 @@
     public TimeZone() {
     }
 
-    private void appendNumber(StringBuffer buffer, int count, int value) {
-        String string = Integer.toString(value);
-        if (count > string.length()) {
-            for (int i = 0; i < count - string.length(); i++) {
-                buffer.append('0');
-            }
-        }
-        buffer.append(string);
-    }
-
     /**
      * Answers a new TimeZone with the same ID, rawOffset and daylight savings
      * time rules as this TimeZone.
@@ -216,37 +207,11 @@
      * @return the TimeZone name
      */
     public String getDisplayName(boolean daylightTime, int style, Locale locale) {
-        if (style == SHORT || style == LONG) {
-            boolean useDaylight = daylightTime && useDaylightTime();
-            DateFormatSymbols data = new DateFormatSymbols(locale);
-            String id = getID();
-            String[][] zones = data.getZoneStrings();
-            for (int i = 0; i < zones.length; i++) {
-                if (id.equals(zones[i][0])) {
-                    String res = style == SHORT ? zones[i][useDaylight ? 4 : 2]
-                            : zones[i][useDaylight ? 3 : 1];
-                    return res == null ? "" : res;
-                }
-            }
-            int offset = getRawOffset();
-            if (useDaylight && this instanceof SimpleTimeZone) {
-                offset += ((SimpleTimeZone) this).getDSTSavings();
-            }
-            offset /= 60000;
-            char sign = '+';
-            if (offset < 0) {
-                sign = '-';
-                offset = -offset;
-            }
-            StringBuffer buffer = new StringBuffer(9);
-            buffer.append("GMT");
-            buffer.append(sign);
-            appendNumber(buffer, 2, offset / 60);
-            buffer.append(':');
-            appendNumber(buffer, 2, offset % 60);
-            return buffer.toString();
+        if(icuTimeZone == null || !ID.equals(icuTimeZone.getID())){
+            icuTimeZone = com.ibm.icu.util.TimeZone.getTimeZone(ID);
         }
-        throw new IllegalArgumentException();
+        return icuTimeZone.getDisplayName(
+                daylightTime, style, locale);
     }
 
     /**

Modified: harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/TimeZoneTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/TimeZoneTest.java?rev=617962&r1=617961&r2=617962&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/TimeZoneTest.java (original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/TimeZoneTest.java Sat Feb  2 23:33:04 2008
@@ -18,7 +18,9 @@
 package org.apache.harmony.luni.tests.java.util;
 
 import java.util.Calendar;
+import java.util.Formatter;
 import java.util.GregorianCalendar;
+import java.util.Locale;
 import java.util.SimpleTimeZone;
 import java.util.TimeZone;
 
@@ -161,7 +163,31 @@
 		assertEquals("default not restored",
                              oldDefault, TimeZone.getDefault());
 	}
-
+    
+    /**
+     * @tests java.util.TimeZone#getDisplayName(java.util.Locale)
+     */
+    public void test_getDisplayNameLjava_util_Locale() {
+        TimeZone timezone = TimeZone.getTimeZone("Asia/Shanghai");
+        assertEquals("\u4e2d\u56fd\u6807\u51c6\u65f6\u95f4", timezone
+                .getDisplayName(Locale.CHINA));
+    }
+    
+    /**
+     * @tests java.util.TimeZone#getDisplayName(boolean, int, java.util.Locale)
+     */
+    public void test_getDisplayNameZILjava_util_Locale() {
+        TimeZone timezone = TimeZone.getTimeZone("Asia/Shanghai");
+        assertEquals("GMT+0800", timezone.getDisplayName(false, TimeZone.SHORT,
+                Locale.CHINA));
+        try {
+            timezone.getDisplayName(false, 100, Locale.CHINA);
+            fail("should throw IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            // expected
+        }
+    }
+    
 	protected void setUp() {
 	}