]> git.saurik.com Git - apple/icu.git/blob - icuSources/i18n/unicode/dtitvfmt.h
ICU-64260.0.1.tar.gz
[apple/icu.git] / icuSources / i18n / unicode / dtitvfmt.h
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /********************************************************************************
4 * Copyright (C) 2008-2016, International Business Machines Corporation and
5 * others. All Rights Reserved.
6 *******************************************************************************
7 *
8 * File DTITVFMT.H
9 *
10 *******************************************************************************
11 */
12
13 #ifndef __DTITVFMT_H__
14 #define __DTITVFMT_H__
15
16
17 #include "unicode/utypes.h"
18
19 /**
20 * \file
21 * \brief C++ API: Format and parse date interval in a language-independent manner.
22 */
23
24 #if !UCONFIG_NO_FORMATTING
25
26 #include "unicode/ucal.h"
27 #include "unicode/smpdtfmt.h"
28 #include "unicode/dtintrv.h"
29 #include "unicode/dtitvinf.h"
30 #include "unicode/dtptngen.h"
31 #include "unicode/formattedvalue.h"
32 #include "unicode/udisplaycontext.h"
33 // Apple-specific
34 #include "unicode/udateintervalformat.h"
35
36 #if U_SHOW_CPLUSPLUS_API
37 U_NAMESPACE_BEGIN
38
39
40 class FormattedDateIntervalData;
41 class DateIntervalFormat;
42
43 #ifndef U_HIDE_DRAFT_API
44 /**
45 * An immutable class containing the result of a date interval formatting operation.
46 *
47 * Instances of this class are immutable and thread-safe.
48 *
49 * When calling nextPosition():
50 * The fields are returned from left to right. The special field category
51 * UFIELD_CATEGORY_DATE_INTERVAL_SPAN is used to indicate which datetime
52 * primitives came from which arguments: 0 means fromCalendar, and 1 means
53 * toCalendar. The span category will always occur before the
54 * corresponding fields in UFIELD_CATEGORY_DATE
55 * in the nextPosition() iterator.
56 *
57 * Not intended for public subclassing.
58 *
59 * @draft ICU 64
60 */
61 class U_I18N_API FormattedDateInterval : public UMemory, public FormattedValue {
62 public:
63 /**
64 * Default constructor; makes an empty FormattedDateInterval.
65 * @draft ICU 64
66 */
67 FormattedDateInterval() : fData(nullptr), fErrorCode(U_INVALID_STATE_ERROR) {}
68
69 /**
70 * Move constructor: Leaves the source FormattedDateInterval in an undefined state.
71 * @draft ICU 64
72 */
73 FormattedDateInterval(FormattedDateInterval&& src) U_NOEXCEPT;
74
75 /**
76 * Destruct an instance of FormattedDateInterval.
77 * @draft ICU 64
78 */
79 virtual ~FormattedDateInterval() U_OVERRIDE;
80
81 /** Copying not supported; use move constructor instead. */
82 FormattedDateInterval(const FormattedDateInterval&) = delete;
83
84 /** Copying not supported; use move assignment instead. */
85 FormattedDateInterval& operator=(const FormattedDateInterval&) = delete;
86
87 /**
88 * Move assignment: Leaves the source FormattedDateInterval in an undefined state.
89 * @draft ICU 64
90 */
91 FormattedDateInterval& operator=(FormattedDateInterval&& src) U_NOEXCEPT;
92
93 /** @copydoc FormattedValue::toString() */
94 UnicodeString toString(UErrorCode& status) const U_OVERRIDE;
95
96 /** @copydoc FormattedValue::toTempString() */
97 UnicodeString toTempString(UErrorCode& status) const U_OVERRIDE;
98
99 /** @copydoc FormattedValue::appendTo() */
100 Appendable &appendTo(Appendable& appendable, UErrorCode& status) const U_OVERRIDE;
101
102 /** @copydoc FormattedValue::nextPosition() */
103 UBool nextPosition(ConstrainedFieldPosition& cfpos, UErrorCode& status) const U_OVERRIDE;
104
105 private:
106 FormattedDateIntervalData *fData;
107 UErrorCode fErrorCode;
108 explicit FormattedDateInterval(FormattedDateIntervalData *results)
109 : fData(results), fErrorCode(U_ZERO_ERROR) {}
110 explicit FormattedDateInterval(UErrorCode errorCode)
111 : fData(nullptr), fErrorCode(errorCode) {}
112 friend class DateIntervalFormat;
113 };
114 #endif /* U_HIDE_DRAFT_API */
115
116
117 /**
118 * DateIntervalFormat is a class for formatting and parsing date
119 * intervals in a language-independent manner.
120 * Only formatting is supported, parsing is not supported.
121 *
122 * <P>
123 * Date interval means from one date to another date,
124 * for example, from "Jan 11, 2008" to "Jan 18, 2008".
125 * We introduced class DateInterval to represent it.
126 * DateInterval is a pair of UDate, which is
127 * the standard milliseconds since 24:00 GMT, Jan 1, 1970.
128 *
129 * <P>
130 * DateIntervalFormat formats a DateInterval into
131 * text as compactly as possible.
132 * For example, the date interval format from "Jan 11, 2008" to "Jan 18,. 2008"
133 * is "Jan 11-18, 2008" for English.
134 * And it parses text into DateInterval,
135 * although initially, parsing is not supported.
136 *
137 * <P>
138 * There is no structural information in date time patterns.
139 * For any punctuations and string literals inside a date time pattern,
140 * we do not know whether it is just a separator, or a prefix, or a suffix.
141 * Without such information, so, it is difficult to generate a sub-pattern
142 * (or super-pattern) by algorithm.
143 * So, formatting a DateInterval is pattern-driven. It is very
144 * similar to formatting in SimpleDateFormat.
145 * We introduce class DateIntervalInfo to save date interval
146 * patterns, similar to date time pattern in SimpleDateFormat.
147 *
148 * <P>
149 * Logically, the interval patterns are mappings
150 * from (skeleton, the_largest_different_calendar_field)
151 * to (date_interval_pattern).
152 *
153 * <P>
154 * A skeleton
155 * <ol>
156 * <li>
157 * only keeps the field pattern letter and ignores all other parts
158 * in a pattern, such as space, punctuations, and string literals.
159 * </li>
160 * <li>
161 * hides the order of fields.
162 * </li>
163 * <li>
164 * might hide a field's pattern letter length.
165 * </li>
166 * </ol>
167 *
168 * For those non-digit calendar fields, the pattern letter length is
169 * important, such as MMM, MMMM, and MMMMM; EEE and EEEE,
170 * and the field's pattern letter length is honored.
171 *
172 * For the digit calendar fields, such as M or MM, d or dd, yy or yyyy,
173 * the field pattern length is ignored and the best match, which is defined
174 * in date time patterns, will be returned without honor the field pattern
175 * letter length in skeleton.
176 *
177 * <P>
178 * The calendar fields we support for interval formatting are:
179 * year, month, date, day-of-week, am-pm, hour, hour-of-day, minute, and second
180 * (though we do not currently have specific intervalFormat date for skeletons
181 * with seconds).
182 * Those calendar fields can be defined in the following order:
183 * year > month > date > hour (in day) > minute > second
184 *
185 * The largest different calendar fields between 2 calendars is the
186 * first different calendar field in above order.
187 *
188 * For example: the largest different calendar fields between "Jan 10, 2007"
189 * and "Feb 20, 2008" is year.
190 *
191 * <P>
192 * For other calendar fields, the compact interval formatting is not
193 * supported. And the interval format will be fall back to fall-back
194 * patterns, which is mostly "{date0} - {date1}".
195 *
196 * <P>
197 * There is a set of pre-defined static skeleton strings.
198 * There are pre-defined interval patterns for those pre-defined skeletons
199 * in locales' resource files.
200 * For example, for a skeleton UDAT_YEAR_ABBR_MONTH_DAY, which is &quot;yMMMd&quot;,
201 * in en_US, if the largest different calendar field between date1 and date2
202 * is &quot;year&quot;, the date interval pattern is &quot;MMM d, yyyy - MMM d, yyyy&quot;,
203 * such as &quot;Jan 10, 2007 - Jan 10, 2008&quot;.
204 * If the largest different calendar field between date1 and date2 is &quot;month&quot;,
205 * the date interval pattern is &quot;MMM d - MMM d, yyyy&quot;,
206 * such as &quot;Jan 10 - Feb 10, 2007&quot;.
207 * If the largest different calendar field between date1 and date2 is &quot;day&quot;,
208 * the date interval pattern is &quot;MMM d-d, yyyy&quot;, such as &quot;Jan 10-20, 2007&quot;.
209 *
210 * For date skeleton, the interval patterns when year, or month, or date is
211 * different are defined in resource files.
212 * For time skeleton, the interval patterns when am/pm, or hour, or minute is
213 * different are defined in resource files.
214 *
215 * <P>
216 * If a skeleton is not found in a locale's DateIntervalInfo, which means
217 * the interval patterns for the skeleton is not defined in resource file,
218 * the interval pattern will falls back to the interval "fallback" pattern
219 * defined in resource file.
220 * If the interval "fallback" pattern is not defined, the default fall-back
221 * is "{date0} - {data1}".
222 *
223 * <P>
224 * For the combination of date and time,
225 * The rule to generate interval patterns are:
226 * <ol>
227 * <li>
228 * when the year, month, or day differs, falls back to fall-back
229 * interval pattern, which mostly is the concatenate the two original
230 * expressions with a separator between,
231 * For example, interval pattern from "Jan 10, 2007 10:10 am"
232 * to "Jan 11, 2007 10:10am" is
233 * "Jan 10, 2007 10:10 am - Jan 11, 2007 10:10am"
234 * </li>
235 * <li>
236 * otherwise, present the date followed by the range expression
237 * for the time.
238 * For example, interval pattern from "Jan 10, 2007 10:10 am"
239 * to "Jan 10, 2007 11:10am" is "Jan 10, 2007 10:10 am - 11:10am"
240 * </li>
241 * </ol>
242 *
243 *
244 * <P>
245 * If two dates are the same, the interval pattern is the single date pattern.
246 * For example, interval pattern from "Jan 10, 2007" to "Jan 10, 2007" is
247 * "Jan 10, 2007".
248 *
249 * Or if the presenting fields between 2 dates have the exact same values,
250 * the interval pattern is the single date pattern.
251 * For example, if user only requests year and month,
252 * the interval pattern from "Jan 10, 2007" to "Jan 20, 2007" is "Jan 2007".
253 *
254 * <P>
255 * DateIntervalFormat needs the following information for correct
256 * formatting: time zone, calendar type, pattern, date format symbols,
257 * and date interval patterns.
258 * It can be instantiated in 2 ways:
259 * <ol>
260 * <li>
261 * create an instance using default or given locale plus given skeleton.
262 * Users are encouraged to created date interval formatter this way and
263 * to use the pre-defined skeleton macros, such as
264 * UDAT_YEAR_NUM_MONTH, which consists the calendar fields and
265 * the format style.
266 * </li>
267 * <li>
268 * create an instance using default or given locale plus given skeleton
269 * plus a given DateIntervalInfo.
270 * This factory method is for powerful users who want to provide their own
271 * interval patterns.
272 * Locale provides the timezone, calendar, and format symbols information.
273 * Local plus skeleton provides full pattern information.
274 * DateIntervalInfo provides the date interval patterns.
275 * </li>
276 * </ol>
277 *
278 * <P>
279 * For the calendar field pattern letter, such as G, y, M, d, a, h, H, m, s etc.
280 * DateIntervalFormat uses the same syntax as that of
281 * DateTime format.
282 *
283 * <P>
284 * Code Sample: general usage
285 * <pre>
286 * \code
287 * // the date interval object which the DateIntervalFormat formats on
288 * // and parses into
289 * DateInterval* dtInterval = new DateInterval(1000*3600*24, 1000*3600*24*2);
290 * UErrorCode status = U_ZERO_ERROR;
291 * DateIntervalFormat* dtIntervalFmt = DateIntervalFormat::createInstance(
292 * UDAT_YEAR_MONTH_DAY,
293 * Locale("en", "GB", ""), status);
294 * UnicodeUnicodeString dateIntervalString;
295 * FieldPosition pos = 0;
296 * // formatting
297 * dtIntervalFmt->format(dtInterval, dateIntervalUnicodeString, pos, status);
298 * delete dtIntervalFmt;
299 * \endcode
300 * </pre>
301 */
302 class U_I18N_API DateIntervalFormat : public Format {
303 public:
304
305 /**
306 * Construct a DateIntervalFormat from skeleton and the default locale.
307 *
308 * This is a convenient override of
309 * createInstance(const UnicodeString& skeleton, const Locale& locale,
310 * UErrorCode&)
311 * with the value of locale as default locale.
312 *
313 * @param skeleton the skeleton on which interval format based.
314 * @param status output param set to success/failure code on exit
315 * @return a date time interval formatter which the caller owns.
316 * @stable ICU 4.0
317 */
318 static DateIntervalFormat* U_EXPORT2 createInstance(
319 const UnicodeString& skeleton,
320 UErrorCode& status);
321
322 /**
323 * Construct a DateIntervalFormat from skeleton and a given locale.
324 * <P>
325 * In this factory method,
326 * the date interval pattern information is load from resource files.
327 * Users are encouraged to created date interval formatter this way and
328 * to use the pre-defined skeleton macros.
329 *
330 * <P>
331 * There are pre-defined skeletons (defined in udate.h) having predefined
332 * interval patterns in resource files.
333 * Users are encouraged to use those macros.
334 * For example:
335 * DateIntervalFormat::createInstance(UDAT_MONTH_DAY, status)
336 *
337 * The given Locale provides the interval patterns.
338 * For example, for en_GB, if skeleton is UDAT_YEAR_ABBR_MONTH_WEEKDAY_DAY,
339 * which is "yMMMEEEd",
340 * the interval patterns defined in resource file to above skeleton are:
341 * "EEE, d MMM, yyyy - EEE, d MMM, yyyy" for year differs,
342 * "EEE, d MMM - EEE, d MMM, yyyy" for month differs,
343 * "EEE, d - EEE, d MMM, yyyy" for day differs,
344 * @param skeleton the skeleton on which the interval format is based.
345 * @param locale the given locale
346 * @param status output param set to success/failure code on exit
347 * @return a date time interval formatter which the caller owns.
348 * @stable ICU 4.0
349 * <p>
350 * <h4>Sample code</h4>
351 * \snippet samples/dtitvfmtsample/dtitvfmtsample.cpp dtitvfmtPreDefined1
352 * \snippet samples/dtitvfmtsample/dtitvfmtsample.cpp dtitvfmtPreDefined
353 * <p>
354 */
355
356 static DateIntervalFormat* U_EXPORT2 createInstance(
357 const UnicodeString& skeleton,
358 const Locale& locale,
359 UErrorCode& status);
360
361 /**
362 * Construct a DateIntervalFormat from skeleton
363 * DateIntervalInfo, and default locale.
364 *
365 * This is a convenient override of
366 * createInstance(const UnicodeString& skeleton, const Locale& locale,
367 * const DateIntervalInfo& dtitvinf, UErrorCode&)
368 * with the locale value as default locale.
369 *
370 * @param skeleton the skeleton on which interval format based.
371 * @param dtitvinf the DateIntervalInfo object.
372 * @param status output param set to success/failure code on exit
373 * @return a date time interval formatter which the caller owns.
374 * @stable ICU 4.0
375 */
376 static DateIntervalFormat* U_EXPORT2 createInstance(
377 const UnicodeString& skeleton,
378 const DateIntervalInfo& dtitvinf,
379 UErrorCode& status);
380
381 /**
382 * Construct a DateIntervalFormat from skeleton
383 * a DateIntervalInfo, and the given locale.
384 *
385 * <P>
386 * In this factory method, user provides its own date interval pattern
387 * information, instead of using those pre-defined data in resource file.
388 * This factory method is for powerful users who want to provide their own
389 * interval patterns.
390 * <P>
391 * There are pre-defined skeletons (defined in udate.h) having predefined
392 * interval patterns in resource files.
393 * Users are encouraged to use those macros.
394 * For example:
395 * DateIntervalFormat::createInstance(UDAT_MONTH_DAY, status)
396 *
397 * The DateIntervalInfo provides the interval patterns.
398 * and the DateIntervalInfo ownership remains to the caller.
399 *
400 * User are encouraged to set default interval pattern in DateIntervalInfo
401 * as well, if they want to set other interval patterns ( instead of
402 * reading the interval patterns from resource files).
403 * When the corresponding interval pattern for a largest calendar different
404 * field is not found ( if user not set it ), interval format fallback to
405 * the default interval pattern.
406 * If user does not provide default interval pattern, it fallback to
407 * "{date0} - {date1}"
408 *
409 * @param skeleton the skeleton on which interval format based.
410 * @param locale the given locale
411 * @param dtitvinf the DateIntervalInfo object.
412 * @param status output param set to success/failure code on exit
413 * @return a date time interval formatter which the caller owns.
414 * @stable ICU 4.0
415 * <p>
416 * <h4>Sample code</h4>
417 * \snippet samples/dtitvfmtsample/dtitvfmtsample.cpp dtitvfmtPreDefined1
418 * \snippet samples/dtitvfmtsample/dtitvfmtsample.cpp dtitvfmtCustomized
419 * <p>
420 */
421 static DateIntervalFormat* U_EXPORT2 createInstance(
422 const UnicodeString& skeleton,
423 const Locale& locale,
424 const DateIntervalInfo& dtitvinf,
425 UErrorCode& status);
426
427 /**
428 * Destructor.
429 * @stable ICU 4.0
430 */
431 virtual ~DateIntervalFormat();
432
433 /**
434 * Clone this Format object polymorphically. The caller owns the result and
435 * should delete it when done.
436 * @return A copy of the object.
437 * @stable ICU 4.0
438 */
439 virtual Format* clone(void) const;
440
441 /**
442 * Return true if the given Format objects are semantically equal. Objects
443 * of different subclasses are considered unequal.
444 * @param other the object to be compared with.
445 * @return true if the given Format objects are semantically equal.
446 * @stable ICU 4.0
447 */
448 virtual UBool operator==(const Format& other) const;
449
450 /**
451 * Return true if the given Format objects are not semantically equal.
452 * Objects of different subclasses are considered unequal.
453 * @param other the object to be compared with.
454 * @return true if the given Format objects are not semantically equal.
455 * @stable ICU 4.0
456 */
457 UBool operator!=(const Format& other) const;
458
459
460 using Format::format;
461
462 /**
463 * Format an object to produce a string. This method handles Formattable
464 * objects with a DateInterval type.
465 * If a the Formattable object type is not a DateInterval,
466 * then it returns a failing UErrorCode.
467 *
468 * @param obj The object to format.
469 * Must be a DateInterval.
470 * @param appendTo Output parameter to receive result.
471 * Result is appended to existing contents.
472 * @param fieldPosition On input: an alignment field, if desired.
473 * On output: the offsets of the alignment field.
474 * There may be multiple instances of a given field type
475 * in an interval format; in this case the fieldPosition
476 * offsets refer to the first instance.
477 * @param status Output param filled with success/failure status.
478 * @return Reference to 'appendTo' parameter.
479 * @stable ICU 4.0
480 */
481 virtual UnicodeString& format(const Formattable& obj,
482 UnicodeString& appendTo,
483 FieldPosition& fieldPosition,
484 UErrorCode& status) const ;
485
486
487
488 /**
489 * Format a DateInterval to produce a string.
490 *
491 * @param dtInterval DateInterval to be formatted.
492 * @param appendTo Output parameter to receive result.
493 * Result is appended to existing contents.
494 * @param fieldPosition On input: an alignment field, if desired.
495 * On output: the offsets of the alignment field.
496 * There may be multiple instances of a given field type
497 * in an interval format; in this case the fieldPosition
498 * offsets refer to the first instance.
499 * @param status Output param filled with success/failure status.
500 * @return Reference to 'appendTo' parameter.
501 * @stable ICU 4.0
502 */
503 UnicodeString& format(const DateInterval* dtInterval,
504 UnicodeString& appendTo,
505 FieldPosition& fieldPosition,
506 UErrorCode& status) const ;
507
508 #ifndef U_HIDE_DRAFT_API
509 /**
510 * Format a DateInterval to produce a FormattedDateInterval.
511 *
512 * The FormattedDateInterval exposes field information about the formatted string.
513 *
514 * @param dtInterval DateInterval to be formatted.
515 * @param status Set if an error occurs.
516 * @return A FormattedDateInterval containing the format result.
517 * @draft ICU 64
518 */
519 FormattedDateInterval formatToValue(
520 const DateInterval& dtInterval,
521 UErrorCode& status) const;
522 #endif /* U_HIDE_DRAFT_API */
523
524 /**
525 * Format 2 Calendars to produce a string.
526 *
527 * Note: "fromCalendar" and "toCalendar" are not const,
528 * since calendar is not const in SimpleDateFormat::format(Calendar&),
529 *
530 * @param fromCalendar calendar set to the from date in date interval
531 * to be formatted into date interval string
532 * @param toCalendar calendar set to the to date in date interval
533 * to be formatted into date interval string
534 * @param appendTo Output parameter to receive result.
535 * Result is appended to existing contents.
536 * @param fieldPosition On input: an alignment field, if desired.
537 * On output: the offsets of the alignment field.
538 * There may be multiple instances of a given field type
539 * in an interval format; in this case the fieldPosition
540 * offsets refer to the first instance.
541 * @param status Output param filled with success/failure status.
542 * Caller needs to make sure it is SUCCESS
543 * at the function entrance
544 * @return Reference to 'appendTo' parameter.
545 * @stable ICU 4.0
546 */
547 UnicodeString& format(Calendar& fromCalendar,
548 Calendar& toCalendar,
549 UnicodeString& appendTo,
550 FieldPosition& fieldPosition,
551 UErrorCode& status) const ;
552
553 #ifndef U_HIDE_DRAFT_API
554 /**
555 * Format 2 Calendars to produce a FormattedDateInterval.
556 *
557 * The FormattedDateInterval exposes field information about the formatted string.
558 *
559 * Note: "fromCalendar" and "toCalendar" are not const,
560 * since calendar is not const in SimpleDateFormat::format(Calendar&),
561 *
562 * @param fromCalendar calendar set to the from date in date interval
563 * to be formatted into date interval string
564 * @param toCalendar calendar set to the to date in date interval
565 * to be formatted into date interval string
566 * @param status Set if an error occurs.
567 * @return A FormattedDateInterval containing the format result.
568 * @draft ICU 64
569 */
570 FormattedDateInterval formatToValue(
571 Calendar& fromCalendar,
572 Calendar& toCalendar,
573 UErrorCode& status) const;
574 #endif /* U_HIDE_DRAFT_API */
575
576 /**
577 * Date interval parsing is not supported. Please do not use.
578 * <P>
579 * This method should handle parsing of
580 * date time interval strings into Formattable objects with
581 * DateInterval type, which is a pair of UDate.
582 * <P>
583 * Before calling, set parse_pos.index to the offset you want to start
584 * parsing at in the source. After calling, parse_pos.index is the end of
585 * the text you parsed. If error occurs, index is unchanged.
586 * <P>
587 * When parsing, leading whitespace is discarded (with a successful parse),
588 * while trailing whitespace is left as is.
589 * <P>
590 * See Format::parseObject() for more.
591 *
592 * @param source The string to be parsed into an object.
593 * @param result Formattable to be set to the parse result.
594 * If parse fails, return contents are undefined.
595 * @param parse_pos The position to start parsing at. Since no parsing
596 * is supported, upon return this param is unchanged.
597 * @return A newly created Formattable* object, or NULL
598 * on failure. The caller owns this and should
599 * delete it when done.
600 * @internal ICU 4.0
601 */
602 virtual void parseObject(const UnicodeString& source,
603 Formattable& result,
604 ParsePosition& parse_pos) const;
605
606
607 /**
608 * Gets the date time interval patterns.
609 * @return the date time interval patterns associated with
610 * this date interval formatter.
611 * @stable ICU 4.0
612 */
613 const DateIntervalInfo* getDateIntervalInfo(void) const;
614
615
616 /**
617 * Set the date time interval patterns.
618 * @param newIntervalPatterns the given interval patterns to copy.
619 * @param status output param set to success/failure code on exit
620 * @stable ICU 4.0
621 */
622 void setDateIntervalInfo(const DateIntervalInfo& newIntervalPatterns,
623 UErrorCode& status);
624
625
626 /**
627 * Gets the date formatter. The DateIntervalFormat instance continues to own
628 * the returned DateFormatter object, and will use and possibly modify it
629 * during format operations. In a multi-threaded environment, the returned
630 * DateFormat can only be used if it is certain that no other threads are
631 * concurrently using this DateIntervalFormatter, even for nominally const
632 * functions.
633 *
634 * @return the date formatter associated with this date interval formatter.
635 * @stable ICU 4.0
636 */
637 const DateFormat* getDateFormat(void) const;
638
639 /**
640 * Returns a reference to the TimeZone used by this DateIntervalFormat's calendar.
641 * @return the time zone associated with the calendar of DateIntervalFormat.
642 * @stable ICU 4.8
643 */
644 virtual const TimeZone& getTimeZone(void) const;
645
646 /**
647 * Sets the time zone for the calendar used by this DateIntervalFormat object. The
648 * caller no longer owns the TimeZone object and should not delete it after this call.
649 * @param zoneToAdopt the TimeZone to be adopted.
650 * @stable ICU 4.8
651 */
652 virtual void adoptTimeZone(TimeZone* zoneToAdopt);
653
654 /**
655 * Sets the time zone for the calendar used by this DateIntervalFormat object.
656 * @param zone the new time zone.
657 * @stable ICU 4.8
658 */
659 virtual void setTimeZone(const TimeZone& zone);
660
661 /**
662 * Change attributes for the DateIntervalFormat object.
663 * @param attr
664 * The attribute to change.
665 * @param value
666 * The new value for the attribute.
667 * @param status
668 * A UErrorCode to receive any errors.
669 * @internal
670 */
671 virtual void setAttribute(UDateIntervalFormatAttribute attr,
672 UDateIntervalFormatAttributeValue value,
673 UErrorCode &status);
674
675 /**
676 * Set a particular UDisplayContext value in the formatter, such as
677 * UDISPCTX_CAPITALIZATION_FOR_STANDALONE. This causes the formatted
678 * result to be capitalized appropriately for the context in which
679 * it is intended to be used, considering both the locale and the
680 * type of field at the beginning of the formatted result.
681 * @param value The UDisplayContext value to set.
682 * @param status Input/output status. If at entry this indicates a failure
683 * status, the function will do nothing; otherwise this will be
684 * updated with any new status from the function.
685 * @draft ICU 65
686 */
687 virtual void setContext(UDisplayContext value, UErrorCode& status);
688
689 /**
690 * Get the formatter's UDisplayContext value for the specified UDisplayContextType,
691 * such as UDISPCTX_TYPE_CAPITALIZATION.
692 * @param type The UDisplayContextType whose value to return
693 * @param status Input/output status. If at entry this indicates a failure
694 * status, the function will do nothing; otherwise this will be
695 * updated with any new status from the function.
696 * @return The UDisplayContextValue for the specified type.
697 * @draft ICU 65
698 */
699 virtual UDisplayContext getContext(UDisplayContextType type, UErrorCode& status) const;
700
701 /**
702 * Return the class ID for this class. This is useful only for comparing to
703 * a return value from getDynamicClassID(). For example:
704 * <pre>
705 * . Base* polymorphic_pointer = createPolymorphicObject();
706 * . if (polymorphic_pointer->getDynamicClassID() ==
707 * . erived::getStaticClassID()) ...
708 * </pre>
709 * @return The class ID for all objects of this class.
710 * @stable ICU 4.0
711 */
712 static UClassID U_EXPORT2 getStaticClassID(void);
713
714 /**
715 * Returns a unique class ID POLYMORPHICALLY. Pure virtual override. This
716 * method is to implement a simple version of RTTI, since not all C++
717 * compilers support genuine RTTI. Polymorphic operator==() and clone()
718 * methods call this method.
719 *
720 * @return The class ID for this object. All objects of a
721 * given class have the same class ID. Objects of
722 * other classes have different class IDs.
723 * @stable ICU 4.0
724 */
725 virtual UClassID getDynamicClassID(void) const;
726
727 protected:
728
729 /**
730 * Copy constructor.
731 * @stable ICU 4.0
732 */
733 DateIntervalFormat(const DateIntervalFormat&);
734
735 /**
736 * Assignment operator.
737 * @stable ICU 4.0
738 */
739 DateIntervalFormat& operator=(const DateIntervalFormat&);
740
741 private:
742
743 /*
744 * This is for ICU internal use only. Please do not use.
745 * Save the interval pattern information.
746 * Interval pattern consists of 2 single date patterns and the separator.
747 * For example, interval pattern "MMM d - MMM d, yyyy" consists
748 * a single date pattern "MMM d", another single date pattern "MMM d, yyyy",
749 * and a separator "-".
750 * The pattern is divided into 2 parts. For above example,
751 * the first part is "MMM d - ", and the second part is "MMM d, yyyy".
752 * Also, the first date appears in an interval pattern could be
753 * the earlier date or the later date.
754 * And such information is saved in the interval pattern as well.
755 */
756 struct PatternInfo {
757 UnicodeString firstPart;
758 UnicodeString secondPart;
759 /**
760 * Whether the first date in interval pattern is later date or not.
761 * Fallback format set the default ordering.
762 * And for a particular interval pattern, the order can be
763 * overriden by prefixing the interval pattern with "latestFirst:" or
764 * "earliestFirst:"
765 * For example, given 2 date, Jan 10, 2007 to Feb 10, 2007.
766 * if the fallback format is "{0} - {1}",
767 * and the pattern is "d MMM - d MMM yyyy", the interval format is
768 * "10 Jan - 10 Feb, 2007".
769 * If the pattern is "latestFirst:d MMM - d MMM yyyy",
770 * the interval format is "10 Feb - 10 Jan, 2007"
771 */
772 UBool laterDateFirst;
773 };
774
775
776 /**
777 * default constructor
778 * @internal (private)
779 */
780 DateIntervalFormat();
781
782 /**
783 * Construct a DateIntervalFormat from DateFormat,
784 * a DateIntervalInfo, and skeleton.
785 * DateFormat provides the timezone, calendar,
786 * full pattern, and date format symbols information.
787 * It should be a SimpleDateFormat object which
788 * has a pattern in it.
789 * the DateIntervalInfo provides the interval patterns.
790 *
791 * Note: the DateIntervalFormat takes ownership of both
792 * DateFormat and DateIntervalInfo objects.
793 * Caller should not delete them.
794 *
795 * @param locale the locale of this date interval formatter.
796 * @param dtItvInfo the DateIntervalInfo object to be adopted.
797 * @param skeleton the skeleton of the date formatter
798 * @param status output param set to success/failure code on exit
799 */
800 DateIntervalFormat(const Locale& locale, DateIntervalInfo* dtItvInfo,
801 const UnicodeString* skeleton, UErrorCode& status);
802
803
804 /**
805 * Construct a DateIntervalFormat from DateFormat
806 * and a DateIntervalInfo.
807 *
808 * It is a wrapper of the constructor.
809 *
810 * @param locale the locale of this date interval formatter.
811 * @param dtitvinf the DateIntervalInfo object to be adopted.
812 * @param skeleton the skeleton of this formatter.
813 * @param status Output param set to success/failure code.
814 * @return a date time interval formatter which the caller owns.
815 */
816 static DateIntervalFormat* U_EXPORT2 create(const Locale& locale,
817 DateIntervalInfo* dtitvinf,
818 const UnicodeString* skeleton,
819 UErrorCode& status);
820
821 /**
822 * Below are for generating interval patterns local to the formatter
823 */
824
825 /** Like fallbackFormat, but only formats the range part of the fallback. */
826 void fallbackFormatRange(
827 Calendar& fromCalendar,
828 Calendar& toCalendar,
829 UnicodeString& appendTo,
830 int8_t& firstIndex,
831 FieldPositionHandler& fphandler,
832 UErrorCode& status) const;
833
834 /**
835 * Format 2 Calendars using fall-back interval pattern
836 *
837 * The full pattern used in this fall-back format is the
838 * full pattern of the date formatter.
839 *
840 * gFormatterMutex must already be locked when calling this function.
841 *
842 * @param fromCalendar calendar set to the from date in date interval
843 * to be formatted into date interval string
844 * @param toCalendar calendar set to the to date in date interval
845 * to be formatted into date interval string
846 * @param fromToOnSameDay TRUE iff from and to dates are on the same day
847 * (any difference is in ampm/hours or below)
848 * @param appendTo Output parameter to receive result.
849 * Result is appended to existing contents.
850 * @param firstIndex See formatImpl for more information.
851 * @param fphandler See formatImpl for more information.
852 * @param status output param set to success/failure code on exit
853 * @return Reference to 'appendTo' parameter.
854 * @internal (private)
855 */
856 UnicodeString& fallbackFormat(Calendar& fromCalendar,
857 Calendar& toCalendar,
858 UBool fromToOnSameDay,
859 UnicodeString& appendTo,
860 int8_t& firstIndex,
861 FieldPositionHandler& fphandler,
862 UErrorCode& status) const;
863
864
865
866 /**
867 * Initialize interval patterns locale to this formatter
868 *
869 * This code is a bit complicated since
870 * 1. the interval patterns saved in resource bundle files are interval
871 * patterns based on date or time only.
872 * It does not have interval patterns based on both date and time.
873 * Interval patterns on both date and time are algorithm generated.
874 *
875 * For example, it has interval patterns on skeleton "dMy" and "hm",
876 * but it does not have interval patterns on skeleton "dMyhm".
877 *
878 * The rule to generate interval patterns for both date and time skeleton are
879 * 1) when the year, month, or day differs, concatenate the two original
880 * expressions with a separator between,
881 * For example, interval pattern from "Jan 10, 2007 10:10 am"
882 * to "Jan 11, 2007 10:10am" is
883 * "Jan 10, 2007 10:10 am - Jan 11, 2007 10:10am"
884 *
885 * 2) otherwise, present the date followed by the range expression
886 * for the time.
887 * For example, interval pattern from "Jan 10, 2007 10:10 am"
888 * to "Jan 10, 2007 11:10am" is
889 * "Jan 10, 2007 10:10 am - 11:10am"
890 *
891 * 2. even a pattern does not request a certain calendar field,
892 * the interval pattern needs to include such field if such fields are
893 * different between 2 dates.
894 * For example, a pattern/skeleton is "hm", but the interval pattern
895 * includes year, month, and date when year, month, and date differs.
896 *
897 *
898 * @param status output param set to success/failure code on exit
899 */
900 void initializePattern(UErrorCode& status);
901
902
903
904 /**
905 * Set fall back interval pattern given a calendar field,
906 * a skeleton, and a date time pattern generator.
907 * @param field the largest different calendar field
908 * @param skeleton a skeleton
909 * @param status output param set to success/failure code on exit
910 */
911 void setFallbackPattern(UCalendarDateFields field,
912 const UnicodeString& skeleton,
913 UErrorCode& status);
914
915
916
917 /**
918 * get separated date and time skeleton from a combined skeleton.
919 *
920 * The difference between date skeleton and normalizedDateSkeleton are:
921 * 1. both 'y' and 'd' are appeared only once in normalizeDateSkeleton
922 * 2. 'E' and 'EE' are normalized into 'EEE'
923 * 3. 'MM' is normalized into 'M'
924 *
925 ** the difference between time skeleton and normalizedTimeSkeleton are:
926 * 1. both 'H' and 'h' are normalized as 'h' in normalized time skeleton,
927 * 2. 'a' is omitted in normalized time skeleton.
928 * 3. there is only one appearance for 'h', 'm','v', 'z' in normalized time
929 * skeleton
930 *
931 *
932 * @param skeleton given combined skeleton.
933 * @param date Output parameter for date only skeleton.
934 * @param normalizedDate Output parameter for normalized date only
935 *
936 * @param time Output parameter for time only skeleton.
937 * @param normalizedTime Output parameter for normalized time only
938 * skeleton.
939 *
940 */
941 static void U_EXPORT2 getDateTimeSkeleton(const UnicodeString& skeleton,
942 UnicodeString& date,
943 UnicodeString& normalizedDate,
944 UnicodeString& time,
945 UnicodeString& normalizedTime);
946
947
948
949 /**
950 * Generate date or time interval pattern from resource,
951 * and set them into the interval pattern locale to this formatter.
952 *
953 * It needs to handle the following:
954 * 1. need to adjust field width.
955 * For example, the interval patterns saved in DateIntervalInfo
956 * includes "dMMMy", but not "dMMMMy".
957 * Need to get interval patterns for dMMMMy from dMMMy.
958 * Another example, the interval patterns saved in DateIntervalInfo
959 * includes "hmv", but not "hmz".
960 * Need to get interval patterns for "hmz' from 'hmv'
961 *
962 * 2. there might be no pattern for 'y' differ for skeleton "Md",
963 * in order to get interval patterns for 'y' differ,
964 * need to look for it from skeleton 'yMd'
965 *
966 * @param dateSkeleton normalized date skeleton
967 * @param timeSkeleton normalized time skeleton
968 * @return whether the resource is found for the skeleton.
969 * TRUE if interval pattern found for the skeleton,
970 * FALSE otherwise.
971 */
972 UBool setSeparateDateTimePtn(const UnicodeString& dateSkeleton,
973 const UnicodeString& timeSkeleton);
974
975
976
977
978 /**
979 * Generate interval pattern from existing resource
980 *
981 * It not only save the interval patterns,
982 * but also return the extended skeleton and its best match skeleton.
983 *
984 * @param field largest different calendar field
985 * @param skeleton skeleton
986 * @param bestSkeleton the best match skeleton which has interval pattern
987 * defined in resource
988 * @param differenceInfo the difference between skeleton and best skeleton
989 * 0 means the best matched skeleton is the same as input skeleton
990 * 1 means the fields are the same, but field width are different
991 * 2 means the only difference between fields are v/z,
992 * -1 means there are other fields difference
993 *
994 * @param extendedSkeleton extended skeleton
995 * @param extendedBestSkeleton extended best match skeleton
996 * @return whether the interval pattern is found
997 * through extending skeleton or not.
998 * TRUE if interval pattern is found by
999 * extending skeleton, FALSE otherwise.
1000 */
1001 UBool setIntervalPattern(UCalendarDateFields field,
1002 const UnicodeString* skeleton,
1003 const UnicodeString* bestSkeleton,
1004 int8_t differenceInfo,
1005 UnicodeString* extendedSkeleton = NULL,
1006 UnicodeString* extendedBestSkeleton = NULL);
1007
1008 /**
1009 * Adjust field width in best match interval pattern to match
1010 * the field width in input skeleton.
1011 *
1012 * TODO (xji) make a general solution
1013 * The adjusting rule can be:
1014 * 1. always adjust
1015 * 2. never adjust
1016 * 3. default adjust, which means adjust according to the following rules
1017 * 3.1 always adjust string, such as MMM and MMMM
1018 * 3.2 never adjust between string and numeric, such as MM and MMM
1019 * 3.3 always adjust year
1020 * 3.4 do not adjust 'd', 'h', or 'm' if h presents
1021 * 3.5 do not adjust 'M' if it is numeric(?)
1022 *
1023 * Since date interval format is well-formed format,
1024 * date and time skeletons are normalized previously,
1025 * till this stage, the adjust here is only "adjust strings, such as MMM
1026 * and MMMM, EEE and EEEE.
1027 *
1028 * @param inputSkeleton the input skeleton
1029 * @param bestMatchSkeleton the best match skeleton
1030 * @param bestMatchIntervalPattern the best match interval pattern
1031 * @param differenceInfo the difference between 2 skeletons
1032 * 1 means only field width differs
1033 * 2 means v/z exchange
1034 * @param adjustedIntervalPattern adjusted interval pattern
1035 */
1036 static void U_EXPORT2 adjustFieldWidth(
1037 const UnicodeString& inputSkeleton,
1038 const UnicodeString& bestMatchSkeleton,
1039 const UnicodeString& bestMatchIntervalPattern,
1040 int8_t differenceInfo,
1041 UnicodeString& adjustedIntervalPattern);
1042
1043 /**
1044 * Concat a single date pattern with a time interval pattern,
1045 * set it into the intervalPatterns, while field is time field.
1046 * This is used to handle time interval patterns on skeleton with
1047 * both time and date. Present the date followed by
1048 * the range expression for the time.
1049 * @param format date and time format
1050 * @param datePattern date pattern
1051 * @param field time calendar field: AM_PM, HOUR, MINUTE
1052 * @param status output param set to success/failure code on exit
1053 */
1054 void concatSingleDate2TimeInterval(UnicodeString& format,
1055 const UnicodeString& datePattern,
1056 UCalendarDateFields field,
1057 UErrorCode& status);
1058
1059 /**
1060 * check whether a calendar field present in a skeleton.
1061 * @param field calendar field need to check
1062 * @param skeleton given skeleton on which to check the calendar field
1063 * @return true if field present in a skeleton.
1064 */
1065 static UBool U_EXPORT2 fieldExistsInSkeleton(UCalendarDateFields field,
1066 const UnicodeString& skeleton);
1067
1068
1069 /**
1070 * Split interval patterns into 2 part.
1071 * @param intervalPattern interval pattern
1072 * @return the index in interval pattern which split the pattern into 2 part
1073 */
1074 static int32_t U_EXPORT2 splitPatternInto2Part(const UnicodeString& intervalPattern);
1075
1076
1077 /**
1078 * Break interval patterns as 2 part and save them into pattern info.
1079 * @param field calendar field
1080 * @param intervalPattern interval pattern
1081 */
1082 void setIntervalPattern(UCalendarDateFields field,
1083 const UnicodeString& intervalPattern);
1084
1085
1086 /**
1087 * Break interval patterns as 2 part and save them into pattern info.
1088 * @param field calendar field
1089 * @param intervalPattern interval pattern
1090 * @param laterDateFirst whether later date appear first in interval pattern
1091 */
1092 void setIntervalPattern(UCalendarDateFields field,
1093 const UnicodeString& intervalPattern,
1094 UBool laterDateFirst);
1095
1096
1097 /**
1098 * Set pattern information.
1099 *
1100 * @param field calendar field
1101 * @param firstPart the first part in interval pattern
1102 * @param secondPart the second part in interval pattern
1103 * @param laterDateFirst whether the first date in intervalPattern
1104 * is earlier date or later date
1105 */
1106 void setPatternInfo(UCalendarDateFields field,
1107 const UnicodeString* firstPart,
1108 const UnicodeString* secondPart,
1109 UBool laterDateFirst);
1110
1111 /**
1112 * Format 2 Calendars to produce a string.
1113 * Implementation of the similar public format function.
1114 * Must be called with gFormatterMutex already locked.
1115 *
1116 * Note: "fromCalendar" and "toCalendar" are not const,
1117 * since calendar is not const in SimpleDateFormat::format(Calendar&),
1118 *
1119 * @param fromCalendar calendar set to the from date in date interval
1120 * to be formatted into date interval string
1121 * @param toCalendar calendar set to the to date in date interval
1122 * to be formatted into date interval string
1123 * @param appendTo Output parameter to receive result.
1124 * Result is appended to existing contents.
1125 * @param firstIndex 0 if the first output date is fromCalendar;
1126 * 1 if it corresponds to toCalendar;
1127 * -1 if there is only one date printed.
1128 * @param fphandler Handler for field position information.
1129 * The fields will be from the UDateFormatField enum.
1130 * @param status Output param filled with success/failure status.
1131 * Caller needs to make sure it is SUCCESS
1132 * at the function entrance
1133 * @return Reference to 'appendTo' parameter.
1134 * @internal (private)
1135 */
1136 UnicodeString& formatImpl(Calendar& fromCalendar,
1137 Calendar& toCalendar,
1138 UnicodeString& appendTo,
1139 int8_t& firstIndex,
1140 FieldPositionHandler& fphandler,
1141 UErrorCode& status) const ;
1142
1143 /** Version of formatImpl for DateInterval. */
1144 UnicodeString& formatIntervalImpl(const DateInterval& dtInterval,
1145 UnicodeString& appendTo,
1146 int8_t& firstIndex,
1147 FieldPositionHandler& fphandler,
1148 UErrorCode& status) const;
1149
1150
1151 // from calendar field to pattern letter
1152 static const char16_t fgCalendarFieldToPatternLetter[];
1153
1154
1155 /**
1156 * The interval patterns for this locale.
1157 */
1158 DateIntervalInfo* fInfo;
1159
1160 /**
1161 * The DateFormat object used to format single pattern
1162 */
1163 SimpleDateFormat* fDateFormat;
1164
1165 /**
1166 * The 2 calendars with the from and to date.
1167 * could re-use the calendar in fDateFormat,
1168 * but keeping 2 calendars make it clear and clean.
1169 */
1170 Calendar* fFromCalendar;
1171 Calendar* fToCalendar;
1172
1173 Locale fLocale;
1174
1175 /**
1176 * Following are interval information relevant (locale) to this formatter.
1177 */
1178 UnicodeString fSkeleton;
1179 PatternInfo fIntervalPatterns[DateIntervalInfo::kIPI_MAX_INDEX];
1180
1181 /**
1182 * Patterns for fallback formatting.
1183 */
1184 UnicodeString* fDatePattern;
1185 UnicodeString* fTimePattern;
1186 UnicodeString* fDateTimeFormat;
1187
1188 /**
1189 * Atttributes
1190 */
1191 int32_t fMinimizeType;
1192
1193 UDisplayContext fCapitalizationContext;
1194 };
1195
1196 inline UBool
1197 DateIntervalFormat::operator!=(const Format& other) const {
1198 return !operator==(other);
1199 }
1200
1201 U_NAMESPACE_END
1202 #endif // U_SHOW_CPLUSPLUS_API
1203
1204 #endif /* #if !UCONFIG_NO_FORMATTING */
1205
1206 #endif // _DTITVFMT_H__
1207 //eof