ICU-9735 Added a C API ucal_getTimeZoneID.

X-SVN-Rev: 33005
This commit is contained in:
Yoshito Umaoka 2013-01-02 16:58:24 +00:00
parent 162a3e851a
commit 08223859e3
3 changed files with 58 additions and 7 deletions

View 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. * 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 U_CAPI int32_t U_EXPORT2
ucal_getTimeZoneDisplayName(const UCalendar* cal, ucal_getTimeZoneDisplayName(const UCalendar* cal,
UCalendarDisplayNameType type, UCalendarDisplayNameType type,

View 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. * others. All Rights Reserved.
******************************************************************************* *******************************************************************************
*/ */
@ -745,6 +745,22 @@ ucal_setTimeZone(UCalendar* cal,
int32_t len, int32_t len,
UErrorCode* status); 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 * Possible formats for a UCalendar's display name
* @stable ICU 2.0 * @stable ICU 2.0

View 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. * Corporation and others. All Rights Reserved.
******************************************************************** ********************************************************************
* *
@ -492,6 +492,8 @@ static void TestGetSetDateAPI()
UChar temp[30]; UChar temp[30];
double testMillis; double testMillis;
int32_t dateBit; int32_t dateBit;
UChar id[4];
int32_t idLen;
log_verbose("\nOpening the calendars()\n"); log_verbose("\nOpening the calendars()\n");
u_strcpy(tzID, fgGMTID); 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)); log_data_err("error in creating the dateformat : %s (Are you missing data?)\n", u_errorName(status));
return; return;
} }
/*Testing getMillis and setMillis */ /*Testing getMillis and setMillis */
log_verbose("\nTesting the date and time fetched in millis for a calendar using getMillis\n"); 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); ctest_setTimeZone(NULL, &status);
/*testing ucal_setTimeZone() function*/ /*testing ucal_setTimeZone() and ucal_getTimeZoneID function*/
log_verbose("\nTesting if the function ucal_setTimeZone() works fine\n"); 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); ucal_setMillis(caldef2, d2, &status);
if(U_FAILURE(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); hour=ucal_get(caldef2, UCAL_HOUR_OF_DAY, &status);
@ -597,6 +607,16 @@ static void TestGetSetDateAPI()
} }
else else
log_verbose("ucal_setTimeZone worked fine\n"); 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)) 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"); 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 */ else if((hour-8 + 1) != ucal_get(caldef2, UCAL_HOUR_OF_DAY, &status)) /*because it is not in daylight savings time */