ICU-6492 Add override based constructors for SimpleDateFormat

X-SVN-Rev: 25435
This commit is contained in:
John Emmons 2009-02-18 13:29:17 +00:00
parent 9b6fa39608
commit 9f6245828b
3 changed files with 116 additions and 6 deletions

View File

@ -217,6 +217,28 @@ SimpleDateFormat::SimpleDateFormat(const UnicodeString& pattern,
initializeSymbols(fLocale, initializeCalendar(NULL,fLocale,status), status);
initialize(fLocale, status);
initializeDefaultCentury();
}
//----------------------------------------------------------------------
SimpleDateFormat::SimpleDateFormat(const UnicodeString& pattern,
const UnicodeString& override,
UErrorCode &status)
: fPattern(pattern),
fLocale(Locale::getDefault()),
fSymbols(NULL),
fGMTFormatters(NULL),
fNumberFormatters(NULL),
fOverrideList(NULL)
{
fDateOverride.setTo(override);
fTimeOverride.setToBogus();
initializeSymbols(fLocale, initializeCalendar(NULL,fLocale,status), status);
initialize(fLocale, status);
initializeDefaultCentury();
processOverrideString(fLocale,override,kOvrStrBoth,status);
}
//----------------------------------------------------------------------
@ -241,6 +263,30 @@ SimpleDateFormat::SimpleDateFormat(const UnicodeString& pattern,
//----------------------------------------------------------------------
SimpleDateFormat::SimpleDateFormat(const UnicodeString& pattern,
const UnicodeString& override,
const Locale& locale,
UErrorCode& status)
: fPattern(pattern),
fLocale(locale),
fGMTFormatters(NULL),
fNumberFormatters(NULL),
fOverrideList(NULL)
{
fDateOverride.setTo(override);
fTimeOverride.setToBogus();
initializeSymbols(fLocale, initializeCalendar(NULL,fLocale,status), status);
initialize(fLocale, status);
initializeDefaultCentury();
processOverrideString(locale,override,kOvrStrBoth,status);
}
//----------------------------------------------------------------------
SimpleDateFormat::SimpleDateFormat(const UnicodeString& pattern,
DateFormatSymbols* symbolsToAdopt,
UErrorCode& status)
@ -1235,8 +1281,7 @@ SimpleDateFormat::initNumberFormatters(const Locale &locale,UErrorCode &status)
}
void
SimpleDateFormat::processOverrideString(const Locale &locale, UnicodeString &str, int8_t type, UErrorCode &status) {
SimpleDateFormat::processOverrideString(const Locale &locale, const UnicodeString &str, int8_t type, UErrorCode &status) {
if (str.isBogus()) {
return;
}

View File

@ -237,6 +237,31 @@ public:
SimpleDateFormat(const UnicodeString& pattern,
UErrorCode& status);
/**
* Construct a SimpleDateFormat using the given pattern, numbering system override, and the default locale.
* The locale is used to obtain the symbols used in formatting (e.g., the
* names of the months), but not to provide the pattern.
* <P>
* A numbering system override is a string containing either the name of a known numbering system,
* or a set of field and numbering system pairs that specify which fields are to be formattied with
* the alternate numbering system. For example, to specify that all numeric fields in the specified
* date or time pattern are to be rendered using Thai digits, simply specify the numbering system override
* as "thai". To specify that just the year portion of the date be formatted using Hebrew numbering,
* use the override string "y=hebrew". Numbering system overrides can be combined using a semi-colon
* character in the override string, such as "d=decimal;M=arabic;y=hebrew", etc.
*
* <P>
* [Note:] Not all locales support SimpleDateFormat; for full generality,
* use the factory methods in the DateFormat class.
* @param pattern the pattern for the format.
* @param override the override string.
* @param status Output param set to success/failure code.
* @draft ICU 4.2
*/
SimpleDateFormat(const UnicodeString& pattern,
const UnicodeString& override,
UErrorCode& status);
/**
* Construct a SimpleDateFormat using the given pattern and locale.
* The locale is used to obtain the symbols used in formatting (e.g., the
@ -253,6 +278,32 @@ public:
const Locale& locale,
UErrorCode& status);
/**
* Construct a SimpleDateFormat using the given pattern, numbering system override, and locale.
* The locale is used to obtain the symbols used in formatting (e.g., the
* names of the months), but not to provide the pattern.
* <P>
* A numbering system override is a string containing either the name of a known numbering system,
* or a set of field and numbering system pairs that specify which fields are to be formattied with
* the alternate numbering system. For example, to specify that all numeric fields in the specified
* date or time pattern are to be rendered using Thai digits, simply specify the numbering system override
* as "thai". To specify that just the year portion of the date be formatted using Hebrew numbering,
* use the override string "y=hebrew". Numbering system overrides can be combined using a semi-colon
* character in the override string, such as "d=decimal;M=arabic;y=hebrew", etc.
* <P>
* [Note:] Not all locales support SimpleDateFormat; for full generality,
* use the factory methods in the DateFormat class.
* @param pattern the pattern for the format.
* @param override the numbering system override.
* @param locale the given locale.
* @param status Output param set to success/failure code.
* @draft ICU 4.2
*/
SimpleDateFormat(const UnicodeString& pattern,
const UnicodeString& override,
const Locale& locale,
UErrorCode& status);
/**
* Construct a SimpleDateFormat using the given pattern and locale-specific
* symbol data. The formatter takes ownership of the DateFormatSymbols object;
@ -901,7 +952,7 @@ private:
/**
* Parse the given override string and set up structures for number formats
*/
void processOverrideString(const Locale &locale, UnicodeString &str, int8_t type, UErrorCode &status);
void processOverrideString(const Locale &locale, const UnicodeString &str, int8_t type, UErrorCode &status);
/**
* Used to map pattern characters to Calendar field identifiers.

View File

@ -1,7 +1,7 @@
/********************************************************************
* COPYRIGHT:
* Copyright (c) 1997-2008, International Business Machines Corporation and
* Copyright (c) 1997-2009, International Business Machines Corporation and
* others. All Rights Reserved.
********************************************************************/
@ -59,6 +59,8 @@ void IntlTestSimpleDateFormatAPI::testAPI(/*char *par*/)
status = U_ZERO_ERROR;
const UnicodeString pattern("yyyy.MM.dd G 'at' hh:mm:ss z");
const UnicodeString override("y=hebrew;d=thai;s=arabic");
SimpleDateFormat pat(pattern, status);
if(U_FAILURE(status)) {
errln("ERROR: Could not create SimpleDateFormat (pattern)");
@ -89,6 +91,19 @@ void IntlTestSimpleDateFormatAPI::testAPI(/*char *par*/)
errln("ERROR: Could not create SimpleDateFormat (pattern, symbols)");
}
status = U_ZERO_ERROR;
SimpleDateFormat ovr1(pattern, override, status);
if(U_FAILURE(status)) {
errln("ERROR: Could not create SimpleDateFormat (pattern, override)");
}
status = U_ZERO_ERROR;
SimpleDateFormat ovr2(pattern, override, Locale::getGerman(), status);
if(U_FAILURE(status)) {
errln("ERROR: Could not create SimpleDateFormat (pattern, override, locale)");
}
SimpleDateFormat copy(pat);
// ======= Test clone(), assignment, and equality
@ -235,13 +250,12 @@ void IntlTestSimpleDateFormatAPI::testAPI(/*char *par*/)
delete test;
// ======= Test Ticket 5684 (Parsing with 'e' and 'Y')
SimpleDateFormat object("en", "us", status);
SimpleDateFormat object(UNICODE_STRING_SIMPLE("YYYY'W'wwe"), status);
if(U_FAILURE(status)) {
errln("ERROR: Couldn't create a SimpleDateFormat");
}
object.setLenient(false);
ParsePosition pp(0);
object.applyPattern("YYYY'W'wwe");
UDate udDate = object.parse("2007W014", pp);
if ((double)udDate == 0.0) {
errln("ERROR: Parsing failed using 'Y' and 'e'");