2 *******************************************************************************
3 * Copyright (C) 1997-2014, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
9 * Modification History:
11 * Date Name Description
12 * 02/19/97 aliu Converted from java.
13 * 03/31/97 aliu Modified extensively to work with 50 locales.
14 * 04/01/97 aliu Added support for centuries.
15 * 08/12/97 aliu Fixed operator== to use Calendar::equivalentTo.
16 * 07/20/98 stephen Changed ParsePosition initialization
17 ********************************************************************************
20 #include "unicode/utypes.h"
22 #if !UCONFIG_NO_FORMATTING
24 #include "unicode/ures.h"
25 #include "unicode/datefmt.h"
26 #include "unicode/smpdtfmt.h"
27 #include "unicode/dtptngen.h"
28 #include "unicode/udisplaycontext.h"
34 #if defined( U_DEBUG_CALSVC ) || defined (U_DEBUG_CAL)
38 // *****************************************************************************
40 // *****************************************************************************
44 DateFormat::DateFormat()
47 fCapitalizationContext(UDISPCTX_CAPITALIZATION_NONE
)
51 //----------------------------------------------------------------------
53 DateFormat::DateFormat(const DateFormat
& other
)
57 fCapitalizationContext(UDISPCTX_CAPITALIZATION_NONE
)
62 //----------------------------------------------------------------------
64 DateFormat
& DateFormat::operator=(const DateFormat
& other
)
71 fCalendar
= other
.fCalendar
->clone();
75 if(other
.fNumberFormat
) {
76 fNumberFormat
= (NumberFormat
*)other
.fNumberFormat
->clone();
80 fBoolFlags
= other
.fBoolFlags
;
81 fCapitalizationContext
= other
.fCapitalizationContext
;
86 //----------------------------------------------------------------------
88 DateFormat::~DateFormat()
94 //----------------------------------------------------------------------
97 DateFormat::operator==(const Format
& other
) const
99 // This protected comparison operator should only be called by subclasses
100 // which have confirmed that the other object being compared against is
101 // an instance of a sublcass of DateFormat. THIS IS IMPORTANT.
103 // Format::operator== guarantees that this cast is safe
104 DateFormat
* fmt
= (DateFormat
*)&other
;
106 return (this == fmt
) ||
107 (Format::operator==(other
) &&
108 fCalendar
&&(fCalendar
->isEquivalentTo(*fmt
->fCalendar
)) &&
109 (fNumberFormat
&& *fNumberFormat
== *fmt
->fNumberFormat
) &&
110 (fCapitalizationContext
== fmt
->fCapitalizationContext
) );
113 //----------------------------------------------------------------------
116 DateFormat::format(const Formattable
& obj
,
117 UnicodeString
& appendTo
,
118 FieldPosition
& fieldPosition
,
119 UErrorCode
& status
) const
121 if (U_FAILURE(status
)) return appendTo
;
123 // if the type of the Formattable is double or long, treat it as if it were a Date
125 switch (obj
.getType())
127 case Formattable::kDate
:
128 date
= obj
.getDate();
130 case Formattable::kDouble
:
131 date
= (UDate
)obj
.getDouble();
133 case Formattable::kLong
:
134 date
= (UDate
)obj
.getLong();
137 status
= U_ILLEGAL_ARGUMENT_ERROR
;
142 //if (fieldPosition.getBeginIndex() == fieldPosition.getEndIndex())
143 // status = U_ILLEGAL_ARGUMENT_ERROR;
145 return format(date
, appendTo
, fieldPosition
);
148 //----------------------------------------------------------------------
151 DateFormat::format(const Formattable
& obj
,
152 UnicodeString
& appendTo
,
153 FieldPositionIterator
* posIter
,
154 UErrorCode
& status
) const
156 if (U_FAILURE(status
)) return appendTo
;
158 // if the type of the Formattable is double or long, treat it as if it were a Date
160 switch (obj
.getType())
162 case Formattable::kDate
:
163 date
= obj
.getDate();
165 case Formattable::kDouble
:
166 date
= (UDate
)obj
.getDouble();
168 case Formattable::kLong
:
169 date
= (UDate
)obj
.getLong();
172 status
= U_ILLEGAL_ARGUMENT_ERROR
;
177 //if (fieldPosition.getBeginIndex() == fieldPosition.getEndIndex())
178 // status = U_ILLEGAL_ARGUMENT_ERROR;
180 return format(date
, appendTo
, posIter
, status
);
183 //----------------------------------------------------------------------
185 // Default implementation for backwards compatibility, subclasses should implement.
187 DateFormat::format(Calendar
& /* unused cal */,
188 UnicodeString
& appendTo
,
189 FieldPositionIterator
* /* unused posIter */,
190 UErrorCode
& status
) const {
191 if (U_SUCCESS(status
)) {
192 status
= U_UNSUPPORTED_ERROR
;
197 //----------------------------------------------------------------------
200 DateFormat::format(UDate date
, UnicodeString
& appendTo
, FieldPosition
& fieldPosition
) const {
201 if (fCalendar
!= NULL
) {
202 // Use a clone of our calendar instance
203 Calendar
* calClone
= fCalendar
->clone();
204 if (calClone
!= NULL
) {
205 UErrorCode ec
= U_ZERO_ERROR
;
206 calClone
->setTime(date
, ec
);
208 format(*calClone
, appendTo
, fieldPosition
);
216 //----------------------------------------------------------------------
219 DateFormat::format(UDate date
, UnicodeString
& appendTo
, FieldPositionIterator
* posIter
,
220 UErrorCode
& status
) const {
221 if (fCalendar
!= NULL
) {
222 Calendar
* calClone
= fCalendar
->clone();
223 if (calClone
!= NULL
) {
224 calClone
->setTime(date
, status
);
225 if (U_SUCCESS(status
)) {
226 format(*calClone
, appendTo
, posIter
, status
);
234 //----------------------------------------------------------------------
237 DateFormat::format(UDate date
, UnicodeString
& appendTo
) const
239 // Note that any error information is just lost. That's okay
240 // for this convenience method.
241 FieldPosition
fpos(0);
242 return format(date
, appendTo
, fpos
);
245 //----------------------------------------------------------------------
248 DateFormat::parse(const UnicodeString
& text
,
249 ParsePosition
& pos
) const
251 UDate d
= 0; // Error return UDate is 0 (the epoch)
252 if (fCalendar
!= NULL
) {
253 Calendar
* calClone
= fCalendar
->clone();
254 if (calClone
!= NULL
) {
255 int32_t start
= pos
.getIndex();
257 parse(text
, *calClone
, pos
);
258 if (pos
.getIndex() != start
) {
259 UErrorCode ec
= U_ZERO_ERROR
;
260 d
= calClone
->getTime(ec
);
262 // We arrive here if fCalendar => calClone is non-lenient and
263 // there is an out-of-range field. We don't know which field
264 // was illegal so we set the error index to the start.
266 pos
.setErrorIndex(start
);
276 //----------------------------------------------------------------------
279 DateFormat::parse(const UnicodeString
& text
,
280 UErrorCode
& status
) const
282 if (U_FAILURE(status
)) return 0;
284 ParsePosition
pos(0);
285 UDate result
= parse(text
, pos
);
286 if (pos
.getIndex() == 0) {
287 #if defined (U_DEBUG_CAL)
288 fprintf(stderr
, "%s:%d - - failed to parse - err index %d\n"
289 , __FILE__
, __LINE__
, pos
.getErrorIndex() );
291 status
= U_ILLEGAL_ARGUMENT_ERROR
;
296 //----------------------------------------------------------------------
299 DateFormat::parseObject(const UnicodeString
& source
,
301 ParsePosition
& pos
) const
303 result
.setDate(parse(source
, pos
));
306 //----------------------------------------------------------------------
308 DateFormat
* U_EXPORT2
309 DateFormat::createTimeInstance(DateFormat::EStyle style
,
310 const Locale
& aLocale
)
312 return create(style
, kNone
, aLocale
);
315 //----------------------------------------------------------------------
317 DateFormat
* U_EXPORT2
318 DateFormat::createDateInstance(DateFormat::EStyle style
,
319 const Locale
& aLocale
)
321 // +4 to set the correct index for getting data out of
325 style
= (EStyle
) (style
+ kDateOffset
);
327 return create(kNone
, (EStyle
) (style
), aLocale
);
330 //----------------------------------------------------------------------
332 DateFormat
* U_EXPORT2
333 DateFormat::createDateTimeInstance(EStyle dateStyle
,
335 const Locale
& aLocale
)
337 if(dateStyle
!= kNone
)
339 dateStyle
= (EStyle
) (dateStyle
+ kDateOffset
);
341 return create(timeStyle
, dateStyle
, aLocale
);
344 //----------------------------------------------------------------------
346 DateFormat
* U_EXPORT2
347 DateFormat::createInstance()
349 return create(kShort
, (EStyle
) (kShort
+ kDateOffset
), Locale::getDefault());
352 //----------------------------------------------------------------------
354 DateFormat
* U_EXPORT2
355 DateFormat::create(EStyle timeStyle
, EStyle dateStyle
, const Locale
& locale
)
357 UErrorCode status
= U_ZERO_ERROR
;
358 #if U_PLATFORM_HAS_WIN32_API
360 int32_t count
= locale
.getKeywordValue("compat", buffer
, sizeof(buffer
), status
);
362 // if the locale has "@compat=host", create a host-specific DateFormat...
363 if (count
> 0 && uprv_strcmp(buffer
, "host") == 0) {
364 Win32DateFormat
*f
= new Win32DateFormat(timeStyle
, dateStyle
, locale
, status
);
366 if (U_SUCCESS(status
)) {
375 if(/*((timeStyle!=UDAT_NONE)&&(timeStyle & UDAT_RELATIVE)) || */((dateStyle
!=kNone
)&&((dateStyle
-kDateOffset
) & UDAT_RELATIVE
))) {
376 RelativeDateFormat
*r
= new RelativeDateFormat((UDateFormatStyle
)timeStyle
, (UDateFormatStyle
)(dateStyle
-kDateOffset
), locale
, status
);
377 if(U_SUCCESS(status
)) return r
;
379 status
= U_ZERO_ERROR
;
382 // Try to create a SimpleDateFormat of the desired style.
383 SimpleDateFormat
*f
= new SimpleDateFormat(timeStyle
, dateStyle
, locale
, status
);
384 if (U_SUCCESS(status
)) return f
;
387 // If that fails, try to create a format using the default pattern and
388 // the DateFormatSymbols for this locale.
389 status
= U_ZERO_ERROR
;
390 f
= new SimpleDateFormat(locale
, status
);
391 if (U_SUCCESS(status
)) return f
;
394 // This should never really happen, because the preceding constructor
395 // should always succeed. If the resource data is unavailable, a last
396 // resort object should be returned.
400 //----------------------------------------------------------------------
402 const Locale
* U_EXPORT2
403 DateFormat::getAvailableLocales(int32_t& count
)
405 // Get the list of installed locales.
406 // Even if root has the correct date format for this locale,
407 // it's still a valid locale (we don't worry about data fallbacks).
408 return Locale::getAvailableLocales(count
);
411 //----------------------------------------------------------------------
414 DateFormat::adoptCalendar(Calendar
* newCalendar
)
417 fCalendar
= newCalendar
;
420 //----------------------------------------------------------------------
422 DateFormat::setCalendar(const Calendar
& newCalendar
)
424 Calendar
* newCalClone
= newCalendar
.clone();
425 if (newCalClone
!= NULL
) {
426 adoptCalendar(newCalClone
);
430 //----------------------------------------------------------------------
433 DateFormat::getCalendar() const
438 //----------------------------------------------------------------------
441 DateFormat::adoptNumberFormat(NumberFormat
* newNumberFormat
)
443 delete fNumberFormat
;
444 fNumberFormat
= newNumberFormat
;
445 newNumberFormat
->setParseIntegerOnly(TRUE
);
447 //----------------------------------------------------------------------
450 DateFormat::setNumberFormat(const NumberFormat
& newNumberFormat
)
452 NumberFormat
* newNumFmtClone
= (NumberFormat
*)newNumberFormat
.clone();
453 if (newNumFmtClone
!= NULL
) {
454 adoptNumberFormat(newNumFmtClone
);
458 //----------------------------------------------------------------------
461 DateFormat::getNumberFormat() const
463 return fNumberFormat
;
466 //----------------------------------------------------------------------
469 DateFormat::adoptTimeZone(TimeZone
* zone
)
471 if (fCalendar
!= NULL
) {
472 fCalendar
->adoptTimeZone(zone
);
475 //----------------------------------------------------------------------
478 DateFormat::setTimeZone(const TimeZone
& zone
)
480 if (fCalendar
!= NULL
) {
481 fCalendar
->setTimeZone(zone
);
485 //----------------------------------------------------------------------
488 DateFormat::getTimeZone() const
490 if (fCalendar
!= NULL
) {
491 return fCalendar
->getTimeZone();
493 // If calendar doesn't exists, create default timezone.
494 // fCalendar is rarely null
495 return *(TimeZone::createDefault());
498 //----------------------------------------------------------------------
501 DateFormat::setLenient(UBool lenient
)
503 if (fCalendar
!= NULL
) {
504 fCalendar
->setLenient(lenient
);
506 UErrorCode status
= U_ZERO_ERROR
;
507 setBooleanAttribute(UDAT_PARSE_ALLOW_WHITESPACE
, lenient
, status
);
508 setBooleanAttribute(UDAT_PARSE_ALLOW_NUMERIC
, lenient
, status
);
511 //----------------------------------------------------------------------
514 DateFormat::isLenient() const
516 UBool lenient
= TRUE
;
517 if (fCalendar
!= NULL
) {
518 lenient
= fCalendar
->isLenient();
520 UErrorCode status
= U_ZERO_ERROR
;
522 && getBooleanAttribute(UDAT_PARSE_ALLOW_WHITESPACE
, status
)
523 && getBooleanAttribute(UDAT_PARSE_ALLOW_NUMERIC
, status
);
527 DateFormat::setCalendarLenient(UBool lenient
)
529 if (fCalendar
!= NULL
) {
530 fCalendar
->setLenient(lenient
);
534 //----------------------------------------------------------------------
537 DateFormat::isCalendarLenient() const
539 if (fCalendar
!= NULL
) {
540 return fCalendar
->isLenient();
542 // fCalendar is rarely null
547 //----------------------------------------------------------------------
550 void DateFormat::setContext(UDisplayContext value
, UErrorCode
& status
)
552 if (U_FAILURE(status
))
554 if ( (UDisplayContextType
)((uint32_t)value
>> 8) == UDISPCTX_TYPE_CAPITALIZATION
) {
555 fCapitalizationContext
= value
;
557 status
= U_ILLEGAL_ARGUMENT_ERROR
;
562 //----------------------------------------------------------------------
565 UDisplayContext
DateFormat::getContext(UDisplayContextType type
, UErrorCode
& status
) const
567 if (U_FAILURE(status
))
568 return (UDisplayContext
)0;
569 if (type
!= UDISPCTX_TYPE_CAPITALIZATION
) {
570 status
= U_ILLEGAL_ARGUMENT_ERROR
;
571 return (UDisplayContext
)0;
573 return fCapitalizationContext
;
577 //----------------------------------------------------------------------
581 DateFormat::setBooleanAttribute(UDateFormatBooleanAttribute attr
,
583 UErrorCode
&status
) {
584 if(!fBoolFlags
.isValidValue(newValue
)) {
585 status
= U_ILLEGAL_ARGUMENT_ERROR
;
587 fBoolFlags
.set(attr
, newValue
);
593 //----------------------------------------------------------------------
596 DateFormat::getBooleanAttribute(UDateFormatBooleanAttribute attr
, UErrorCode
&/*status*/) const {
598 return fBoolFlags
.get(attr
);
603 #endif /* #if !UCONFIG_NO_FORMATTING */