#include "unicode/utypes.h"
#if !UCONFIG_NO_FORMATTING
#include "unicode/smpdtfmt.h"
#include "unicode/dtfmtsym.h"
#include "unicode/resbund.h"
#include "unicode/msgfmt.h"
#include "unicode/calendar.h"
#include "unicode/gregocal.h"
#include "unicode/timezone.h"
#include "unicode/decimfmt.h"
#include "unicode/dcfmtsym.h"
#include "unicode/uchar.h"
#include "unicode/ustring.h"
#include "uprops.h"
#include <float.h>
U_NAMESPACE_BEGIN
const UChar SimpleDateFormat::fgGmt[] = {0x0047, 0x004D, 0x0054, 0x0000}; const UChar SimpleDateFormat::fgGmtPlus[] = {0x0047, 0x004D, 0x0054, 0x002B, 0x0000}; const UChar SimpleDateFormat::fgGmtMinus[] = {0x0047, 0x004D, 0x0054, 0x002D, 0x0000};
const UChar SimpleDateFormat::fgDefaultPattern[] =
{
0x79, 0x79, 0x79, 0x79, 0x4D, 0x4D, 0x64, 0x64, 0x20, 0x68, 0x68, 0x3A, 0x6D, 0x6D, 0x20, 0x61, 0
};
static const UChar SUPPRESS_NEGATIVE_PREFIX[] = {0xAB00, 0};
const char SimpleDateFormat::fgDateTimePatternsTag[]="DateTimePatterns";
const char SimpleDateFormat::fgClassID = 0;
const UDate SimpleDateFormat::fgSystemDefaultCentury = DBL_MIN;
static const UChar QUOTE = 0x27;
SimpleDateFormat::~SimpleDateFormat()
{
delete fSymbols;
}
SimpleDateFormat::SimpleDateFormat(UErrorCode& status)
: fLocale(Locale::getDefault()),
fSymbols(NULL)
{
construct(kShort, (EStyle) (kShort + kDateOffset), fLocale, status);
initializeDefaultCentury();
}
SimpleDateFormat::SimpleDateFormat(const UnicodeString& pattern,
UErrorCode &status)
: fPattern(pattern),
fLocale(Locale::getDefault()),
fSymbols(NULL)
{
initializeSymbols(fLocale, initializeCalendar(NULL,fLocale,status), status);
initialize(fLocale, status);
initializeDefaultCentury();
}
SimpleDateFormat::SimpleDateFormat(const UnicodeString& pattern,
const Locale& locale,
UErrorCode& status)
: fPattern(pattern),
fLocale(locale)
{
initializeSymbols(fLocale, initializeCalendar(NULL,fLocale,status), status);
initialize(fLocale, status);
initializeDefaultCentury();
}
SimpleDateFormat::SimpleDateFormat(const UnicodeString& pattern,
DateFormatSymbols* symbolsToAdopt,
UErrorCode& status)
: fPattern(pattern),
fLocale(Locale::getDefault()),
fSymbols(symbolsToAdopt)
{
initializeCalendar(NULL,fLocale,status);
initialize(fLocale, status);
initializeDefaultCentury();
}
SimpleDateFormat::SimpleDateFormat(const UnicodeString& pattern,
const DateFormatSymbols& symbols,
UErrorCode& status)
: fPattern(pattern),
fLocale(Locale::getDefault()),
fSymbols(new DateFormatSymbols(symbols))
{
initializeCalendar(NULL, fLocale, status);
initialize(fLocale, status);
initializeDefaultCentury();
}
SimpleDateFormat::SimpleDateFormat(EStyle timeStyle,
EStyle dateStyle,
const Locale& locale,
UErrorCode& status)
: fLocale(locale),
fSymbols(NULL)
{
construct(timeStyle, dateStyle, fLocale, status);
if(U_SUCCESS(status)) {
initializeDefaultCentury();
}
}
SimpleDateFormat::SimpleDateFormat(const Locale& locale,
UErrorCode& status)
: fPattern(fgDefaultPattern),
fLocale(locale),
fSymbols(NULL)
{
if (U_FAILURE(status)) return;
initializeSymbols(fLocale, initializeCalendar(NULL, fLocale, status),status);
if (U_FAILURE(status))
{
status = U_ZERO_ERROR;
delete fSymbols;
fSymbols = new DateFormatSymbols(status);
if (fSymbols == 0) {
status = U_MEMORY_ALLOCATION_ERROR;
return;
}
}
initialize(fLocale, status);
if(U_SUCCESS(status)) {
initializeDefaultCentury();
}
}
SimpleDateFormat::SimpleDateFormat(const SimpleDateFormat& other)
: DateFormat(other),
fSymbols(NULL)
{
*this = other;
}
SimpleDateFormat& SimpleDateFormat::operator=(const SimpleDateFormat& other)
{
DateFormat::operator=(other);
delete fSymbols;
fSymbols = NULL;
if (other.fSymbols)
fSymbols = new DateFormatSymbols(*other.fSymbols);
fDefaultCenturyStart = other.fDefaultCenturyStart;
fDefaultCenturyStartYear = other.fDefaultCenturyStartYear;
fHaveDefaultCentury = other.fHaveDefaultCentury;
fPattern = other.fPattern;
return *this;
}
Format*
SimpleDateFormat::clone() const
{
return new SimpleDateFormat(*this);
}
UBool
SimpleDateFormat::operator==(const Format& other) const
{
if (DateFormat::operator==(other) &&
other.getDynamicClassID() == getStaticClassID())
{
SimpleDateFormat* that = (SimpleDateFormat*)&other;
return (fPattern == that->fPattern &&
fSymbols != NULL && that->fSymbols != NULL && *fSymbols == *that->fSymbols &&
fHaveDefaultCentury == that->fHaveDefaultCentury &&
fDefaultCenturyStart == that->fDefaultCenturyStart);
}
return FALSE;
}
void SimpleDateFormat::construct(EStyle timeStyle,
EStyle dateStyle,
const Locale& locale,
UErrorCode& status)
{
if (U_FAILURE(status)) return;
ResourceBundle resources((char *)0, locale, status);
initializeCalendar(NULL, locale, status);
ResourceBundle dateTimePatterns = DateFormatSymbols::getData(resources, fgDateTimePatternsTag, fCalendar?fCalendar->getType():NULL, status);
if (U_FAILURE(status)) return;
if (dateTimePatterns.getSize() <= kDateTime)
{
status = U_INVALID_FORMAT_ERROR;
return;
}
initializeSymbols(locale,fCalendar, status);
if (U_FAILURE(status)) return;
if (fSymbols == 0) {
status = U_MEMORY_ALLOCATION_ERROR;
return;
}
UnicodeString str;
if ((timeStyle != kNone) &&
(dateStyle != kNone))
{
Formattable *timeDateArray = new Formattable[2];
if (timeDateArray == 0) {
status = U_MEMORY_ALLOCATION_ERROR;
return;
}
timeDateArray[0].adoptString(&(new UnicodeString)->fastCopyFrom(dateTimePatterns.getStringEx(timeStyle, status)));
timeDateArray[1].adoptString(&(new UnicodeString)->fastCopyFrom(dateTimePatterns.getStringEx(dateStyle, status)));
MessageFormat::format(dateTimePatterns.getStringEx(kDateTime, status), timeDateArray, 2, fPattern, status);
delete [] timeDateArray;
}
else if (timeStyle != kNone) fPattern.fastCopyFrom(dateTimePatterns.getStringEx(timeStyle, status));
else if (dateStyle != kNone) fPattern.fastCopyFrom(dateTimePatterns.getStringEx(dateStyle, status));
else status = U_INVALID_FORMAT_ERROR;
initialize(locale, status);
}
Calendar*
SimpleDateFormat::initializeCalendar(TimeZone* adoptZone, const Locale& locale, UErrorCode& status)
{
if(!U_FAILURE(status)) {
fCalendar = Calendar::createInstance(adoptZone?adoptZone:TimeZone::createDefault(), locale, status);
}
return fCalendar;
}
void
SimpleDateFormat::initializeSymbols(const Locale& locale, Calendar* calendar, UErrorCode& status)
{
if(U_FAILURE(status)) {
fSymbols = NULL;
} else {
fSymbols = new DateFormatSymbols(locale, calendar?calendar->getType() :NULL , status);
}
}
void
SimpleDateFormat::initialize(const Locale& locale,
UErrorCode& status)
{
if (U_FAILURE(status)) return;
if (fSymbols->fZoneStringsColCount < 1)
{
status = U_INVALID_FORMAT_ERROR; return;
}
fNumberFormat = NumberFormat::createInstance(locale, status);
if (fNumberFormat != NULL && U_SUCCESS(status))
{
fNumberFormat->setGroupingUsed(FALSE);
if (fNumberFormat->getDynamicClassID() == DecimalFormat::getStaticClassID())
((DecimalFormat*)fNumberFormat)->setDecimalSeparatorAlwaysShown(FALSE);
fNumberFormat->setParseIntegerOnly(TRUE);
fNumberFormat->setMinimumFractionDigits(0); }
else if (U_SUCCESS(status))
{
status = U_MISSING_RESOURCE_ERROR;
}
}
void SimpleDateFormat::initializeDefaultCentury()
{
if(fCalendar) {
fHaveDefaultCentury = fCalendar->haveDefaultCentury();
if(fHaveDefaultCentury) {
fDefaultCenturyStart = fCalendar->defaultCenturyStart();
fDefaultCenturyStartYear = fCalendar->defaultCenturyStartYear();
} else {
fDefaultCenturyStart = DBL_MIN;
fDefaultCenturyStartYear = -1;
}
}
}
void SimpleDateFormat::parseAmbiguousDatesAsAfter(UDate startDate, UErrorCode& status)
{
if(U_FAILURE(status)) {
return;
}
if(!fCalendar) {
status = U_ILLEGAL_ARGUMENT_ERROR;
return;
}
fCalendar->setTime(startDate, status);
if(U_SUCCESS(status)) {
fHaveDefaultCentury = TRUE;
fDefaultCenturyStart = startDate;
fDefaultCenturyStartYear = fCalendar->get(UCAL_YEAR, status);
}
}
UnicodeString&
SimpleDateFormat::format(Calendar& cal, UnicodeString& appendTo, FieldPosition& pos) const
{
UErrorCode status = U_ZERO_ERROR;
pos.setBeginIndex(0);
pos.setEndIndex(0);
UBool inQuote = FALSE;
UChar prevCh = 0;
int32_t count = 0;
for (int32_t i = 0; i < fPattern.length() && U_SUCCESS(status); ++i) {
UChar ch = fPattern[i];
if (ch != prevCh && count > 0) {
subFormat(appendTo, prevCh, count, pos, cal, status);
count = 0;
}
if (ch == QUOTE) {
if ((i+1) < fPattern.length() && fPattern[i+1] == QUOTE) {
appendTo += (UChar)QUOTE;
++i;
} else {
inQuote = ! inQuote;
}
}
else if ( ! inQuote && ((ch >= 0x0061 && ch <= 0x007A )
|| (ch >= 0x0041 && ch <= 0x005A ))) {
prevCh = ch;
++count;
}
else {
appendTo += ch;
}
}
if (count > 0) {
subFormat(appendTo, prevCh, count, pos, cal, status);
}
if (U_FAILURE(status)) {
pos.setBeginIndex(0);
pos.setEndIndex(0);
}
return appendTo;
}
UnicodeString&
SimpleDateFormat::format(const Formattable& obj,
UnicodeString& appendTo,
FieldPosition& pos,
UErrorCode& status) const
{
return DateFormat::format(obj, appendTo, pos, status);
}
const UCalendarDateFields
SimpleDateFormat::fgPatternIndexToCalendarField[] =
{
UCAL_ERA, UCAL_YEAR, UCAL_MONTH, UCAL_DATE,
UCAL_HOUR_OF_DAY, UCAL_HOUR_OF_DAY, UCAL_MINUTE,
UCAL_SECOND, UCAL_MILLISECOND, UCAL_DAY_OF_WEEK,
UCAL_DAY_OF_YEAR, UCAL_DAY_OF_WEEK_IN_MONTH,
UCAL_WEEK_OF_YEAR, UCAL_WEEK_OF_MONTH,
UCAL_AM_PM, UCAL_HOUR, UCAL_HOUR, UCAL_ZONE_OFFSET,
UCAL_YEAR_WOY, UCAL_DOW_LOCAL
};
const DateFormat::EField
SimpleDateFormat::fgPatternIndexToDateFormatField[] = {
DateFormat::kEraField, DateFormat::kYearField, DateFormat::kMonthField,
DateFormat::kDateField, DateFormat::kHourOfDay1Field,
DateFormat::kHourOfDay0Field, DateFormat::kMinuteField,
DateFormat::kSecondField, DateFormat::kMillisecondField,
DateFormat::kDayOfWeekField, DateFormat::kDayOfYearField,
DateFormat::kDayOfWeekInMonthField, DateFormat::kWeekOfYearField,
DateFormat::kWeekOfMonthField, DateFormat::kAmPmField,
DateFormat::kHour1Field, DateFormat::kHour0Field,
DateFormat::kTimezoneField, DateFormat::kYearWOYField,
DateFormat::kDOWLocalField
};
void
SimpleDateFormat::subFormat(UnicodeString &appendTo,
UChar ch,
int32_t count,
FieldPosition& pos,
Calendar& cal,
UErrorCode& status) const
{
UChar *patternCharPtr = u_strchr(DateFormatSymbols::getPatternUChars(), ch);
EField patternCharIndex;
const int32_t maxIntCount = 10;
int32_t beginOffset = appendTo.length();
if (patternCharPtr == NULL)
{
status = U_INVALID_FORMAT_ERROR;
}
patternCharIndex = (EField)(patternCharPtr - DateFormatSymbols::getPatternUChars());
UCalendarDateFields field = fgPatternIndexToCalendarField[patternCharIndex];
int32_t value = cal.get(field, status);
if (U_FAILURE(status)) {
return;
}
switch (patternCharIndex) {
case kEraField:
appendTo += fSymbols->fEras[value];
break;
case kYearField:
case kYearWOYField:
if (count >= 4)
zeroPaddingNumber(appendTo, value, 4, maxIntCount);
else if(count == 1)
zeroPaddingNumber(appendTo, value, count, maxIntCount);
else
zeroPaddingNumber(appendTo, value, 2, 2);
break;
case kMonthField:
if (count >= 4)
appendTo += fSymbols->fMonths[value];
else if (count == 3)
appendTo += fSymbols->fShortMonths[value];
else
zeroPaddingNumber(appendTo, value + 1, count, maxIntCount);
break;
case kHourOfDay1Field:
if (value == 0)
zeroPaddingNumber(appendTo, cal.getMaximum(UCAL_HOUR_OF_DAY) + 1, count, maxIntCount);
else
zeroPaddingNumber(appendTo, value, count, maxIntCount);
break;
case kMillisecondField:
if (count > 3)
count = 3;
else if (count == 2)
value = value / 10;
else if (count == 1)
value = value / 100;
zeroPaddingNumber(appendTo, value, count, maxIntCount);
break;
case kDayOfWeekField:
if (count >= 4)
appendTo += fSymbols->fWeekdays[value];
else
appendTo += fSymbols->fShortWeekdays[value];
break;
case kAmPmField:
appendTo += fSymbols->fAmPms[value];
break;
case kHour1Field:
if (value == 0)
zeroPaddingNumber(appendTo, cal.getLeastMaximum(UCAL_HOUR) + 1, count, maxIntCount);
else
zeroPaddingNumber(appendTo, value, count, maxIntCount);
break;
case kTimezoneField: {
UnicodeString str;
int32_t zoneIndex = fSymbols->getZoneIndex(cal.getTimeZone().getID(str));
if (zoneIndex == -1) {
value = cal.get(UCAL_ZONE_OFFSET, status) +
cal.get(UCAL_DST_OFFSET, status);
if (value < 0) {
appendTo += fgGmtMinus;
value = -value; }
else
appendTo += fgGmtPlus;
zeroPaddingNumber(appendTo, (int32_t)(value/U_MILLIS_PER_HOUR), 2, 2);
appendTo += (UChar)0x003A ;
zeroPaddingNumber(appendTo, (int32_t)((value%U_MILLIS_PER_HOUR)/U_MILLIS_PER_MINUTE), 2, 2);
}
else if (cal.get(UCAL_DST_OFFSET, status) != 0) {
if (count >= 4)
appendTo += fSymbols->fZoneStrings[zoneIndex][3];
else
appendTo += fSymbols->fZoneStrings[zoneIndex][4];
}
else {
if (count >= 4)
appendTo += fSymbols->fZoneStrings[zoneIndex][1];
else
appendTo += fSymbols->fZoneStrings[zoneIndex][2];
}
}
break;
default:
zeroPaddingNumber(appendTo, value, count, maxIntCount);
break;
}
if (pos.getField() == fgPatternIndexToDateFormatField[patternCharIndex]) {
if (pos.getBeginIndex() == 0 && pos.getEndIndex() == 0) {
pos.setBeginIndex(beginOffset);
pos.setEndIndex(appendTo.length());
}
}
}
void
SimpleDateFormat::zeroPaddingNumber(UnicodeString &appendTo, int32_t value, int32_t minDigits, int32_t maxDigits) const
{
FieldPosition pos(0);
fNumberFormat->setMinimumIntegerDigits(minDigits);
fNumberFormat->setMaximumIntegerDigits(maxDigits);
fNumberFormat->format(value, appendTo, pos); }
static const UChar NUMERIC_FORMAT_CHARS[] = {0x4D, 0x79, 0x75, 0x64, 0x68, 0x48, 0x6D, 0x73, 0x53, 0x44, 0x46, 0x77, 0x57, 0x6B, 0x4B, 0x00};
UBool SimpleDateFormat::isNumeric(UChar formatChar, int32_t count) {
UnicodeString s(NUMERIC_FORMAT_CHARS);
int32_t i = s.indexOf(formatChar);
return (i > 0 || (i == 0 && count < 3));
}
void
SimpleDateFormat::parse(const UnicodeString& text, Calendar& cal, ParsePosition& parsePos) const
{
int32_t pos = parsePos.getIndex();
int32_t start = pos;
UBool ambiguousYear[] = { FALSE };
int32_t count = 0;
int32_t abutPat = -1; int32_t abutStart = 0;
int32_t abutPass = 0;
UBool inQuote = FALSE;
const UnicodeString numericFormatChars(NUMERIC_FORMAT_CHARS);
for (int32_t i=0; i<fPattern.length(); ++i) {
UChar ch = fPattern.charAt(i);
if (!inQuote && ((ch >= 0x41 && ch <= 0x5A) || (ch >= 0x61 && ch <= 0x7A))) { int32_t fieldPat = i;
count = 1;
while ((i+1)<fPattern.length() &&
fPattern.charAt(i+1) == ch) {
++count;
++i;
}
if (isNumeric(ch, count)) {
if (abutPat < 0) {
if ((i+1)<fPattern.length()) {
UBool abutting;
UChar nextCh = fPattern.charAt(i+1);
int32_t k = numericFormatChars.indexOf(nextCh);
if (k == 0) {
int32_t j = i+2;
while (j<fPattern.length() &&
fPattern.charAt(j) == nextCh) {
++j;
}
abutting = (j-i) < 4; } else {
abutting = k > 0;
}
if (abutting) {
abutPat = fieldPat;
abutStart = pos;
abutPass = 0;
}
}
}
} else {
abutPat = -1; }
if (abutPat >= 0) {
if (fieldPat == abutPat) {
count -= abutPass++;
if (count == 0) {
parsePos.setIndex(start);
parsePos.setErrorIndex(pos);
return;
}
}
pos = subParse(text, pos, ch, count,
TRUE, FALSE, ambiguousYear, cal);
if (pos < 0) {
i = abutPat - 1;
pos = abutStart;
continue;
}
}
else {
int32_t s = pos;
pos = subParse(text, pos, ch, count,
FALSE, TRUE, ambiguousYear, cal);
if (pos < 0) {
parsePos.setErrorIndex(s);
parsePos.setIndex(start);
return;
}
}
}
else {
abutPat = -1;
if (ch == QUOTE) {
if ((i+1)<fPattern.length() && fPattern.charAt(i+1)==ch) {
++i; } else {
inQuote = !inQuote;
continue;
}
}
if (uprv_isRuleWhiteSpace(ch)) {
while ((i+1)<fPattern.length() &&
uprv_isRuleWhiteSpace(fPattern.charAt(i+1))) {
++i;
}
int32_t s = pos;
while (pos<text.length() &&
u_isUWhiteSpace(text.charAt(pos))) {
++pos;
}
if (pos > s) {
continue;
}
} else if (pos<text.length() && text.charAt(pos)==ch) {
++pos;
continue;
}
parsePos.setIndex(start);
parsePos.setErrorIndex(pos);
return;
}
}
parsePos.setIndex(pos);
UErrorCode status = U_ZERO_ERROR;
if (ambiguousYear[0]) {
Calendar *copy = cal.clone();
UDate parsedDate = copy->getTime(status);
if (fHaveDefaultCentury && (parsedDate < fDefaultCenturyStart))
{
cal.set(UCAL_YEAR, fDefaultCenturyStartYear + 100);
}
delete copy;
}
if (U_FAILURE(status)) {
parsePos.setErrorIndex(pos);
parsePos.setIndex(start);
}
}
UDate
SimpleDateFormat::parse( const UnicodeString& text,
ParsePosition& pos) const {
return DateFormat::parse(text, pos);
}
UDate
SimpleDateFormat::parse(const UnicodeString& text, UErrorCode& status) const
{
return DateFormat::parse(text, status);
}
int32_t SimpleDateFormat::matchString(const UnicodeString& text,
int32_t start,
UCalendarDateFields field,
const UnicodeString* data,
int32_t dataCount,
Calendar& cal) const
{
int32_t i = 0;
int32_t count = dataCount;
if (field == UCAL_DAY_OF_WEEK) i = 1;
int32_t bestMatchLength = 0, bestMatch = -1;
UnicodeString lcaseText;
lcaseText.fastCopyFrom(text).foldCase();
for (; i < count; ++i)
{
int32_t length = data[i].length();
UnicodeString lcase;
lcase.fastCopyFrom(data[i]).foldCase();
if (length > bestMatchLength && (lcaseText.compareBetween(start, start + length, lcase, 0, length)) == 0)
{
bestMatch = i;
bestMatchLength = length;
}
}
if (bestMatch >= 0)
{
cal.set(field, bestMatch);
return start + bestMatchLength;
}
return -start;
}
void
SimpleDateFormat::set2DigitYearStart(UDate d, UErrorCode& status)
{
parseAmbiguousDatesAsAfter(d, status);
}
int32_t SimpleDateFormat::subParse(const UnicodeString& text, int32_t& start, UChar ch, int32_t count,
UBool obeyCount, UBool allowNegative, UBool ambiguousYear[], Calendar& cal) const
{
Formattable number;
int32_t value = 0;
int32_t i;
ParsePosition pos(0);
int32_t patternCharIndex;
UnicodeString temp;
UChar *patternCharPtr = u_strchr(DateFormatSymbols::getPatternUChars(), ch);
if (patternCharPtr == NULL) {
return -start;
}
patternCharIndex = (EField)(patternCharPtr - DateFormatSymbols::getPatternUChars());
UCalendarDateFields field = fgPatternIndexToCalendarField[patternCharIndex];
for (;;) {
if (start >= text.length()) {
return -start;
}
UChar32 c = text.char32At(start);
if (!u_isUWhiteSpace(c)) {
break;
}
start += UTF_CHAR_LENGTH(c);
}
pos.setIndex(start);
if (patternCharIndex == kHourOfDay1Field ||
patternCharIndex == kHour1Field ||
(patternCharIndex == kMonthField && count <= 2) ||
patternCharIndex == kYearField ||
patternCharIndex == kYearWOYField)
{
int32_t parseStart = pos.getIndex(); const UnicodeString* src;
if (obeyCount) {
if ((start+count) > text.length()) {
return -start;
}
text.extractBetween(0, start + count, temp);
src = &temp;
} else {
src = &text;
}
parseInt(*src, number, pos, allowNegative);
if (pos.getIndex() == parseStart)
return -start;
value = number.getLong();
}
switch (patternCharIndex) {
case kEraField:
return matchString(text, start, UCAL_ERA, fSymbols->fEras, fSymbols->fErasCount, cal);
case kYearField:
if (count <= 2 && (pos.getIndex() - start) == 2
&& u_isdigit(text.charAt(start))
&& u_isdigit(text.charAt(start+1)))
{
if(fHaveDefaultCentury) { int32_t ambiguousTwoDigitYear = fDefaultCenturyStartYear % 100;
ambiguousYear[0] = (value == ambiguousTwoDigitYear);
value += (fDefaultCenturyStartYear/100)*100 +
(value < ambiguousTwoDigitYear ? 100 : 0);
}
}
cal.set(UCAL_YEAR, value);
return pos.getIndex();
case kYearWOYField:
if (count <= 2 && (pos.getIndex() - start) == 2
&& u_isdigit(text.charAt(start))
&& u_isdigit(text.charAt(start+1))
&& fHaveDefaultCentury )
{
int32_t ambiguousTwoDigitYear = fDefaultCenturyStartYear % 100;
ambiguousYear[0] = (value == ambiguousTwoDigitYear);
value += (fDefaultCenturyStartYear/100)*100 +
(value < ambiguousTwoDigitYear ? 100 : 0);
}
cal.set(UCAL_YEAR_WOY, value);
return pos.getIndex();
case kMonthField:
if (count <= 2) {
cal.set(UCAL_MONTH, value - 1);
return pos.getIndex();
}
else
{
int32_t newStart = 0;
if ((newStart = matchString(text, start, UCAL_MONTH,
fSymbols->fMonths, fSymbols->fMonthsCount, cal)) > 0)
return newStart;
else return matchString(text, start, UCAL_MONTH,
fSymbols->fShortMonths, fSymbols->fShortMonthsCount, cal);
}
case kHourOfDay1Field:
if (value == cal.getMaximum(UCAL_HOUR_OF_DAY) + 1)
value = 0;
cal.set(UCAL_HOUR_OF_DAY, value);
return pos.getIndex();
case kDayOfWeekField:
{
int32_t newStart = 0;
if ((newStart = matchString(text, start, UCAL_DAY_OF_WEEK,
fSymbols->fWeekdays, fSymbols->fWeekdaysCount, cal)) > 0)
return newStart;
else return matchString(text, start, UCAL_DAY_OF_WEEK,
fSymbols->fShortWeekdays, fSymbols->fShortWeekdaysCount, cal);
}
case kAmPmField:
return matchString(text, start, UCAL_AM_PM, fSymbols->fAmPms, fSymbols->fAmPmsCount, cal);
case kHour1Field:
if (value == cal.getLeastMaximum(UCAL_HOUR)+1)
value = 0;
cal.set(UCAL_HOUR, value);
return pos.getIndex();
case kTimezoneField:
{
UnicodeString lcaseText(text);
UnicodeString lcaseGMT(fgGmt);
int32_t sign = 0;
int32_t offset;
int32_t gmtLen = lcaseGMT.length();
lcaseText.toLower();
lcaseGMT.toLower();
if ((text.length() - start) > gmtLen &&
(lcaseText.compare(start, gmtLen, lcaseGMT, 0, gmtLen)) == 0)
{
cal.set(UCAL_DST_OFFSET, 0);
pos.setIndex(start + gmtLen);
if( text[pos.getIndex()] == 0x002B )
sign = 1;
else if( text[pos.getIndex()] == 0x002D )
sign = -1;
else {
cal.set(UCAL_ZONE_OFFSET, 0 );
return pos.getIndex();
}
pos.setIndex(pos.getIndex() + 1);
int32_t parseStart = pos.getIndex();
Formattable tzNumber;
fNumberFormat->parse(text, tzNumber, pos);
if( pos.getIndex() == parseStart) {
return -start;
}
if( text[pos.getIndex()] == 0x003A ) {
offset = tzNumber.getLong() * 60;
pos.setIndex(pos.getIndex() + 1);
parseStart = pos.getIndex();
fNumberFormat->parse(text, tzNumber, pos);
if( pos.getIndex() == parseStart) {
return -start;
}
offset += tzNumber.getLong();
}
else {
offset = tzNumber.getLong();
if( offset < 24 )
offset *= 60;
else
offset = offset % 100 + offset / 100 * 60;
}
}
else {
const UnicodeString *zs;
int32_t j;
for (i = 0; i < fSymbols->fZoneStringsRowCount; i++)
{
for (j = 1; j <= 4; ++j)
{
zs = &fSymbols->fZoneStrings[i][j];
if (0 == (text.caseCompare(start, zs->length(), *zs, 0))) {
TimeZone *tz = TimeZone::createTimeZone(fSymbols->fZoneStrings[i][0]);
cal.set(UCAL_ZONE_OFFSET, tz->getRawOffset());
delete tz;
cal.set(UCAL_DST_OFFSET, j >= 3 ? U_MILLIS_PER_HOUR : 0);
return (start + fSymbols->fZoneStrings[i][j].length());
}
}
}
UErrorCode status = U_ZERO_ERROR;
DecimalFormat *fmt = new DecimalFormat("+####;-####", status);
if(U_FAILURE(status))
return -start;
fmt->setParseIntegerOnly(TRUE);
int32_t parseStart = pos.getIndex();
Formattable tzNumber;
fmt->parse( text, tzNumber, pos );
if( pos.getIndex() == parseStart) {
return -start; }
offset = tzNumber.getLong();
sign = 1;
if( offset < 0 ) {
sign = -1;
offset = -offset;
}
if( offset < 24 )
offset = offset * 60;
else
offset = offset % 100 + offset / 100 * 60;
}
if (sign != 0)
{
offset *= U_MILLIS_PER_MINUTE * sign;
if (cal.getTimeZone().useDaylightTime())
{
cal.set(UCAL_DST_OFFSET, U_MILLIS_PER_HOUR);
offset -= U_MILLIS_PER_HOUR;
}
cal.set(UCAL_ZONE_OFFSET, offset);
return pos.getIndex();
}
return -start;
}
default:
int32_t parseStart = pos.getIndex();
const UnicodeString* src;
if (obeyCount) {
if ((start+count) > text.length()) {
return -start;
}
text.extractBetween(0, start + count, temp);
src = &temp;
} else {
src = &text;
}
parseInt(*src, number, pos, allowNegative);
if (pos.getIndex() != parseStart) {
cal.set(field, number.getLong());
return pos.getIndex();
}
return -start;
}
}
void SimpleDateFormat::parseInt(const UnicodeString& text,
Formattable& number,
ParsePosition& pos,
UBool allowNegative) const {
UnicodeString oldPrefix;
DecimalFormat* df = NULL;
if (!allowNegative &&
fNumberFormat->getDynamicClassID() == DecimalFormat::getStaticClassID()) {
df = (DecimalFormat*)fNumberFormat;
df->getNegativePrefix(oldPrefix);
df->setNegativePrefix(SUPPRESS_NEGATIVE_PREFIX);
}
fNumberFormat->parse(text, number, pos);
if (df != NULL) {
df->setNegativePrefix(oldPrefix);
}
}
void SimpleDateFormat::translatePattern(const UnicodeString& originalPattern,
UnicodeString& translatedPattern,
const UnicodeString& from,
const UnicodeString& to,
UErrorCode& status)
{
if (U_FAILURE(status))
return;
translatedPattern.remove();
UBool inQuote = FALSE;
for (int32_t i = 0; i < originalPattern.length(); ++i) {
UChar c = originalPattern[i];
if (inQuote) {
if (c == QUOTE)
inQuote = FALSE;
}
else {
if (c == QUOTE)
inQuote = TRUE;
else if ((c >= 0x0061 && c <= 0x007A)
|| (c >= 0x0041 && c <= 0x005A )) {
int32_t ci = from.indexOf(c);
if (ci == -1) {
status = U_INVALID_FORMAT_ERROR;
return;
}
c = to[ci];
}
}
translatedPattern += c;
}
if (inQuote) {
status = U_INVALID_FORMAT_ERROR;
return;
}
}
UnicodeString&
SimpleDateFormat::toPattern(UnicodeString& result) const
{
result = fPattern;
return result;
}
UnicodeString&
SimpleDateFormat::toLocalizedPattern(UnicodeString& result,
UErrorCode& status) const
{
translatePattern(fPattern, result, DateFormatSymbols::getPatternUChars(), fSymbols->fLocalPatternChars, status);
return result;
}
void
SimpleDateFormat::applyPattern(const UnicodeString& pattern)
{
fPattern = pattern;
}
void
SimpleDateFormat::applyLocalizedPattern(const UnicodeString& pattern,
UErrorCode &status)
{
translatePattern(pattern, fPattern, fSymbols->fLocalPatternChars, DateFormatSymbols::getPatternUChars(), status);
}
const DateFormatSymbols*
SimpleDateFormat::getDateFormatSymbols() const
{
return fSymbols;
}
void
SimpleDateFormat::adoptDateFormatSymbols(DateFormatSymbols* newFormatSymbols)
{
delete fSymbols;
fSymbols = newFormatSymbols;
}
void
SimpleDateFormat::setDateFormatSymbols(const DateFormatSymbols& newFormatSymbols)
{
delete fSymbols;
fSymbols = new DateFormatSymbols(newFormatSymbols);
}
void SimpleDateFormat::adoptCalendar(Calendar* calendarToAdopt)
{
UErrorCode status = U_ZERO_ERROR;
DateFormat::adoptCalendar(calendarToAdopt);
delete fSymbols;
fSymbols=NULL;
initializeSymbols(fLocale, fCalendar, status); initializeDefaultCentury(); }
U_NAMESPACE_END
#endif