]>
git.saurik.com Git - apple/icu.git/blob - icuSources/i18n/datefmt.cpp
2 *******************************************************************************
3 * Copyright (C) 1997-2004, 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"
28 #if defined( U_DEBUG_CALSVC ) || defined (U_DEBUG_CAL)
32 // *****************************************************************************
34 // *****************************************************************************
38 DateFormat::DateFormat()
44 //----------------------------------------------------------------------
46 DateFormat::DateFormat(const DateFormat
& other
)
54 //----------------------------------------------------------------------
56 DateFormat
& DateFormat::operator=(const DateFormat
& other
)
63 fCalendar
= other
.fCalendar
->clone();
67 if(other
.fNumberFormat
) {
68 fNumberFormat
= (NumberFormat
*)other
.fNumberFormat
->clone();
76 //----------------------------------------------------------------------
78 DateFormat::~DateFormat()
84 //----------------------------------------------------------------------
87 DateFormat::operator==(const Format
& other
) const
89 // This protected comparison operator should only be called by subclasses
90 // which have confirmed that the other object being compared against is
91 // an instance of a sublcass of DateFormat. THIS IS IMPORTANT.
93 // Format::operator== guarantees that this cast is safe
94 DateFormat
* fmt
= (DateFormat
*)&other
;
96 return (this == fmt
) ||
97 (Format::operator==(other
) &&
98 fCalendar
&&(fCalendar
->isEquivalentTo(*fmt
->fCalendar
)) &&
99 (fNumberFormat
&& *fNumberFormat
== *fmt
->fNumberFormat
));
102 //----------------------------------------------------------------------
105 DateFormat::format(const Formattable
& obj
,
106 UnicodeString
& appendTo
,
107 FieldPosition
& fieldPosition
,
108 UErrorCode
& status
) const
110 if (U_FAILURE(status
)) return appendTo
;
112 // if the type of the Formattable is double or long, treat it as if it were a Date
114 switch (obj
.getType())
116 case Formattable::kDate
:
117 date
= obj
.getDate();
119 case Formattable::kDouble
:
120 date
= (UDate
)obj
.getDouble();
122 case Formattable::kLong
:
123 date
= (UDate
)obj
.getLong();
126 status
= U_ILLEGAL_ARGUMENT_ERROR
;
131 //if (fieldPosition.getBeginIndex() == fieldPosition.getEndIndex())
132 // status = U_ILLEGAL_ARGUMENT_ERROR;
134 return format(date
, appendTo
, fieldPosition
);
137 //----------------------------------------------------------------------
140 DateFormat::format(UDate date
, UnicodeString
& appendTo
, FieldPosition
& fieldPosition
) const {
141 if (fCalendar
!= NULL
) {
142 // Use our calendar instance
143 UErrorCode ec
= U_ZERO_ERROR
;
144 fCalendar
->setTime(date
, ec
);
146 return format(*fCalendar
, appendTo
, fieldPosition
);
152 //----------------------------------------------------------------------
155 DateFormat::format(UDate date
, UnicodeString
& appendTo
) const
157 // Note that any error information is just lost. That's okay
158 // for this convenience method.
159 FieldPosition
fpos(0);
160 return format(date
, appendTo
, fpos
);
163 //----------------------------------------------------------------------
166 DateFormat::parse(const UnicodeString
& text
,
167 ParsePosition
& pos
) const
169 if (fCalendar
!= NULL
) {
170 int32_t start
= pos
.getIndex();
172 parse(text
, *fCalendar
, pos
);
173 if (pos
.getIndex() != start
) {
174 UErrorCode ec
= U_ZERO_ERROR
;
175 UDate d
= fCalendar
->getTime(ec
);
177 return d
; // Successful function exit
179 // We arrive here if fCalendar is non-lenient and there
180 // is an out-of-range field. We don't know which field
181 // was illegal so we set the error index to the start.
183 pos
.setErrorIndex(start
);
186 return 0; // Error return UDate is 0 (the epoch)
189 //----------------------------------------------------------------------
192 DateFormat::parse(const UnicodeString
& text
,
193 UErrorCode
& status
) const
195 if (U_FAILURE(status
)) return 0;
197 ParsePosition
pos(0);
198 UDate result
= parse(text
, pos
);
199 if (pos
.getIndex() == 0) {
200 #if defined (U_DEBUG_CAL)
201 fprintf(stderr
, "%s:%d - - failed to parse - err index %d\n"
202 , __FILE__
, __LINE__
, pos
.getErrorIndex() );
204 status
= U_ILLEGAL_ARGUMENT_ERROR
;
209 //----------------------------------------------------------------------
212 DateFormat::parseObject(const UnicodeString
& source
,
214 ParsePosition
& pos
) const
216 result
.setDate(parse(source
, pos
));
219 //----------------------------------------------------------------------
221 DateFormat
* U_EXPORT2
222 DateFormat::createTimeInstance(DateFormat::EStyle style
,
223 const Locale
& aLocale
)
225 return create(style
, kNone
, aLocale
);
228 //----------------------------------------------------------------------
230 DateFormat
* U_EXPORT2
231 DateFormat::createDateInstance(DateFormat::EStyle style
,
232 const Locale
& aLocale
)
234 // +4 to set the correct index for getting data out of
238 style
= (EStyle
) (style
+ kDateOffset
);
240 return create(kNone
, (EStyle
) (style
), aLocale
);
243 //----------------------------------------------------------------------
245 DateFormat
* U_EXPORT2
246 DateFormat::createDateTimeInstance(EStyle dateStyle
,
248 const Locale
& aLocale
)
250 if(dateStyle
!= kNone
)
252 dateStyle
= (EStyle
) (dateStyle
+ kDateOffset
);
254 return create(timeStyle
, dateStyle
, aLocale
);
257 //----------------------------------------------------------------------
259 DateFormat
* U_EXPORT2
260 DateFormat::createInstance()
262 return create(kShort
, (EStyle
) (kShort
+ kDateOffset
), Locale::getDefault());
265 //----------------------------------------------------------------------
267 DateFormat
* U_EXPORT2
268 DateFormat::create(EStyle timeStyle
, EStyle dateStyle
, const Locale
& locale
)
270 // Try to create a SimpleDateFormat of the desired style.
271 UErrorCode status
= U_ZERO_ERROR
;
272 SimpleDateFormat
*f
= new SimpleDateFormat(timeStyle
, dateStyle
, locale
, status
);
273 if (U_SUCCESS(status
)) return f
;
276 // If that fails, try to create a format using the default pattern and
277 // the DateFormatSymbols for this locale.
278 status
= U_ZERO_ERROR
;
279 f
= new SimpleDateFormat(locale
, status
);
280 if (U_SUCCESS(status
)) return f
;
283 // This should never really happen, because the preceding constructor
284 // should always succeed. If the resource data is unavailable, a last
285 // resort object should be returned.
289 //----------------------------------------------------------------------
291 const Locale
* U_EXPORT2
292 DateFormat::getAvailableLocales(int32_t& count
)
294 // Get the list of installed locales.
295 // Even if root has the correct date format for this locale,
296 // it's still a valid locale (we don't worry about data fallbacks).
297 return Locale::getAvailableLocales(count
);
300 //----------------------------------------------------------------------
303 DateFormat::adoptCalendar(Calendar
* newCalendar
)
306 fCalendar
= newCalendar
;
309 //----------------------------------------------------------------------
311 DateFormat::setCalendar(const Calendar
& newCalendar
)
313 adoptCalendar(newCalendar
.clone());
316 //----------------------------------------------------------------------
319 DateFormat::getCalendar() const
324 //----------------------------------------------------------------------
327 DateFormat::adoptNumberFormat(NumberFormat
* newNumberFormat
)
329 delete fNumberFormat
;
330 fNumberFormat
= newNumberFormat
;
331 newNumberFormat
->setParseIntegerOnly(TRUE
);
333 //----------------------------------------------------------------------
336 DateFormat::setNumberFormat(const NumberFormat
& newNumberFormat
)
338 adoptNumberFormat((NumberFormat
*)newNumberFormat
.clone());
341 //----------------------------------------------------------------------
344 DateFormat::getNumberFormat() const
346 return fNumberFormat
;
349 //----------------------------------------------------------------------
352 DateFormat::adoptTimeZone(TimeZone
* zone
)
354 fCalendar
->adoptTimeZone(zone
);
356 //----------------------------------------------------------------------
359 DateFormat::setTimeZone(const TimeZone
& zone
)
361 fCalendar
->setTimeZone(zone
);
364 //----------------------------------------------------------------------
367 DateFormat::getTimeZone() const
369 return fCalendar
->getTimeZone();
372 //----------------------------------------------------------------------
375 DateFormat::setLenient(UBool lenient
)
377 fCalendar
->setLenient(lenient
);
380 //----------------------------------------------------------------------
383 DateFormat::isLenient() const
385 return fCalendar
->isLenient();
390 #endif /* #if !UCONFIG_NO_FORMATTING */