Debian Codegraph @

intl/icu/source/i18n/unicode/dtitvfmt.h

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