]> granicus.if.org Git - icu/commitdiff
ICU-9735 Added a C API ucal_getTimeZoneID.
authorYoshito Umaoka <y.umaoka@gmail.com>
Wed, 2 Jan 2013 16:58:24 +0000 (16:58 +0000)
committerYoshito Umaoka <y.umaoka@gmail.com>
Wed, 2 Jan 2013 16:58:24 +0000 (16:58 +0000)
X-SVN-Rev: 33005

icu4c/source/i18n/ucal.cpp
icu4c/source/i18n/unicode/ucal.h
icu4c/source/test/cintltst/ccaltst.c

index 0015baef24ac52b416d4e6d1eda7377c7c6c7c6b..a2edadb8154c02a1a9c86939bf6e39591e9833d1 100644 (file)
@@ -1,6 +1,6 @@
 /*
 *******************************************************************************
-*   Copyright (C) 1996-2012, International Business Machines
+*   Copyright (C) 1996-2013, International Business Machines
 *   Corporation and others.  All Rights Reserved.
 *******************************************************************************
 */
@@ -200,6 +200,21 @@ ucal_setTimeZone(    UCalendar*      cal,
   }
 }
 
+U_CAPI int32_t U_EXPORT2
+ucal_getTimeZoneID(const UCalendar *cal,
+                   UChar *result,
+                   int32_t resultLength,
+                   UErrorCode *status)
+{
+    if (U_FAILURE(*status)) {
+        return 0;
+    }
+    const TimeZone& tz = ((Calendar*)cal)->getTimeZone();
+    UnicodeString id;
+    tz.getID(id);
+    return id.extract(result, resultLength, *status);
+}
+
 U_CAPI int32_t U_EXPORT2
 ucal_getTimeZoneDisplayName(const     UCalendar*                 cal,
                     UCalendarDisplayNameType     type,
index fab9bb7823e4f2fdccbdd8479ce3143e9c5fa269..583525393e5940196ece048f79b3a19cf56c6b07 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *******************************************************************************
- * Copyright (C) 1996-2012, International Business Machines Corporation and
+ * Copyright (C) 1996-2013, International Business Machines Corporation and
  * others. All Rights Reserved.
  *******************************************************************************
  */
@@ -745,6 +745,22 @@ ucal_setTimeZone(UCalendar*    cal,
                  int32_t       len,
                  UErrorCode*   status);
 
+/** 
+ * Get the ID of the UCalendar's time zone. 
+ * 
+ * @param cal           The UCalendar to query. 
+ * @param result        Receives the UCalendar's time zone ID. 
+ * @param resultLength  The maximum size of result. 
+ * @param status        Receives the status. 
+ * @return              The total buffer size needed; if greater than resultLength, the output was truncated. 
+ * @draft ICU 51 
+ */ 
+U_DRAFT int32_t U_EXPORT2 
+ucal_getTimeZoneID(const UCalendar *cal,
+                   UChar *result,
+                   int32_t resultLength,
+                   UErrorCode *status);
+
 /**
  * Possible formats for a UCalendar's display name 
  * @stable ICU 2.0
index 7c51424052f0cf7fd4e75f7539aa89c137bfeb8d..d0fd9a87de227438854bdaab8fd5e849c5a92e03 100644 (file)
@@ -1,5 +1,5 @@
 /********************************************************************
- * Copyright (c) 1997-2012, International Business Machines
+ * Copyright (c) 1997-2013, International Business Machines
  * Corporation and others. All Rights Reserved.
  ********************************************************************
  *
@@ -492,6 +492,8 @@ static void TestGetSetDateAPI()
     UChar temp[30];
        double testMillis;
        int32_t dateBit;
+    UChar id[4];
+    int32_t idLen;
 
     log_verbose("\nOpening the calendars()\n");
     u_strcpy(tzID, fgGMTID);
@@ -509,7 +511,6 @@ static void TestGetSetDateAPI()
         log_data_err("error in creating the dateformat : %s (Are you missing data?)\n", u_errorName(status));
         return;
     }
-    
 
     /*Testing getMillis and setMillis */
     log_verbose("\nTesting the date and time fetched in millis for a calendar using getMillis\n");
@@ -582,11 +583,20 @@ static void TestGetSetDateAPI()
     
     ctest_setTimeZone(NULL, &status);
 
-    /*testing ucal_setTimeZone() function*/
-    log_verbose("\nTesting if the function ucal_setTimeZone() works fine\n");
+    /*testing ucal_setTimeZone() and ucal_getTimeZoneID function*/
+    log_verbose("\nTesting if the function ucal_setTimeZone() and ucal_getTimeZoneID work fine\n");
+    idLen = ucal_getTimeZoneID(caldef2, id, sizeof(id)/sizeof(id[0]), &status);
+    if (U_FAILURE(status)) {
+        log_err("Error in getTimeZoneID : %s\n", u_errorName(status));
+    } else if (u_strcmp(id, fgGMTID) != 0) {
+        log_err("FAIL: getTimeZoneID returns a wrong ID: actual=%d, expected=%s\n", austrdup(id), austrdup(fgGMTID));
+    } else {
+        log_verbose("PASS: getTimeZoneID works fine\n");
+    }
+
     ucal_setMillis(caldef2, d2, &status); 
     if(U_FAILURE(status)){
-        log_err("Error in getMillis : %s\n", u_errorName(status));;
+        log_err("Error in getMillis : %s\n", u_errorName(status));
     }
     hour=ucal_get(caldef2, UCAL_HOUR_OF_DAY, &status);
         
@@ -597,6 +607,16 @@ static void TestGetSetDateAPI()
     }
     else
         log_verbose("ucal_setTimeZone worked fine\n");
+
+    idLen = ucal_getTimeZoneID(caldef2, id, sizeof(id)/sizeof(id[0]), &status);
+    if (U_FAILURE(status)) {
+        log_err("Error in getTimeZoneID : %s\n", u_errorName(status));
+    } else if (u_strcmp(id, tzID) != 0) {
+        log_err("FAIL: getTimeZoneID returns a wrong ID: actual=%d, expected=%s\n", austrdup(id), austrdup(tzID));
+    } else {
+        log_verbose("PASS: getTimeZoneID works fine\n");
+    }
+
     if(hour == ucal_get(caldef2, UCAL_HOUR_OF_DAY, &status))
         log_err("FAIL: Error setting the time zone doesn't change the represented time\n");
     else if((hour-8 + 1) != ucal_get(caldef2, UCAL_HOUR_OF_DAY, &status)) /*because it is not in daylight savings time */