#define ZID_KEY_MAX 128
#include "unicode/utypes.h"
#if !UCONFIG_NO_FORMATTING
#include "unicode/smpdtfmt.h"
#include "unicode/dtfmtsym.h"
#include "unicode/ures.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/uniset.h"
#include "unicode/ustring.h"
#include "unicode/basictz.h"
#include "unicode/simpletz.h"
#include "unicode/rbtz.h"
#include "unicode/vtzone.h"
#include "olsontz.h"
#include "util.h"
#include "gregoimp.h"
#include "cstring.h"
#include "uassert.h"
#include "zstrfmt.h"
#include "cmemory.h"
#include "umutex.h"
#include "smpdtfst.h"
#include <float.h>
#if defined( U_DEBUG_CALSVC ) || defined (U_DEBUG_CAL)
#include <stdio.h>
#endif
U_NAMESPACE_BEGIN
static const UChar PATTERN_CHAR_BASE = 0x40;
static const UChar gGmt[] = {0x0047, 0x004D, 0x0054, 0x0000}; static const UChar gGmtPlus[] = {0x0047, 0x004D, 0x0054, 0x002B, 0x0000}; static const UChar gGmtMinus[] = {0x0047, 0x004D, 0x0054, 0x002D, 0x0000}; static const UChar gDefGmtPat[] = {0x0047, 0x004D, 0x0054, 0x007B, 0x0030, 0x007D, 0x0000};
static const UChar gDefGmtNegHmsPat[] = {0x002D, 0x0048, 0x0048, 0x003A, 0x006D, 0x006D, 0x003A, 0x0073, 0x0073, 0x0000};
static const UChar gDefGmtNegHmPat[] = {0x002D, 0x0048, 0x0048, 0x003A, 0x006D, 0x006D, 0x0000};
static const UChar gDefGmtPosHmsPat[] = {0x002B, 0x0048, 0x0048, 0x003A, 0x006D, 0x006D, 0x003A, 0x0073, 0x0073, 0x0000};
static const UChar gDefGmtPosHmPat[] = {0x002B, 0x0048, 0x0048, 0x003A, 0x006D, 0x006D, 0x0000};
typedef enum GmtPatSize {
kGmtLen = 3,
kGmtPatLen = 6,
kNegHmsLen = 9,
kNegHmLen = 6,
kPosHmsLen = 9,
kPosHmLen = 6
} GmtPatSize;
static const UChar gDefaultPattern[] =
{
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};
static const char gDateTimePatternsTag[]="DateTimePatterns";
static const UChar gEtcUTC[] = {0x45, 0x74, 0x63, 0x2F, 0x55, 0x54, 0x43, 0x00}; static const UChar QUOTE = 0x27; enum {
kGMTNegativeHMS = 0,
kGMTNegativeHM,
kGMTPositiveHMS,
kGMTPositiveHM,
kNumGMTFormatters
};
static const int32_t gFieldRangeBias[] = {
-1, -1, 1, 0, -1, -1, 0, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, -1, -1, -1 };
static UMTX LOCK;
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(SimpleDateFormat)
SimpleDateFormat::~SimpleDateFormat()
{
delete fSymbols;
if (fGMTFormatters) {
for (int32_t i = 0; i < kNumGMTFormatters; i++) {
if (fGMTFormatters[i]) {
delete fGMTFormatters[i];
}
}
uprv_free(fGMTFormatters);
}
}
SimpleDateFormat::SimpleDateFormat(UErrorCode& status)
: fLocale(Locale::getDefault()),
fSymbols(NULL),
fGMTFormatters(NULL)
{
construct(kShort, (EStyle) (kShort + kDateOffset), fLocale, status);
initializeDefaultCentury();
}
SimpleDateFormat::SimpleDateFormat(const UnicodeString& pattern,
UErrorCode &status)
: fPattern(pattern),
fLocale(Locale::getDefault()),
fSymbols(NULL),
fGMTFormatters(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),
fGMTFormatters(NULL)
{
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),
fGMTFormatters(NULL)
{
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)),
fGMTFormatters(NULL)
{
initializeCalendar(NULL, fLocale, status);
initialize(fLocale, status);
initializeDefaultCentury();
}
SimpleDateFormat::SimpleDateFormat(EStyle timeStyle,
EStyle dateStyle,
const Locale& locale,
UErrorCode& status)
: fLocale(locale),
fSymbols(NULL),
fGMTFormatters(NULL)
{
construct(timeStyle, dateStyle, fLocale, status);
if(U_SUCCESS(status)) {
initializeDefaultCentury();
}
}
SimpleDateFormat::SimpleDateFormat(const Locale& locale,
UErrorCode& status)
: fPattern(gDefaultPattern),
fLocale(locale),
fSymbols(NULL),
fGMTFormatters(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),
fGMTFormatters(NULL)
{
*this = other;
}
SimpleDateFormat& SimpleDateFormat::operator=(const SimpleDateFormat& other)
{
if (this == &other) {
return *this;
}
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)) {
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;
initializeCalendar(NULL, locale, status);
if (U_FAILURE(status)) return;
CalendarData calData(locale, fCalendar?fCalendar->getType():NULL, status);
UResourceBundle *dateTimePatterns = calData.getByKey(gDateTimePatternsTag, status);
if (U_FAILURE(status)) return;
if (ures_getSize(dateTimePatterns) <= kDateTime)
{
status = U_INVALID_FORMAT_ERROR;
return;
}
setLocaleIDs(ures_getLocaleByType(dateTimePatterns, ULOC_VALID_LOCALE, &status),
ures_getLocaleByType(dateTimePatterns, ULOC_ACTUAL_LOCALE, &status));
initializeSymbols(locale,fCalendar, status);
if (U_FAILURE(status)) return;
if (fSymbols == 0) {
status = U_MEMORY_ALLOCATION_ERROR;
return;
}
const UChar *resStr;
int32_t resStrLen = 0;
if ((timeStyle != kNone) && (dateStyle != kNone))
{
Formattable timeDateArray[2];
resStr = ures_getStringByIndex(dateTimePatterns, (int32_t)timeStyle, &resStrLen, &status);
UnicodeString *tempus1 = new UnicodeString(TRUE, resStr, resStrLen);
if (tempus1 == NULL) {
status = U_MEMORY_ALLOCATION_ERROR;
return;
}
timeDateArray[0].adoptString(tempus1);
resStr = ures_getStringByIndex(dateTimePatterns, (int32_t)dateStyle, &resStrLen, &status);
UnicodeString *tempus2 = new UnicodeString(TRUE, resStr, resStrLen);
if (tempus2 == NULL) {
status = U_MEMORY_ALLOCATION_ERROR;
return;
}
timeDateArray[1].adoptString(tempus2);
resStr = ures_getStringByIndex(dateTimePatterns, (int32_t)kDateTime, &resStrLen, &status);
MessageFormat::format(UnicodeString(TRUE, resStr, resStrLen), timeDateArray, 2, fPattern, status);
}
else if (timeStyle != kNone) {
resStr = ures_getStringByIndex(dateTimePatterns, (int32_t)timeStyle, &resStrLen, &status);
fPattern.setTo(TRUE, resStr, resStrLen);
}
else if (dateStyle != kNone) {
resStr = ures_getStringByIndex(dateTimePatterns, (int32_t)dateStyle, &resStrLen, &status);
fPattern.setTo(TRUE, resStr, resStrLen);
}
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);
}
if (U_SUCCESS(status) && fCalendar == NULL) {
status = U_MEMORY_ALLOCATION_ERROR;
}
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);
if (fSymbols == NULL) {
status = U_MEMORY_ALLOCATION_ERROR;
return;
}
}
}
void
SimpleDateFormat::initialize(const Locale& locale,
UErrorCode& status)
{
if (U_FAILURE(status)) 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);
fNumberFormat->setParseStrict(FALSE);
}
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 int32_t
SimpleDateFormat::fgCalendarFieldToLevel[] =
{
0, 10, 20,
20, 30,
30, 20, 30, 30,
40, 50, 50, 60,
70, 80,
0, 0, 10,
30, 10, 0,
40
};
const int32_t
SimpleDateFormat::fgPatternCharToLevel[] = {
-1, 40, -1, -1, 20, 30, 30, 0, 50, -1, -1, 50, 20, 20, -1, -1,
-1, 20, -1, 80, -1, -1, 0, 30, -1, 10, 0, -1, -1, -1, -1, -1,
-1, 40, -1, 30, 30, 30, -1, 0, 50, -1, -1, 50, -1, 60, -1, -1,
-1, 20, -1, 70, -1, 10, 0, 20, -1, 10, 0, -1, -1, -1, -1, -1
};
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, UCAL_EXTENDED_YEAR,
UCAL_JULIAN_DAY, UCAL_MILLISECONDS_IN_DAY, UCAL_ZONE_OFFSET,
UCAL_ZONE_OFFSET,
UCAL_DOW_LOCAL,
UCAL_MONTH,
UCAL_MONTH,
UCAL_MONTH,
UCAL_ZONE_OFFSET,
};
const UDateFormatField
SimpleDateFormat::fgPatternIndexToDateFormatField[] = {
UDAT_ERA_FIELD, UDAT_YEAR_FIELD, UDAT_MONTH_FIELD,
UDAT_DATE_FIELD, UDAT_HOUR_OF_DAY1_FIELD, UDAT_HOUR_OF_DAY0_FIELD,
UDAT_MINUTE_FIELD, UDAT_SECOND_FIELD, UDAT_FRACTIONAL_SECOND_FIELD,
UDAT_DAY_OF_WEEK_FIELD, UDAT_DAY_OF_YEAR_FIELD, UDAT_DAY_OF_WEEK_IN_MONTH_FIELD,
UDAT_WEEK_OF_YEAR_FIELD, UDAT_WEEK_OF_MONTH_FIELD, UDAT_AM_PM_FIELD,
UDAT_HOUR1_FIELD, UDAT_HOUR0_FIELD, UDAT_TIMEZONE_FIELD,
UDAT_YEAR_WOY_FIELD, UDAT_DOW_LOCAL_FIELD, UDAT_EXTENDED_YEAR_FIELD,
UDAT_JULIAN_DAY_FIELD, UDAT_MILLISECONDS_IN_DAY_FIELD, UDAT_TIMEZONE_RFC_FIELD,
UDAT_TIMEZONE_GENERIC_FIELD,
UDAT_STANDALONE_DAY_FIELD,
UDAT_STANDALONE_MONTH_FIELD,
UDAT_QUARTER_FIELD,
UDAT_STANDALONE_QUARTER_FIELD,
UDAT_TIMEZONE_SPECIAL_FIELD,
};
static inline void
_appendSymbol(UnicodeString& dst,
int32_t value,
const UnicodeString* symbols,
int32_t symbolsCount) {
U_ASSERT(0 <= value && value < symbolsCount);
if (0 <= value && value < symbolsCount) {
dst += symbols[value];
}
}
void
SimpleDateFormat::appendGMT(UnicodeString &appendTo, Calendar& cal, UErrorCode& status) const{
int32_t offset = cal.get(UCAL_ZONE_OFFSET, status) + cal.get(UCAL_DST_OFFSET, status);
if (U_FAILURE(status)) {
return;
}
if (isDefaultGMTFormat()) {
formatGMTDefault(appendTo, offset);
} else {
((SimpleDateFormat*)this)->initGMTFormatters(status);
if (U_SUCCESS(status)) {
int32_t type;
if (offset < 0) {
offset = -offset;
type = (offset % U_MILLIS_PER_MINUTE) == 0 ? kGMTNegativeHM : kGMTNegativeHMS;
} else {
type = (offset % U_MILLIS_PER_MINUTE) == 0 ? kGMTPositiveHM : kGMTPositiveHMS;
}
Formattable param(offset, Formattable::kIsDate);
FieldPosition fpos(0);
fGMTFormatters[type]->format(¶m, 1, appendTo, fpos, status);
}
}
}
int32_t
SimpleDateFormat::parseGMT(const UnicodeString &text, ParsePosition &pos) const {
if (!isDefaultGMTFormat()) {
int32_t start = pos.getIndex();
UBool prefixMatch = FALSE;
int32_t prefixLen = fSymbols->fGmtFormat.indexOf((UChar)0x007B );
if (prefixLen > 0 && text.compare(start, prefixLen, fSymbols->fGmtFormat, 0, prefixLen) == 0) {
prefixMatch = TRUE;
}
if (prefixMatch) {
UErrorCode status = U_ZERO_ERROR;
((SimpleDateFormat*)this)->initGMTFormatters(status);
if (U_SUCCESS(status)) {
Formattable parsed;
int32_t parsedCount;
fGMTFormatters[kGMTNegativeHMS]->parseObject(text, parsed, pos);
if (pos.getErrorIndex() == -1 && pos.getIndex() > start) {
parsed.getArray(parsedCount);
if (parsedCount == 1 && parsed[0].getType() == Formattable::kDate) {
return (int32_t)(-1 * (int64_t)parsed[0].getDate());
}
}
pos.setIndex(start);
pos.setErrorIndex(-1);
fGMTFormatters[kGMTPositiveHMS]->parseObject(text, parsed, pos);
if (pos.getErrorIndex() == -1 && pos.getIndex() > start) {
parsed.getArray(parsedCount);
if (parsedCount == 1 && parsed[0].getType() == Formattable::kDate) {
return (int32_t)((int64_t)parsed[0].getDate());
}
}
pos.setIndex(start);
pos.setErrorIndex(-1);
fGMTFormatters[kGMTNegativeHM]->parseObject(text, parsed, pos);
if (pos.getErrorIndex() == -1 && pos.getIndex() > start) {
parsed.getArray(parsedCount);
if (parsedCount == 1 && parsed[0].getType() == Formattable::kDate) {
return (int32_t)(-1 * (int64_t)parsed[0].getDate());
}
}
pos.setIndex(start);
pos.setErrorIndex(-1);
fGMTFormatters[kGMTPositiveHM]->parseObject(text, parsed, pos);
if (pos.getErrorIndex() == -1 && pos.getIndex() > start) {
parsed.getArray(parsedCount);
if (parsedCount == 1 && parsed[0].getType() == Formattable::kDate) {
return (int32_t)((int64_t)parsed[0].getDate());
}
}
pos.setIndex(start);
pos.setErrorIndex(-1);
}
}
}
return parseGMTDefault(text, pos);
}
void
SimpleDateFormat::formatGMTDefault(UnicodeString &appendTo, int32_t offset) const {
if (offset < 0) {
appendTo += gGmtMinus;
offset = -offset; }else{
appendTo += gGmtPlus;
}
offset /= U_MILLIS_PER_SECOND; int32_t sec = offset % 60;
offset /= 60;
int32_t min = offset % 60;
int32_t hour = offset / 60;
zeroPaddingNumber(appendTo, hour, 2, 2);
appendTo += (UChar)0x003A ;
zeroPaddingNumber(appendTo, min, 2, 2);
if (sec != 0) {
appendTo += (UChar)0x003A ;
zeroPaddingNumber(appendTo, sec, 2, 2);
}
}
int32_t
SimpleDateFormat::parseGMTDefault(const UnicodeString &text, ParsePosition &pos) const {
int32_t start = pos.getIndex();
if (start + kGmtLen + 1 >= text.length()) {
pos.setErrorIndex(start);
return 0;
}
int32_t cur = start;
if (text.compare(start, kGmtLen, gGmt) != 0) {
pos.setErrorIndex(start);
return 0;
}
cur += kGmtLen;
UBool negative = FALSE;
if (text.charAt(cur) == (UChar)0x002D ) {
negative = TRUE;
} else if (text.charAt(cur) != (UChar)0x002B ) {
pos.setErrorIndex(cur);
return 0;
}
cur++;
int32_t numLen;
pos.setIndex(cur);
Formattable number;
parseInt(text, number, 6, pos, FALSE);
numLen = pos.getIndex() - cur;
if (numLen <= 0) {
pos.setIndex(start);
pos.setErrorIndex(cur);
return 0;
}
int32_t numVal = number.getLong();
int32_t hour = 0;
int32_t min = 0;
int32_t sec = 0;
if (numLen <= 2) {
hour = numVal;
cur += numLen;
if (cur + 2 < text.length() && text.charAt(cur) == (UChar)0x003A ) {
cur++;
pos.setIndex(cur);
parseInt(text, number, 2, pos, FALSE);
numLen = pos.getIndex() - cur;
if (numLen == 2) {
min = number.getLong();
cur += numLen;
if (cur + 2 < text.length() && text.charAt(cur) == (UChar)0x003A ) {
cur++;
pos.setIndex(cur);
parseInt(text, number, 2, pos, FALSE);
numLen = pos.getIndex() - cur;
if (numLen == 2) {
sec = number.getLong();
} else {
pos.setIndex(cur - 1);
pos.setErrorIndex(-1);
}
}
} else {
pos.setIndex(cur - 1);
pos.setErrorIndex(-1);
}
}
} else if (numLen == 3 || numLen == 4) {
hour = numVal / 100;
min = numVal % 100;
} else if (numLen == 5 || numLen == 6) {
hour = numVal / 10000;
min = (numVal % 10000) / 100;
sec = numVal % 100;
} else {
pos.setIndex(cur + 6);
int32_t shift = numLen - 6;
while (shift > 0) {
numVal /= 10;
shift--;
}
hour = numVal / 10000;
min = (numVal % 10000) / 100;
sec = numVal % 100;
}
int32_t offset = ((hour*60 + min)*60 + sec)*1000;
if (negative) {
offset = -offset;
}
return offset;
}
UBool
SimpleDateFormat::isDefaultGMTFormat() const {
if (fSymbols->fGmtFormat.length() == 0) {
return TRUE;
} else if (fSymbols->fGmtFormat.compare(gDefGmtPat, kGmtPatLen) != 0) {
return FALSE;
}
if (fSymbols->fGmtHourFormats == NULL || fSymbols->fGmtHourFormatsCount != DateFormatSymbols::GMT_HOUR_COUNT) {
return TRUE;
} else if ((fSymbols->fGmtHourFormats[DateFormatSymbols::GMT_NEGATIVE_HMS].compare(gDefGmtNegHmsPat, kNegHmsLen) != 0)
|| (fSymbols->fGmtHourFormats[DateFormatSymbols::GMT_NEGATIVE_HM].compare(gDefGmtNegHmPat, kNegHmLen) != 0)
|| (fSymbols->fGmtHourFormats[DateFormatSymbols::GMT_POSITIVE_HMS].compare(gDefGmtPosHmsPat, kPosHmsLen) != 0)
|| (fSymbols->fGmtHourFormats[DateFormatSymbols::GMT_POSITIVE_HM].compare(gDefGmtPosHmPat, kPosHmLen) != 0)) {
return FALSE;
}
return TRUE;
}
void
SimpleDateFormat::formatRFC822TZ(UnicodeString &appendTo, int32_t offset) const {
UChar sign = 0x002B ;
if (offset < 0) {
offset = -offset;
sign = 0x002D ;
}
appendTo.append(sign);
int32_t offsetH = offset / U_MILLIS_PER_HOUR;
offset = offset % U_MILLIS_PER_HOUR;
int32_t offsetM = offset / U_MILLIS_PER_MINUTE;
offset = offset % U_MILLIS_PER_MINUTE;
int32_t offsetS = offset / U_MILLIS_PER_SECOND;
int32_t num = 0, denom = 0;
if (offsetS == 0) {
offset = offsetH*100 + offsetM; num = offset % 10000;
denom = 1000;
} else {
offset = offsetH*10000 + offsetM*100 + offsetS; num = offset % 1000000;
denom = 100000;
}
while (denom >= 1) {
UChar digit = (UChar)0x0030 + (num / denom);
appendTo.append(digit);
num = num % denom;
denom /= 10;
}
}
void
SimpleDateFormat::initGMTFormatters(UErrorCode &status) {
if (U_FAILURE(status)) {
return;
}
umtx_lock(&LOCK);
if (fGMTFormatters == NULL) {
fGMTFormatters = (MessageFormat**)uprv_malloc(kNumGMTFormatters * sizeof(MessageFormat*));
if (fGMTFormatters) {
for (int32_t i = 0; i < kNumGMTFormatters; i++) {
const UnicodeString *hourPattern = NULL; switch (i) {
case kGMTNegativeHMS:
hourPattern = &(fSymbols->fGmtHourFormats[DateFormatSymbols::GMT_NEGATIVE_HMS]);
break;
case kGMTNegativeHM:
hourPattern = &(fSymbols->fGmtHourFormats[DateFormatSymbols::GMT_NEGATIVE_HM]);
break;
case kGMTPositiveHMS:
hourPattern = &(fSymbols->fGmtHourFormats[DateFormatSymbols::GMT_POSITIVE_HMS]);
break;
case kGMTPositiveHM:
hourPattern = &(fSymbols->fGmtHourFormats[DateFormatSymbols::GMT_POSITIVE_HM]);
break;
}
fGMTFormatters[i] = new MessageFormat(fSymbols->fGmtFormat, status);
if (U_FAILURE(status)) {
break;
}
SimpleDateFormat *sdf = (SimpleDateFormat*)this->clone();
sdf->adoptTimeZone(TimeZone::createTimeZone(UnicodeString(gEtcUTC)));
sdf->applyPattern(*hourPattern);
fGMTFormatters[i]->adoptFormat(0, sdf);
}
} else {
status = U_MEMORY_ALLOCATION_ERROR;
}
}
umtx_unlock(&LOCK);
}
void
SimpleDateFormat::subFormat(UnicodeString &appendTo,
UChar ch,
int32_t count,
FieldPosition& pos,
Calendar& cal,
UErrorCode& status) const
{
if (U_FAILURE(status)) {
return;
}
UChar *patternCharPtr = u_strchr(DateFormatSymbols::getPatternUChars(), ch);
UDateFormatField patternCharIndex;
const int32_t maxIntCount = 10;
int32_t beginOffset = appendTo.length();
if (patternCharPtr == NULL)
{
status = U_INVALID_FORMAT_ERROR;
return;
}
patternCharIndex = (UDateFormatField)(patternCharPtr - DateFormatSymbols::getPatternUChars());
UCalendarDateFields field = fgPatternIndexToCalendarField[patternCharIndex];
int32_t value = cal.get(field, status);
if (U_FAILURE(status)) {
return;
}
switch (patternCharIndex) {
case UDAT_ERA_FIELD:
if (count == 5)
_appendSymbol(appendTo, value, fSymbols->fNarrowEras, fSymbols->fNarrowErasCount);
else if (count == 4)
_appendSymbol(appendTo, value, fSymbols->fEraNames, fSymbols->fEraNamesCount);
else
_appendSymbol(appendTo, value, fSymbols->fEras, fSymbols->fErasCount);
break;
case UDAT_YEAR_FIELD:
case UDAT_YEAR_WOY_FIELD:
if(count == 2)
zeroPaddingNumber(appendTo, value, 2, 2);
else
zeroPaddingNumber(appendTo, value, count, maxIntCount);
break;
case UDAT_MONTH_FIELD:
if (count == 5)
_appendSymbol(appendTo, value, fSymbols->fNarrowMonths,
fSymbols->fNarrowMonthsCount);
else if (count == 4)
_appendSymbol(appendTo, value, fSymbols->fMonths,
fSymbols->fMonthsCount);
else if (count == 3)
_appendSymbol(appendTo, value, fSymbols->fShortMonths,
fSymbols->fShortMonthsCount);
else
zeroPaddingNumber(appendTo, value + 1, count, maxIntCount);
break;
case UDAT_STANDALONE_MONTH_FIELD:
if (count == 5)
_appendSymbol(appendTo, value, fSymbols->fStandaloneNarrowMonths,
fSymbols->fStandaloneNarrowMonthsCount);
else if (count == 4)
_appendSymbol(appendTo, value, fSymbols->fStandaloneMonths,
fSymbols->fStandaloneMonthsCount);
else if (count == 3)
_appendSymbol(appendTo, value, fSymbols->fStandaloneShortMonths,
fSymbols->fStandaloneShortMonthsCount);
else
zeroPaddingNumber(appendTo, value + 1, count, maxIntCount);
break;
case UDAT_HOUR_OF_DAY1_FIELD:
if (value == 0)
zeroPaddingNumber(appendTo, cal.getMaximum(UCAL_HOUR_OF_DAY) + 1, count, maxIntCount);
else
zeroPaddingNumber(appendTo, value, count, maxIntCount);
break;
case UDAT_FRACTIONAL_SECOND_FIELD:
{
fNumberFormat->setMinimumIntegerDigits((count > 3) ? 3 : count);
fNumberFormat->setMaximumIntegerDigits(maxIntCount);
if (count == 1) {
value = (value + 50) / 100;
} else if (count == 2) {
value = (value + 5) / 10;
}
FieldPosition p(0);
fNumberFormat->format(value, appendTo, p);
if (count > 3) {
fNumberFormat->setMinimumIntegerDigits(count - 3);
fNumberFormat->format((int32_t)0, appendTo, p);
}
}
break;
case UDAT_DOW_LOCAL_FIELD:
if ( count < 3 ) {
zeroPaddingNumber(appendTo, value, count, maxIntCount);
break;
}
value = cal.get(UCAL_DAY_OF_WEEK, status);
if (U_FAILURE(status)) {
return;
}
case UDAT_DAY_OF_WEEK_FIELD:
if (count == 5)
_appendSymbol(appendTo, value, fSymbols->fNarrowWeekdays,
fSymbols->fNarrowWeekdaysCount);
else if (count == 4)
_appendSymbol(appendTo, value, fSymbols->fWeekdays,
fSymbols->fWeekdaysCount);
else
_appendSymbol(appendTo, value, fSymbols->fShortWeekdays,
fSymbols->fShortWeekdaysCount);
break;
case UDAT_STANDALONE_DAY_FIELD:
if ( count < 3 ) {
zeroPaddingNumber(appendTo, value, 1, maxIntCount);
break;
}
value = cal.get(UCAL_DAY_OF_WEEK, status);
if (U_FAILURE(status)) {
return;
}
if (count == 5)
_appendSymbol(appendTo, value, fSymbols->fStandaloneNarrowWeekdays,
fSymbols->fStandaloneNarrowWeekdaysCount);
else if (count == 4)
_appendSymbol(appendTo, value, fSymbols->fStandaloneWeekdays,
fSymbols->fStandaloneWeekdaysCount);
else _appendSymbol(appendTo, value, fSymbols->fStandaloneShortWeekdays,
fSymbols->fStandaloneShortWeekdaysCount);
break;
case UDAT_AM_PM_FIELD:
_appendSymbol(appendTo, value, fSymbols->fAmPms,
fSymbols->fAmPmsCount);
break;
case UDAT_HOUR1_FIELD:
if (value == 0)
zeroPaddingNumber(appendTo, cal.getLeastMaximum(UCAL_HOUR) + 1, count, maxIntCount);
else
zeroPaddingNumber(appendTo, value, count, maxIntCount);
break;
case UDAT_TIMEZONE_FIELD:
case UDAT_TIMEZONE_GENERIC_FIELD:
case UDAT_TIMEZONE_SPECIAL_FIELD:
{
UnicodeString zoneString;
const ZoneStringFormat *zsf = fSymbols->getZoneStringFormat();
if (zsf) {
if (patternCharIndex == UDAT_TIMEZONE_FIELD) {
if (count < 4) {
zsf->getSpecificShortString(cal, TRUE ,
zoneString, status);
} else {
zsf->getSpecificLongString(cal, zoneString, status);
}
} else if (patternCharIndex == UDAT_TIMEZONE_GENERIC_FIELD) {
if (count == 1) {
zsf->getGenericShortString(cal, TRUE ,
zoneString, status);
} else if (count == 4) {
zsf->getGenericLongString(cal, zoneString, status);
}
} else { if (count == 1) {
zsf->getSpecificShortString(cal, FALSE ,
zoneString, status);
} else if (count == 4) {
zsf->getGenericLocationString(cal, zoneString, status);
}
}
}
if (zoneString.isEmpty()) {
appendGMT(appendTo, cal, status);
} else {
appendTo += zoneString;
}
}
break;
case UDAT_TIMEZONE_RFC_FIELD: if (count < 4) {
value = (cal.get(UCAL_ZONE_OFFSET, status) + cal.get(UCAL_DST_OFFSET, status));
formatRFC822TZ(appendTo, value);
} else {
appendGMT(appendTo, cal, status);
}
break;
case UDAT_QUARTER_FIELD:
if (count >= 4)
_appendSymbol(appendTo, value/3, fSymbols->fQuarters,
fSymbols->fQuartersCount);
else if (count == 3)
_appendSymbol(appendTo, value/3, fSymbols->fShortQuarters,
fSymbols->fShortQuartersCount);
else
zeroPaddingNumber(appendTo, (value/3) + 1, count, maxIntCount);
break;
case UDAT_STANDALONE_QUARTER_FIELD:
if (count >= 4)
_appendSymbol(appendTo, value/3, fSymbols->fStandaloneQuarters,
fSymbols->fStandaloneQuartersCount);
else if (count == 3)
_appendSymbol(appendTo, value/3, fSymbols->fStandaloneShortQuarters,
fSymbols->fStandaloneShortQuartersCount);
else
zeroPaddingNumber(appendTo, (value/3) + 1, count, maxIntCount);
break;
default:
zeroPaddingNumber(appendTo, value, count, maxIntCount);
break;
}
if (pos.getBeginIndex() == pos.getEndIndex() &&
pos.getField() == fgPatternIndexToDateFormatField[patternCharIndex]) {
pos.setBeginIndex(beginOffset);
pos.setEndIndex(appendTo.length());
}
}
void
SimpleDateFormat::zeroPaddingNumber(UnicodeString &appendTo, int32_t value, int32_t minDigits, int32_t maxDigits) const
{
if (fNumberFormat!=NULL) {
FieldPosition pos(0);
fNumberFormat->setMinimumIntegerDigits(minDigits);
fNumberFormat->setMaximumIntegerDigits(maxDigits);
fNumberFormat->format(value, appendTo, pos); }
}
static const UChar NUMERIC_FORMAT_CHARS[] = {0x4D, 0x59, 0x79, 0x75, 0x64, 0x65, 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
{
UErrorCode status = U_ZERO_ERROR;
int32_t pos = parsePos.getIndex();
int32_t start = pos;
UBool ambiguousYear[] = { FALSE };
int32_t count = 0;
UBool lenient = isLenient();
((SimpleDateFormat*)this)->tztype = TZTYPE_UNK;
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 = subParse(text, pos, ch, count,
FALSE, TRUE, ambiguousYear, cal);
if (s <= 0) {
status = U_PARSE_ERROR;
goto ExitParse;
}
pos = s;
}
}
else {
abutPat = -1;
if (! matchLiterals(fPattern, i, text, pos, lenient)) {
status = U_PARSE_ERROR;
goto ExitParse;
}
}
}
parsePos.setIndex(pos);
if (ambiguousYear[0] || tztype != TZTYPE_UNK) {
Calendar *copy;
if (ambiguousYear[0]) {
copy = cal.clone();
if (copy == NULL) {
status = U_MEMORY_ALLOCATION_ERROR;
goto ExitParse;
}
UDate parsedDate = copy->getTime(status);
if (fHaveDefaultCentury && (parsedDate < fDefaultCenturyStart)) {
cal.set(UCAL_YEAR, fDefaultCenturyStartYear + 100);
}
delete copy;
}
if (tztype != TZTYPE_UNK) {
copy = cal.clone();
if (copy == NULL) {
status = U_MEMORY_ALLOCATION_ERROR;
goto ExitParse;
}
const TimeZone & tz = cal.getTimeZone();
BasicTimeZone *btz = NULL;
if (tz.getDynamicClassID() == OlsonTimeZone::getStaticClassID()
|| tz.getDynamicClassID() == SimpleTimeZone::getStaticClassID()
|| tz.getDynamicClassID() == RuleBasedTimeZone::getStaticClassID()
|| tz.getDynamicClassID() == VTimeZone::getStaticClassID()) {
btz = (BasicTimeZone*)&tz;
}
copy->set(UCAL_ZONE_OFFSET, 0);
copy->set(UCAL_DST_OFFSET, 0);
UDate localMillis = copy->getTime(status);
int32_t raw, dst;
if (btz != NULL) {
if (tztype == TZTYPE_STD) {
btz->getOffsetFromLocal(localMillis,
BasicTimeZone::kStandard, BasicTimeZone::kStandard, raw, dst, status);
} else {
btz->getOffsetFromLocal(localMillis,
BasicTimeZone::kDaylight, BasicTimeZone::kDaylight, raw, dst, status);
}
} else {
tz.getOffset(localMillis, TRUE, raw, dst, status);
}
int32_t resolvedSavings = dst;
if (tztype == TZTYPE_STD) {
if (dst != 0) {
resolvedSavings = 0;
}
} else { if (dst == 0) {
if (btz != NULL) {
UDate time = localMillis + raw;
TimeZoneTransition beforeTrs, afterTrs;
UDate beforeT = time, afterT = time;
int32_t beforeSav = 0, afterSav = 0;
UBool beforeTrsAvail, afterTrsAvail;
while (TRUE) {
beforeTrsAvail = btz->getPreviousTransition(beforeT, TRUE, beforeTrs);
if (!beforeTrsAvail) {
break;
}
beforeT = beforeTrs.getTime() - 1;
beforeSav = beforeTrs.getFrom()->getDSTSavings();
if (beforeSav != 0) {
break;
}
}
while (TRUE) {
afterTrsAvail = btz->getNextTransition(afterT, FALSE, afterTrs);
if (!afterTrsAvail) {
break;
}
afterT = afterTrs.getTime();
afterSav = afterTrs.getTo()->getDSTSavings();
if (afterSav != 0) {
break;
}
}
if (beforeTrsAvail && afterTrsAvail) {
if (time - beforeT > afterT - time) {
resolvedSavings = afterSav;
} else {
resolvedSavings = beforeSav;
}
} else if (beforeTrsAvail && beforeSav != 0) {
resolvedSavings = beforeSav;
} else if (afterTrsAvail && afterSav != 0) {
resolvedSavings = afterSav;
} else {
resolvedSavings = btz->getDSTSavings();
}
} else {
resolvedSavings = tz.getDSTSavings();
}
if (resolvedSavings == 0) {
resolvedSavings = U_MILLIS_PER_HOUR;
}
}
}
cal.set(UCAL_ZONE_OFFSET, raw);
cal.set(UCAL_DST_OFFSET, resolvedSavings);
delete copy;
}
}
ExitParse:
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::matchQuarterString(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;
int32_t bestMatchLength = 0, bestMatch = -1;
UnicodeString lcase, lcaseText;
text.extract(start, INT32_MAX, lcaseText);
lcaseText.foldCase();
for (; i < count; ++i)
{
lcase.fastCopyFrom(data[i]).foldCase();
int32_t length = lcase.length();
if (length > bestMatchLength &&
lcaseText.compareBetween(0, length, lcase, 0, length) == 0)
{
bestMatch = i;
bestMatchLength = length;
}
}
if (bestMatch >= 0)
{
cal.set(field, bestMatch * 3);
lcase.fastCopyFrom(data[bestMatch]).foldCase();
int32_t len = data[bestMatch].length(); int32_t n = text.length() - start;
for (i=0; i<=n; ++i) {
int32_t j=i;
if (i == 0) {
j = len;
} else if (i == len) {
continue; }
text.extract(start, j, lcaseText);
lcaseText.foldCase();
if (lcase == lcaseText) {
return start + j;
}
}
}
return -start;
}
UBool SimpleDateFormat::matchLiterals(const UnicodeString &pattern,
int32_t &patternOffset,
const UnicodeString &text,
int32_t &textOffset,
UBool lenient)
{
UBool inQuote = FALSE;
UnicodeString literal;
int32_t i = patternOffset;
for ( ; i < pattern.length(); i += 1) {
UChar ch = pattern.charAt(i);
if (!inQuote && ((ch >= 0x41 && ch <= 0x5A) || (ch >= 0x61 && ch <= 0x7A))) { break;
}
if (ch == QUOTE) {
if ((i + 1) < pattern.length() && pattern.charAt(i + 1) == QUOTE) {
i += 1;
} else {
inQuote = !inQuote;
continue;
}
}
literal += ch;
}
int32_t p;
int32_t t = textOffset;
if (lenient) {
literal.trim();
while (t < text.length() && u_isWhitespace(text.charAt(t))) {
t += 1;
}
}
for (p = 0; p < literal.length() && t < text.length(); p += 1, t += 1) {
UBool needWhitespace = FALSE;
while (p < literal.length() && uprv_isRuleWhiteSpace(literal.charAt(p))) {
needWhitespace = TRUE;
p += 1;
}
if (needWhitespace) {
int32_t tStart = t;
while (t < text.length()) {
UChar tch = text.charAt(t);
if (!u_isUWhiteSpace(tch) && !uprv_isRuleWhiteSpace(tch)) {
break;
}
t += 1;
}
if (!lenient && t == tStart) {
return FALSE;
}
if (p >= literal.length()) {
break;
}
}
if (t >= text.length() || literal.charAt(p) != text.charAt(t)) {
if (lenient) {
break;
}
return FALSE;
}
}
if (p <= 0) {
const UnicodeSet *ignorables = NULL;
UChar *patternCharPtr = u_strchr(DateFormatSymbols::getPatternUChars(), pattern.charAt(i));
if (patternCharPtr != NULL) {
UDateFormatField patternCharIndex = (UDateFormatField) (patternCharPtr - DateFormatSymbols::getPatternUChars());
ignorables = SimpleDateFormatStaticSets::getIgnorables(patternCharIndex);
}
for (t = textOffset; t < text.length(); t += 1) {
UChar ch = text.charAt(t);
if (ignorables == NULL || !ignorables->contains(ch)) {
break;
}
}
}
patternOffset = i - 1;
textOffset = t;
return TRUE;
}
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 lcase, lcaseText;
text.extract(start, INT32_MAX, lcaseText);
lcaseText.foldCase();
for (; i < count; ++i)
{
lcase.fastCopyFrom(data[i]).foldCase();
int32_t length = lcase.length();
if (length > bestMatchLength &&
lcaseText.compareBetween(0, length, lcase, 0, length) == 0)
{
bestMatch = i;
bestMatchLength = length;
}
}
if (bestMatch >= 0)
{
cal.set(field, bestMatch);
lcase.fastCopyFrom(data[bestMatch]).foldCase();
int32_t len = data[bestMatch].length(); int32_t n = text.length() - start;
for (i=0; i<=n; ++i) {
int32_t j=i;
if (i == 0) {
j = len;
} else if (i == len) {
continue; }
text.extract(start, j, lcaseText);
lcaseText.foldCase();
if (lcase == lcaseText) {
return start + j;
}
}
}
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);
UBool lenient = isLenient();
UBool gotNumber = FALSE;
#if defined (U_DEBUG_CAL)
#endif
if (patternCharPtr == NULL) {
return -start;
}
patternCharIndex = (UDateFormatField)(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 == UDAT_HOUR_OF_DAY1_FIELD ||
patternCharIndex == UDAT_HOUR_OF_DAY0_FIELD ||
patternCharIndex == UDAT_HOUR1_FIELD ||
patternCharIndex == UDAT_HOUR0_FIELD ||
patternCharIndex == UDAT_DOW_LOCAL_FIELD ||
patternCharIndex == UDAT_STANDALONE_DAY_FIELD ||
patternCharIndex == UDAT_MONTH_FIELD ||
patternCharIndex == UDAT_STANDALONE_MONTH_FIELD ||
patternCharIndex == UDAT_QUARTER_FIELD ||
patternCharIndex == UDAT_STANDALONE_QUARTER_FIELD ||
patternCharIndex == UDAT_YEAR_FIELD ||
patternCharIndex == UDAT_YEAR_WOY_FIELD ||
patternCharIndex == UDAT_FRACTIONAL_SECOND_FIELD)
{
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) {
value = number.getLong();
gotNumber = TRUE;
int32_t bias = gFieldRangeBias[patternCharIndex];
if (bias >= 0 && (value > cal.getMaximum(field) + bias || value < cal.getMinimum(field) + bias)) {
return -start;
}
}
}
switch (patternCharIndex) {
case UDAT_HOUR_OF_DAY1_FIELD:
case UDAT_HOUR_OF_DAY0_FIELD:
case UDAT_HOUR1_FIELD:
case UDAT_HOUR0_FIELD:
if (value < 0 || value > 24) {
return -start;
}
case UDAT_YEAR_FIELD:
case UDAT_YEAR_WOY_FIELD:
case UDAT_FRACTIONAL_SECOND_FIELD:
if (! gotNumber) {
return -start;
}
break;
case UDAT_DOW_LOCAL_FIELD:
case UDAT_STANDALONE_DAY_FIELD:
case UDAT_MONTH_FIELD:
case UDAT_STANDALONE_MONTH_FIELD:
case UDAT_QUARTER_FIELD:
case UDAT_STANDALONE_QUARTER_FIELD:
if (!lenient && gotNumber && count > 2) {
gotNumber = FALSE;
pos.setIndex(start);
}
break;
default:
break;
}
switch (patternCharIndex) {
case UDAT_ERA_FIELD:
if (count == 5) {
return matchString(text, start, UCAL_ERA, fSymbols->fNarrowEras, fSymbols->fNarrowErasCount, cal);
}
if (count == 4) {
return matchString(text, start, UCAL_ERA, fSymbols->fEraNames, fSymbols->fEraNamesCount, cal);
}
return matchString(text, start, UCAL_ERA, fSymbols->fEras, fSymbols->fErasCount, cal);
case UDAT_YEAR_FIELD:
if ((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 UDAT_YEAR_WOY_FIELD:
if ((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 UDAT_MONTH_FIELD:
if (gotNumber) {
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 UDAT_STANDALONE_MONTH_FIELD:
if (gotNumber) {
cal.set(UCAL_MONTH, value - 1);
return pos.getIndex();
} else {
int32_t newStart = 0;
if ((newStart = matchString(text, start, UCAL_MONTH,
fSymbols->fStandaloneMonths, fSymbols->fStandaloneMonthsCount, cal)) > 0)
return newStart;
else return matchString(text, start, UCAL_MONTH,
fSymbols->fStandaloneShortMonths, fSymbols->fStandaloneShortMonthsCount, cal);
}
case UDAT_HOUR_OF_DAY1_FIELD:
if (value == cal.getMaximum(UCAL_HOUR_OF_DAY) + 1)
value = 0;
case UDAT_HOUR_OF_DAY0_FIELD:
cal.set(UCAL_HOUR_OF_DAY, value);
return pos.getIndex();
case UDAT_FRACTIONAL_SECOND_FIELD:
i = pos.getIndex() - start;
if (i < 3) {
while (i < 3) {
value *= 10;
i++;
}
} else {
int32_t a = 1;
while (i > 3) {
a *= 10;
i--;
}
value = (value + (a>>1)) / a;
}
cal.set(UCAL_MILLISECOND, value);
return pos.getIndex();
case UDAT_DOW_LOCAL_FIELD:
if (gotNumber) {
cal.set(UCAL_DOW_LOCAL, value);
return pos.getIndex();
}
case UDAT_DAY_OF_WEEK_FIELD:
{
int32_t newStart = 0;
if ((newStart = matchString(text, start, UCAL_DAY_OF_WEEK,
fSymbols->fWeekdays, fSymbols->fWeekdaysCount, cal)) > 0)
return newStart;
else if ((newStart = matchString(text, start, UCAL_DAY_OF_WEEK,
fSymbols->fShortWeekdays, fSymbols->fShortWeekdaysCount, cal)) > 0)
return newStart;
else
return matchString(text, start, UCAL_DAY_OF_WEEK,
fSymbols->fNarrowWeekdays, fSymbols->fNarrowWeekdaysCount, cal);
}
case UDAT_STANDALONE_DAY_FIELD:
{
if (gotNumber) {
cal.set(UCAL_DOW_LOCAL, value);
return pos.getIndex();
}
int32_t newStart = 0;
if ((newStart = matchString(text, start, UCAL_DAY_OF_WEEK,
fSymbols->fStandaloneWeekdays, fSymbols->fStandaloneWeekdaysCount, cal)) > 0)
return newStart;
else return matchString(text, start, UCAL_DAY_OF_WEEK,
fSymbols->fStandaloneShortWeekdays, fSymbols->fStandaloneShortWeekdaysCount, cal);
}
case UDAT_AM_PM_FIELD:
return matchString(text, start, UCAL_AM_PM, fSymbols->fAmPms, fSymbols->fAmPmsCount, cal);
case UDAT_HOUR1_FIELD:
if (value == cal.getLeastMaximum(UCAL_HOUR)+1)
value = 0;
case UDAT_HOUR0_FIELD:
cal.set(UCAL_HOUR, value);
return pos.getIndex();
case UDAT_QUARTER_FIELD:
if (gotNumber) {
cal.set(UCAL_MONTH, (value - 1) * 3);
return pos.getIndex();
} else {
int32_t newStart = 0;
if ((newStart = matchQuarterString(text, start, UCAL_MONTH,
fSymbols->fQuarters, fSymbols->fQuartersCount, cal)) > 0)
return newStart;
else return matchQuarterString(text, start, UCAL_MONTH,
fSymbols->fShortQuarters, fSymbols->fShortQuartersCount, cal);
}
case UDAT_STANDALONE_QUARTER_FIELD:
if (gotNumber) {
cal.set(UCAL_MONTH, (value - 1) * 3);
return pos.getIndex();
} else {
int32_t newStart = 0;
if ((newStart = matchQuarterString(text, start, UCAL_MONTH,
fSymbols->fStandaloneQuarters, fSymbols->fStandaloneQuartersCount, cal)) > 0)
return newStart;
else return matchQuarterString(text, start, UCAL_MONTH,
fSymbols->fStandaloneShortQuarters, fSymbols->fStandaloneShortQuartersCount, cal);
}
case UDAT_TIMEZONE_FIELD:
case UDAT_TIMEZONE_RFC_FIELD:
case UDAT_TIMEZONE_GENERIC_FIELD:
case UDAT_TIMEZONE_SPECIAL_FIELD:
{
int32_t offset = 0;
UBool parsed = FALSE;
offset = parseGMT(text, pos);
if (pos.getIndex() - start > 0) {
parsed = TRUE;
}
if (!parsed) {
do {
int32_t sign = 0;
UChar signChar = text.charAt(start);
if (signChar == (UChar)0x002B ) {
sign = 1;
} else if (signChar == (UChar)0x002D ) {
sign = -1;
} else {
break;
}
int32_t orgPos = start + 1;
pos.setIndex(orgPos);
parseInt(text, number, 6, pos, FALSE);
int32_t numLen = pos.getIndex() - orgPos;
if (numLen <= 0) {
break;
}
int32_t val = number.getLong();
int32_t hour = 0, min = 0, sec = 0;
switch(numLen) {
case 1: case 2: hour = val;
break;
case 3: case 4: hour = val / 100;
min = val % 100;
break;
case 5: case 6: hour = val / 10000;
min = (val % 10000) / 100;
sec = val % 100;
break;
}
if (hour > 23 || min > 59 || sec > 59) {
break;
}
offset = (((hour * 60) + min) * 60 + sec) * 1000 * sign;
parsed = TRUE;
} while (FALSE);
if (!parsed) {
pos.setIndex(start);
}
}
if (parsed) {
UnicodeString tzID(gGmt);
formatRFC822TZ(tzID, offset);
TimeZone *customTZ = new SimpleTimeZone(offset, tzID); cal.adoptTimeZone(customTZ);
return pos.getIndex();
}
const ZoneStringFormat *zsf = fSymbols->getZoneStringFormat();
if (zsf) {
UErrorCode status = U_ZERO_ERROR;
const ZoneStringInfo *zsinfo = NULL;
int32_t matchLen;
switch (patternCharIndex) {
case UDAT_TIMEZONE_FIELD: if (count < 4) {
zsinfo = zsf->findSpecificShort(text, start, matchLen, status);
} else {
zsinfo = zsf->findSpecificLong(text, start, matchLen, status);
}
break;
case UDAT_TIMEZONE_GENERIC_FIELD: if (count == 1) {
zsinfo = zsf->findGenericShort(text, start, matchLen, status);
} else if (count == 4) {
zsinfo = zsf->findGenericLong(text, start, matchLen, status);
}
break;
case UDAT_TIMEZONE_SPECIAL_FIELD: if (count == 1) {
zsinfo = zsf->findSpecificShort(text, start, matchLen, status);
} else if (count == 4) {
zsinfo = zsf->findGenericLocation(text, start, matchLen, status);
}
break;
}
if (U_SUCCESS(status) && zsinfo != NULL) {
if (zsinfo->isStandard()) {
((SimpleDateFormat*)this)->tztype = TZTYPE_STD;
} else if (zsinfo->isDaylight()) {
((SimpleDateFormat*)this)->tztype = TZTYPE_DST;
}
UnicodeString tzid;
zsinfo->getID(tzid);
UnicodeString current;
cal.getTimeZone().getID(current);
if (tzid != current) {
TimeZone *tz = TimeZone::createTimeZone(tzid);
cal.adoptTimeZone(tz);
}
return start + matchLen;
}
}
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) {
int32_t value = number.getLong();
int32_t bias = gFieldRangeBias[patternCharIndex];
if (bias < 0 || (value >= cal.getMinimum(field) + bias && value <= cal.getMaximum(field) + bias)) {
cal.set(field, value);
return pos.getIndex();
}
}
return -start;
}
}
void SimpleDateFormat::parseInt(const UnicodeString& text,
Formattable& number,
ParsePosition& pos,
UBool allowNegative) const {
parseInt(text, number, -1, pos, allowNegative);
}
void SimpleDateFormat::parseInt(const UnicodeString& text,
Formattable& number,
int32_t maxDigits,
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);
}
int32_t oldPos = pos.getIndex();
fNumberFormat->parse(text, number, pos);
if (df != NULL) {
df->setNegativePrefix(oldPrefix);
}
if (maxDigits > 0) {
int32_t nDigits = pos.getIndex() - oldPos;
if (nDigits > maxDigits) {
int32_t val = number.getLong();
nDigits -= maxDigits;
while (nDigits > 0) {
val /= 10;
nDigits--;
}
pos.setIndex(oldPos + maxDigits);
number.setLong(val);
}
}
}
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(); }
UBool
SimpleDateFormat::isFieldUnitIgnored(UCalendarDateFields field) const {
return isFieldUnitIgnored(fPattern, field);
}
UBool
SimpleDateFormat::isFieldUnitIgnored(const UnicodeString& pattern,
UCalendarDateFields field) {
int32_t fieldLevel = fgCalendarFieldToLevel[field];
int32_t level;
UChar ch;
UBool inQuote = FALSE;
UChar prevCh = 0;
int32_t count = 0;
for (int32_t i = 0; i < pattern.length(); ++i) {
ch = pattern[i];
if (ch != prevCh && count > 0) {
level = fgPatternCharToLevel[prevCh - PATTERN_CHAR_BASE];
if ( fieldLevel <= level ) {
return FALSE;
}
count = 0;
}
if (ch == QUOTE) {
if ((i+1) < pattern.length() && pattern[i+1] == QUOTE) {
++i;
} else {
inQuote = ! inQuote;
}
}
else if ( ! inQuote && ((ch >= 0x0061 && ch <= 0x007A )
|| (ch >= 0x0041 && ch <= 0x005A ))) {
prevCh = ch;
++count;
}
}
if ( count > 0 ) {
level = fgPatternCharToLevel[prevCh - PATTERN_CHAR_BASE];
if ( fieldLevel <= level ) {
return FALSE;
}
}
return TRUE;
}
const Locale&
SimpleDateFormat::getSmpFmtLocale(void) const {
return fLocale;
}
U_NAMESPACE_END
#endif