ICU-5561 Added another version of getCanonicalID which tells you if the given ID is a system timezon in C++. Updated ucal_getCanonicalTimeZone to support the feature as well.

X-SVN-Rev: 23354
This commit is contained in:
Yoshito Umaoka 2008-02-05 19:14:43 +00:00
parent 88193c9aaf
commit ba7bd65b8f
6 changed files with 87 additions and 36 deletions

View File

@ -334,16 +334,16 @@ static UBool loadOlsonIDs() {
ids = new UnicodeString[(count > 0) ? count : 1]; ids = new UnicodeString[(count > 0) ? count : 1];
// Null pointer check // Null pointer check
if (ids != NULL) { if (ids != NULL) {
for (int32_t i=0; i<count; ++i) { for (int32_t i=0; i<count; ++i) {
int32_t idLen = 0; int32_t idLen = 0;
const UChar* id = ures_getStringByIndex(nres, i, &idLen, &ec); const UChar* id = ures_getStringByIndex(nres, i, &idLen, &ec);
ids[i].fastCopyFrom(UnicodeString(TRUE, id, idLen)); ids[i].fastCopyFrom(UnicodeString(TRUE, id, idLen));
if (U_FAILURE(ec)) { if (U_FAILURE(ec)) {
break; break;
} }
} }
} else { } else {
ec = U_MEMORY_ALLOCATION_ERROR; ec = U_MEMORY_ALLOCATION_ERROR;
} }
} }
ures_close(nres); ures_close(nres);
@ -465,9 +465,9 @@ TimeZone::createTimeZone(const UnicodeString& ID)
U_DEBUG_TZ_MSG(("failed to load time zone with id - falling to GMT")); U_DEBUG_TZ_MSG(("failed to load time zone with id - falling to GMT"));
const TimeZone* temptz = getGMT(); const TimeZone* temptz = getGMT();
if (temptz == NULL) { if (temptz == NULL) {
result = NULL; result = NULL;
} else { } else {
result = temptz->clone(); result = temptz->clone();
} }
} }
return result; return result;
@ -624,11 +624,11 @@ TimeZone::initDefault()
// If we _still_ don't have a time zone, use GMT. // If we _still_ don't have a time zone, use GMT.
if (default_zone == NULL) { if (default_zone == NULL) {
const TimeZone* temptz = getGMT(); const TimeZone* temptz = getGMT();
// If we can't use GMT, get out. // If we can't use GMT, get out.
if (temptz == NULL) { if (temptz == NULL) {
return; return;
} }
default_zone = temptz->clone(); default_zone = temptz->clone();
} }
@ -1443,13 +1443,24 @@ TimeZone::getTZDataVersion(UErrorCode& status)
UnicodeString& UnicodeString&
TimeZone::getCanonicalID(const UnicodeString& id, UnicodeString& canonicalID, UErrorCode& status) TimeZone::getCanonicalID(const UnicodeString& id, UnicodeString& canonicalID, UErrorCode& status)
{
UBool isSystemID = FALSE;
return getCanonicalID(id, canonicalID, isSystemID, status);
}
UnicodeString&
TimeZone::getCanonicalID(const UnicodeString& id, UnicodeString& canonicalID, UBool& isSystemID,
UErrorCode& status)
{ {
canonicalID.remove(); canonicalID.remove();
isSystemID = FALSE;
if (U_FAILURE(status)) { if (U_FAILURE(status)) {
return canonicalID; return canonicalID;
} }
ZoneMeta::getCanonicalSystemID(id, canonicalID, status); ZoneMeta::getCanonicalSystemID(id, canonicalID, status);
if (U_FAILURE(status)) { if (U_SUCCESS(status)) {
isSystemID = TRUE;
} else {
// Not a system ID // Not a system ID
status = U_ZERO_ERROR; status = U_ZERO_ERROR;
getCustomID(id, canonicalID, status); getCustomID(id, canonicalID, status);

View File

@ -471,18 +471,25 @@ ucal_getTZDataVersion(UErrorCode* status)
U_CAPI int32_t U_EXPORT2 U_CAPI int32_t U_EXPORT2
ucal_getCanonicalTimeZoneID(const UChar* id, int32_t len, ucal_getCanonicalTimeZoneID(const UChar* id, int32_t len,
UChar* result, int32_t resultCapacity, UErrorCode* status) { UChar* result, int32_t resultCapacity, UBool *isSystemID, UErrorCode* status) {
if(status == 0 || U_FAILURE(*status)) { if(status == 0 || U_FAILURE(*status)) {
return 0; return 0;
} }
if (isSystemID) {
*isSystemID = FALSE;
}
if (id == 0 || len == 0 || result == 0 || resultCapacity <= 0) { if (id == 0 || len == 0 || result == 0 || resultCapacity <= 0) {
*status = U_ILLEGAL_ARGUMENT_ERROR; *status = U_ILLEGAL_ARGUMENT_ERROR;
return 0; return 0;
} }
int32_t reslen = 0; int32_t reslen = 0;
UnicodeString canonical; UnicodeString canonical;
TimeZone::getCanonicalID(UnicodeString(id, len), canonical, *status); UBool systemID = FALSE;
TimeZone::getCanonicalID(UnicodeString(id, len), canonical, systemID, *status);
if (U_SUCCESS(*status)) { if (U_SUCCESS(*status)) {
if (isSystemID) {
*isSystemID = systemID;
}
reslen = canonical.extract(result, resultCapacity, *status); reslen = canonical.extract(result, resultCapacity, *status);
} }
return reslen; return reslen;

View File

@ -349,6 +349,24 @@ public:
static UnicodeString& U_EXPORT2 getCanonicalID(const UnicodeString& id, static UnicodeString& U_EXPORT2 getCanonicalID(const UnicodeString& id,
UnicodeString& canonicalID, UErrorCode& status); UnicodeString& canonicalID, UErrorCode& status);
/**
* Returns the canonical system timezone ID or the normalized
* custom time zone ID for the given time zone ID.
* @param id The input timezone ID to be canonicalized.
* @param canonicalID Receives the canonical system timezone ID
* or the custom timezone ID in normalized format.
* @param isSystemID Receives if the given ID is a known system
* timezone ID.
* @param status Recevies the status. When the given timezone ID
* is neither a known system time zone ID nor a
* valid custom timezone ID, U_ILLEGAL_ARGUMENT_ERROR
* is set.
* @return A reference to the result.
* @draft ICU 4.0
*/
static UnicodeString& U_EXPORT2 getCanonicalID(const UnicodeString& id,
UnicodeString& canonicalID, UBool& isSystemID, UErrorCode& status);
/** /**
* Returns true if the two TimeZones are equal. (The TimeZone version only compares * Returns true if the two TimeZones are equal. (The TimeZone version only compares
* IDs, but subclasses are expected to also compare the fields they add.) * IDs, but subclasses are expected to also compare the fields they add.)

View File

@ -1098,6 +1098,8 @@ ucal_getTZDataVersion(UErrorCode* status);
* @param result The buffer receives the canonical system timezone ID * @param result The buffer receives the canonical system timezone ID
* or the custom timezone ID in normalized format. * or the custom timezone ID in normalized format.
* @param resultCapacity The capacity of the result buffer. * @param resultCapacity The capacity of the result buffer.
* @param isSystemID Receives if the given ID is a known system
* timezone ID.
* @param status Recevies the status. When the given timezone ID * @param status Recevies the status. When the given timezone ID
* is neither a known system time zone ID nor a * is neither a known system time zone ID nor a
* valid custom timezone ID, U_ILLEGAL_ARGUMENT_ERROR * valid custom timezone ID, U_ILLEGAL_ARGUMENT_ERROR
@ -1108,7 +1110,7 @@ ucal_getTZDataVersion(UErrorCode* status);
*/ */
U_DRAFT int32_t U_EXPORT2 U_DRAFT int32_t U_EXPORT2
ucal_getCanonicalTimeZoneID(const UChar* id, int32_t len, ucal_getCanonicalTimeZoneID(const UChar* id, int32_t len,
UChar* result, int32_t resultCapacity, UErrorCode* status); UChar* result, int32_t resultCapacity, UBool *isSystemID, UErrorCode* status);
#endif /* #if !UCONFIG_NO_FORMATTING */ #endif /* #if !UCONFIG_NO_FORMATTING */
#endif #endif

View File

@ -72,6 +72,7 @@ static void TestCalendar()
UChar zone1[32], zone2[32]; UChar zone1[32], zone2[32];
const char *tzver = 0; const char *tzver = 0;
UChar canonicalID[64]; UChar canonicalID[64];
UBool isSystemID = FALSE;
#ifdef U_USE_UCAL_OBSOLETE_2_8 #ifdef U_USE_UCAL_OBSOLETE_2_8
/*Testing countAvailableTimeZones*/ /*Testing countAvailableTimeZones*/
@ -197,12 +198,18 @@ static void TestCalendar()
/*Testing ucal_getCanonicalTimeZoneID*/ /*Testing ucal_getCanonicalTimeZoneID*/
status = U_ZERO_ERROR; status = U_ZERO_ERROR;
resultlength = ucal_getCanonicalTimeZoneID(PST, -1, resultlength = ucal_getCanonicalTimeZoneID(PST, -1,
canonicalID, sizeof(canonicalID)/sizeof(UChar), &status); canonicalID, sizeof(canonicalID)/sizeof(UChar), &isSystemID, &status);
if (U_FAILURE(status)) { if (U_FAILURE(status)) {
log_err("FAIL: error in ucal_getCanonicalTimeZoneID : %s\n", u_errorName(status)); log_err("FAIL: error in ucal_getCanonicalTimeZoneID : %s\n", u_errorName(status));
} else if (u_strcmp(AMERICA_LOS_ANGELES, canonicalID) != 0) { } else {
log_err("FAIL: ucal_getCanonicalTimeZoneID(%s) returned %s : expected - %s\n", if (u_strcmp(AMERICA_LOS_ANGELES, canonicalID) != 0) {
PST, canonicalID, AMERICA_LOS_ANGELES); log_err("FAIL: ucal_getCanonicalTimeZoneID(%s) returned %s : expected - %s\n",
PST, canonicalID, AMERICA_LOS_ANGELES);
}
if (!isSystemID) {
log_err("FAIL: ucal_getCanonicalTimeZoneID(%s) set %d to isSystemID\n",
PST, isSystemID);
}
} }
/*Testing the ucal_open() function*/ /*Testing the ucal_open() function*/

View File

@ -1766,22 +1766,24 @@ void TimeZoneTest::TestCanonicalID() {
struct { struct {
const char *id; const char *id;
const char *expected; const char *expected;
const UBool isSystem;
} data[] = { } data[] = {
{"GMT-03", "GMT-0300"}, {"GMT-03", "GMT-0300", FALSE},
{"GMT+4", "GMT+0400"}, {"GMT+4", "GMT+0400", FALSE},
{"GMT-055", "GMT-0055"}, {"GMT-055", "GMT-0055", FALSE},
{"GMT+430", "GMT+0430"}, {"GMT+430", "GMT+0430", FALSE},
{"GMT-12:15", "GMT-1215"}, {"GMT-12:15", "GMT-1215", FALSE},
{"GMT-091015", "GMT-091015"}, {"GMT-091015", "GMT-091015", FALSE},
{"GMT+1:90", 0}, {"GMT+1:90", 0, FALSE},
{"America/Argentina/Buenos_Aires", "America/Buenos_Aires"}, {"America/Argentina/Buenos_Aires", "America/Buenos_Aires", TRUE},
{"bogus", 0}, {"bogus", 0, FALSE},
{"", 0}, {"", 0, FALSE},
{0, 0} {0, 0, FALSE}
}; };
UBool isSystemID;
for (i = 0; data[i].id != 0; i++) { for (i = 0; data[i].id != 0; i++) {
TimeZone::getCanonicalID(UnicodeString(data[i].id), canonicalID, ec); TimeZone::getCanonicalID(UnicodeString(data[i].id), canonicalID, isSystemID, ec);
if (U_FAILURE(ec)) { if (U_FAILURE(ec)) {
if (ec != U_ILLEGAL_ARGUMENT_ERROR || data[i].expected != 0) { if (ec != U_ILLEGAL_ARGUMENT_ERROR || data[i].expected != 0) {
errln((UnicodeString)"FAIL: getCanonicalID(\"" + data[i].id errln((UnicodeString)"FAIL: getCanonicalID(\"" + data[i].id
@ -1794,6 +1796,10 @@ void TimeZoneTest::TestCanonicalID() {
errln((UnicodeString)"FAIL: getCanonicalID(\"" + data[i].id errln((UnicodeString)"FAIL: getCanonicalID(\"" + data[i].id
+ "\") returned " + canonicalID + " - expected: " + data[i].expected); + "\") returned " + canonicalID + " - expected: " + data[i].expected);
} }
if (isSystemID != data[i].isSystem) {
errln((UnicodeString)"FAIL: getCanonicalID(\"" + data[i].id
+ "\") set " + isSystemID + " to isSystemID");
}
} }
} }