1999-08-16 21:50:52 +00:00
|
|
|
/*
|
1999-11-22 20:25:35 +00:00
|
|
|
*******************************************************************************
|
2006-03-22 08:52:04 +00:00
|
|
|
* Copyright (C) 1997-2006, International Business Machines Corporation and *
|
1999-11-22 20:25:35 +00:00
|
|
|
* others. All Rights Reserved. *
|
|
|
|
*******************************************************************************
|
1999-08-16 21:50:52 +00:00
|
|
|
*
|
|
|
|
* File SMPDTFMT.CPP
|
|
|
|
*
|
|
|
|
* Modification History:
|
|
|
|
*
|
|
|
|
* Date Name Description
|
|
|
|
* 02/19/97 aliu Converted from java.
|
|
|
|
* 03/31/97 aliu Modified extensively to work with 50 locales.
|
|
|
|
* 04/01/97 aliu Added support for centuries.
|
|
|
|
* 07/09/97 helena Made ParsePosition into a class.
|
|
|
|
* 07/21/98 stephen Added initializeDefaultCentury.
|
|
|
|
* Removed getZoneIndex (added in DateFormatSymbols)
|
|
|
|
* Removed subParseLong
|
|
|
|
* Removed chk
|
|
|
|
* 02/22/99 stephen Removed character literals for EBCDIC safety
|
1999-10-15 21:33:00 +00:00
|
|
|
* 10/14/99 aliu Updated 2-digit year parsing so that only "00" thru
|
|
|
|
* "99" are recognized. {j28 4182066}
|
1999-11-20 00:10:39 +00:00
|
|
|
* 11/15/99 weiv Added support for week of year/day of week format
|
1999-08-16 21:50:52 +00:00
|
|
|
********************************************************************************
|
|
|
|
*/
|
|
|
|
|
2002-09-20 01:54:48 +00:00
|
|
|
#include "unicode/utypes.h"
|
|
|
|
|
|
|
|
#if !UCONFIG_NO_FORMATTING
|
|
|
|
|
1999-12-28 23:57:50 +00:00
|
|
|
#include "unicode/smpdtfmt.h"
|
|
|
|
#include "unicode/dtfmtsym.h"
|
2004-09-02 19:08:28 +00:00
|
|
|
#include "unicode/ures.h"
|
1999-12-28 23:57:50 +00:00
|
|
|
#include "unicode/msgfmt.h"
|
|
|
|
#include "unicode/calendar.h"
|
|
|
|
#include "unicode/gregocal.h"
|
|
|
|
#include "unicode/timezone.h"
|
|
|
|
#include "unicode/decimfmt.h"
|
|
|
|
#include "unicode/dcfmtsym.h"
|
2002-03-19 03:01:28 +00:00
|
|
|
#include "unicode/uchar.h"
|
2001-09-18 21:36:45 +00:00
|
|
|
#include "unicode/ustring.h"
|
2004-10-15 22:56:26 +00:00
|
|
|
#include "util.h"
|
2004-05-18 02:06:28 +00:00
|
|
|
#include "gregoimp.h"
|
2003-11-19 23:31:45 +00:00
|
|
|
#include "cstring.h"
|
2004-05-24 05:59:54 +00:00
|
|
|
#include "uassert.h"
|
1999-08-16 21:50:52 +00:00
|
|
|
#include <float.h>
|
|
|
|
|
2003-10-26 10:24:58 +00:00
|
|
|
#if defined( U_DEBUG_CALSVC ) || defined (U_DEBUG_CAL)
|
|
|
|
#include <stdio.h>
|
|
|
|
#endif
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
// *****************************************************************************
|
|
|
|
// class SimpleDateFormat
|
|
|
|
// *****************************************************************************
|
|
|
|
|
2001-10-08 23:26:58 +00:00
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
2003-08-31 20:53:46 +00:00
|
|
|
/**
|
|
|
|
* Last-resort string to use for "GMT" when constructing time zone strings.
|
|
|
|
*/
|
1999-08-16 21:50:52 +00:00
|
|
|
// For time zones that have no names, use strings GMT+minutes and
|
|
|
|
// GMT-minutes. For instance, in France the time zone is GMT+60.
|
|
|
|
// Also accepted are GMT+H:MM or GMT-H:MM.
|
2003-08-31 20:53:46 +00:00
|
|
|
static const UChar gGmt[] = {0x0047, 0x004D, 0x0054, 0x0000}; // "GMT"
|
|
|
|
static const UChar gGmtPlus[] = {0x0047, 0x004D, 0x0054, 0x002B, 0x0000}; // "GMT+"
|
|
|
|
static const UChar gGmtMinus[] = {0x0047, 0x004D, 0x0054, 0x002D, 0x0000}; // "GMT-"
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
// This is a pattern-of-last-resort used when we can't load a usable pattern out
|
|
|
|
// of a resource.
|
2003-08-31 20:53:46 +00:00
|
|
|
static const UChar gDefaultPattern[] =
|
2001-08-10 23:00:30 +00:00
|
|
|
{
|
|
|
|
0x79, 0x79, 0x79, 0x79, 0x4D, 0x4D, 0x64, 0x64, 0x20, 0x68, 0x68, 0x3A, 0x6D, 0x6D, 0x20, 0x61, 0
|
|
|
|
}; /* "yyyyMMdd hh:mm a" */
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2003-03-07 01:04:43 +00:00
|
|
|
// This prefix is designed to NEVER MATCH real text, in order to
|
|
|
|
// suppress the parsing of negative numbers. Adjust as needed (if
|
|
|
|
// this becomes valid Unicode).
|
|
|
|
static const UChar SUPPRESS_NEGATIVE_PREFIX[] = {0xAB00, 0};
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
/**
|
|
|
|
* These are the tags we expect to see in normal resource bundle files associated
|
|
|
|
* with a locale.
|
|
|
|
*/
|
2003-08-31 20:53:46 +00:00
|
|
|
static const char gDateTimePatternsTag[]="DateTimePatterns";
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2003-08-31 20:53:46 +00:00
|
|
|
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(SimpleDateFormat)
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2003-03-14 00:49:21 +00:00
|
|
|
static const UChar QUOTE = 0x27; // Single quote
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
|
|
|
SimpleDateFormat::~SimpleDateFormat()
|
|
|
|
{
|
|
|
|
delete fSymbols;
|
2005-06-04 03:08:25 +00:00
|
|
|
delete parsedTimeZone; // sanity check
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
|
|
|
SimpleDateFormat::SimpleDateFormat(UErrorCode& status)
|
2003-05-29 17:19:34 +00:00
|
|
|
: fLocale(Locale::getDefault()),
|
2005-06-04 03:08:25 +00:00
|
|
|
fSymbols(NULL),
|
|
|
|
parsedTimeZone(NULL)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2003-05-16 22:05:41 +00:00
|
|
|
construct(kShort, (EStyle) (kShort + kDateOffset), fLocale, status);
|
2003-05-25 07:12:19 +00:00
|
|
|
initializeDefaultCentury();
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
|
|
|
SimpleDateFormat::SimpleDateFormat(const UnicodeString& pattern,
|
|
|
|
UErrorCode &status)
|
|
|
|
: fPattern(pattern),
|
2003-05-29 17:19:34 +00:00
|
|
|
fLocale(Locale::getDefault()),
|
2005-06-04 03:08:25 +00:00
|
|
|
fSymbols(NULL),
|
|
|
|
parsedTimeZone(NULL)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2003-05-16 22:05:41 +00:00
|
|
|
initializeSymbols(fLocale, initializeCalendar(NULL,fLocale,status), status);
|
|
|
|
initialize(fLocale, status);
|
2003-05-25 07:12:19 +00:00
|
|
|
initializeDefaultCentury();
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
|
|
|
SimpleDateFormat::SimpleDateFormat(const UnicodeString& pattern,
|
|
|
|
const Locale& locale,
|
|
|
|
UErrorCode& status)
|
|
|
|
: fPattern(pattern),
|
2005-06-04 03:08:25 +00:00
|
|
|
fLocale(locale),
|
|
|
|
parsedTimeZone(NULL)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2003-05-16 22:05:41 +00:00
|
|
|
initializeSymbols(fLocale, initializeCalendar(NULL,fLocale,status), status);
|
|
|
|
initialize(fLocale, status);
|
2003-05-25 07:12:19 +00:00
|
|
|
initializeDefaultCentury();
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
|
|
|
SimpleDateFormat::SimpleDateFormat(const UnicodeString& pattern,
|
|
|
|
DateFormatSymbols* symbolsToAdopt,
|
|
|
|
UErrorCode& status)
|
|
|
|
: fPattern(pattern),
|
2003-05-29 17:19:34 +00:00
|
|
|
fLocale(Locale::getDefault()),
|
2005-06-04 03:08:25 +00:00
|
|
|
fSymbols(symbolsToAdopt),
|
|
|
|
parsedTimeZone(NULL)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2003-05-16 22:05:41 +00:00
|
|
|
initializeCalendar(NULL,fLocale,status);
|
|
|
|
initialize(fLocale, status);
|
2003-05-25 07:12:19 +00:00
|
|
|
initializeDefaultCentury();
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
|
|
|
SimpleDateFormat::SimpleDateFormat(const UnicodeString& pattern,
|
|
|
|
const DateFormatSymbols& symbols,
|
|
|
|
UErrorCode& status)
|
|
|
|
: fPattern(pattern),
|
2003-05-29 17:19:34 +00:00
|
|
|
fLocale(Locale::getDefault()),
|
2005-06-04 03:08:25 +00:00
|
|
|
fSymbols(new DateFormatSymbols(symbols)),
|
|
|
|
parsedTimeZone(NULL)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2003-05-16 22:05:41 +00:00
|
|
|
initializeCalendar(NULL, fLocale, status);
|
|
|
|
initialize(fLocale, status);
|
2003-05-25 07:12:19 +00:00
|
|
|
initializeDefaultCentury();
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Not for public consumption; used by DateFormat
|
|
|
|
SimpleDateFormat::SimpleDateFormat(EStyle timeStyle,
|
|
|
|
EStyle dateStyle,
|
|
|
|
const Locale& locale,
|
|
|
|
UErrorCode& status)
|
2003-05-29 17:19:34 +00:00
|
|
|
: fLocale(locale),
|
2005-06-04 03:08:25 +00:00
|
|
|
fSymbols(NULL),
|
|
|
|
parsedTimeZone(NULL)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2003-05-16 22:05:41 +00:00
|
|
|
construct(timeStyle, dateStyle, fLocale, status);
|
2003-06-04 22:02:07 +00:00
|
|
|
if(U_SUCCESS(status)) {
|
|
|
|
initializeDefaultCentury();
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Not for public consumption; used by DateFormat. This constructor
|
|
|
|
* never fails. If the resource data is not available, it uses the
|
|
|
|
* the last resort symbols.
|
|
|
|
*/
|
|
|
|
SimpleDateFormat::SimpleDateFormat(const Locale& locale,
|
|
|
|
UErrorCode& status)
|
2003-08-31 20:53:46 +00:00
|
|
|
: fPattern(gDefaultPattern),
|
2003-05-29 17:19:34 +00:00
|
|
|
fLocale(locale),
|
2005-06-04 03:08:25 +00:00
|
|
|
fSymbols(NULL),
|
|
|
|
parsedTimeZone(NULL)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
1999-10-18 22:48:32 +00:00
|
|
|
if (U_FAILURE(status)) return;
|
2003-05-20 23:16:06 +00:00
|
|
|
initializeSymbols(fLocale, initializeCalendar(NULL, fLocale, status),status);
|
1999-10-18 22:48:32 +00:00
|
|
|
if (U_FAILURE(status))
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
1999-10-07 00:07:53 +00:00
|
|
|
status = U_ZERO_ERROR;
|
1999-08-16 21:50:52 +00:00
|
|
|
delete fSymbols;
|
|
|
|
// This constructor doesn't fail; it uses last resort data
|
|
|
|
fSymbols = new DateFormatSymbols(status);
|
2002-07-02 15:10:30 +00:00
|
|
|
/* test for NULL */
|
2002-06-29 09:31:05 +00:00
|
|
|
if (fSymbols == 0) {
|
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2003-05-20 23:16:06 +00:00
|
|
|
|
2003-05-16 22:05:41 +00:00
|
|
|
initialize(fLocale, status);
|
2003-06-04 22:02:07 +00:00
|
|
|
if(U_SUCCESS(status)) {
|
|
|
|
initializeDefaultCentury();
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
|
|
|
SimpleDateFormat::SimpleDateFormat(const SimpleDateFormat& other)
|
|
|
|
: DateFormat(other),
|
2005-06-04 03:08:25 +00:00
|
|
|
fSymbols(NULL),
|
|
|
|
parsedTimeZone(NULL)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
|
|
|
*this = other;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
|
|
|
SimpleDateFormat& SimpleDateFormat::operator=(const SimpleDateFormat& other)
|
|
|
|
{
|
|
|
|
DateFormat::operator=(other);
|
|
|
|
|
|
|
|
delete fSymbols;
|
|
|
|
fSymbols = NULL;
|
|
|
|
|
2005-06-04 03:08:25 +00:00
|
|
|
delete parsedTimeZone; parsedTimeZone = NULL;
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
if (other.fSymbols)
|
|
|
|
fSymbols = new DateFormatSymbols(*other.fSymbols);
|
|
|
|
|
|
|
|
fDefaultCenturyStart = other.fDefaultCenturyStart;
|
|
|
|
fDefaultCenturyStartYear = other.fDefaultCenturyStartYear;
|
2003-05-25 07:12:19 +00:00
|
|
|
fHaveDefaultCentury = other.fHaveDefaultCentury;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
fPattern = other.fPattern;
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
|
|
|
Format*
|
|
|
|
SimpleDateFormat::clone() const
|
|
|
|
{
|
|
|
|
return new SimpleDateFormat(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
2000-05-18 22:08:39 +00:00
|
|
|
UBool
|
1999-08-16 21:50:52 +00:00
|
|
|
SimpleDateFormat::operator==(const Format& other) const
|
|
|
|
{
|
2004-02-13 01:50:59 +00:00
|
|
|
if (DateFormat::operator==(other)) {
|
|
|
|
// DateFormat::operator== guarantees following cast is safe
|
1999-08-16 21:50:52 +00:00
|
|
|
SimpleDateFormat* that = (SimpleDateFormat*)&other;
|
2005-06-04 03:08:25 +00:00
|
|
|
return (fPattern == that->fPattern &&
|
1999-08-16 21:50:52 +00:00
|
|
|
fSymbols != NULL && // Check for pathological object
|
2005-06-04 03:08:25 +00:00
|
|
|
that->fSymbols != NULL && // Check for pathological object
|
|
|
|
*fSymbols == *that->fSymbols &&
|
|
|
|
fHaveDefaultCentury == that->fHaveDefaultCentury &&
|
1999-08-16 21:50:52 +00:00
|
|
|
fDefaultCenturyStart == that->fDefaultCenturyStart);
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
|
|
|
void SimpleDateFormat::construct(EStyle timeStyle,
|
|
|
|
EStyle dateStyle,
|
|
|
|
const Locale& locale,
|
|
|
|
UErrorCode& status)
|
|
|
|
{
|
|
|
|
// called by several constructors to load pattern data from the resources
|
1999-10-18 22:48:32 +00:00
|
|
|
if (U_FAILURE(status)) return;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2003-05-25 07:12:19 +00:00
|
|
|
// We will need the calendar to know what type of symbols to load.
|
|
|
|
initializeCalendar(NULL, locale, status);
|
2005-06-22 04:40:32 +00:00
|
|
|
if (U_FAILURE(status)) return;
|
2003-05-25 07:12:19 +00:00
|
|
|
|
2004-05-18 02:06:28 +00:00
|
|
|
CalendarData calData(locale, fCalendar?fCalendar->getType():NULL, status);
|
2004-09-02 19:08:28 +00:00
|
|
|
UResourceBundle *dateTimePatterns = calData.getByKey(gDateTimePatternsTag, status);
|
1999-10-18 22:48:32 +00:00
|
|
|
if (U_FAILURE(status)) return;
|
2000-06-30 20:31:39 +00:00
|
|
|
|
2004-09-02 19:08:28 +00:00
|
|
|
if (ures_getSize(dateTimePatterns) <= kDateTime)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
1999-10-07 00:07:53 +00:00
|
|
|
status = U_INVALID_FORMAT_ERROR;
|
1999-08-16 21:50:52 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-09-02 19:08:28 +00:00
|
|
|
setLocaleIDs(ures_getLocaleByType(dateTimePatterns, ULOC_VALID_LOCALE, &status),
|
|
|
|
ures_getLocaleByType(dateTimePatterns, ULOC_ACTUAL_LOCALE, &status));
|
2004-01-20 23:06:38 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
// create a symbols object from the locale
|
2003-05-25 07:12:19 +00:00
|
|
|
initializeSymbols(locale,fCalendar, status);
|
2003-05-16 22:05:41 +00:00
|
|
|
if (U_FAILURE(status)) return;
|
2002-07-02 15:10:30 +00:00
|
|
|
/* test for NULL */
|
2002-06-29 09:31:05 +00:00
|
|
|
if (fSymbols == 0) {
|
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2004-09-02 19:08:28 +00:00
|
|
|
const UChar *resStr;
|
|
|
|
int32_t resStrLen = 0;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
// if the pattern should include both date and time information, use the date/time
|
|
|
|
// pattern string as a guide to tell use how to glue together the appropriate date
|
|
|
|
// and time pattern strings. The actual gluing-together is handled by a convenience
|
|
|
|
// method on MessageFormat.
|
2004-09-02 19:08:28 +00:00
|
|
|
if ((timeStyle != kNone) && (dateStyle != kNone))
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2004-09-02 19:08:28 +00:00
|
|
|
Formattable timeDateArray[2];
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2002-10-12 01:09:00 +00:00
|
|
|
// use Formattable::adoptString() so that we can use fastCopyFrom()
|
|
|
|
// instead of Formattable::setString()'s unaware, safe, deep string clone
|
|
|
|
// see Jitterbug 2296
|
2004-09-02 19:08:28 +00:00
|
|
|
resStr = ures_getStringByIndex(dateTimePatterns, (int32_t)timeStyle, &resStrLen, &status);
|
|
|
|
timeDateArray[0].adoptString(new UnicodeString(TRUE, resStr, resStrLen));
|
|
|
|
resStr = ures_getStringByIndex(dateTimePatterns, (int32_t)dateStyle, &resStrLen, &status);
|
|
|
|
timeDateArray[1].adoptString(new UnicodeString(TRUE, resStr, resStrLen));
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2004-09-02 19:08:28 +00:00
|
|
|
resStr = ures_getStringByIndex(dateTimePatterns, (int32_t)kDateTime, &resStrLen, &status);
|
|
|
|
MessageFormat::format(UnicodeString(TRUE, resStr, resStrLen), timeDateArray, 2, fPattern, status);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
// if the pattern includes just time data or just date date, load the appropriate
|
|
|
|
// pattern string from the resources
|
2004-09-02 19:08:28 +00:00
|
|
|
// setTo() - see DateFormatSymbols::assignArray comments
|
|
|
|
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);
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
// and if it includes _neither_, that's an error
|
2004-09-02 19:08:28 +00:00
|
|
|
else
|
|
|
|
status = U_INVALID_FORMAT_ERROR;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
// finally, finish initializing by creating a Calendar and a NumberFormat
|
|
|
|
initialize(locale, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
2003-05-16 22:05:41 +00:00
|
|
|
Calendar*
|
|
|
|
SimpleDateFormat::initializeCalendar(TimeZone* adoptZone, const Locale& locale, UErrorCode& status)
|
|
|
|
{
|
2005-06-22 04:40:32 +00:00
|
|
|
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;
|
2003-05-16 22:05:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SimpleDateFormat::initializeSymbols(const Locale& locale, Calendar* calendar, UErrorCode& status)
|
|
|
|
{
|
|
|
|
if(U_FAILURE(status)) {
|
|
|
|
fSymbols = NULL;
|
|
|
|
} else {
|
|
|
|
// pass in calendar type - use NULL (default) if no calendar set (or err).
|
|
|
|
fSymbols = new DateFormatSymbols(locale, calendar?calendar->getType() :NULL , status);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
void
|
|
|
|
SimpleDateFormat::initialize(const Locale& locale,
|
|
|
|
UErrorCode& status)
|
|
|
|
{
|
1999-10-18 22:48:32 +00:00
|
|
|
if (U_FAILURE(status)) return;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
// We don't need to check that the row count is >= 1, since all 2d arrays have at
|
|
|
|
// least one row
|
|
|
|
fNumberFormat = NumberFormat::createInstance(locale, status);
|
1999-10-18 22:48:32 +00:00
|
|
|
if (fNumberFormat != NULL && U_SUCCESS(status))
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
|
|
|
// no matter what the locale's default number format looked like, we want
|
|
|
|
// to modify it so that it doesn't use thousands separators, doesn't always
|
|
|
|
// show the decimal point, and recognizes integers only when parsing
|
|
|
|
|
|
|
|
fNumberFormat->setGroupingUsed(FALSE);
|
|
|
|
if (fNumberFormat->getDynamicClassID() == DecimalFormat::getStaticClassID())
|
|
|
|
((DecimalFormat*)fNumberFormat)->setDecimalSeparatorAlwaysShown(FALSE);
|
|
|
|
fNumberFormat->setParseIntegerOnly(TRUE);
|
|
|
|
fNumberFormat->setMinimumFractionDigits(0); // To prevent "Jan 1.00, 1997.00"
|
|
|
|
}
|
1999-10-18 22:48:32 +00:00
|
|
|
else if (U_SUCCESS(status))
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
1999-10-07 00:07:53 +00:00
|
|
|
status = U_MISSING_RESOURCE_ERROR;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize the fields we use to disambiguate ambiguous years. Separate
|
|
|
|
* so we can call it from readObject().
|
|
|
|
*/
|
|
|
|
void SimpleDateFormat::initializeDefaultCentury()
|
|
|
|
{
|
2003-06-04 22:02:07 +00:00
|
|
|
if(fCalendar) {
|
|
|
|
fHaveDefaultCentury = fCalendar->haveDefaultCentury();
|
|
|
|
if(fHaveDefaultCentury) {
|
|
|
|
fDefaultCenturyStart = fCalendar->defaultCenturyStart();
|
|
|
|
fDefaultCenturyStartYear = fCalendar->defaultCenturyStartYear();
|
|
|
|
} else {
|
|
|
|
fDefaultCenturyStart = DBL_MIN;
|
|
|
|
fDefaultCenturyStartYear = -1;
|
|
|
|
}
|
2003-05-25 07:12:19 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Define one-century window into which to disambiguate dates using
|
|
|
|
* two-digit years. Make public in JDK 1.2.
|
|
|
|
*/
|
|
|
|
void SimpleDateFormat::parseAmbiguousDatesAsAfter(UDate startDate, UErrorCode& status)
|
|
|
|
{
|
2003-06-06 05:05:22 +00:00
|
|
|
if(U_FAILURE(status)) {
|
1999-08-16 21:50:52 +00:00
|
|
|
return;
|
2003-06-06 05:05:22 +00:00
|
|
|
}
|
|
|
|
if(!fCalendar) {
|
|
|
|
status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
fCalendar->setTime(startDate, status);
|
1999-10-18 22:48:32 +00:00
|
|
|
if(U_SUCCESS(status)) {
|
2003-05-25 07:12:19 +00:00
|
|
|
fHaveDefaultCentury = TRUE;
|
1999-08-16 21:50:52 +00:00
|
|
|
fDefaultCenturyStart = startDate;
|
2003-02-21 08:32:22 +00:00
|
|
|
fDefaultCenturyStartYear = fCalendar->get(UCAL_YEAR, status);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
|
|
|
UnicodeString&
|
2002-11-25 18:31:23 +00:00
|
|
|
SimpleDateFormat::format(Calendar& cal, UnicodeString& appendTo, FieldPosition& pos) const
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
1999-10-07 00:07:53 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
1999-08-16 21:50:52 +00:00
|
|
|
pos.setBeginIndex(0);
|
|
|
|
pos.setEndIndex(0);
|
|
|
|
|
2000-05-18 22:08:39 +00:00
|
|
|
UBool inQuote = FALSE;
|
1999-08-16 21:50:52 +00:00
|
|
|
UChar prevCh = 0;
|
|
|
|
int32_t count = 0;
|
|
|
|
|
|
|
|
// loop through the pattern string character by character
|
1999-12-08 02:11:04 +00:00
|
|
|
for (int32_t i = 0; i < fPattern.length() && U_SUCCESS(status); ++i) {
|
1999-08-16 21:50:52 +00:00
|
|
|
UChar ch = fPattern[i];
|
|
|
|
|
|
|
|
// Use subFormat() to format a repeated pattern character
|
|
|
|
// when a different pattern or non-pattern character is seen
|
|
|
|
if (ch != prevCh && count > 0) {
|
2002-11-25 18:31:23 +00:00
|
|
|
subFormat(appendTo, prevCh, count, pos, cal, status);
|
1999-08-16 21:50:52 +00:00
|
|
|
count = 0;
|
|
|
|
}
|
2003-03-14 00:49:21 +00:00
|
|
|
if (ch == QUOTE) {
|
1999-08-16 21:50:52 +00:00
|
|
|
// Consecutive single quotes are a single quote literal,
|
|
|
|
// either outside of quotes or between quotes
|
2003-03-14 00:49:21 +00:00
|
|
|
if ((i+1) < fPattern.length() && fPattern[i+1] == QUOTE) {
|
|
|
|
appendTo += (UChar)QUOTE;
|
1999-08-16 21:50:52 +00:00
|
|
|
++i;
|
|
|
|
} else {
|
|
|
|
inQuote = ! inQuote;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ( ! inQuote && ((ch >= 0x0061 /*'a'*/ && ch <= 0x007A /*'z'*/)
|
|
|
|
|| (ch >= 0x0041 /*'A'*/ && ch <= 0x005A /*'Z'*/))) {
|
|
|
|
// ch is a date-time pattern character to be interpreted
|
|
|
|
// by subFormat(); count the number of times it is repeated
|
|
|
|
prevCh = ch;
|
|
|
|
++count;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Append quoted characters and unquoted non-pattern characters
|
2002-11-25 18:31:23 +00:00
|
|
|
appendTo += ch;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Format the last item in the pattern, if any
|
|
|
|
if (count > 0) {
|
2002-11-25 18:31:23 +00:00
|
|
|
subFormat(appendTo, prevCh, count, pos, cal, status);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// and if something failed (e.g., an invalid format character), reset our FieldPosition
|
|
|
|
// to (0, 0) to show that
|
|
|
|
// {sfb} look at this later- are these being set correctly?
|
1999-10-18 22:48:32 +00:00
|
|
|
if (U_FAILURE(status)) {
|
1999-08-16 21:50:52 +00:00
|
|
|
pos.setBeginIndex(0);
|
|
|
|
pos.setEndIndex(0);
|
|
|
|
}
|
|
|
|
|
2002-11-25 18:31:23 +00:00
|
|
|
return appendTo;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString&
|
|
|
|
SimpleDateFormat::format(const Formattable& obj,
|
2002-11-25 18:31:23 +00:00
|
|
|
UnicodeString& appendTo,
|
1999-08-16 21:50:52 +00:00
|
|
|
FieldPosition& pos,
|
|
|
|
UErrorCode& status) const
|
|
|
|
{
|
|
|
|
// this is just here to get around the hiding problem
|
|
|
|
// (the previous format() override would hide the version of
|
|
|
|
// format() on DateFormat that this function correspond to, so we
|
|
|
|
// have to redefine it here)
|
2002-11-25 18:31:23 +00:00
|
|
|
return DateFormat::format(obj, appendTo, pos, status);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Map index into pattern character string to Calendar field number.
|
2003-02-21 08:32:22 +00:00
|
|
|
const UCalendarDateFields
|
1999-08-16 21:50:52 +00:00
|
|
|
SimpleDateFormat::fgPatternIndexToCalendarField[] =
|
|
|
|
{
|
2004-03-11 14:03:37 +00:00
|
|
|
/*GyM*/ UCAL_ERA, UCAL_YEAR, UCAL_MONTH,
|
|
|
|
/*dkH*/ UCAL_DATE, UCAL_HOUR_OF_DAY, UCAL_HOUR_OF_DAY,
|
|
|
|
/*msS*/ UCAL_MINUTE, UCAL_SECOND, UCAL_MILLISECOND,
|
|
|
|
/*EDF*/ UCAL_DAY_OF_WEEK, UCAL_DAY_OF_YEAR, UCAL_DAY_OF_WEEK_IN_MONTH,
|
|
|
|
/*wWa*/ UCAL_WEEK_OF_YEAR, UCAL_WEEK_OF_MONTH, UCAL_AM_PM,
|
|
|
|
/*hKz*/ UCAL_HOUR, UCAL_HOUR, UCAL_ZONE_OFFSET,
|
|
|
|
/*Yeu*/ UCAL_YEAR_WOY, UCAL_DOW_LOCAL, UCAL_EXTENDED_YEAR,
|
2005-06-17 20:41:10 +00:00
|
|
|
/*gAZ*/ UCAL_JULIAN_DAY, UCAL_MILLISECONDS_IN_DAY, UCAL_ZONE_OFFSET,
|
2005-06-23 18:25:29 +00:00
|
|
|
/*v*/ UCAL_ZONE_OFFSET,
|
|
|
|
/*c*/ UCAL_DAY_OF_WEEK,
|
|
|
|
/*L*/ UCAL_MONTH,
|
2006-06-26 17:51:25 +00:00
|
|
|
/*Q*/ UCAL_MONTH,
|
|
|
|
/*q*/ UCAL_MONTH,
|
1999-08-16 21:50:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Map index into pattern character string to DateFormat field number
|
2004-03-11 14:03:37 +00:00
|
|
|
const UDateFormatField
|
1999-08-16 21:50:52 +00:00
|
|
|
SimpleDateFormat::fgPatternIndexToDateFormatField[] = {
|
2004-03-11 14:03:37 +00:00
|
|
|
/*GyM*/ UDAT_ERA_FIELD, UDAT_YEAR_FIELD, UDAT_MONTH_FIELD,
|
|
|
|
/*dkH*/ UDAT_DATE_FIELD, UDAT_HOUR_OF_DAY1_FIELD, UDAT_HOUR_OF_DAY0_FIELD,
|
|
|
|
/*msS*/ UDAT_MINUTE_FIELD, UDAT_SECOND_FIELD, UDAT_FRACTIONAL_SECOND_FIELD,
|
|
|
|
/*EDF*/ UDAT_DAY_OF_WEEK_FIELD, UDAT_DAY_OF_YEAR_FIELD, UDAT_DAY_OF_WEEK_IN_MONTH_FIELD,
|
|
|
|
/*wWa*/ UDAT_WEEK_OF_YEAR_FIELD, UDAT_WEEK_OF_MONTH_FIELD, UDAT_AM_PM_FIELD,
|
|
|
|
/*hKz*/ UDAT_HOUR1_FIELD, UDAT_HOUR0_FIELD, UDAT_TIMEZONE_FIELD,
|
|
|
|
/*Yeu*/ UDAT_YEAR_WOY_FIELD, UDAT_DOW_LOCAL_FIELD, UDAT_EXTENDED_YEAR_FIELD,
|
2005-06-17 20:41:10 +00:00
|
|
|
/*gAZ*/ UDAT_JULIAN_DAY_FIELD, UDAT_MILLISECONDS_IN_DAY_FIELD, UDAT_TIMEZONE_RFC_FIELD,
|
2005-06-23 18:25:29 +00:00
|
|
|
/*v*/ UDAT_TIMEZONE_GENERIC_FIELD,
|
|
|
|
/*c*/ UDAT_STANDALONE_DAY_FIELD,
|
|
|
|
/*L*/ UDAT_STANDALONE_MONTH_FIELD,
|
2006-06-26 17:51:25 +00:00
|
|
|
/*Q*/ UDAT_QUARTER_FIELD,
|
|
|
|
/*q*/ UDAT_STANDALONE_QUARTER_FIELD,
|
1999-08-16 21:50:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
2004-05-27 21:00:21 +00:00
|
|
|
/**
|
|
|
|
* Append symbols[value] to dst. Make sure the array index is not out
|
|
|
|
* of bounds.
|
|
|
|
*/
|
2006-03-22 08:52:04 +00:00
|
|
|
static inline void
|
2004-05-27 21:00:21 +00:00
|
|
|
_appendSymbol(UnicodeString& dst,
|
|
|
|
int32_t value,
|
|
|
|
const UnicodeString* symbols,
|
|
|
|
int32_t symbolsCount) {
|
2006-03-22 08:52:04 +00:00
|
|
|
U_ASSERT(0 <= value && value < symbolsCount);
|
|
|
|
if (0 <= value && value < symbolsCount) {
|
|
|
|
dst += symbols[value];
|
|
|
|
}
|
2004-05-27 21:00:21 +00:00
|
|
|
}
|
|
|
|
|
2005-08-12 22:01:55 +00:00
|
|
|
//---------------------------------------------------------------------
|
|
|
|
inline void SimpleDateFormat::appendGMT(UnicodeString &appendTo, Calendar& cal, UErrorCode& status) const{
|
|
|
|
int32_t value = cal.get(UCAL_ZONE_OFFSET, status) +
|
|
|
|
cal.get(UCAL_DST_OFFSET, status);
|
|
|
|
|
|
|
|
if (value < 0) {
|
|
|
|
appendTo += gGmtMinus;
|
|
|
|
value = -value; // suppress the '-' sign for text display.
|
|
|
|
}else{
|
|
|
|
appendTo += gGmtPlus;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------
|
2002-10-12 01:09:00 +00:00
|
|
|
void
|
2002-11-25 18:31:23 +00:00
|
|
|
SimpleDateFormat::subFormat(UnicodeString &appendTo,
|
1999-08-16 21:50:52 +00:00
|
|
|
UChar ch,
|
|
|
|
int32_t count,
|
|
|
|
FieldPosition& pos,
|
2002-03-02 00:29:54 +00:00
|
|
|
Calendar& cal,
|
1999-08-16 21:50:52 +00:00
|
|
|
UErrorCode& status) const
|
|
|
|
{
|
2003-10-13 17:15:39 +00:00
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
// this function gets called by format() to produce the appropriate substitution
|
|
|
|
// text for an individual pattern symbol (e.g., "HH" or "yyyy")
|
|
|
|
|
2001-09-18 21:36:45 +00:00
|
|
|
UChar *patternCharPtr = u_strchr(DateFormatSymbols::getPatternUChars(), ch);
|
2004-03-11 14:03:37 +00:00
|
|
|
UDateFormatField patternCharIndex;
|
2002-10-12 01:09:00 +00:00
|
|
|
const int32_t maxIntCount = 10;
|
2002-11-25 18:31:23 +00:00
|
|
|
int32_t beginOffset = appendTo.length();
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
// if the pattern character is unrecognized, signal an error and dump out
|
2001-09-18 21:36:45 +00:00
|
|
|
if (patternCharPtr == NULL)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
1999-10-07 00:07:53 +00:00
|
|
|
status = U_INVALID_FORMAT_ERROR;
|
2003-10-13 17:15:39 +00:00
|
|
|
return;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2004-03-11 14:03:37 +00:00
|
|
|
patternCharIndex = (UDateFormatField)(patternCharPtr - DateFormatSymbols::getPatternUChars());
|
2003-02-21 08:32:22 +00:00
|
|
|
UCalendarDateFields field = fgPatternIndexToCalendarField[patternCharIndex];
|
2002-03-02 00:29:54 +00:00
|
|
|
int32_t value = cal.get(field, status);
|
2002-10-12 01:09:00 +00:00
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return;
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
switch (patternCharIndex) {
|
|
|
|
|
|
|
|
// for any "G" symbol, write out the appropriate era string
|
2005-07-01 17:30:36 +00:00
|
|
|
// "GGGG" is wide era name, anything else is abbreviated name
|
2004-03-11 14:03:37 +00:00
|
|
|
case UDAT_ERA_FIELD:
|
2005-07-01 17:30:36 +00:00
|
|
|
if (count >= 4)
|
|
|
|
_appendSymbol(appendTo, value, fSymbols->fEraNames, fSymbols->fEraNamesCount);
|
|
|
|
else
|
|
|
|
_appendSymbol(appendTo, value, fSymbols->fEras, fSymbols->fErasCount);
|
1999-08-16 21:50:52 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
// for "yyyy", write out the whole year; for "yy", write out the last 2 digits
|
2004-03-11 14:03:37 +00:00
|
|
|
case UDAT_YEAR_FIELD:
|
|
|
|
case UDAT_YEAR_WOY_FIELD:
|
1999-08-16 21:50:52 +00:00
|
|
|
if (count >= 4)
|
2002-11-25 18:31:23 +00:00
|
|
|
zeroPaddingNumber(appendTo, value, 4, maxIntCount);
|
2003-05-30 00:53:37 +00:00
|
|
|
else if(count == 1)
|
|
|
|
zeroPaddingNumber(appendTo, value, count, maxIntCount);
|
|
|
|
else
|
2002-11-25 18:31:23 +00:00
|
|
|
zeroPaddingNumber(appendTo, value, 2, 2);
|
2003-05-30 00:53:37 +00:00
|
|
|
break; // TODO: this needs to be synced with Java, with GCL/Shanghai's work
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
// for "MMMM", write out the whole month name, for "MMM", write out the month
|
|
|
|
// abbreviation, for "M" or "MM", write out the month as a number with the
|
|
|
|
// appropriate number of digits
|
2005-07-01 17:30:36 +00:00
|
|
|
// for "MMMMM", use the narrow form
|
2004-03-11 14:03:37 +00:00
|
|
|
case UDAT_MONTH_FIELD:
|
2005-07-01 17:30:36 +00:00
|
|
|
if (count == 5)
|
|
|
|
_appendSymbol(appendTo, value, fSymbols->fNarrowMonths,
|
|
|
|
fSymbols->fNarrowMonthsCount);
|
|
|
|
else if (count == 4)
|
2004-05-27 21:00:21 +00:00
|
|
|
_appendSymbol(appendTo, value, fSymbols->fMonths,
|
|
|
|
fSymbols->fMonthsCount);
|
1999-08-16 21:50:52 +00:00
|
|
|
else if (count == 3)
|
2004-05-27 21:00:21 +00:00
|
|
|
_appendSymbol(appendTo, value, fSymbols->fShortMonths,
|
|
|
|
fSymbols->fShortMonthsCount);
|
1999-08-16 21:50:52 +00:00
|
|
|
else
|
2002-11-25 18:31:23 +00:00
|
|
|
zeroPaddingNumber(appendTo, value + 1, count, maxIntCount);
|
1999-08-16 21:50:52 +00:00
|
|
|
break;
|
|
|
|
|
2005-07-01 17:30:36 +00:00
|
|
|
// for "LLLL", write out the whole month name, for "LLL", write out the month
|
|
|
|
// abbreviation, for "L" or "LL", write out the month as a number with the
|
|
|
|
// appropriate number of digits
|
|
|
|
// for "LLLLL", use the narrow form
|
|
|
|
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;
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
// for "k" and "kk", write out the hour, adjusting midnight to appear as "24"
|
2004-03-11 14:03:37 +00:00
|
|
|
case UDAT_HOUR_OF_DAY1_FIELD:
|
1999-08-16 21:50:52 +00:00
|
|
|
if (value == 0)
|
2003-02-21 08:32:22 +00:00
|
|
|
zeroPaddingNumber(appendTo, cal.getMaximum(UCAL_HOUR_OF_DAY) + 1, count, maxIntCount);
|
1999-08-16 21:50:52 +00:00
|
|
|
else
|
2002-11-25 18:31:23 +00:00
|
|
|
zeroPaddingNumber(appendTo, value, count, maxIntCount);
|
1999-08-16 21:50:52 +00:00
|
|
|
break;
|
|
|
|
|
2004-03-11 14:03:37 +00:00
|
|
|
case UDAT_FRACTIONAL_SECOND_FIELD:
|
|
|
|
// Fractional seconds left-justify
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
break;
|
|
|
|
|
2005-07-01 17:30:36 +00:00
|
|
|
// for "EEE", write out the abbreviated day-of-the-week name
|
|
|
|
// for "EEEE", write out the wide day-of-the-week name
|
|
|
|
// for "EEEEE", use the narrow day-of-the-week name
|
2004-03-11 14:03:37 +00:00
|
|
|
case UDAT_DAY_OF_WEEK_FIELD:
|
2005-07-01 17:30:36 +00:00
|
|
|
if (count == 5)
|
|
|
|
_appendSymbol(appendTo, value, fSymbols->fNarrowWeekdays,
|
|
|
|
fSymbols->fNarrowWeekdaysCount);
|
|
|
|
else if (count == 4)
|
2004-05-27 21:00:21 +00:00
|
|
|
_appendSymbol(appendTo, value, fSymbols->fWeekdays,
|
|
|
|
fSymbols->fWeekdaysCount);
|
2005-07-02 02:54:59 +00:00
|
|
|
else
|
2004-05-27 21:00:21 +00:00
|
|
|
_appendSymbol(appendTo, value, fSymbols->fShortWeekdays,
|
|
|
|
fSymbols->fShortWeekdaysCount);
|
1999-08-16 21:50:52 +00:00
|
|
|
break;
|
|
|
|
|
2005-07-01 17:30:36 +00:00
|
|
|
// for "ccc", write out the abbreviated day-of-the-week name
|
|
|
|
// for "cccc", write out the wide day-of-the-week name
|
|
|
|
// for "ccccc", use the narrow day-of-the-week name
|
|
|
|
case UDAT_STANDALONE_DAY_FIELD:
|
|
|
|
if (count == 5)
|
|
|
|
_appendSymbol(appendTo, value, fSymbols->fStandaloneNarrowWeekdays,
|
|
|
|
fSymbols->fStandaloneNarrowWeekdaysCount);
|
|
|
|
else if (count == 4)
|
|
|
|
_appendSymbol(appendTo, value, fSymbols->fStandaloneWeekdays,
|
|
|
|
fSymbols->fStandaloneWeekdaysCount);
|
2005-07-02 02:54:59 +00:00
|
|
|
else if (count == 3)
|
2005-07-01 17:30:36 +00:00
|
|
|
_appendSymbol(appendTo, value, fSymbols->fStandaloneShortWeekdays,
|
|
|
|
fSymbols->fStandaloneShortWeekdaysCount);
|
2005-07-02 02:54:59 +00:00
|
|
|
else
|
|
|
|
zeroPaddingNumber(appendTo, value, 1, maxIntCount);
|
2005-07-01 17:30:36 +00:00
|
|
|
break;
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
// for and "a" symbol, write out the whole AM/PM string
|
2004-03-11 14:03:37 +00:00
|
|
|
case UDAT_AM_PM_FIELD:
|
2004-05-27 21:00:21 +00:00
|
|
|
_appendSymbol(appendTo, value, fSymbols->fAmPms,
|
|
|
|
fSymbols->fAmPmsCount);
|
1999-08-16 21:50:52 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
// for "h" and "hh", write out the hour, adjusting noon and midnight to show up
|
|
|
|
// as "12"
|
2004-03-11 14:03:37 +00:00
|
|
|
case UDAT_HOUR1_FIELD:
|
1999-08-16 21:50:52 +00:00
|
|
|
if (value == 0)
|
2003-02-21 08:32:22 +00:00
|
|
|
zeroPaddingNumber(appendTo, cal.getLeastMaximum(UCAL_HOUR) + 1, count, maxIntCount);
|
1999-08-16 21:50:52 +00:00
|
|
|
else
|
2002-11-25 18:31:23 +00:00
|
|
|
zeroPaddingNumber(appendTo, value, count, maxIntCount);
|
1999-08-16 21:50:52 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
// for the "z" symbols, we have to check our time zone data first. If we have a
|
2005-06-04 03:08:25 +00:00
|
|
|
// localized name for the time zone, then "zzzz" / "zzz" indicate whether
|
|
|
|
// daylight time is in effect (long/short) and "zz" / "z" do not (long/short).
|
|
|
|
// If we don't have a localized time zone name,
|
1999-08-16 21:50:52 +00:00
|
|
|
// 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
|
2005-06-17 20:41:10 +00:00
|
|
|
case UDAT_TIMEZONE_FIELD:
|
2005-08-26 21:55:55 +00:00
|
|
|
case UDAT_TIMEZONE_GENERIC_FIELD: {
|
2002-10-12 01:09:00 +00:00
|
|
|
UnicodeString str;
|
2005-08-26 21:55:55 +00:00
|
|
|
UnicodeString zid;
|
|
|
|
UnicodeString displayString;
|
|
|
|
zid = fSymbols->getZoneID(cal.getTimeZone().getID(str), zid, status);
|
|
|
|
if(U_FAILURE(status)){
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (zid.length() == 0) {
|
2005-08-12 22:01:55 +00:00
|
|
|
appendGMT(appendTo, cal, status);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
else {
|
2005-08-26 21:55:55 +00:00
|
|
|
|
|
|
|
if (patternCharIndex == UDAT_TIMEZONE_GENERIC_FIELD) {
|
|
|
|
if(count < 4){
|
|
|
|
fSymbols->getZoneString(zid, DateFormatSymbols::TIMEZONE_SHORT_GENERIC, displayString, status);
|
|
|
|
}else{
|
|
|
|
fSymbols->getZoneString(zid, DateFormatSymbols::TIMEZONE_LONG_GENERIC, displayString, status);
|
|
|
|
}
|
2005-08-12 22:01:55 +00:00
|
|
|
} else {
|
2005-08-26 21:55:55 +00:00
|
|
|
if (cal.get(UCAL_DST_OFFSET, status) != 0) {
|
|
|
|
if(count < 4){
|
|
|
|
fSymbols->getZoneString(zid, DateFormatSymbols::TIMEZONE_SHORT_DAYLIGHT, displayString, status);
|
|
|
|
}else{
|
|
|
|
fSymbols->getZoneString(zid, DateFormatSymbols::TIMEZONE_LONG_DAYLIGHT, displayString, status);
|
2005-08-12 22:01:55 +00:00
|
|
|
}
|
2005-08-26 21:55:55 +00:00
|
|
|
}else{
|
|
|
|
if(count < 4){
|
|
|
|
fSymbols->getZoneString(zid, DateFormatSymbols::TIMEZONE_SHORT_STANDARD, displayString, status);
|
|
|
|
}else{
|
|
|
|
fSymbols->getZoneString(zid, DateFormatSymbols::TIMEZONE_LONG_STANDARD, displayString, status);
|
|
|
|
}
|
|
|
|
}
|
2005-08-12 22:01:55 +00:00
|
|
|
}
|
2005-08-26 21:55:55 +00:00
|
|
|
if(displayString.length()==0){
|
2005-08-12 22:01:55 +00:00
|
|
|
appendGMT(appendTo, cal, status);
|
|
|
|
}else{
|
2005-08-26 21:55:55 +00:00
|
|
|
appendTo += displayString;
|
2005-08-12 22:01:55 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2005-06-04 03:08:25 +00:00
|
|
|
}
|
|
|
|
break;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2004-03-11 14:03:37 +00:00
|
|
|
case 23: // 'Z' - TIMEZONE_RFC
|
|
|
|
{
|
|
|
|
UChar sign = 43/*'+'*/;
|
|
|
|
value = (cal.get(UCAL_ZONE_OFFSET, status) +
|
|
|
|
cal.get(UCAL_DST_OFFSET, status)) / U_MILLIS_PER_MINUTE;
|
|
|
|
if (value < 0) {
|
|
|
|
value = -value;
|
|
|
|
sign = 45/*'-'*/;
|
|
|
|
}
|
|
|
|
value = (value / 3) * 5 + (value % 60); // minutes => KKmm
|
|
|
|
appendTo += sign;
|
|
|
|
zeroPaddingNumber(appendTo, value, 4, 4);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2006-06-26 17:51:25 +00:00
|
|
|
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;
|
|
|
|
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
// all of the other pattern symbols can be formatted as simple numbers with
|
|
|
|
// appropriate zero padding
|
|
|
|
default:
|
2002-11-25 18:31:23 +00:00
|
|
|
zeroPaddingNumber(appendTo, value, count, maxIntCount);
|
1999-08-16 21:50:52 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the field we're formatting is the one the FieldPosition says it's interested
|
|
|
|
// in, fill in the FieldPosition with this field's positions
|
2004-03-11 14:03:37 +00:00
|
|
|
if (pos.getBeginIndex() == pos.getEndIndex() &&
|
|
|
|
pos.getField() == fgPatternIndexToDateFormatField[patternCharIndex]) {
|
|
|
|
pos.setBeginIndex(beginOffset);
|
|
|
|
pos.setEndIndex(appendTo.length());
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
2002-10-12 01:09:00 +00:00
|
|
|
void
|
2002-11-25 18:31:23 +00:00
|
|
|
SimpleDateFormat::zeroPaddingNumber(UnicodeString &appendTo, int32_t value, int32_t minDigits, int32_t maxDigits) const
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2000-11-02 22:15:50 +00:00
|
|
|
FieldPosition pos(0);
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
fNumberFormat->setMinimumIntegerDigits(minDigits);
|
|
|
|
fNumberFormat->setMaximumIntegerDigits(maxDigits);
|
2002-11-25 18:31:23 +00:00
|
|
|
fNumberFormat->format(value, appendTo, pos); // 3rd arg is there to speed up processing
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
2003-03-14 00:49:21 +00:00
|
|
|
/**
|
|
|
|
* Format characters that indicate numeric fields. The character
|
|
|
|
* at index 0 is treated specially.
|
|
|
|
*/
|
|
|
|
static const UChar NUMERIC_FORMAT_CHARS[] = {0x4D, 0x79, 0x75, 0x64, 0x68, 0x48, 0x6D, 0x73, 0x53, 0x44, 0x46, 0x77, 0x57, 0x6B, 0x4B, 0x00}; /* "MyudhHmsSDFwWkK" */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return true if the given format character, occuring count
|
|
|
|
* times, represents a numeric field.
|
|
|
|
*/
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2002-03-02 00:29:54 +00:00
|
|
|
void
|
2003-04-04 20:55:10 +00:00
|
|
|
SimpleDateFormat::parse(const UnicodeString& text, Calendar& cal, ParsePosition& parsePos) const
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2003-04-04 20:55:10 +00:00
|
|
|
int32_t pos = parsePos.getIndex();
|
|
|
|
int32_t start = pos;
|
2000-05-18 22:08:39 +00:00
|
|
|
UBool ambiguousYear[] = { FALSE };
|
1999-08-16 21:50:52 +00:00
|
|
|
int32_t count = 0;
|
|
|
|
|
2005-06-04 03:08:25 +00:00
|
|
|
// hack, clear parsedTimeZone, cast away const
|
|
|
|
delete parsedTimeZone;
|
|
|
|
((SimpleDateFormat*)this)->parsedTimeZone = NULL;
|
|
|
|
|
2003-03-14 00:49:21 +00:00
|
|
|
// For parsing abutting numeric fields. 'abutPat' is the
|
|
|
|
// offset into 'pattern' of the first of 2 or more abutting
|
|
|
|
// numeric fields. 'abutStart' is the offset into 'text'
|
|
|
|
// where parsing the fields begins. 'abutPass' starts off as 0
|
|
|
|
// and increments each time we try to parse the fields.
|
|
|
|
int32_t abutPat = -1; // If >=0, we are in a run of abutting numeric fields
|
|
|
|
int32_t abutStart = 0;
|
|
|
|
int32_t abutPass = 0;
|
2003-04-04 20:55:10 +00:00
|
|
|
UBool inQuote = FALSE;
|
2003-03-14 00:49:21 +00:00
|
|
|
|
|
|
|
const UnicodeString numericFormatChars(NUMERIC_FORMAT_CHARS);
|
|
|
|
|
|
|
|
for (int32_t i=0; i<fPattern.length(); ++i) {
|
|
|
|
UChar ch = fPattern.charAt(i);
|
|
|
|
|
|
|
|
// Handle alphabetic field characters.
|
2003-04-04 20:55:10 +00:00
|
|
|
if (!inQuote && ((ch >= 0x41 && ch <= 0x5A) || (ch >= 0x61 && ch <= 0x7A))) { // [A-Za-z]
|
2003-03-14 00:49:21 +00:00
|
|
|
int32_t fieldPat = i;
|
|
|
|
|
|
|
|
// Count the length of this field specifier
|
|
|
|
count = 1;
|
|
|
|
while ((i+1)<fPattern.length() &&
|
|
|
|
fPattern.charAt(i+1) == ch) {
|
|
|
|
++count;
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isNumeric(ch, count)) {
|
|
|
|
if (abutPat < 0) {
|
|
|
|
// Determine if there is an abutting numeric field. For
|
|
|
|
// most fields we can just look at the next characters,
|
|
|
|
// but the 'm' field is either numeric or text,
|
|
|
|
// depending on the count, so we have to look ahead for
|
|
|
|
// that field.
|
|
|
|
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; // nextCount < 3
|
|
|
|
} else {
|
|
|
|
abutting = k > 0;
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2003-03-14 00:49:21 +00:00
|
|
|
// Record the start of a set of abutting numeric
|
|
|
|
// fields.
|
|
|
|
if (abutting) {
|
|
|
|
abutPat = fieldPat;
|
2003-04-04 20:55:10 +00:00
|
|
|
abutStart = pos;
|
2003-03-14 00:49:21 +00:00
|
|
|
abutPass = 0;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
}
|
2003-03-14 00:49:21 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
abutPat = -1; // End of any abutting fields
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2003-03-14 00:49:21 +00:00
|
|
|
|
|
|
|
// Handle fields within a run of abutting numeric fields. Take
|
|
|
|
// the pattern "HHmmss" as an example. We will try to parse
|
|
|
|
// 2/2/2 characters of the input text, then if that fails,
|
|
|
|
// 1/2/2. We only adjust the width of the leftmost field; the
|
|
|
|
// others remain fixed. This allows "123456" => 12:34:56, but
|
|
|
|
// "12345" => 1:23:45. Likewise, for the pattern "yyyyMMdd" we
|
|
|
|
// try 4/2/2, 3/2/2, 2/2/2, and finally 1/2/2.
|
|
|
|
if (abutPat >= 0) {
|
|
|
|
// If we are at the start of a run of abutting fields, then
|
|
|
|
// shorten this field in each pass. If we can't shorten
|
|
|
|
// this field any more, then the parse of this set of
|
|
|
|
// abutting numeric fields has failed.
|
|
|
|
if (fieldPat == abutPat) {
|
|
|
|
count -= abutPass++;
|
|
|
|
if (count == 0) {
|
2003-04-04 20:55:10 +00:00
|
|
|
parsePos.setIndex(start);
|
|
|
|
parsePos.setErrorIndex(pos);
|
2002-03-02 00:29:54 +00:00
|
|
|
return;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-04-04 20:55:10 +00:00
|
|
|
pos = subParse(text, pos, ch, count,
|
|
|
|
TRUE, FALSE, ambiguousYear, cal);
|
2003-03-14 00:49:21 +00:00
|
|
|
|
|
|
|
// If the parse fails anywhere in the run, back up to the
|
|
|
|
// start of the run and retry.
|
2003-04-04 20:55:10 +00:00
|
|
|
if (pos < 0) {
|
2003-03-14 00:49:21 +00:00
|
|
|
i = abutPat - 1;
|
2003-04-04 20:55:10 +00:00
|
|
|
pos = abutStart;
|
2003-03-14 00:49:21 +00:00
|
|
|
continue;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-03-14 00:49:21 +00:00
|
|
|
// Handle non-numeric fields and non-abutting numeric
|
|
|
|
// fields.
|
|
|
|
else {
|
2003-04-04 20:55:10 +00:00
|
|
|
int32_t s = pos;
|
|
|
|
pos = subParse(text, pos, ch, count,
|
2003-03-14 00:49:21 +00:00
|
|
|
FALSE, TRUE, ambiguousYear, cal);
|
2000-01-19 04:18:44 +00:00
|
|
|
|
2003-04-04 20:55:10 +00:00
|
|
|
if (pos < 0) {
|
|
|
|
parsePos.setErrorIndex(s);
|
|
|
|
parsePos.setIndex(start);
|
2002-03-02 00:29:54 +00:00
|
|
|
return;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-04-04 20:55:10 +00:00
|
|
|
// Handle literal pattern characters. These are any
|
|
|
|
// quoted characters and non-alphabetic unquoted
|
|
|
|
// characters.
|
2003-03-14 00:49:21 +00:00
|
|
|
else {
|
2003-04-04 20:55:10 +00:00
|
|
|
|
2003-03-14 00:49:21 +00:00
|
|
|
abutPat = -1; // End of any abutting fields
|
2003-04-04 20:55:10 +00:00
|
|
|
|
|
|
|
// Handle quotes. Two consecutive quotes is a quote
|
|
|
|
// literal, inside or outside of quotes. Otherwise a
|
|
|
|
// quote indicates entry or exit from a quoted region.
|
|
|
|
if (ch == QUOTE) {
|
|
|
|
// Match a quote literal '' within OR outside of quotes
|
|
|
|
if ((i+1)<fPattern.length() && fPattern.charAt(i+1)==ch) {
|
|
|
|
++i; // Skip over doubled quote
|
|
|
|
// Fall through and treat quote as a literal
|
|
|
|
} else {
|
|
|
|
// Enter or exit quoted region
|
|
|
|
inQuote = !inQuote;
|
|
|
|
continue;
|
|
|
|
}
|
2003-03-14 00:49:21 +00:00
|
|
|
}
|
2003-04-04 20:55:10 +00:00
|
|
|
|
|
|
|
// A run of white space in the pattern matches a run
|
|
|
|
// of white space in the input text.
|
|
|
|
if (uprv_isRuleWhiteSpace(ch)) {
|
|
|
|
// Advance over run in pattern
|
|
|
|
while ((i+1)<fPattern.length() &&
|
|
|
|
uprv_isRuleWhiteSpace(fPattern.charAt(i+1))) {
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Advance over run in input text
|
|
|
|
int32_t s = pos;
|
|
|
|
while (pos<text.length() &&
|
|
|
|
u_isUWhiteSpace(text.charAt(pos))) {
|
|
|
|
++pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Must see at least one white space char in input
|
|
|
|
if (pos > s) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else if (pos<text.length() && text.charAt(pos)==ch) {
|
|
|
|
// Match a literal
|
|
|
|
++pos;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We fall through to this point if the match fails
|
|
|
|
parsePos.setIndex(start);
|
|
|
|
parsePos.setErrorIndex(pos);
|
|
|
|
return;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// At this point the fields of Calendar have been set. Calendar
|
|
|
|
// will fill in default values for missing fields when the time
|
|
|
|
// is computed.
|
|
|
|
|
2003-04-04 20:55:10 +00:00
|
|
|
parsePos.setIndex(pos);
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
// This part is a problem: When we call parsedDate.after, we compute the time.
|
|
|
|
// Take the date April 3 2004 at 2:30 am. When this is first set up, the year
|
|
|
|
// will be wrong if we're parsing a 2-digit year pattern. It will be 1904.
|
|
|
|
// April 3 1904 is a Sunday (unlike 2004) so it is the DST onset day. 2:30 am
|
|
|
|
// is therefore an "impossible" time, since the time goes from 1:59 to 3:00 am
|
|
|
|
// on that day. It is therefore parsed out to fields as 3:30 am. Then we
|
|
|
|
// add 100 years, and get April 3 2004 at 3:30 am. Note that April 3 2004 is
|
|
|
|
// a Saturday, so it can have a 2:30 am -- and it should. [LIU]
|
|
|
|
/*
|
|
|
|
UDate parsedDate = calendar.getTime();
|
|
|
|
if( ambiguousYear[0] && !parsedDate.after(fDefaultCenturyStart) ) {
|
|
|
|
calendar.add(Calendar.YEAR, 100);
|
|
|
|
parsedDate = calendar.getTime();
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
// Because of the above condition, save off the fields in case we need to readjust.
|
|
|
|
// The procedure we use here is not particularly efficient, but there is no other
|
|
|
|
// way to do this given the API restrictions present in Calendar. We minimize
|
|
|
|
// inefficiency by only performing this computation when it might apply, that is,
|
|
|
|
// when the two-digit year is equal to the start year, and thus might fall at the
|
|
|
|
// 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().
|
1999-10-07 00:07:53 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
2005-06-04 03:08:25 +00:00
|
|
|
if (ambiguousYear[0] || parsedTimeZone != NULL) // If this is true then the two-digit year == the default start year
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
|
|
|
// 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().
|
2002-03-02 00:29:54 +00:00
|
|
|
Calendar *copy = cal.clone();
|
2005-06-04 03:08:25 +00:00
|
|
|
if (ambiguousYear[0]) {
|
|
|
|
UDate parsedDate = copy->getTime(status);
|
|
|
|
// {sfb} check internalGetDefaultCenturyStart
|
|
|
|
if (fHaveDefaultCentury && (parsedDate < fDefaultCenturyStart)) {
|
|
|
|
// We can't use add here because that does a complete() first.
|
|
|
|
cal.set(UCAL_YEAR, fDefaultCenturyStartYear + 100);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parsedTimeZone != NULL) {
|
|
|
|
TimeZone *tz = parsedTimeZone;
|
|
|
|
|
|
|
|
// the calendar represents the parse as gmt time
|
|
|
|
// we need to turn this into local time, so we add the raw offset
|
|
|
|
// then we ask the timezone to handle this local time
|
|
|
|
int32_t rawOffset = 0;
|
|
|
|
int32_t dstOffset = 0;
|
|
|
|
tz->getOffset(copy->getTime(status)+tz->getRawOffset(), TRUE,
|
|
|
|
rawOffset, dstOffset, status);
|
|
|
|
if (U_SUCCESS(status)) {
|
|
|
|
cal.set(UCAL_ZONE_OFFSET, rawOffset);
|
|
|
|
cal.set(UCAL_DST_OFFSET, dstOffset);
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2005-06-04 03:08:25 +00:00
|
|
|
|
2002-03-02 00:29:54 +00:00
|
|
|
delete copy;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If any Calendar calls failed, we pretend that we
|
|
|
|
// couldn't parse the string, when in reality this isn't quite accurate--
|
|
|
|
// we did parse it; the Calendar calls just failed.
|
1999-10-18 22:48:32 +00:00
|
|
|
if (U_FAILURE(status)) {
|
2003-04-04 20:55:10 +00:00
|
|
|
parsePos.setErrorIndex(pos);
|
|
|
|
parsePos.setIndex(start);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2002-03-02 00:29:54 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2002-03-02 00:29:54 +00:00
|
|
|
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);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UDate
|
|
|
|
SimpleDateFormat::parse(const UnicodeString& text, UErrorCode& status) const
|
|
|
|
{
|
|
|
|
// redefined here because the other parse() function hides this function's
|
2002-03-02 00:29:54 +00:00
|
|
|
// counterpart on DateFormat
|
1999-08-16 21:50:52 +00:00
|
|
|
return DateFormat::parse(text, status);
|
|
|
|
}
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
2006-06-26 17:51:25 +00:00
|
|
|
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;
|
|
|
|
|
|
|
|
// There may be multiple strings in the data[] array which begin with
|
|
|
|
// the same prefix (e.g., Cerven and Cervenec (June and July) in Czech).
|
|
|
|
// We keep track of the longest match, and return that. Note that this
|
|
|
|
// unfortunately requires us to test all array elements.
|
|
|
|
int32_t bestMatchLength = 0, bestMatch = -1;
|
|
|
|
|
|
|
|
// {sfb} kludge to support case-insensitive comparison
|
|
|
|
// {markus 2002oct11} do not just use caseCompareBetween because we do not know
|
|
|
|
// the length of the match after case folding
|
|
|
|
// {alan 20040607} don't case change the whole string, since the length
|
|
|
|
// can change
|
|
|
|
// TODO we need a case-insensitive startsWith function
|
|
|
|
UnicodeString lcase, lcaseText;
|
|
|
|
text.extract(start, INT32_MAX, lcaseText);
|
|
|
|
lcaseText.foldCase();
|
|
|
|
|
|
|
|
for (; i < count; ++i)
|
|
|
|
{
|
|
|
|
// Always compare if we have no match yet; otherwise only compare
|
|
|
|
// against potentially better matches (longer strings).
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
// Once we have a match, we have to determine the length of the
|
|
|
|
// original source string. This will usually be == the length of
|
|
|
|
// the case folded string, but it may differ (e.g. sharp s).
|
|
|
|
lcase.fastCopyFrom(data[bestMatch]).foldCase();
|
|
|
|
|
|
|
|
// Most of the time, the length will be the same as the length
|
|
|
|
// of the string from the locale data. Sometimes it will be
|
|
|
|
// different, in which case we will have to figure it out by
|
|
|
|
// adding a character at a time, until we have a match. We do
|
|
|
|
// this all in one loop, where we try 'len' first (at index
|
|
|
|
// i==0).
|
|
|
|
int32_t len = data[bestMatch].length(); // 99+% of the time
|
|
|
|
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; // already tried this when i was 0
|
|
|
|
}
|
|
|
|
text.extract(start, j, lcaseText);
|
|
|
|
lcaseText.foldCase();
|
|
|
|
if (lcase == lcaseText) {
|
|
|
|
return start + j;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -start;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
int32_t SimpleDateFormat::matchString(const UnicodeString& text,
|
|
|
|
int32_t start,
|
2003-02-21 08:32:22 +00:00
|
|
|
UCalendarDateFields field,
|
1999-08-16 21:50:52 +00:00
|
|
|
const UnicodeString* data,
|
2002-03-02 00:29:54 +00:00
|
|
|
int32_t dataCount,
|
|
|
|
Calendar& cal) const
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
|
|
|
int32_t i = 0;
|
|
|
|
int32_t count = dataCount;
|
|
|
|
|
2003-02-21 08:32:22 +00:00
|
|
|
if (field == UCAL_DAY_OF_WEEK) i = 1;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
// There may be multiple strings in the data[] array which begin with
|
|
|
|
// the same prefix (e.g., Cerven and Cervenec (June and July) in Czech).
|
|
|
|
// We keep track of the longest match, and return that. Note that this
|
|
|
|
// unfortunately requires us to test all array elements.
|
|
|
|
int32_t bestMatchLength = 0, bestMatch = -1;
|
|
|
|
|
|
|
|
// {sfb} kludge to support case-insensitive comparison
|
2002-10-12 01:09:00 +00:00
|
|
|
// {markus 2002oct11} do not just use caseCompareBetween because we do not know
|
|
|
|
// the length of the match after case folding
|
2004-06-07 21:58:56 +00:00
|
|
|
// {alan 20040607} don't case change the whole string, since the length
|
|
|
|
// can change
|
|
|
|
// TODO we need a case-insensitive startsWith function
|
|
|
|
UnicodeString lcase, lcaseText;
|
|
|
|
text.extract(start, INT32_MAX, lcaseText);
|
|
|
|
lcaseText.foldCase();
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
for (; i < count; ++i)
|
|
|
|
{
|
|
|
|
// Always compare if we have no match yet; otherwise only compare
|
|
|
|
// against potentially better matches (longer strings).
|
|
|
|
|
2002-10-12 01:09:00 +00:00
|
|
|
lcase.fastCopyFrom(data[i]).foldCase();
|
2004-06-07 21:58:56 +00:00
|
|
|
int32_t length = lcase.length();
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2004-06-07 21:58:56 +00:00
|
|
|
if (length > bestMatchLength &&
|
|
|
|
lcaseText.compareBetween(0, length, lcase, 0, length) == 0)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
|
|
|
bestMatch = i;
|
|
|
|
bestMatchLength = length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (bestMatch >= 0)
|
|
|
|
{
|
2002-03-02 00:29:54 +00:00
|
|
|
cal.set(field, bestMatch);
|
2004-06-07 21:58:56 +00:00
|
|
|
|
|
|
|
// Once we have a match, we have to determine the length of the
|
|
|
|
// original source string. This will usually be == the length of
|
|
|
|
// the case folded string, but it may differ (e.g. sharp s).
|
|
|
|
lcase.fastCopyFrom(data[bestMatch]).foldCase();
|
|
|
|
|
|
|
|
// Most of the time, the length will be the same as the length
|
|
|
|
// of the string from the locale data. Sometimes it will be
|
|
|
|
// different, in which case we will have to figure it out by
|
|
|
|
// adding a character at a time, until we have a match. We do
|
|
|
|
// this all in one loop, where we try 'len' first (at index
|
|
|
|
// i==0).
|
|
|
|
int32_t len = data[bestMatch].length(); // 99+% of the time
|
|
|
|
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; // already tried this when i was 0
|
|
|
|
}
|
|
|
|
text.extract(start, j, lcaseText);
|
|
|
|
lcaseText.foldCase();
|
|
|
|
if (lcase == lcaseText) {
|
|
|
|
return start + j;
|
|
|
|
}
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return -start;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
|
|
|
void
|
|
|
|
SimpleDateFormat::set2DigitYearStart(UDate d, UErrorCode& status)
|
|
|
|
{
|
|
|
|
parseAmbiguousDatesAsAfter(d, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Private member function that converts the parsed date strings into
|
|
|
|
* timeFields. Returns -start (for ParsePosition) if failed.
|
|
|
|
* @param text the time text to be parsed.
|
|
|
|
* @param start where to start parsing.
|
|
|
|
* @param ch the pattern character for the date field text to be parsed.
|
|
|
|
* @param count the count of a pattern character.
|
|
|
|
* @return the new start position if matching succeeded; a negative number
|
|
|
|
* indicating matching failure, otherwise.
|
|
|
|
*/
|
|
|
|
int32_t SimpleDateFormat::subParse(const UnicodeString& text, int32_t& start, UChar ch, int32_t count,
|
2003-03-07 01:04:43 +00:00
|
|
|
UBool obeyCount, UBool allowNegative, UBool ambiguousYear[], Calendar& cal) const
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
|
|
|
Formattable number;
|
|
|
|
int32_t value = 0;
|
|
|
|
int32_t i;
|
|
|
|
ParsePosition pos(0);
|
2001-09-18 21:36:45 +00:00
|
|
|
int32_t patternCharIndex;
|
2003-03-07 01:04:43 +00:00
|
|
|
UnicodeString temp;
|
2001-09-18 21:36:45 +00:00
|
|
|
UChar *patternCharPtr = u_strchr(DateFormatSymbols::getPatternUChars(), ch);
|
|
|
|
|
2003-10-26 10:24:58 +00:00
|
|
|
#if defined (U_DEBUG_CAL)
|
|
|
|
//fprintf(stderr, "%s:%d - [%c] st=%d \n", __FILE__, __LINE__, (char) ch, start);
|
|
|
|
#endif
|
|
|
|
|
2001-09-18 21:36:45 +00:00
|
|
|
if (patternCharPtr == NULL) {
|
1999-08-16 21:50:52 +00:00
|
|
|
return -start;
|
2001-09-18 21:36:45 +00:00
|
|
|
}
|
|
|
|
|
2004-03-11 14:03:37 +00:00
|
|
|
patternCharIndex = (UDateFormatField)(patternCharPtr - DateFormatSymbols::getPatternUChars());
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2003-02-21 08:32:22 +00:00
|
|
|
UCalendarDateFields field = fgPatternIndexToCalendarField[patternCharIndex];
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
// If there are any spaces here, skip over them. If we hit the end
|
|
|
|
// of the string, then fail.
|
|
|
|
for (;;) {
|
2002-08-22 21:55:51 +00:00
|
|
|
if (start >= text.length()) {
|
1999-08-16 21:50:52 +00:00
|
|
|
return -start;
|
2002-08-22 21:55:51 +00:00
|
|
|
}
|
|
|
|
UChar32 c = text.char32At(start);
|
|
|
|
if (!u_isUWhiteSpace(c)) {
|
1999-08-16 21:50:52 +00:00
|
|
|
break;
|
2002-08-22 21:55:51 +00:00
|
|
|
}
|
|
|
|
start += UTF_CHAR_LENGTH(c);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2002-08-22 21:55:51 +00:00
|
|
|
pos.setIndex(start);
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
// We handle a few special cases here where we need to parse
|
|
|
|
// a number value. We handle further, more generic cases below. We need
|
|
|
|
// to handle some of them here because some fields require extra processing on
|
|
|
|
// the parsed value.
|
2004-03-11 14:03:37 +00:00
|
|
|
if (patternCharIndex == UDAT_HOUR_OF_DAY1_FIELD ||
|
|
|
|
patternCharIndex == UDAT_HOUR1_FIELD ||
|
|
|
|
(patternCharIndex == UDAT_MONTH_FIELD && count <= 2) ||
|
2006-06-26 17:51:25 +00:00
|
|
|
(patternCharIndex == UDAT_STANDALONE_MONTH_FIELD && count <= 2) ||
|
|
|
|
(patternCharIndex == UDAT_QUARTER_FIELD && count <= 2) ||
|
|
|
|
(patternCharIndex == UDAT_STANDALONE_QUARTER_FIELD && count <= 2) ||
|
2004-03-11 14:03:37 +00:00
|
|
|
patternCharIndex == UDAT_YEAR_FIELD ||
|
|
|
|
patternCharIndex == UDAT_YEAR_WOY_FIELD ||
|
|
|
|
patternCharIndex == UDAT_FRACTIONAL_SECOND_FIELD)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2004-03-11 14:03:37 +00:00
|
|
|
int32_t parseStart = pos.getIndex();
|
1999-08-16 21:50:52 +00:00
|
|
|
// It would be good to unify this with the obeyCount logic below,
|
|
|
|
// but that's going to be difficult.
|
2003-03-07 01:04:43 +00:00
|
|
|
const UnicodeString* src;
|
2006-06-26 17:51:25 +00:00
|
|
|
|
2003-03-07 01:04:43 +00:00
|
|
|
if (obeyCount) {
|
|
|
|
if ((start+count) > text.length()) {
|
1999-08-16 21:50:52 +00:00
|
|
|
return -start;
|
2003-03-07 01:04:43 +00:00
|
|
|
}
|
2006-06-26 17:51:25 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
text.extractBetween(0, start + count, temp);
|
2003-03-07 01:04:43 +00:00
|
|
|
src = &temp;
|
|
|
|
} else {
|
|
|
|
src = &text;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2006-06-26 17:51:25 +00:00
|
|
|
|
2003-03-07 01:04:43 +00:00
|
|
|
parseInt(*src, number, pos, allowNegative);
|
2006-06-26 17:51:25 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
if (pos.getIndex() == parseStart)
|
|
|
|
return -start;
|
|
|
|
value = number.getLong();
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (patternCharIndex) {
|
2004-03-11 14:03:37 +00:00
|
|
|
case UDAT_ERA_FIELD:
|
2006-06-26 17:51:25 +00:00
|
|
|
if (count == 4) {
|
|
|
|
return matchString(text, start, UCAL_ERA, fSymbols->fEraNames, fSymbols->fEraNamesCount, cal);
|
|
|
|
}
|
|
|
|
|
2003-02-21 08:32:22 +00:00
|
|
|
return matchString(text, start, UCAL_ERA, fSymbols->fEras, fSymbols->fErasCount, cal);
|
2006-06-26 17:51:25 +00:00
|
|
|
|
2004-03-11 14:03:37 +00:00
|
|
|
case UDAT_YEAR_FIELD:
|
1999-10-15 21:33:00 +00:00
|
|
|
// If there are 3 or more YEAR pattern characters, this indicates
|
1999-08-16 21:50:52 +00:00
|
|
|
// that the year value is to be treated literally, without any
|
|
|
|
// two-digit year adjustments (e.g., from "01" to 2001). Otherwise
|
|
|
|
// we made adjustments to place the 2-digit year in the proper
|
1999-10-15 21:33:00 +00:00
|
|
|
// century, for parsed strings from "00" to "99". Any other string
|
|
|
|
// is treated literally: "2250", "-1", "1", "002".
|
|
|
|
if (count <= 2 && (pos.getIndex() - start) == 2
|
2002-03-19 03:01:28 +00:00
|
|
|
&& u_isdigit(text.charAt(start))
|
|
|
|
&& u_isdigit(text.charAt(start+1)))
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
|
|
|
// Assume for example that the defaultCenturyStart is 6/18/1903.
|
|
|
|
// This means that two-digit years will be forced into the range
|
|
|
|
// 6/18/1903 to 6/17/2003. As a result, years 00, 01, and 02
|
|
|
|
// correspond to 2000, 2001, and 2002. Years 04, 05, etc. correspond
|
|
|
|
// to 1904, 1905, etc. If the year is 03, then it is 2003 if the
|
|
|
|
// other fields specify a date before 6/18, or 1903 if they specify a
|
|
|
|
// date afterwards. As a result, 03 is an ambiguous year. All other
|
|
|
|
// two-digit years are unambiguous.
|
2003-05-25 07:12:19 +00:00
|
|
|
if(fHaveDefaultCentury) { // check if this formatter even has a pivot year
|
|
|
|
int32_t ambiguousTwoDigitYear = fDefaultCenturyStartYear % 100;
|
|
|
|
ambiguousYear[0] = (value == ambiguousTwoDigitYear);
|
|
|
|
value += (fDefaultCenturyStartYear/100)*100 +
|
1999-08-16 21:50:52 +00:00
|
|
|
(value < ambiguousTwoDigitYear ? 100 : 0);
|
2003-05-25 07:12:19 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2003-02-21 08:32:22 +00:00
|
|
|
cal.set(UCAL_YEAR, value);
|
1999-08-16 21:50:52 +00:00
|
|
|
return pos.getIndex();
|
2006-06-26 17:51:25 +00:00
|
|
|
|
2004-03-11 14:03:37 +00:00
|
|
|
case UDAT_YEAR_WOY_FIELD:
|
|
|
|
// Comment is the same as for UDAT_Year_FIELDs - look above
|
2000-01-19 04:18:44 +00:00
|
|
|
if (count <= 2 && (pos.getIndex() - start) == 2
|
2002-03-19 03:01:28 +00:00
|
|
|
&& u_isdigit(text.charAt(start))
|
2003-05-25 07:12:19 +00:00
|
|
|
&& u_isdigit(text.charAt(start+1))
|
|
|
|
&& fHaveDefaultCentury )
|
2000-01-19 04:18:44 +00:00
|
|
|
{
|
|
|
|
int32_t ambiguousTwoDigitYear = fDefaultCenturyStartYear % 100;
|
|
|
|
ambiguousYear[0] = (value == ambiguousTwoDigitYear);
|
|
|
|
value += (fDefaultCenturyStartYear/100)*100 +
|
|
|
|
(value < ambiguousTwoDigitYear ? 100 : 0);
|
|
|
|
}
|
2003-02-21 08:32:22 +00:00
|
|
|
cal.set(UCAL_YEAR_WOY, value);
|
2000-01-19 04:18:44 +00:00
|
|
|
return pos.getIndex();
|
2006-06-26 17:51:25 +00:00
|
|
|
|
2004-03-11 14:03:37 +00:00
|
|
|
case UDAT_MONTH_FIELD:
|
1999-08-16 21:50:52 +00:00
|
|
|
if (count <= 2) // i.e., M or MM.
|
|
|
|
{
|
|
|
|
// Don't want to parse the month if it is a string
|
|
|
|
// while pattern uses numeric style: M or MM.
|
|
|
|
// [We computed 'value' above.]
|
2003-02-21 08:32:22 +00:00
|
|
|
cal.set(UCAL_MONTH, value - 1);
|
1999-08-16 21:50:52 +00:00
|
|
|
return pos.getIndex();
|
2006-06-26 17:51:25 +00:00
|
|
|
} else {
|
1999-08-16 21:50:52 +00:00
|
|
|
// count >= 3 // i.e., MMM or MMMM
|
|
|
|
// Want to be able to parse both short and long forms.
|
|
|
|
// Try count == 4 first:
|
|
|
|
int32_t newStart = 0;
|
2006-06-26 17:51:25 +00:00
|
|
|
|
2003-02-21 08:32:22 +00:00
|
|
|
if ((newStart = matchString(text, start, UCAL_MONTH,
|
2002-03-02 00:29:54 +00:00
|
|
|
fSymbols->fMonths, fSymbols->fMonthsCount, cal)) > 0)
|
1999-08-16 21:50:52 +00:00
|
|
|
return newStart;
|
|
|
|
else // count == 4 failed, now try count == 3
|
2003-02-21 08:32:22 +00:00
|
|
|
return matchString(text, start, UCAL_MONTH,
|
2002-03-02 00:29:54 +00:00
|
|
|
fSymbols->fShortMonths, fSymbols->fShortMonthsCount, cal);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2006-06-26 17:51:25 +00:00
|
|
|
|
|
|
|
case UDAT_STANDALONE_MONTH_FIELD:
|
|
|
|
if (count <= 2) // i.e., L or LL.
|
|
|
|
{
|
|
|
|
// Don't want to parse the month if it is a string
|
|
|
|
// while pattern uses numeric style: M or MM.
|
|
|
|
// [We computed 'value' above.]
|
|
|
|
cal.set(UCAL_MONTH, value - 1);
|
|
|
|
return pos.getIndex();
|
|
|
|
} else {
|
|
|
|
// count >= 3 // i.e., LLL or LLLL
|
|
|
|
// Want to be able to parse both short and long forms.
|
|
|
|
// Try count == 4 first:
|
|
|
|
int32_t newStart = 0;
|
|
|
|
|
|
|
|
if ((newStart = matchString(text, start, UCAL_MONTH,
|
|
|
|
fSymbols->fStandaloneMonths, fSymbols->fStandaloneMonthsCount, cal)) > 0)
|
|
|
|
return newStart;
|
|
|
|
else // count == 4 failed, now try count == 3
|
|
|
|
return matchString(text, start, UCAL_MONTH,
|
|
|
|
fSymbols->fStandaloneShortMonths, fSymbols->fStandaloneShortMonthsCount, cal);
|
|
|
|
}
|
|
|
|
|
2004-03-11 14:03:37 +00:00
|
|
|
case UDAT_HOUR_OF_DAY1_FIELD:
|
1999-08-16 21:50:52 +00:00
|
|
|
// [We computed 'value' above.]
|
2003-02-21 08:32:22 +00:00
|
|
|
if (value == cal.getMaximum(UCAL_HOUR_OF_DAY) + 1)
|
1999-08-16 21:50:52 +00:00
|
|
|
value = 0;
|
2003-02-21 08:32:22 +00:00
|
|
|
cal.set(UCAL_HOUR_OF_DAY, value);
|
1999-08-16 21:50:52 +00:00
|
|
|
return pos.getIndex();
|
2006-06-26 17:51:25 +00:00
|
|
|
|
2004-03-11 14:03:37 +00:00
|
|
|
case UDAT_FRACTIONAL_SECOND_FIELD:
|
|
|
|
// Fractional seconds left-justify
|
|
|
|
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();
|
2006-06-26 17:51:25 +00:00
|
|
|
|
2004-03-11 14:03:37 +00:00
|
|
|
case UDAT_DAY_OF_WEEK_FIELD:
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
|
|
|
// Want to be able to parse both short and long forms.
|
|
|
|
// Try count == 4 (DDDD) first:
|
|
|
|
int32_t newStart = 0;
|
2003-02-21 08:32:22 +00:00
|
|
|
if ((newStart = matchString(text, start, UCAL_DAY_OF_WEEK,
|
2002-03-02 00:29:54 +00:00
|
|
|
fSymbols->fWeekdays, fSymbols->fWeekdaysCount, cal)) > 0)
|
1999-08-16 21:50:52 +00:00
|
|
|
return newStart;
|
|
|
|
else // DDDD failed, now try DDD
|
2003-02-21 08:32:22 +00:00
|
|
|
return matchString(text, start, UCAL_DAY_OF_WEEK,
|
2002-03-02 00:29:54 +00:00
|
|
|
fSymbols->fShortWeekdays, fSymbols->fShortWeekdaysCount, cal);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2006-06-26 17:51:25 +00:00
|
|
|
|
|
|
|
case UDAT_STANDALONE_DAY_FIELD:
|
|
|
|
{
|
|
|
|
// Want to be able to parse both short and long forms.
|
|
|
|
// Try count == 4 (DDDD) first:
|
|
|
|
int32_t newStart = 0;
|
|
|
|
if ((newStart = matchString(text, start, UCAL_DAY_OF_WEEK,
|
|
|
|
fSymbols->fStandaloneWeekdays, fSymbols->fStandaloneWeekdaysCount, cal)) > 0)
|
|
|
|
return newStart;
|
|
|
|
else // DDDD failed, now try DDD
|
|
|
|
return matchString(text, start, UCAL_DAY_OF_WEEK,
|
|
|
|
fSymbols->fStandaloneShortWeekdays, fSymbols->fStandaloneShortWeekdaysCount, cal);
|
|
|
|
}
|
|
|
|
|
2004-03-11 14:03:37 +00:00
|
|
|
case UDAT_AM_PM_FIELD:
|
2003-02-21 08:32:22 +00:00
|
|
|
return matchString(text, start, UCAL_AM_PM, fSymbols->fAmPms, fSymbols->fAmPmsCount, cal);
|
2006-06-26 17:51:25 +00:00
|
|
|
|
2004-03-11 14:03:37 +00:00
|
|
|
case UDAT_HOUR1_FIELD:
|
1999-08-16 21:50:52 +00:00
|
|
|
// [We computed 'value' above.]
|
2003-02-21 08:32:22 +00:00
|
|
|
if (value == cal.getLeastMaximum(UCAL_HOUR)+1)
|
1999-08-16 21:50:52 +00:00
|
|
|
value = 0;
|
2003-02-21 08:32:22 +00:00
|
|
|
cal.set(UCAL_HOUR, value);
|
1999-08-16 21:50:52 +00:00
|
|
|
return pos.getIndex();
|
2006-06-26 17:51:25 +00:00
|
|
|
|
|
|
|
case UDAT_QUARTER_FIELD:
|
|
|
|
if (count <= 2) // i.e., Q or QQ.
|
|
|
|
{
|
|
|
|
// Don't want to parse the month if it is a string
|
|
|
|
// while pattern uses numeric style: Q or QQ.
|
|
|
|
// [We computed 'value' above.]
|
|
|
|
cal.set(UCAL_MONTH, (value - 1) * 3);
|
|
|
|
return pos.getIndex();
|
|
|
|
} else {
|
|
|
|
// count >= 3 // i.e., QQQ or QQQQ
|
|
|
|
// Want to be able to parse both short and long forms.
|
|
|
|
// Try count == 4 first:
|
|
|
|
int32_t newStart = 0;
|
|
|
|
|
|
|
|
if ((newStart = matchQuarterString(text, start, UCAL_MONTH,
|
|
|
|
fSymbols->fQuarters, fSymbols->fQuartersCount, cal)) > 0)
|
|
|
|
return newStart;
|
|
|
|
else // count == 4 failed, now try count == 3
|
|
|
|
return matchQuarterString(text, start, UCAL_MONTH,
|
|
|
|
fSymbols->fShortQuarters, fSymbols->fShortQuartersCount, cal);
|
|
|
|
}
|
|
|
|
|
|
|
|
case UDAT_STANDALONE_QUARTER_FIELD:
|
|
|
|
if (count <= 2) // i.e., q or qq.
|
|
|
|
{
|
|
|
|
// Don't want to parse the month if it is a string
|
|
|
|
// while pattern uses numeric style: q or q.
|
|
|
|
// [We computed 'value' above.]
|
|
|
|
cal.set(UCAL_MONTH, (value - 1) * 3);
|
|
|
|
return pos.getIndex();
|
|
|
|
} else {
|
|
|
|
// count >= 3 // i.e., qqq or qqqq
|
|
|
|
// Want to be able to parse both short and long forms.
|
|
|
|
// Try count == 4 first:
|
|
|
|
int32_t newStart = 0;
|
|
|
|
|
|
|
|
if ((newStart = matchQuarterString(text, start, UCAL_MONTH,
|
|
|
|
fSymbols->fStandaloneQuarters, fSymbols->fStandaloneQuartersCount, cal)) > 0)
|
|
|
|
return newStart;
|
|
|
|
else // count == 4 failed, now try count == 3
|
|
|
|
return matchQuarterString(text, start, UCAL_MONTH,
|
|
|
|
fSymbols->fStandaloneShortQuarters, fSymbols->fStandaloneShortQuartersCount, cal);
|
|
|
|
}
|
|
|
|
|
2004-03-11 14:03:37 +00:00
|
|
|
case UDAT_TIMEZONE_FIELD:
|
|
|
|
case UDAT_TIMEZONE_RFC_FIELD:
|
2005-08-26 21:55:55 +00:00
|
|
|
case UDAT_TIMEZONE_GENERIC_FIELD:
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
|
|
|
// First try to parse generic forms such as GMT-07:00. Do this first
|
|
|
|
// in case localized DateFormatZoneData contains the string "GMT"
|
|
|
|
// for a zone; in that case, we don't want to match the first three
|
|
|
|
// characters of GMT+/-HH:MM etc.
|
2001-05-16 20:47:21 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
int32_t sign = 0;
|
|
|
|
int32_t offset;
|
2005-01-01 21:31:21 +00:00
|
|
|
int32_t gmtLen = u_strlen(gGmt);
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
// For time zones that have no known names, look for strings
|
|
|
|
// of the form:
|
|
|
|
// GMT[+-]hours:minutes or
|
|
|
|
// GMT[+-]hhmm or
|
|
|
|
// GMT.
|
|
|
|
|
2005-06-04 03:08:25 +00:00
|
|
|
if ((text.length() - start) >= gmtLen &&
|
|
|
|
(text.caseCompare(start, gmtLen, gGmt, 0, gmtLen, U_FOLD_CASE_DEFAULT)) == 0)
|
|
|
|
{
|
2003-02-21 08:32:22 +00:00
|
|
|
cal.set(UCAL_DST_OFFSET, 0);
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2001-05-16 20:47:21 +00:00
|
|
|
pos.setIndex(start + gmtLen);
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
if( text[pos.getIndex()] == 0x002B /*'+'*/ )
|
2005-06-04 03:08:25 +00:00
|
|
|
sign = 1;
|
1999-08-16 21:50:52 +00:00
|
|
|
else if( text[pos.getIndex()] == 0x002D /*'-'*/ )
|
2005-06-04 03:08:25 +00:00
|
|
|
sign = -1;
|
1999-08-16 21:50:52 +00:00
|
|
|
else {
|
2005-06-04 03:08:25 +00:00
|
|
|
cal.set(UCAL_ZONE_OFFSET, 0 );
|
|
|
|
return pos.getIndex();
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Look for hours:minutes or hhmm.
|
|
|
|
pos.setIndex(pos.getIndex() + 1);
|
|
|
|
int32_t parseStart = pos.getIndex();
|
|
|
|
Formattable tzNumber;
|
|
|
|
fNumberFormat->parse(text, tzNumber, pos);
|
|
|
|
if( pos.getIndex() == parseStart) {
|
2005-06-04 03:08:25 +00:00
|
|
|
return -start;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
if( text[pos.getIndex()] == 0x003A /*':'*/ ) {
|
2005-06-04 03:08:25 +00:00
|
|
|
// This is the hours:minutes case
|
|
|
|
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();
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
else {
|
2005-06-04 03:08:25 +00:00
|
|
|
// This is the hhmm case.
|
|
|
|
offset = tzNumber.getLong();
|
|
|
|
if( offset < 24 )
|
|
|
|
offset *= 60;
|
|
|
|
else
|
|
|
|
offset = offset % 100 + offset / 100 * 60;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fall through for final processing below of 'offset' and 'sign'.
|
2005-06-04 03:08:25 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
else {
|
|
|
|
// At this point, check for named time zones by looking through
|
|
|
|
// the locale data from the DateFormatZoneData strings.
|
|
|
|
// Want to be able to parse both short and long forms.
|
2005-06-04 03:08:25 +00:00
|
|
|
// !!! side effect, might set parsedZoneString
|
2005-08-26 21:55:55 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
int32_t result = subParseZoneString(text, start, cal, status);
|
2005-06-04 03:08:25 +00:00
|
|
|
if (result != 0) {
|
|
|
|
return result;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// As a last resort, look for numeric timezones of the form
|
|
|
|
// [+-]hhmm as specified by RFC 822. This code is actually
|
|
|
|
// a little more permissive than RFC 822. It will try to do
|
2005-08-26 21:55:55 +00:00
|
|
|
// its best with numbers that aren't strictly 4 digits long
|
2004-09-15 23:44:48 +00:00
|
|
|
DecimalFormat fmt(UNICODE_STRING_SIMPLE("+####;-####"), status);
|
1999-10-18 22:48:32 +00:00
|
|
|
if(U_FAILURE(status))
|
1999-08-16 21:50:52 +00:00
|
|
|
return -start;
|
2003-11-19 23:44:51 +00:00
|
|
|
fmt.setParseIntegerOnly(TRUE);
|
1999-08-16 21:50:52 +00:00
|
|
|
int32_t parseStart = pos.getIndex();
|
|
|
|
Formattable tzNumber;
|
2003-11-19 23:44:51 +00:00
|
|
|
fmt.parse( text, tzNumber, pos );
|
1999-08-16 21:50:52 +00:00
|
|
|
if( pos.getIndex() == parseStart) {
|
|
|
|
return -start; // Wasn't actually a number.
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
|
|
|
|
// Fall through for final processing below of 'offset' and 'sign'.
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do the final processing for both of the above cases. We only
|
|
|
|
// arrive here if the form GMT+/-... or an RFC 822 form was seen.
|
|
|
|
if (sign != 0)
|
|
|
|
{
|
1999-10-18 22:48:32 +00:00
|
|
|
offset *= U_MILLIS_PER_MINUTE * sign;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2002-03-02 00:29:54 +00:00
|
|
|
if (cal.getTimeZone().useDaylightTime())
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2003-02-21 08:32:22 +00:00
|
|
|
cal.set(UCAL_DST_OFFSET, U_MILLIS_PER_HOUR);
|
1999-10-18 22:48:32 +00:00
|
|
|
offset -= U_MILLIS_PER_HOUR;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2003-02-21 08:32:22 +00:00
|
|
|
cal.set(UCAL_ZONE_OFFSET, offset);
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
return pos.getIndex();
|
|
|
|
}
|
|
|
|
|
|
|
|
// All efforts to parse a zone failed.
|
|
|
|
return -start;
|
|
|
|
}
|
2006-06-26 17:51:25 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
default:
|
|
|
|
// Handle "generic" fields
|
2004-03-11 14:03:37 +00:00
|
|
|
int32_t parseStart = pos.getIndex();
|
2003-03-07 01:04:43 +00:00
|
|
|
const UnicodeString* src;
|
|
|
|
if (obeyCount) {
|
|
|
|
if ((start+count) > text.length()) {
|
1999-08-16 21:50:52 +00:00
|
|
|
return -start;
|
2003-03-07 01:04:43 +00:00
|
|
|
}
|
|
|
|
text.extractBetween(0, start + count, temp);
|
|
|
|
src = &temp;
|
|
|
|
} else {
|
|
|
|
src = &text;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2003-03-07 01:04:43 +00:00
|
|
|
parseInt(*src, number, pos, allowNegative);
|
1999-08-16 21:50:52 +00:00
|
|
|
if (pos.getIndex() != parseStart) {
|
2002-03-02 00:29:54 +00:00
|
|
|
cal.set(field, number.getLong());
|
1999-08-16 21:50:52 +00:00
|
|
|
return pos.getIndex();
|
|
|
|
}
|
|
|
|
return -start;
|
|
|
|
}
|
|
|
|
}
|
2005-06-04 03:08:25 +00:00
|
|
|
|
|
|
|
int32_t
|
2005-08-26 21:55:55 +00:00
|
|
|
SimpleDateFormat::subParseZoneString(const UnicodeString& text, int32_t start, Calendar& cal, UErrorCode& status) const
|
2005-06-04 03:08:25 +00:00
|
|
|
{
|
|
|
|
// At this point, check for named time zones by looking through
|
|
|
|
// the locale data from the DateFormatZoneData strings.
|
|
|
|
// Want to be able to parse both short and long forms.
|
|
|
|
|
|
|
|
// optimize for calendar's current time zone
|
2005-08-26 21:55:55 +00:00
|
|
|
TimeZone *tz = NULL;
|
|
|
|
UnicodeString id;
|
|
|
|
UnicodeString zid, value;
|
|
|
|
DateFormatSymbols::TimeZoneTranslationType type;
|
|
|
|
fSymbols->getZoneID(getTimeZone().getID(id), zid, status);
|
|
|
|
if(zid.length() > 0){
|
|
|
|
fSymbols->getZoneType(zid, text, start, type, value, status);
|
2005-06-04 03:08:25 +00:00
|
|
|
tz = getTimeZone().clone();
|
|
|
|
}
|
2005-08-26 21:55:55 +00:00
|
|
|
|
2005-06-04 03:08:25 +00:00
|
|
|
// optimize for default time zone, assume different from caller
|
|
|
|
if (tz == NULL) {
|
2005-08-26 21:55:55 +00:00
|
|
|
TimeZone* defaultZone = TimeZone::createDefault();
|
|
|
|
fSymbols->getZoneID(defaultZone->getID(id), zid, status);
|
|
|
|
if(zid.length() > 0){
|
|
|
|
fSymbols->getZoneType(zid, text, start, type, value, status);
|
|
|
|
tz = defaultZone;
|
|
|
|
}
|
|
|
|
if (tz == NULL) {
|
|
|
|
delete defaultZone;
|
|
|
|
}
|
2005-06-04 03:08:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// still no luck, check all time zone strings
|
2005-08-26 21:55:55 +00:00
|
|
|
if(tz == NULL){
|
|
|
|
fSymbols->findZoneIDTypeValue(zid, text, start, type, value, status);
|
|
|
|
if(zid.length() > 0){
|
|
|
|
tz = TimeZone::createTimeZone(zid);
|
2005-06-04 03:08:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-08-26 21:55:55 +00:00
|
|
|
if(U_FAILURE(status)){
|
|
|
|
return 0;
|
|
|
|
}
|
2005-06-04 03:08:25 +00:00
|
|
|
if (tz != NULL) { // Matched any ?
|
|
|
|
// always set zone offset, needed to get correct hour in wall time
|
|
|
|
// when checking daylight savings
|
|
|
|
cal.set(UCAL_ZONE_OFFSET, tz->getRawOffset());
|
2005-08-26 21:55:55 +00:00
|
|
|
if (type==DateFormatSymbols::TIMEZONE_SHORT_STANDARD || type==DateFormatSymbols::TIMEZONE_LONG_STANDARD ) {
|
2005-06-04 03:08:25 +00:00
|
|
|
// standard time
|
|
|
|
cal.set(UCAL_DST_OFFSET, 0);
|
|
|
|
delete tz; tz = NULL;
|
2005-08-26 21:55:55 +00:00
|
|
|
} else if (type==DateFormatSymbols::TIMEZONE_SHORT_DAYLIGHT || type==DateFormatSymbols::TIMEZONE_LONG_DAYLIGHT ) {
|
2005-06-04 03:08:25 +00:00
|
|
|
// daylight time
|
|
|
|
// !!! todo - no getDSTSavings() in ICU's timezone
|
|
|
|
// use the correct DST SAVINGS for the zone.
|
|
|
|
// cal.set(UCAL_DST_OFFSET, tz->getDSTSavings());
|
|
|
|
cal.set(UCAL_DST_OFFSET, U_MILLIS_PER_HOUR);
|
|
|
|
delete tz; tz = NULL;
|
|
|
|
} else {
|
|
|
|
// either standard or daylight
|
|
|
|
// need to finish getting the date, then compute dst offset as appropriate
|
|
|
|
|
|
|
|
// !!! hack for api compatibility, can't modify subParse(...) so can't
|
|
|
|
// pass this back any other way. cast away const.
|
|
|
|
((SimpleDateFormat*)this)->parsedTimeZone = tz;
|
|
|
|
}
|
|
|
|
|
2005-08-26 21:55:55 +00:00
|
|
|
return start + value.length();
|
2005-06-04 03:08:25 +00:00
|
|
|
}
|
|
|
|
|
2005-08-26 21:55:55 +00:00
|
|
|
|
2005-06-04 03:08:25 +00:00
|
|
|
// complete failure
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-03-07 01:04:43 +00:00
|
|
|
/**
|
|
|
|
* Parse an integer using fNumberFormat. This method is semantically
|
|
|
|
* const, but actually may modify fNumberFormat.
|
|
|
|
*/
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
|
|
|
void SimpleDateFormat::translatePattern(const UnicodeString& originalPattern,
|
|
|
|
UnicodeString& translatedPattern,
|
|
|
|
const UnicodeString& from,
|
|
|
|
const UnicodeString& to,
|
|
|
|
UErrorCode& status)
|
|
|
|
{
|
|
|
|
// run through the pattern and convert any pattern symbols from the version
|
|
|
|
// in "from" to the corresponding character ion "to". This code takes
|
|
|
|
// quoted strings into account (it doesn't try to translate them), and it signals
|
|
|
|
// an error if a particular "pattern character" doesn't appear in "from".
|
|
|
|
// Depending on the values of "from" and "to" this can convert from generic
|
|
|
|
// to localized patterns or localized to generic.
|
1999-10-18 22:48:32 +00:00
|
|
|
if (U_FAILURE(status))
|
1999-08-16 21:50:52 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
translatedPattern.remove();
|
2000-05-18 22:08:39 +00:00
|
|
|
UBool inQuote = FALSE;
|
2002-03-12 01:32:42 +00:00
|
|
|
for (int32_t i = 0; i < originalPattern.length(); ++i) {
|
1999-08-16 21:50:52 +00:00
|
|
|
UChar c = originalPattern[i];
|
|
|
|
if (inQuote) {
|
2003-03-14 00:49:21 +00:00
|
|
|
if (c == QUOTE)
|
1999-08-16 21:50:52 +00:00
|
|
|
inQuote = FALSE;
|
|
|
|
}
|
|
|
|
else {
|
2003-03-14 00:49:21 +00:00
|
|
|
if (c == QUOTE)
|
1999-08-16 21:50:52 +00:00
|
|
|
inQuote = TRUE;
|
|
|
|
else if ((c >= 0x0061 /*'a'*/ && c <= 0x007A) /*'z'*/
|
|
|
|
|| (c >= 0x0041 /*'A'*/ && c <= 0x005A /*'Z'*/)) {
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t ci = from.indexOf(c);
|
1999-08-16 21:50:52 +00:00
|
|
|
if (ci == -1) {
|
1999-10-07 00:07:53 +00:00
|
|
|
status = U_INVALID_FORMAT_ERROR;
|
1999-08-16 21:50:52 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
c = to[ci];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
translatedPattern += c;
|
|
|
|
}
|
|
|
|
if (inQuote) {
|
1999-10-07 00:07:53 +00:00
|
|
|
status = U_INVALID_FORMAT_ERROR;
|
1999-08-16 21:50:52 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
|
|
|
UnicodeString&
|
|
|
|
SimpleDateFormat::toPattern(UnicodeString& result) const
|
|
|
|
{
|
|
|
|
result = fPattern;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
|
|
|
UnicodeString&
|
|
|
|
SimpleDateFormat::toLocalizedPattern(UnicodeString& result,
|
|
|
|
UErrorCode& status) const
|
|
|
|
{
|
2001-09-18 21:36:45 +00:00
|
|
|
translatePattern(fPattern, result, DateFormatSymbols::getPatternUChars(), fSymbols->fLocalPatternChars, status);
|
1999-08-16 21:50:52 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
|
|
|
void
|
|
|
|
SimpleDateFormat::applyPattern(const UnicodeString& pattern)
|
|
|
|
{
|
|
|
|
fPattern = pattern;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
|
|
|
void
|
|
|
|
SimpleDateFormat::applyLocalizedPattern(const UnicodeString& pattern,
|
|
|
|
UErrorCode &status)
|
|
|
|
{
|
2001-09-18 21:36:45 +00:00
|
|
|
translatePattern(pattern, fPattern, fSymbols->fLocalPatternChars, DateFormatSymbols::getPatternUChars(), status);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
2003-05-16 22:05:41 +00:00
|
|
|
void SimpleDateFormat::adoptCalendar(Calendar* calendarToAdopt)
|
|
|
|
{
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
DateFormat::adoptCalendar(calendarToAdopt);
|
|
|
|
delete fSymbols;
|
|
|
|
fSymbols=NULL;
|
2003-05-25 07:12:19 +00:00
|
|
|
initializeSymbols(fLocale, fCalendar, status); // we need new symbols
|
|
|
|
initializeDefaultCentury(); // we need a new century (possibly)
|
2003-05-16 22:05:41 +00:00
|
|
|
}
|
|
|
|
|
2001-10-08 23:26:58 +00:00
|
|
|
U_NAMESPACE_END
|
|
|
|
|
2002-09-20 01:54:48 +00:00
|
|
|
#endif /* #if !UCONFIG_NO_FORMATTING */
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
//eof
|