]> 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 20:56:06 +0000 (20:56 +0000)
committerYoshito Umaoka <y.umaoka@gmail.com>
Wed, 12 Jun 2013 20:56:06 +0000 (20:56 +0000)
X-SVN-Rev: 33819

icu4c/source/i18n/tzfmt.cpp
icu4c/source/test/intltest/tzfmttst.cpp
icu4c/source/test/intltest/tzfmttst.h

index ab5b451d359de6d66be07108130e540e5a68f00e..d4ac316481ac3b0b92f2564c7e10cf44eae139bd 100644 (file)
@@ -639,6 +639,9 @@ TimeZoneFormat::format(UTimeZoneFormatStyle style, const TimeZone& tz, UDate dat
     if (timeType) {
         *timeType = UTZFMT_TIME_TYPE_UNKNOWN;
     }
+
+    UBool noOffsetFormatFallback = FALSE;
+
     switch (style) {
     case UTZFMT_STYLE_GENERIC_LOCATION:
         formatGeneric(tz, UTZGNM_LOCATION, date, name);
@@ -655,12 +658,33 @@ TimeZoneFormat::format(UTimeZoneFormatStyle style, const TimeZone& tz, UDate dat
     case UTZFMT_STYLE_SPECIFIC_SHORT:
         formatSpecific(tz, UTZNM_SHORT_STANDARD, UTZNM_SHORT_DAYLIGHT, date, name, timeType);
         break;
+
+    case UTZFMT_STYLE_ZONE_ID:
+        tz.getID(name);
+        noOffsetFormatFallback = TRUE;
+        break;
+    case UTZFMT_STYLE_ZONE_ID_SHORT:
+        {
+            const UChar* shortID = ZoneMeta::getShortID(tz);
+            if (shortID == NULL) {
+                shortID = UNKNOWN_SHORT_ZONE_ID;
+            }
+            name.setTo(shortID, -1);
+        }
+        noOffsetFormatFallback = TRUE;
+        break;
+
+    case UTZFMT_STYLE_EXEMPLAR_LOCATION:
+        formatExemplarLocation(tz, name);
+        noOffsetFormatFallback = TRUE;
+        break;
+
     default:
         // will be handled below
         break;
     }
 
-    if (name.isEmpty()) {
+    if (name.isEmpty() && !noOffsetFormatFallback) {
         UErrorCode status = U_ZERO_ERROR;
         int32_t rawOffset, dstOffset;
         tz.getOffset(date, FALSE, rawOffset, dstOffset, status);
@@ -719,25 +743,8 @@ TimeZoneFormat::format(UTimeZoneFormatStyle style, const TimeZone& tz, UDate dat
             case UTZFMT_STYLE_ISO_EXTENDED_LOCAL_FULL:
                 formatOffsetISO8601Extended(offset, FALSE, FALSE, FALSE, name, status);
                 break;
-
-            case UTZFMT_STYLE_ZONE_ID:
-                tz.getID(name);
-                break;
-
-            case UTZFMT_STYLE_ZONE_ID_SHORT:
-                {
-                    const UChar* shortID = ZoneMeta::getShortID(tz);
-                    if (shortID == NULL) {
-                        shortID = UNKNOWN_SHORT_ZONE_ID;
-                    }
-                    name.setTo(shortID, -1);
-                }
-                break;
-
-            case UTZFMT_STYLE_EXEMPLAR_LOCATION:
-                formatExemplarLocation(tz, name);
-                break;
             }
+
             if (timeType) {
                 *timeType = (dstOffset != 0) ? UTZFMT_TIME_TYPE_DAYLIGHT : UTZFMT_TIME_TYPE_STANDARD;
             }
index 2662d9628486940a2b2885359cbb397699a89ae3..ffdaeae279c62e374fe7df698060bb59bb5e6201 100644 (file)
@@ -75,6 +75,7 @@ TimeZoneFormatTest::runIndexedTest( int32_t index, UBool exec, const char* &name
         TESTCASE(1, TestTimeRoundTrip);
         TESTCASE(2, TestParse);
         TESTCASE(3, TestISOFormat);
+        TESTCASE(4, TestFormat);
         default: name = ""; break;
     }
 }
@@ -916,4 +917,112 @@ TimeZoneFormatTest::TestISOFormat(void) {
 }
 
 
+typedef struct {
+    const char*     locale;
+    const char*     tzid;
+    UDate           date;
+    UTimeZoneFormatStyle    style;
+    const char*     expected;
+    UTimeZoneFormatTimeType timeType;
+} FormatTestData;
+
+void
+TimeZoneFormatTest::TestFormat(void) {
+    UDate dateJan = 1358208000000.0;    // 2013-01-15T00:00:00Z
+    UDate dateJul = 1373846400000.0;    // 2013-07-15T00:00:00Z
+
+    const FormatTestData DATA[] = {
+        {
+            "en",
+            "America/Los_Angeles", 
+            dateJan,
+            UTZFMT_STYLE_GENERIC_LOCATION,
+            "Los Angeles Time",
+            UTZFMT_TIME_TYPE_UNKNOWN
+        },
+        {
+            "en",
+            "America/Los_Angeles",
+            dateJan,
+            UTZFMT_STYLE_GENERIC_LONG,
+            "Pacific Time",
+            UTZFMT_TIME_TYPE_UNKNOWN
+        },
+        {
+            "en",
+            "America/Los_Angeles",
+            dateJan,
+            UTZFMT_STYLE_SPECIFIC_LONG,
+            "Pacific Standard Time",
+            UTZFMT_TIME_TYPE_STANDARD
+        },
+        {
+            "en",
+            "America/Los_Angeles",
+            dateJul,
+            UTZFMT_STYLE_SPECIFIC_LONG,
+            "Pacific Daylight Time",
+            UTZFMT_TIME_TYPE_DAYLIGHT
+        },
+        {
+            "ja",
+            "America/Los_Angeles",
+            dateJan,
+            UTZFMT_STYLE_ZONE_ID,
+            "America/Los_Angeles",
+            UTZFMT_TIME_TYPE_UNKNOWN
+        },
+        {
+            "fr",
+            "America/Los_Angeles",
+            dateJul,
+            UTZFMT_STYLE_ZONE_ID_SHORT,
+            "uslax",
+            UTZFMT_TIME_TYPE_UNKNOWN
+        },
+        {
+            "en",
+            "America/Los_Angeles",
+            dateJan,
+            UTZFMT_STYLE_EXEMPLAR_LOCATION,
+            "Los Angeles",
+            UTZFMT_TIME_TYPE_UNKNOWN
+        },
+
+        {
+            "ja",
+            "Asia/Tokyo",
+            dateJan,
+            UTZFMT_STYLE_GENERIC_LONG,
+            "\\u65E5\\u672C\\u6A19\\u6E96\\u6642",
+            UTZFMT_TIME_TYPE_UNKNOWN
+        },
+
+        {0, 0, 0.0, UTZFMT_STYLE_GENERIC_LOCATION, 0, UTZFMT_TIME_TYPE_UNKNOWN}
+    };
+
+    for (int32_t i = 0; DATA[i].locale; i++) {
+        UErrorCode status = U_ZERO_ERROR;
+        LocalPointer<TimeZoneFormat> tzfmt(TimeZoneFormat::createInstance(Locale(DATA[i].locale), status));
+        if (U_FAILURE(status)) {
+            dataerrln("Fail TimeZoneFormat::createInstance: %s", u_errorName(status));
+            continue;
+        }
+
+        LocalPointer<TimeZone> tz(TimeZone::createTimeZone(DATA[i].tzid));
+        UnicodeString out;
+        UTimeZoneFormatTimeType timeType;
+
+        tzfmt->format(DATA[i].style, *(tz.getAlias()), DATA[i].date, out, &timeType);
+        UnicodeString expected(DATA[i].expected, -1, US_INV);
+        expected = expected.unescape();
+
+        assertEquals(UnicodeString("Format result for ") + DATA[i].tzid + " (Test Case " + i + ")", expected, out);
+        if (DATA[i].timeType != timeType) {
+            dataerrln(UnicodeString("Formatted time zone type (Test Case ") + i + "), returned="
+                + timeType + ", expected=" + DATA[i].timeType);
+        }
+    }
+}
+
 #endif /* #if !UCONFIG_NO_FORMATTING */
index 4635711f555f704421b2e86fc8425b0364fa1e5d..3d81a82b6dd7e42d54a8a073d9b8ac7891a8979c 100644 (file)
@@ -22,6 +22,7 @@ class TimeZoneFormatTest : public IntlTest {
     void TestTimeRoundTrip(void);
     void TestParse(void);
     void TestISOFormat(void);
+    void TestFormat(void);
 };
 
 #endif /* #if !UCONFIG_NO_FORMATTING */