ICU-4735 Enhanced TimeZone getDisplayName allows for more name formats - ICU4C

X-SVN-Rev: 26553
This commit is contained in:
Jason Spieth 2009-08-26 16:55:52 +00:00
parent 7b753884ce
commit d97a5d4837
3 changed files with 100 additions and 8 deletions

View File

@ -100,6 +100,12 @@ static char gStrBuf[256];
static const UChar GMT_ID[] = {0x47, 0x4D, 0x54, 0x00}; /* "GMT" */
static const UChar Z_STR[] = {0x7A, 0x00}; /* "z" */
static const UChar ZZZZ_STR[] = {0x7A, 0x7A, 0x7A, 0x7A, 0x00}; /* "zzzz" */
static const UChar Z_UC_STR[] = {0x5A, 0x00}; /* "Z" */
static const UChar ZZZZ_UC_STR[] = {0x5A, 0x5A, 0x5A, 0x5A, 0x00}; /* "ZZZZ" */
static const UChar V_STR[] = {0x76, 0x00}; /* "v" */
static const UChar VVVV_STR[] = {0x76, 0x76, 0x76, 0x76, 0x00}; /* "vvvv" */
static const UChar V_UC_STR[] = {0x56, 0x00}; /* "V" */
static const UChar VVVV_UC_STR[] = {0x56, 0x56, 0x56, 0x56, 0x00}; /* "VVVV" */
static const int32_t GMT_ID_LENGTH = 3;
static UMTX LOCK;
@ -1140,7 +1146,37 @@ TimeZone::getDisplayName(UBool daylight, EDisplayType style, const Locale& local
char buf[128];
fID.extract(0, sizeof(buf)-1, buf, sizeof(buf), "");
#endif
SimpleDateFormat format(style == LONG ? ZZZZ_STR : Z_STR, locale, status);
// select the proper format string
UnicodeString pat;
switch(style){
case LONG:
pat = ZZZZ_STR;
break;
case SHORT_GENERIC:
pat = V_STR;
break;
case LONG_GENERIC:
pat = VVVV_STR;
break;
case SHORT_GMT:
pat = Z_UC_STR;
break;
case LONG_GMT:
pat = ZZZZ_UC_STR;
break;
case SHORT_COMMONLY_USED:
pat = V_UC_STR;
break;
case GENERIC_LOCATION:
pat = VVVV_UC_STR;
break;
default: // SHORT
pat = Z_STR;
break;
}
SimpleDateFormat format(pat, locale, status);
U_DEBUG_TZ_MSG(("getDisplayName(%s)\n", buf));
if(!U_SUCCESS(status))
{
@ -1160,7 +1196,11 @@ TimeZone::getDisplayName(UBool daylight, EDisplayType style, const Locale& local
return result.remove();
}
if ((daylight && dstOffset != 0) || (!daylight && dstOffset == 0)) {
if ((daylight && dstOffset != 0) ||
(!daylight && dstOffset == 0) ||
(style == SHORT_GENERIC) ||
(style == LONG_GENERIC)
) {
// Current time and the request (daylight / not daylight) agree.
format.setTimeZone(*this);
return format.format(d, result);
@ -1221,7 +1261,6 @@ TimeZone::getDisplayName(UBool daylight, EDisplayType style, const Locale& local
return result;
}
/**
* Parse a custom time zone identifier and return a corresponding zone.
* @param id a string of the form GMT[+-]hh:mm, GMT[+-]hhmm, or

View File

@ -521,7 +521,41 @@ public:
* Selector for long display name
* @stable ICU 2.4
*/
LONG
LONG,
/**
* Selector for short generic display name
* @draft ICU 4.4
*/
SHORT_GENERIC,
/**
* Selector for long generic display name
* @draft ICU 4.4
*/
LONG_GENERIC,
/**
* Selector for short display name derived
* from time zone offset
* @draft ICU 4.4
*/
SHORT_GMT,
/**
* Selector for long display name derived
* from time zone offset
* @draft ICU 4.4
*/
LONG_GMT,
/**
* Selector for short display name derived
* from the time zone's fallback name
* @draft ICU 4.4
*/
SHORT_COMMONLY_USED,
/**
* Selector for long display name derived
* from the time zone's fallback name
* @draft ICU 4.4
*/
GENERIC_LOCATION
};
/**
@ -559,10 +593,10 @@ public:
* then this method returns a string in the format
* <code>GMT[+-]hh:mm</code>.
* @param daylight if true, return the daylight savings name.
* @param style either <code>LONG</code> or <code>SHORT</code>
* @param style
* @param result the human-readable name of this time zone in the default locale.
* @return A reference to 'result'.
* @stable ICU 2.0
* @draft ICU 4.4
*/
UnicodeString& getDisplayName(UBool daylight, EDisplayType style, UnicodeString& result) const;
@ -573,15 +607,18 @@ public:
* then this method returns a string in the format
* <code>GMT[+-]hh:mm</code>.
* @param daylight if true, return the daylight savings name.
* @param style either <code>LONG</code> or <code>SHORT</code>
* @param style
* @param locale the locale in which to supply the display name.
* @param result the human-readable name of this time zone in the given locale
* or in the default locale if the given locale is not recognized.
* @return A refence to 'result'.
* @stable ICU 2.0
* @draft ICU 4.4
*/
UnicodeString& getDisplayName(UBool daylight, EDisplayType style, const Locale& locale, UnicodeString& result) const;
void setDefaultTimeZoneNameStyle(EDisplayType style);
EDisplayType getDefaultTimeZoneNameStyle();
/**
* Queries if this time zone uses daylight savings time.
* @return true if this time zone uses daylight savings time,
@ -775,6 +812,7 @@ private:
static TimeZone* createSystemTimeZone(const UnicodeString& name);
UnicodeString fID; // this time zone's ID
};

View File

@ -1195,6 +1195,21 @@ TimeZoneTest::TestDisplayName()
{FALSE, TimeZone::LONG, "Pacific Standard Time"},
{TRUE, TimeZone::LONG, "Pacific Daylight Time"},
{FALSE, TimeZone::SHORT_GENERIC, "PT"},
{TRUE, TimeZone::SHORT_GENERIC, "PT"},
{FALSE, TimeZone::LONG_GENERIC, "Pacific Time"},
{TRUE, TimeZone::LONG_GENERIC, "Pacific Time"},
{FALSE, TimeZone::SHORT_GMT, "-0800"},
{TRUE, TimeZone::SHORT_GMT, "-0700"},
{FALSE, TimeZone::LONG_GMT, "GMT-08:00"},
{TRUE, TimeZone::LONG_GMT, "GMT-07:00"},
{FALSE, TimeZone::SHORT_COMMONLY_USED, "PST"},
{TRUE, TimeZone::SHORT_COMMONLY_USED, "PDT"},
{FALSE, TimeZone::GENERIC_LOCATION, "United States (Los Angeles)"},
{TRUE, TimeZone::GENERIC_LOCATION, "United States (Los Angeles)"},
{FALSE, TimeZone::LONG, ""}
};