ICU-162 add DateFormat parse() and format() methods taking a Calendar
X-SVN-Rev: 7857
This commit is contained in:
parent
6997c52395
commit
34a2aecffc
@ -120,6 +120,21 @@ DateFormat::format(const Formattable& obj,
|
||||
|
||||
//----------------------------------------------------------------------
|
||||
|
||||
UnicodeString&
|
||||
DateFormat::format(UDate date, UnicodeString& result, FieldPosition& fieldPosition) const {
|
||||
if (fCalendar != NULL) {
|
||||
// Use our calendar instance
|
||||
UErrorCode ec = U_ZERO_ERROR;
|
||||
fCalendar->setTime(date, ec);
|
||||
if (U_SUCCESS(ec)) {
|
||||
return format(*fCalendar, result, fieldPosition);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------
|
||||
|
||||
UnicodeString&
|
||||
DateFormat::format(UDate date, UnicodeString& result) const
|
||||
{
|
||||
@ -132,6 +147,32 @@ DateFormat::format(UDate date, UnicodeString& result) const
|
||||
|
||||
//----------------------------------------------------------------------
|
||||
|
||||
UDate
|
||||
DateFormat::parse(const UnicodeString& text,
|
||||
ParsePosition& pos) const
|
||||
{
|
||||
if (fCalendar != NULL) {
|
||||
int32_t start = pos.getIndex();
|
||||
fCalendar->clear();
|
||||
parse(text, *fCalendar, pos);
|
||||
if (pos.getIndex() != start) {
|
||||
UErrorCode ec = U_ZERO_ERROR;
|
||||
UDate d = fCalendar->getTime(ec);
|
||||
if (U_SUCCESS(ec)) {
|
||||
return d; // Successful function exit
|
||||
}
|
||||
// We arrive here if fCalendar is non-lenient and there
|
||||
// is an out-of-range field. We don't know which field
|
||||
// was illegal so we set the error index to the start.
|
||||
pos.setIndex(start);
|
||||
pos.setErrorIndex(start);
|
||||
}
|
||||
}
|
||||
return 0; // Error return UDate is 0 (the epoch)
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------
|
||||
|
||||
UDate
|
||||
DateFormat::parse(const UnicodeString& text,
|
||||
UErrorCode& status) const
|
||||
|
@ -380,21 +380,12 @@ void SimpleDateFormat::parseAmbiguousDatesAsAfter(UDate startDate, UErrorCode& s
|
||||
//----------------------------------------------------------------------
|
||||
|
||||
UnicodeString&
|
||||
SimpleDateFormat::format(UDate date, UnicodeString& toAppendTo, FieldPosition& pos) const
|
||||
SimpleDateFormat::format(Calendar& cal, UnicodeString& toAppendTo, FieldPosition& pos) const
|
||||
{
|
||||
if (fCalendar == 0) {
|
||||
return toAppendTo;
|
||||
}
|
||||
|
||||
UErrorCode status = U_ZERO_ERROR;
|
||||
pos.setBeginIndex(0);
|
||||
pos.setEndIndex(0);
|
||||
|
||||
// load up our Calendar with the date/time we're formatting (the subroutines of this
|
||||
// function pick it up from there, since they need it anyway to split the value
|
||||
// into fields)
|
||||
fCalendar->setTime(date, status);
|
||||
|
||||
UBool inQuote = FALSE;
|
||||
UChar prevCh = 0;
|
||||
int32_t count = 0;
|
||||
@ -407,7 +398,7 @@ SimpleDateFormat::format(UDate date, UnicodeString& toAppendTo, FieldPosition& p
|
||||
// Use subFormat() to format a repeated pattern character
|
||||
// when a different pattern or non-pattern character is seen
|
||||
if (ch != prevCh && count > 0) {
|
||||
toAppendTo += subFormat(str, prevCh, count, toAppendTo.length(), pos, status);
|
||||
toAppendTo += subFormat(str, prevCh, count, toAppendTo.length(), pos, cal, status);
|
||||
count = 0;
|
||||
}
|
||||
if (ch == 0x0027 /*'\''*/) {
|
||||
@ -435,7 +426,7 @@ SimpleDateFormat::format(UDate date, UnicodeString& toAppendTo, FieldPosition& p
|
||||
|
||||
// Format the last item in the pattern, if any
|
||||
if (count > 0) {
|
||||
toAppendTo += subFormat(str, prevCh, count, toAppendTo.length(), pos, status);
|
||||
toAppendTo += subFormat(str, prevCh, count, toAppendTo.length(), pos, cal, status);
|
||||
}
|
||||
|
||||
// and if something failed (e.g., an invalid format character), reset our FieldPosition
|
||||
@ -501,6 +492,7 @@ SimpleDateFormat::subFormat(UnicodeString& result,
|
||||
int32_t count,
|
||||
int32_t beginOffset,
|
||||
FieldPosition& pos,
|
||||
Calendar& cal,
|
||||
UErrorCode& status) const
|
||||
{
|
||||
// this function gets called by format() to produce the appropriate substitution
|
||||
@ -522,7 +514,7 @@ SimpleDateFormat::subFormat(UnicodeString& result,
|
||||
|
||||
patternCharIndex = (EField)(patternCharPtr - DateFormatSymbols::getPatternUChars());
|
||||
Calendar::EDateFields field = fgPatternIndexToCalendarField[patternCharIndex];
|
||||
int32_t value = fCalendar->get(field, status);
|
||||
int32_t value = cal.get(field, status);
|
||||
if (U_FAILURE(status)) return result;
|
||||
|
||||
switch (patternCharIndex) {
|
||||
@ -556,7 +548,7 @@ SimpleDateFormat::subFormat(UnicodeString& result,
|
||||
// for "k" and "kk", write out the hour, adjusting midnight to appear as "24"
|
||||
case kHourOfDay1Field:
|
||||
if (value == 0)
|
||||
zeroPaddingNumber(result, fCalendar->getMaximum(Calendar::HOUR_OF_DAY) + 1, count, maxIntCount);
|
||||
zeroPaddingNumber(result, cal.getMaximum(Calendar::HOUR_OF_DAY) + 1, count, maxIntCount);
|
||||
else
|
||||
zeroPaddingNumber(result, value, count, maxIntCount);
|
||||
break;
|
||||
@ -590,7 +582,7 @@ SimpleDateFormat::subFormat(UnicodeString& result,
|
||||
// as "12"
|
||||
case kHour1Field:
|
||||
if (value == 0)
|
||||
zeroPaddingNumber(result, fCalendar->getLeastMaximum(Calendar::HOUR) + 1, count, maxIntCount);
|
||||
zeroPaddingNumber(result, cal.getLeastMaximum(Calendar::HOUR) + 1, count, maxIntCount);
|
||||
else
|
||||
zeroPaddingNumber(result, value, count, maxIntCount);
|
||||
break;
|
||||
@ -602,12 +594,12 @@ SimpleDateFormat::subFormat(UnicodeString& result,
|
||||
// then the time zone shows up as "GMT+hh:mm" or "GMT-hh:mm" (where "hh:mm" is the
|
||||
// offset from GMT) regardless of how many z's were in the pattern symbol
|
||||
case kTimezoneField: {
|
||||
int32_t zoneIndex = fSymbols->getZoneIndex(fCalendar->getTimeZone().getID(str));
|
||||
int32_t zoneIndex = fSymbols->getZoneIndex(cal.getTimeZone().getID(str));
|
||||
if (zoneIndex == -1) {
|
||||
UnicodeString zoneString;
|
||||
|
||||
value = fCalendar->get(Calendar::ZONE_OFFSET, status) +
|
||||
fCalendar->get(Calendar::DST_OFFSET, status);
|
||||
value = cal.get(Calendar::ZONE_OFFSET, status) +
|
||||
cal.get(Calendar::DST_OFFSET, status);
|
||||
|
||||
if (value < 0) {
|
||||
zoneString += fgGmtMinus;
|
||||
@ -622,7 +614,7 @@ SimpleDateFormat::subFormat(UnicodeString& result,
|
||||
|
||||
result = zoneString;
|
||||
}
|
||||
else if (fCalendar->get(Calendar::DST_OFFSET, status) != 0) {
|
||||
else if (cal.get(Calendar::DST_OFFSET, status) != 0) {
|
||||
if (count >= 4)
|
||||
result = fSymbols->fZoneStrings[zoneIndex][3];
|
||||
else
|
||||
@ -716,15 +708,13 @@ parseFailureResult(ParsePosition& pos, ParsePosition& oldStart, ParsePosition& f
|
||||
}
|
||||
*/
|
||||
|
||||
UDate
|
||||
SimpleDateFormat::parse(const UnicodeString& text, ParsePosition& pos) const
|
||||
void
|
||||
SimpleDateFormat::parse(const UnicodeString& text, Calendar& cal, ParsePosition& pos) const
|
||||
{
|
||||
int32_t start = pos.getIndex();
|
||||
int32_t oldStart = start;
|
||||
UBool ambiguousYear[] = { FALSE };
|
||||
|
||||
fCalendar->clear();
|
||||
|
||||
UBool inQuote = FALSE;
|
||||
UChar prevCh = 0;
|
||||
int32_t count = 0;
|
||||
@ -752,8 +742,7 @@ SimpleDateFormat::parse(const UnicodeString& text, ParsePosition& pos) const
|
||||
{
|
||||
pos.setIndex(oldStart);
|
||||
pos.setErrorIndex(start);
|
||||
// {sfb} what is the correct Date for failure?
|
||||
return 0;
|
||||
return;
|
||||
}
|
||||
++start;
|
||||
}
|
||||
@ -769,8 +758,7 @@ SimpleDateFormat::parse(const UnicodeString& text, ParsePosition& pos) const
|
||||
// If fail to match, return null.
|
||||
pos.setIndex(oldStart); // left unchanged
|
||||
pos.setErrorIndex(start);
|
||||
// {sfb} what is correct Date for failure?
|
||||
return 0;
|
||||
return;
|
||||
}
|
||||
++count;
|
||||
++start;
|
||||
@ -787,12 +775,12 @@ SimpleDateFormat::parse(const UnicodeString& text, ParsePosition& pos) const
|
||||
if (count > 0)
|
||||
{
|
||||
int32_t startOffset = start;
|
||||
start = subParse(text, start, prevCh, count, FALSE, ambiguousYear);
|
||||
start = subParse(text, start, prevCh, count, FALSE, ambiguousYear, cal);
|
||||
if ( start < 0 ) {
|
||||
pos.setErrorIndex(startOffset);
|
||||
pos.setIndex(oldStart);
|
||||
// {sfb} correct Date
|
||||
return 0;
|
||||
return;
|
||||
}
|
||||
count = 0;
|
||||
}
|
||||
@ -808,7 +796,7 @@ SimpleDateFormat::parse(const UnicodeString& text, ParsePosition& pos) const
|
||||
pos.setErrorIndex(startOffset);
|
||||
pos.setIndex(oldStart);
|
||||
// {sfb} correct Date
|
||||
return 0;
|
||||
return;
|
||||
}
|
||||
++start;
|
||||
count = 1; // Make it look like we never left
|
||||
@ -829,12 +817,12 @@ SimpleDateFormat::parse(const UnicodeString& text, ParsePosition& pos) const
|
||||
// obeyCount. That's because the next field directly
|
||||
// abuts this one, so we have to use the count to know when
|
||||
// to stop parsing. [LIU]
|
||||
start = subParse(text, start, prevCh, count, TRUE, ambiguousYear);
|
||||
start = subParse(text, start, prevCh, count, TRUE, ambiguousYear, cal);
|
||||
if (start < 0) {
|
||||
pos.setErrorIndex(startOffset);
|
||||
pos.setIndex(oldStart);
|
||||
// {sfb} correct Date
|
||||
return 0;
|
||||
return;
|
||||
}
|
||||
prevCh = ch;
|
||||
count = 1;
|
||||
@ -852,20 +840,18 @@ SimpleDateFormat::parse(const UnicodeString& text, ParsePosition& pos) const
|
||||
// handle cases like: MM-dd-yy, HH:mm:ss, or yyyy MM dd,
|
||||
// where ch = '-', ':', or ' ', repectively.
|
||||
int32_t startOffset = start;
|
||||
start = subParse( text, start, prevCh, count, FALSE, ambiguousYear);
|
||||
start = subParse( text, start, prevCh, count, FALSE, ambiguousYear, cal);
|
||||
if ( start < 0 ) {
|
||||
pos.setErrorIndex(startOffset);
|
||||
pos.setIndex(oldStart);
|
||||
// {sfb} correct Date?
|
||||
return 0;
|
||||
return;
|
||||
}
|
||||
if (start >= text.length() || ch != text[start]) {
|
||||
// handle cases like: 'MMMM dd' in pattern vs. "janx20"
|
||||
// in time text, where ' ' doesn't match with 'x'.
|
||||
pos.setErrorIndex(start);
|
||||
pos.setIndex(oldStart);
|
||||
// {sfb} correct Date?
|
||||
return 0;
|
||||
return;
|
||||
}
|
||||
start++;
|
||||
count = 0;
|
||||
@ -882,8 +868,7 @@ SimpleDateFormat::parse(const UnicodeString& text, ParsePosition& pos) const
|
||||
|
||||
pos.setErrorIndex(start);
|
||||
pos.setIndex(oldStart);
|
||||
// {sfb} correct Date?
|
||||
return 0;
|
||||
return;
|
||||
}
|
||||
start++;
|
||||
}
|
||||
@ -898,12 +883,11 @@ SimpleDateFormat::parse(const UnicodeString& text, ParsePosition& pos) const
|
||||
if (count > 0)
|
||||
{
|
||||
int32_t startOffset = start;
|
||||
start = subParse(text, start, prevCh, count, FALSE, ambiguousYear);
|
||||
start = subParse(text, start, prevCh, count, FALSE, ambiguousYear, cal);
|
||||
if ( start < 0 ) {
|
||||
pos.setIndex(oldStart);
|
||||
pos.setErrorIndex(startOffset);
|
||||
// {sfb} correct Date?>
|
||||
return 0;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
@ -936,25 +920,22 @@ SimpleDateFormat::parse(const UnicodeString& text, ParsePosition& pos) const
|
||||
// front or the back of the default century. This only works because we adjust
|
||||
// the year correctly to start with in other cases -- see subParse().
|
||||
UErrorCode status = U_ZERO_ERROR;
|
||||
UDate parsedDate;
|
||||
if (ambiguousYear[0]) // If this is true then the two-digit year == the default start year
|
||||
{
|
||||
// We need a copy of the fields, and we need to avoid triggering a call to
|
||||
// complete(), which will recalculate the fields. Since we can't access
|
||||
// the fields[] array in Calendar, we clone the entire object. This will
|
||||
// stop working if Calendar.clone() is ever rewritten to call complete().
|
||||
Calendar *savedCalendar = fCalendar->clone();
|
||||
parsedDate = fCalendar->getTime(status);
|
||||
Calendar *copy = cal.clone();
|
||||
UDate parsedDate = copy->getTime(status);
|
||||
// {sfb} check internalGetDefaultCenturyStart
|
||||
if (parsedDate < internalGetDefaultCenturyStart())
|
||||
{
|
||||
// We can't use add here because that does a complete() first.
|
||||
savedCalendar->set(Calendar::YEAR, internalGetDefaultCenturyStartYear() + 100);
|
||||
parsedDate = savedCalendar->getTime(status);
|
||||
cal.set(Calendar::YEAR, internalGetDefaultCenturyStartYear() + 100);
|
||||
}
|
||||
delete savedCalendar;
|
||||
delete copy;
|
||||
}
|
||||
else parsedDate = fCalendar->getTime(status);
|
||||
|
||||
// If any Calendar calls failed, we pretend that we
|
||||
// couldn't parse the string, when in reality this isn't quite accurate--
|
||||
@ -962,17 +943,22 @@ SimpleDateFormat::parse(const UnicodeString& text, ParsePosition& pos) const
|
||||
if (U_FAILURE(status)) {
|
||||
pos.setErrorIndex(start);
|
||||
pos.setIndex(oldStart);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
return parsedDate;
|
||||
UDate
|
||||
SimpleDateFormat::parse( const UnicodeString& text,
|
||||
ParsePosition& pos) const {
|
||||
// redefined here because the other parse() function hides this function's
|
||||
// cunterpart on DateFormat
|
||||
return DateFormat::parse(text, pos);
|
||||
}
|
||||
|
||||
UDate
|
||||
SimpleDateFormat::parse(const UnicodeString& text, UErrorCode& status) const
|
||||
{
|
||||
// redefined here because the other parse() function hides this function's
|
||||
// ounterpart on DateFormat
|
||||
// counterpart on DateFormat
|
||||
return DateFormat::parse(text, status);
|
||||
}
|
||||
//----------------------------------------------------------------------
|
||||
@ -981,7 +967,8 @@ int32_t SimpleDateFormat::matchString(const UnicodeString& text,
|
||||
int32_t start,
|
||||
Calendar::EDateFields field,
|
||||
const UnicodeString* data,
|
||||
int32_t dataCount) const
|
||||
int32_t dataCount,
|
||||
Calendar& cal) const
|
||||
{
|
||||
int32_t i = 0;
|
||||
int32_t count = dataCount;
|
||||
@ -1015,7 +1002,7 @@ int32_t SimpleDateFormat::matchString(const UnicodeString& text,
|
||||
}
|
||||
if (bestMatch >= 0)
|
||||
{
|
||||
fCalendar->set(field, bestMatch);
|
||||
cal.set(field, bestMatch);
|
||||
return start + bestMatchLength;
|
||||
}
|
||||
|
||||
@ -1066,7 +1053,7 @@ SimpleDateFormat::subParseLong(const UnicodeString& text, ParsePosition& pos, in
|
||||
* indicating matching failure, otherwise.
|
||||
*/
|
||||
int32_t SimpleDateFormat::subParse(const UnicodeString& text, int32_t& start, UChar ch, int32_t count,
|
||||
UBool obeyCount, UBool ambiguousYear[]) const
|
||||
UBool obeyCount, UBool ambiguousYear[], Calendar& cal) const
|
||||
{
|
||||
Formattable number;
|
||||
int32_t value = 0;
|
||||
@ -1126,7 +1113,7 @@ int32_t SimpleDateFormat::subParse(const UnicodeString& text, int32_t& start, UC
|
||||
|
||||
switch (patternCharIndex) {
|
||||
case kEraField:
|
||||
return matchString(text, start, Calendar::ERA, fSymbols->fEras, fSymbols->fErasCount);
|
||||
return matchString(text, start, Calendar::ERA, fSymbols->fEras, fSymbols->fErasCount, cal);
|
||||
case kYearField:
|
||||
// If there are 3 or more YEAR pattern characters, this indicates
|
||||
// that the year value is to be treated literally, without any
|
||||
@ -1151,7 +1138,7 @@ int32_t SimpleDateFormat::subParse(const UnicodeString& text, int32_t& start, UC
|
||||
value += (fDefaultCenturyStartYear/100)*100 +
|
||||
(value < ambiguousTwoDigitYear ? 100 : 0);
|
||||
}
|
||||
fCalendar->set(Calendar::YEAR, value);
|
||||
cal.set(Calendar::YEAR, value);
|
||||
return pos.getIndex();
|
||||
case kYearWOYField:
|
||||
// Comment is the same as for kYearFiels - look above
|
||||
@ -1164,7 +1151,7 @@ int32_t SimpleDateFormat::subParse(const UnicodeString& text, int32_t& start, UC
|
||||
value += (fDefaultCenturyStartYear/100)*100 +
|
||||
(value < ambiguousTwoDigitYear ? 100 : 0);
|
||||
}
|
||||
fCalendar->set(Calendar::YEAR_WOY, value);
|
||||
cal.set(Calendar::YEAR_WOY, value);
|
||||
return pos.getIndex();
|
||||
case kMonthField:
|
||||
if (count <= 2) // i.e., M or MM.
|
||||
@ -1172,7 +1159,7 @@ int32_t SimpleDateFormat::subParse(const UnicodeString& text, int32_t& start, UC
|
||||
// Don't want to parse the month if it is a string
|
||||
// while pattern uses numeric style: M or MM.
|
||||
// [We computed 'value' above.]
|
||||
fCalendar->set(Calendar::MONTH, value - 1);
|
||||
cal.set(Calendar::MONTH, value - 1);
|
||||
return pos.getIndex();
|
||||
}
|
||||
else
|
||||
@ -1182,17 +1169,17 @@ int32_t SimpleDateFormat::subParse(const UnicodeString& text, int32_t& start, UC
|
||||
// Try count == 4 first:
|
||||
int32_t newStart = 0;
|
||||
if ((newStart = matchString(text, start, Calendar::MONTH,
|
||||
fSymbols->fMonths, fSymbols->fMonthsCount)) > 0)
|
||||
fSymbols->fMonths, fSymbols->fMonthsCount, cal)) > 0)
|
||||
return newStart;
|
||||
else // count == 4 failed, now try count == 3
|
||||
return matchString(text, start, Calendar::MONTH,
|
||||
fSymbols->fShortMonths, fSymbols->fShortMonthsCount);
|
||||
fSymbols->fShortMonths, fSymbols->fShortMonthsCount, cal);
|
||||
}
|
||||
case kHourOfDay1Field:
|
||||
// [We computed 'value' above.]
|
||||
if (value == fCalendar->getMaximum(Calendar::HOUR_OF_DAY) + 1)
|
||||
if (value == cal.getMaximum(Calendar::HOUR_OF_DAY) + 1)
|
||||
value = 0;
|
||||
fCalendar->set(Calendar::HOUR_OF_DAY, value);
|
||||
cal.set(Calendar::HOUR_OF_DAY, value);
|
||||
return pos.getIndex();
|
||||
case kDayOfWeekField:
|
||||
{
|
||||
@ -1200,19 +1187,19 @@ int32_t SimpleDateFormat::subParse(const UnicodeString& text, int32_t& start, UC
|
||||
// Try count == 4 (DDDD) first:
|
||||
int32_t newStart = 0;
|
||||
if ((newStart = matchString(text, start, Calendar::DAY_OF_WEEK,
|
||||
fSymbols->fWeekdays, fSymbols->fWeekdaysCount)) > 0)
|
||||
fSymbols->fWeekdays, fSymbols->fWeekdaysCount, cal)) > 0)
|
||||
return newStart;
|
||||
else // DDDD failed, now try DDD
|
||||
return matchString(text, start, Calendar::DAY_OF_WEEK,
|
||||
fSymbols->fShortWeekdays, fSymbols->fShortWeekdaysCount);
|
||||
fSymbols->fShortWeekdays, fSymbols->fShortWeekdaysCount, cal);
|
||||
}
|
||||
case kAmPmField:
|
||||
return matchString(text, start, Calendar::AM_PM, fSymbols->fAmPms, fSymbols->fAmPmsCount);
|
||||
return matchString(text, start, Calendar::AM_PM, fSymbols->fAmPms, fSymbols->fAmPmsCount, cal);
|
||||
case kHour1Field:
|
||||
// [We computed 'value' above.]
|
||||
if (value == fCalendar->getLeastMaximum(Calendar::HOUR)+1)
|
||||
if (value == cal.getLeastMaximum(Calendar::HOUR)+1)
|
||||
value = 0;
|
||||
fCalendar->set(Calendar::HOUR, value);
|
||||
cal.set(Calendar::HOUR, value);
|
||||
return pos.getIndex();
|
||||
case kTimezoneField:
|
||||
{
|
||||
@ -1240,7 +1227,7 @@ int32_t SimpleDateFormat::subParse(const UnicodeString& text, int32_t& start, UC
|
||||
if ((text.length() - start) > gmtLen &&
|
||||
(lcaseText.compare(start, gmtLen, lcaseGMT, 0, gmtLen)) == 0)
|
||||
{
|
||||
fCalendar->set(Calendar::DST_OFFSET, 0);
|
||||
cal.set(Calendar::DST_OFFSET, 0);
|
||||
|
||||
pos.setIndex(start + gmtLen);
|
||||
|
||||
@ -1249,7 +1236,7 @@ int32_t SimpleDateFormat::subParse(const UnicodeString& text, int32_t& start, UC
|
||||
else if( text[pos.getIndex()] == 0x002D /*'-'*/ )
|
||||
sign = -1;
|
||||
else {
|
||||
fCalendar->set(Calendar::ZONE_OFFSET, 0 );
|
||||
cal.set(Calendar::ZONE_OFFSET, 0 );
|
||||
return pos.getIndex();
|
||||
}
|
||||
|
||||
@ -1313,11 +1300,11 @@ int32_t SimpleDateFormat::subParse(const UnicodeString& text, int32_t& start, UC
|
||||
if (j <= 4)
|
||||
{
|
||||
TimeZone *tz = TimeZone::createTimeZone(fSymbols->fZoneStrings[i][0]);
|
||||
fCalendar->set(Calendar::ZONE_OFFSET, tz->getRawOffset());
|
||||
cal.set(Calendar::ZONE_OFFSET, tz->getRawOffset());
|
||||
// Must call set() with something -- TODO -- Fix this to
|
||||
// use the correct DST SAVINGS for the zone.
|
||||
delete tz;
|
||||
fCalendar->set(Calendar::DST_OFFSET, j >= 3 ? U_MILLIS_PER_HOUR : 0);
|
||||
cal.set(Calendar::DST_OFFSET, j >= 3 ? U_MILLIS_PER_HOUR : 0);
|
||||
return (start + fSymbols->fZoneStrings[i][j].length());
|
||||
}
|
||||
}
|
||||
@ -1359,12 +1346,12 @@ int32_t SimpleDateFormat::subParse(const UnicodeString& text, int32_t& start, UC
|
||||
{
|
||||
offset *= U_MILLIS_PER_MINUTE * sign;
|
||||
|
||||
if (fCalendar->getTimeZone().useDaylightTime())
|
||||
if (cal.getTimeZone().useDaylightTime())
|
||||
{
|
||||
fCalendar->set(Calendar::DST_OFFSET, U_MILLIS_PER_HOUR);
|
||||
cal.set(Calendar::DST_OFFSET, U_MILLIS_PER_HOUR);
|
||||
offset -= U_MILLIS_PER_HOUR;
|
||||
}
|
||||
fCalendar->set(Calendar::ZONE_OFFSET, offset);
|
||||
cal.set(Calendar::ZONE_OFFSET, offset);
|
||||
|
||||
return pos.getIndex();
|
||||
}
|
||||
@ -1401,7 +1388,7 @@ int32_t SimpleDateFormat::subParse(const UnicodeString& text, int32_t& start, UC
|
||||
fNumberFormat->parse(text, number, pos);
|
||||
if (pos.getIndex() != parseStart) {
|
||||
// WORK AROUND BUG IN NUMBER FORMAT IN 1.2B3
|
||||
fCalendar->set(field, number.getLong());
|
||||
cal.set(field, number.getLong());
|
||||
return pos.getIndex();
|
||||
}
|
||||
return -start;
|
||||
|
@ -265,7 +265,7 @@ public:
|
||||
UErrorCode& status) const;
|
||||
|
||||
/**
|
||||
* Formats a UDate into a date/time string. This is an abstract method which
|
||||
* Formats a date into a date/time string. This is an abstract method which
|
||||
* concrete subclasses must implement.
|
||||
* <P>
|
||||
* On input, the FieldPosition parameter may have its "field" member filled with
|
||||
@ -284,6 +284,38 @@ public:
|
||||
* fieldPosition.getEndIndex will be set to 5 and 8, respectively, for the first
|
||||
* occurence of the timezone pattern character 'z'.
|
||||
*
|
||||
* @param cal a Calendar set to the date and time to be formatted
|
||||
* into a date/time string.
|
||||
* @param toAppendTo the result of the formatting operation is appended to
|
||||
* the end of this string.
|
||||
* @param fieldPosition On input: an alignment field, if desired (see examples above)
|
||||
* On output: the offsets of the alignment field (see examples above)
|
||||
* @return A reference to 'toAppendTo'.
|
||||
* @draft ICU 2.1
|
||||
*/
|
||||
virtual UnicodeString& format( Calendar& cal,
|
||||
UnicodeString& toAppendTo,
|
||||
FieldPosition& fieldPosition) const = 0;
|
||||
|
||||
/**
|
||||
* Formats a UDate into a date/time string.
|
||||
* <P>
|
||||
* On input, the FieldPosition parameter may have its "field" member filled with
|
||||
* an enum value specifying a field. On output, the FieldPosition will be filled
|
||||
* in with the text offsets for that field.
|
||||
* <P> For example, given a time text
|
||||
* "1996.07.10 AD at 15:08:56 PDT", if the given fieldPosition.field is
|
||||
* DateFormat::kYearField, the offsets fieldPosition.beginIndex and
|
||||
* statfieldPositionus.getEndIndex will be set to 0 and 4, respectively.
|
||||
* <P> Notice
|
||||
* that if the same time field appears more than once in a pattern, the status will
|
||||
* be set for the first occurence of that time field. For instance,
|
||||
* formatting a UDate to the time string "1 PM PDT (Pacific Daylight Time)"
|
||||
* using the pattern "h a z (zzzz)" and the alignment field
|
||||
* DateFormat::TIMEZONE_FIELD, the offsets fieldPosition.beginIndex and
|
||||
* fieldPosition.getEndIndex will be set to 5 and 8, respectively, for the first
|
||||
* occurence of the timezone pattern character 'z'.
|
||||
*
|
||||
* @param date a UDate to be formatted into a date/time string.
|
||||
* @param toAppendTo the result of the formatting operation is appended to
|
||||
* the end of this string.
|
||||
@ -292,9 +324,9 @@ public:
|
||||
* @return A reference to 'toAppendTo'.
|
||||
* @stable
|
||||
*/
|
||||
virtual UnicodeString& format( UDate date,
|
||||
UnicodeString& toAppendTo,
|
||||
FieldPosition& fieldPosition) const = 0;
|
||||
UnicodeString& format( UDate date,
|
||||
UnicodeString& toAppendTo,
|
||||
FieldPosition& fieldPosition) const;
|
||||
|
||||
/**
|
||||
* Formats a UDate into a date/time string. If there is a problem, you won't
|
||||
@ -329,6 +361,29 @@ public:
|
||||
virtual UDate parse( const UnicodeString& text,
|
||||
UErrorCode& status) const;
|
||||
|
||||
/**
|
||||
* Parse a date/time string beginning at the given parse position. For
|
||||
* example, a time text "07/10/96 4:5 PM, PDT" will be parsed into a Date
|
||||
* that is equivalent to Date(837039928046).
|
||||
* <P>
|
||||
* By default, parsing is lenient: If the input is not in the form used by
|
||||
* this object's format method but can still be parsed as a date, then the
|
||||
* parse succeeds. Clients may insist on strict adherence to the format by
|
||||
* calling setLenient(false).
|
||||
*
|
||||
* @see DateFormat::setLenient(boolean)
|
||||
*
|
||||
* @param text The date/time string to be parsed
|
||||
* @param pos On input, the position at which to start parsing; on
|
||||
* output, the position at which parsing terminated, or the
|
||||
* start position if the parse failed.
|
||||
* @return A valid UDate if the input could be parsed.
|
||||
* @draft ICU 2.1
|
||||
*/
|
||||
virtual void parse( const UnicodeString& text,
|
||||
Calendar& cal,
|
||||
ParsePosition& pos) const = 0;
|
||||
|
||||
/**
|
||||
* Parse a date/time string beginning at the given parse position. For
|
||||
* example, a time text "07/10/96 4:5 PM, PDT" will be parsed into a Date
|
||||
@ -348,8 +403,8 @@ public:
|
||||
* @return A valid UDate if the input could be parsed.
|
||||
* @stable
|
||||
*/
|
||||
virtual UDate parse( const UnicodeString& text,
|
||||
ParsePosition& pos) const = 0;
|
||||
UDate parse( const UnicodeString& text,
|
||||
ParsePosition& pos) const;
|
||||
|
||||
/**
|
||||
* Parse a string to produce an object. This methods handles parsing of
|
||||
|
@ -268,15 +268,16 @@ public:
|
||||
* Example: using the US locale: "yyyy.MM.dd e 'at' HH:mm:ss zzz" ->>
|
||||
* 1996.07.10 AD at 15:08:56 PDT
|
||||
*
|
||||
* @param date The date-time value to be formatted into a date-time string.
|
||||
* @param cal a Calendar set to the date and time to be formatted
|
||||
* into a date/time string.
|
||||
* @param toAppendTo The result of the formatting operation is appended to this
|
||||
* string.
|
||||
* @param pos The formatting position. On input: an alignment field,
|
||||
* if desired. On output: the offsets of the alignment field.
|
||||
* @return A reference to 'toAppendTo'.
|
||||
* @stable
|
||||
* @draft ICU 2.1
|
||||
*/
|
||||
virtual UnicodeString& format( UDate date,
|
||||
virtual UnicodeString& format( Calendar& cal,
|
||||
UnicodeString& toAppendTo,
|
||||
FieldPosition& pos) const;
|
||||
|
||||
@ -303,6 +304,14 @@ public:
|
||||
FieldPosition& pos,
|
||||
UErrorCode& status) const;
|
||||
|
||||
/**
|
||||
* Redeclared DateFormat method.
|
||||
* @draft ICU 2.1
|
||||
*/
|
||||
UnicodeString& format(UDate date,
|
||||
UnicodeString& result,
|
||||
FieldPosition& fieldPosition) const;
|
||||
|
||||
/**
|
||||
* Redeclared DateFormat method.
|
||||
* @stable
|
||||
@ -317,6 +326,27 @@ public:
|
||||
*/
|
||||
UnicodeString& format(UDate date, UnicodeString& result) const;
|
||||
|
||||
/**
|
||||
* Parse a date/time string beginning at the given parse position. For
|
||||
* example, a time text "07/10/96 4:5 PM, PDT" will be parsed into a Date
|
||||
* that is equivalent to Date(837039928046).
|
||||
* <P>
|
||||
* By default, parsing is lenient: If the input is not in the form used by
|
||||
* this object's format method but can still be parsed as a date, then the
|
||||
* parse succeeds. Clients may insist on strict adherence to the format by
|
||||
* calling setLenient(false).
|
||||
*
|
||||
* @param text The date/time string to be parsed
|
||||
* @param pos On input, the position at which to start parsing; on
|
||||
* output, the position at which parsing terminated, or the
|
||||
* start position if the parse failed.
|
||||
* @return A valid UDate if the input could be parsed.
|
||||
* @draft ICU 2.1
|
||||
*/
|
||||
virtual void parse( const UnicodeString& text,
|
||||
Calendar& cal,
|
||||
ParsePosition& pos) const;
|
||||
|
||||
/**
|
||||
* Parse a date/time string starting at the given parse position. For
|
||||
* example, a time text "07/10/96 4:5 PM, PDT" will be parsed into a Date
|
||||
@ -336,8 +366,8 @@ public:
|
||||
* @return A valid UDate if the input could be parsed.
|
||||
* @stable
|
||||
*/
|
||||
virtual UDate parse( const UnicodeString& text,
|
||||
ParsePosition& pos) const;
|
||||
UDate parse( const UnicodeString& text,
|
||||
ParsePosition& pos) const;
|
||||
|
||||
|
||||
/**
|
||||
@ -547,6 +577,7 @@ private:
|
||||
int32_t count,
|
||||
int32_t beginOffset,
|
||||
FieldPosition& pos,
|
||||
Calendar& cal,
|
||||
UErrorCode& status) const; // in case of illegal argument
|
||||
|
||||
/**
|
||||
@ -593,7 +624,7 @@ private:
|
||||
* indicating matching failure, otherwise.
|
||||
*/
|
||||
int32_t matchString(const UnicodeString& text, UTextOffset start, Calendar::EDateFields field,
|
||||
const UnicodeString* stringArray, int32_t stringArrayCount) const;
|
||||
const UnicodeString* stringArray, int32_t stringArrayCount, Calendar& cal) const;
|
||||
|
||||
/**
|
||||
* Private member function that converts the parsed date strings into
|
||||
@ -607,7 +638,7 @@ private:
|
||||
* indicating matching failure, otherwise.
|
||||
*/
|
||||
int32_t subParse(const UnicodeString& text, int32_t& start, UChar ch, int32_t count,
|
||||
UBool obeyCount, UBool ambiguousYear[]) const;
|
||||
UBool obeyCount, UBool ambiguousYear[], Calendar& cal) const;
|
||||
|
||||
/**
|
||||
* Parse the given text, at the given position, as a numeric value, using
|
||||
@ -745,6 +776,15 @@ SimpleDateFormat::format(const Formattable& obj,
|
||||
return DateFormat::format(obj, result, status);
|
||||
}
|
||||
|
||||
inline UnicodeString&
|
||||
SimpleDateFormat::format(UDate date,
|
||||
UnicodeString& result,
|
||||
FieldPosition& fieldPosition) const {
|
||||
// Don't use Format:: - use immediate base class only,
|
||||
// in case immediate base modifies behavior later.
|
||||
return DateFormat::format(date, result, fieldPosition);
|
||||
}
|
||||
|
||||
inline UnicodeString&
|
||||
SimpleDateFormat::format(UDate date, UnicodeString& result) const {
|
||||
return DateFormat::format(date, result);
|
||||
|
Loading…
Reference in New Issue
Block a user