]>
git.saurik.com Git - apple/icu.git/blob - icuSources/i18n/datefmt.cpp
2 *******************************************************************************
3 * Copyright (C) 1997-2010, 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"
33 #if defined( U_DEBUG_CALSVC ) || defined (U_DEBUG_CAL)
37 // *****************************************************************************
39 // *****************************************************************************
43 DateFormat::DateFormat()
49 //----------------------------------------------------------------------
51 DateFormat::DateFormat(const DateFormat
& other
)
59 //----------------------------------------------------------------------
61 DateFormat
& DateFormat::operator=(const DateFormat
& other
)
68 fCalendar
= other
.fCalendar
->clone();
72 if(other
.fNumberFormat
) {
73 fNumberFormat
= (NumberFormat
*)other
.fNumberFormat
->clone();
81 //----------------------------------------------------------------------
83 DateFormat::~DateFormat()
89 //----------------------------------------------------------------------
92 DateFormat::operator==(const Format
& other
) const
94 // This protected comparison operator should only be called by subclasses
95 // which have confirmed that the other object being compared against is
96 // an instance of a sublcass of DateFormat. THIS IS IMPORTANT.
98 // Format::operator== guarantees that this cast is safe
99 DateFormat
* fmt
= (DateFormat
*)&other
;
101 return (this == fmt
) ||
102 (Format::operator==(other
) &&
103 fCalendar
&&(fCalendar
->isEquivalentTo(*fmt
->fCalendar
)) &&
104 (fNumberFormat
&& *fNumberFormat
== *fmt
->fNumberFormat
));
107 //----------------------------------------------------------------------
110 DateFormat::format(const Formattable
& obj
,
111 UnicodeString
& appendTo
,
112 FieldPosition
& fieldPosition
,
113 UErrorCode
& status
) const
115 if (U_FAILURE(status
)) return appendTo
;
117 // if the type of the Formattable is double or long, treat it as if it were a Date
119 switch (obj
.getType())
121 case Formattable::kDate
:
122 date
= obj
.getDate();
124 case Formattable::kDouble
:
125 date
= (UDate
)obj
.getDouble();
127 case Formattable::kLong
:
128 date
= (UDate
)obj
.getLong();
131 status
= U_ILLEGAL_ARGUMENT_ERROR
;
136 //if (fieldPosition.getBeginIndex() == fieldPosition.getEndIndex())
137 // status = U_ILLEGAL_ARGUMENT_ERROR;
139 return format(date
, appendTo
, fieldPosition
);
142 //----------------------------------------------------------------------
145 DateFormat::format(const Formattable
& obj
,
146 UnicodeString
& appendTo
,
147 FieldPositionIterator
* posIter
,
148 UErrorCode
& status
) const
150 if (U_FAILURE(status
)) return appendTo
;
152 // if the type of the Formattable is double or long, treat it as if it were a Date
154 switch (obj
.getType())
156 case Formattable::kDate
:
157 date
= obj
.getDate();
159 case Formattable::kDouble
:
160 date
= (UDate
)obj
.getDouble();
162 case Formattable::kLong
:
163 date
= (UDate
)obj
.getLong();
166 status
= U_ILLEGAL_ARGUMENT_ERROR
;
171 //if (fieldPosition.getBeginIndex() == fieldPosition.getEndIndex())
172 // status = U_ILLEGAL_ARGUMENT_ERROR;
174 return format(date
, appendTo
, posIter
, status
);
177 //----------------------------------------------------------------------
179 // Default implementation for backwards compatibility, subclasses should implement.
181 DateFormat::format(Calendar
& /* unused cal */,
182 UnicodeString
& appendTo
,
183 FieldPositionIterator
* /* unused posIter */,
184 UErrorCode
& status
) const {
185 if (U_SUCCESS(status
)) {
186 status
= U_UNSUPPORTED_ERROR
;
191 //----------------------------------------------------------------------
194 DateFormat::format(UDate date
, UnicodeString
& appendTo
, FieldPosition
& fieldPosition
) const {
195 if (fCalendar
!= NULL
) {
196 // Use our calendar instance
197 UErrorCode ec
= U_ZERO_ERROR
;
198 fCalendar
->setTime(date
, ec
);
200 return format(*fCalendar
, appendTo
, fieldPosition
);
206 //----------------------------------------------------------------------
209 DateFormat::format(UDate date
, UnicodeString
& appendTo
, FieldPositionIterator
* posIter
,
210 UErrorCode
& status
) const {
211 if (fCalendar
!= NULL
) {
212 fCalendar
->setTime(date
, status
);
213 if (U_SUCCESS(status
)) {
214 return format(*fCalendar
, appendTo
, posIter
, status
);
220 //----------------------------------------------------------------------
223 DateFormat::format(UDate date
, UnicodeString
& appendTo
) const
225 // Note that any error information is just lost. That's okay
226 // for this convenience method.
227 FieldPosition
fpos(0);
228 return format(date
, appendTo
, fpos
);
231 //----------------------------------------------------------------------
234 DateFormat::parse(const UnicodeString
& text
,
235 ParsePosition
& pos
) const
237 UDate d
= 0; // Error return UDate is 0 (the epoch)
238 if (fCalendar
!= NULL
) {
239 int32_t start
= pos
.getIndex();
241 // Parse may update TimeZone used by the calendar.
242 TimeZone
*tzsav
= (TimeZone
*)fCalendar
->getTimeZone().clone();
245 parse(text
, *fCalendar
, pos
);
246 if (pos
.getIndex() != start
) {
247 UErrorCode ec
= U_ZERO_ERROR
;
248 d
= fCalendar
->getTime(ec
);
250 // We arrive here if fCalendar is non-lenient and there
251 // is an out-of-range field. We don't know which field
252 // was illegal so we set the error index to the start.
254 pos
.setErrorIndex(start
);
260 fCalendar
->adoptTimeZone(tzsav
);
265 //----------------------------------------------------------------------
268 DateFormat::parse(const UnicodeString
& text
,
269 UErrorCode
& status
) const
271 if (U_FAILURE(status
)) return 0;
273 ParsePosition
pos(0);
274 UDate result
= parse(text
, pos
);
275 if (pos
.getIndex() == 0) {
276 #if defined (U_DEBUG_CAL)
277 fprintf(stderr
, "%s:%d - - failed to parse - err index %d\n"
278 , __FILE__
, __LINE__
, pos
.getErrorIndex() );
280 status
= U_ILLEGAL_ARGUMENT_ERROR
;
285 //----------------------------------------------------------------------
288 DateFormat::parseObject(const UnicodeString
& source
,
290 ParsePosition
& pos
) const
292 result
.setDate(parse(source
, pos
));
295 //----------------------------------------------------------------------
297 DateFormat
* U_EXPORT2
298 DateFormat::createTimeInstance(DateFormat::EStyle style
,
299 const Locale
& aLocale
)
301 return create(style
, kNone
, aLocale
);
304 //----------------------------------------------------------------------
306 DateFormat
* U_EXPORT2
307 DateFormat::createDateInstance(DateFormat::EStyle style
,
308 const Locale
& aLocale
)
310 // +4 to set the correct index for getting data out of
314 style
= (EStyle
) (style
+ kDateOffset
);
316 return create(kNone
, (EStyle
) (style
), aLocale
);
319 //----------------------------------------------------------------------
321 DateFormat
* U_EXPORT2
322 DateFormat::createDateTimeInstance(EStyle dateStyle
,
324 const Locale
& aLocale
)
326 if(dateStyle
!= kNone
)
328 dateStyle
= (EStyle
) (dateStyle
+ kDateOffset
);
330 return create(timeStyle
, dateStyle
, aLocale
);
333 //----------------------------------------------------------------------
335 DateFormat
* U_EXPORT2
336 DateFormat::createInstance()
338 return create(kShort
, (EStyle
) (kShort
+ kDateOffset
), Locale::getDefault());
341 //----------------------------------------------------------------------
343 DateFormat
* U_EXPORT2
344 DateFormat::create(EStyle timeStyle
, EStyle dateStyle
, const Locale
& locale
)
346 UErrorCode status
= U_ZERO_ERROR
;
349 int32_t count
= locale
.getKeywordValue("compat", buffer
, sizeof(buffer
), status
);
351 // if the locale has "@compat=host", create a host-specific DateFormat...
352 if (count
> 0 && uprv_strcmp(buffer
, "host") == 0) {
353 Win32DateFormat
*f
= new Win32DateFormat(timeStyle
, dateStyle
, locale
, status
);
355 if (U_SUCCESS(status
)) {
364 if(/*((timeStyle!=UDAT_NONE)&&(timeStyle & UDAT_RELATIVE)) || */((dateStyle
!=kNone
)&&((dateStyle
-kDateOffset
) & UDAT_RELATIVE
))) {
365 RelativeDateFormat
*r
= new RelativeDateFormat((UDateFormatStyle
)timeStyle
, (UDateFormatStyle
)(dateStyle
-kDateOffset
), locale
, status
);
366 if(U_SUCCESS(status
)) return r
;
368 status
= U_ZERO_ERROR
;
371 // Try to create a SimpleDateFormat of the desired style.
372 SimpleDateFormat
*f
= new SimpleDateFormat(timeStyle
, dateStyle
, locale
, status
);
373 if (U_SUCCESS(status
)) return f
;
376 // If that fails, try to create a format using the default pattern and
377 // the DateFormatSymbols for this locale.
378 status
= U_ZERO_ERROR
;
379 f
= new SimpleDateFormat(locale
, status
);
380 if (U_SUCCESS(status
)) return f
;
383 // This should never really happen, because the preceding constructor
384 // should always succeed. If the resource data is unavailable, a last
385 // resort object should be returned.
389 //----------------------------------------------------------------------
391 const Locale
* U_EXPORT2
392 DateFormat::getAvailableLocales(int32_t& count
)
394 // Get the list of installed locales.
395 // Even if root has the correct date format for this locale,
396 // it's still a valid locale (we don't worry about data fallbacks).
397 return Locale::getAvailableLocales(count
);
400 //----------------------------------------------------------------------
403 DateFormat::adoptCalendar(Calendar
* newCalendar
)
406 fCalendar
= newCalendar
;
409 //----------------------------------------------------------------------
411 DateFormat::setCalendar(const Calendar
& newCalendar
)
413 Calendar
* newCalClone
= newCalendar
.clone();
414 if (newCalClone
!= NULL
) {
415 adoptCalendar(newCalClone
);
419 //----------------------------------------------------------------------
422 DateFormat::getCalendar() const
427 //----------------------------------------------------------------------
430 DateFormat::adoptNumberFormat(NumberFormat
* newNumberFormat
)
432 delete fNumberFormat
;
433 fNumberFormat
= newNumberFormat
;
434 newNumberFormat
->setParseIntegerOnly(TRUE
);
436 //----------------------------------------------------------------------
439 DateFormat::setNumberFormat(const NumberFormat
& newNumberFormat
)
441 NumberFormat
* newNumFmtClone
= (NumberFormat
*)newNumberFormat
.clone();
442 if (newNumFmtClone
!= NULL
) {
443 adoptNumberFormat(newNumFmtClone
);
447 //----------------------------------------------------------------------
450 DateFormat::getNumberFormat() const
452 return fNumberFormat
;
455 //----------------------------------------------------------------------
458 DateFormat::adoptTimeZone(TimeZone
* zone
)
460 if (fCalendar
!= NULL
) {
461 fCalendar
->adoptTimeZone(zone
);
464 //----------------------------------------------------------------------
467 DateFormat::setTimeZone(const TimeZone
& zone
)
469 if (fCalendar
!= NULL
) {
470 fCalendar
->setTimeZone(zone
);
474 //----------------------------------------------------------------------
477 DateFormat::getTimeZone() const
479 if (fCalendar
!= NULL
) {
480 return fCalendar
->getTimeZone();
482 // If calendar doesn't exists, create default timezone.
483 // fCalendar is rarely null
484 return *(TimeZone::createDefault());
487 //----------------------------------------------------------------------
490 DateFormat::setLenient(UBool lenient
)
492 if (fCalendar
!= NULL
) {
493 fCalendar
->setLenient(lenient
);
497 //----------------------------------------------------------------------
500 DateFormat::isLenient() const
502 if (fCalendar
!= NULL
) {
503 return fCalendar
->isLenient();
505 // fCalendar is rarely null
511 #endif /* #if !UCONFIG_NO_FORMATTING */