]> granicus.if.org Git - icu/commitdiff
ICU-10209 Fixed odd TimeType problem with ZONE_ID, ZONE_ID_SHORT and EXEMPLAR_LOCATIO...
authorYoshito Umaoka <y.umaoka@gmail.com>
Wed, 12 Jun 2013 21:37:46 +0000 (21:37 +0000)
committerYoshito Umaoka <y.umaoka@gmail.com>
Wed, 12 Jun 2013 21:37:46 +0000 (21:37 +0000)
X-SVN-Rev: 33820

icu4j/main/classes/core/src/com/ibm/icu/text/TimeZoneFormat.java
icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/TimeZoneFormatTest.java

index 0c8fab61f213ed87b59b57d7a60e9c1fdb335f59..23c46fa6e1e98dc08bd8fe58833a2f0c25a28607 100644 (file)
@@ -888,6 +888,8 @@ public class TimeZoneFormat extends UFormat implements Freezable<TimeZoneFormat>
             timeType.value = TimeType.UNKNOWN;
         }
 
+        boolean noOffsetFormatFallback = false;
+
         switch (style) {
         case GENERIC_LOCATION:
             result = getTimeZoneGenericNames().getGenericLocationName(ZoneMeta.getCanonicalCLDRID(tz));
@@ -904,12 +906,29 @@ public class TimeZoneFormat extends UFormat implements Freezable<TimeZoneFormat>
         case SPECIFIC_SHORT:
             result = formatSpecific(tz, NameType.SHORT_STANDARD, NameType.SHORT_DAYLIGHT, date, timeType);
             break;
+
+        case ZONE_ID:
+            result = tz.getID();
+            noOffsetFormatFallback = true;
+            break;
+        case ZONE_ID_SHORT:
+            result = ZoneMeta.getShortID(tz);
+            if (result == null) {
+                result = UNKNOWN_SHORT_ZONE_ID;
+            }
+            noOffsetFormatFallback = true;
+            break;
+        case EXEMPLAR_LOCATION:
+            result = formatExemplarLocation(tz);
+            noOffsetFormatFallback = true;
+            break;
+
         default:
             // will be handled below
             break;
         }
 
-        if (result == null) {
+        if (result == null && !noOffsetFormatFallback) {
             int[] offsets = {0, 0};
             tz.getOffset(date, false, offsets);
             int offset = offsets[0] + offsets[1];
@@ -967,21 +986,6 @@ public class TimeZoneFormat extends UFormat implements Freezable<TimeZoneFormat>
             case ISO_EXTENDED_LOCAL_FULL:
                 result = formatOffsetISO8601Extended(offset, false, false, false);
                 break;
-
-            case ZONE_ID:
-                result = tz.getID();
-                break;
-
-            case ZONE_ID_SHORT:
-                result = ZoneMeta.getShortID(tz);
-                if (result == null) {
-                    result = UNKNOWN_SHORT_ZONE_ID;
-                }
-                break;
-
-            case EXEMPLAR_LOCATION:
-                result = formatExemplarLocation(tz);
-                break;
             }
             // time type
             if (timeType != null) {
index 17ab0510bdf80fd608715209daec8e2c4abae9e3..5fcd796e18a5e691a60c6d3f558c42546db00f9f 100644 (file)
@@ -692,4 +692,89 @@ public class TimeZoneFormatTest extends com.ibm.icu.dev.test.TestFmwk {
             }
         }
     }
+
+    public void TestFormat() {
+        final Date dateJan = new Date(1358208000000L);  // 2013-01-15T00:00:00Z
+        final Date dateJul = new Date(1373846400000L);  // 2013-07-15T00:00:00Z
+
+        final Object[][] TESTDATA = {
+            {
+                "en",
+                "America/Los_Angeles", 
+                dateJan,
+                Style.GENERIC_LOCATION,
+                "Los Angeles Time",
+                TimeType.UNKNOWN
+            },
+            {
+                "en",
+                "America/Los_Angeles",
+                dateJan,
+                Style.GENERIC_LONG,
+                "Pacific Time",
+                TimeType.UNKNOWN
+            },
+            {
+                "en",
+                "America/Los_Angeles",
+                dateJan,
+                Style.SPECIFIC_LONG,
+                "Pacific Standard Time",
+                TimeType.STANDARD
+            },
+            {
+                "en",
+                "America/Los_Angeles",
+                dateJul,
+                Style.SPECIFIC_LONG,
+                "Pacific Daylight Time",
+                TimeType.DAYLIGHT
+            },
+            {
+                "ja",
+                "America/Los_Angeles",
+                dateJan,
+                Style.ZONE_ID,
+                "America/Los_Angeles",
+                TimeType.UNKNOWN
+            },
+            {
+                "fr",
+                "America/Los_Angeles",
+                dateJul,
+                Style.ZONE_ID_SHORT,
+                "uslax",
+                TimeType.UNKNOWN
+            },
+            {
+                "en",
+                "America/Los_Angeles",
+                dateJan,
+                Style.EXEMPLAR_LOCATION,
+                "Los Angeles",
+                TimeType.UNKNOWN
+            },
+            {
+                "ja",
+                "Asia/Tokyo",
+                dateJan,
+                Style.GENERIC_LONG,
+                "\u65E5\u672C\u6A19\u6E96\u6642",   // "日本標準時"
+                TimeType.UNKNOWN
+            },
+        };
+
+        for (Object[] testCase : TESTDATA) {
+            TimeZoneFormat tzfmt = TimeZoneFormat.getInstance(new ULocale((String)testCase[0]));
+            TimeZone tz = TimeZone.getTimeZone((String)testCase[1]);
+            Output<TimeType> timeType = new Output<TimeType>();
+            String out = tzfmt.format((Style)testCase[3], tz, ((Date)testCase[2]).getTime(), timeType);
+
+            if (!out.equals((String)testCase[4]) || timeType.value != testCase[5]) {
+                errln("Format result for [locale=" + testCase[0] + ",tzid=" + testCase[1] + ",date=" + testCase[2]
+                        + ",style=" + testCase[3] + "]: expected [output=" + testCase[4] + ",type=" + testCase[5]
+                        + "]; actual [output=" + out + ",type=" + timeType.value + "]");
+            }
+        }
+    }
 }
\ No newline at end of file