DateIntervalFormat.h (4715B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 #ifndef intl_components_DateIntervalFormat_h_ 5 #define intl_components_DateIntervalFormat_h_ 6 7 #include "mozilla/intl/Calendar.h" 8 #include "mozilla/intl/DateTimePart.h" 9 #include "mozilla/intl/ICU4CGlue.h" 10 #include "mozilla/intl/ICUError.h" 11 #include "mozilla/Result.h" 12 #include "mozilla/Span.h" 13 #include "mozilla/UniquePtr.h" 14 15 #include "unicode/udateintervalformat.h" 16 #include "unicode/utypes.h" 17 18 namespace mozilla::intl { 19 class Calendar; 20 class DateTimeFormat; 21 22 using AutoFormattedDateInterval = 23 AutoFormattedResult<UFormattedDateInterval, udtitvfmt_openResult, 24 udtitvfmt_resultAsValue, udtitvfmt_closeResult>; 25 26 /** 27 * This component is a Mozilla-focused API for the date range formatting 28 * provided by ICU. This DateIntervalFormat class helps to format the range 29 * between two date-time values. 30 * 31 * https://tc39.es/ecma402/#sec-formatdatetimerange 32 * https://tc39.es/ecma402/#sec-formatdatetimerangetoparts 33 */ 34 class DateIntervalFormat final { 35 public: 36 /** 37 * Create a DateIntervalFormat object from locale, skeleton and time zone. 38 * The format of skeleton can be found in [1]. 39 * 40 * Note: Skeleton will be removed in the future. 41 * 42 * [1]: https://unicode.org/reports/tr35/tr35-dates.html#Date_Format_Patterns 43 */ 44 static Result<UniquePtr<DateIntervalFormat>, ICUError> TryCreate( 45 Span<const char> aLocale, Span<const char16_t> aSkeleton, 46 Span<const char16_t> aTimeZone); 47 48 ~DateIntervalFormat(); 49 50 /** 51 * Format a date-time range between two Calendar objects. 52 * 53 * DateIntervalFormat cannot be changed to use a proleptic Gregorian 54 * calendar, so use this method if the start date is before the Gregorian 55 * calendar is introduced(October 15, 1582), otherwise use TryFormatDateTime 56 * instead. 57 * 58 * The result will be stored in aFormatted, caller can use 59 * AutoFormattedDateInterval::ToSpan() to get the formatted string, or pass 60 * the aFormatted to TryFormattedToParts to get the parts vector. 61 * 62 * aPracticallyEqual will be set to true if the date times of the two 63 * calendars are equal. 64 */ 65 ICUResult TryFormatCalendar(const Calendar& aStart, const Calendar& aEnd, 66 AutoFormattedDateInterval& aFormatted, 67 bool* aPracticallyEqual) const; 68 69 /** 70 * Format a date-time range between two Unix epoch times in milliseconds. 71 * 72 * The result will be stored in aFormatted, caller can use 73 * AutoFormattedDateInterval::ToSpan() to get the formatted string, or pass 74 * the aFormatted to TryFormattedToParts to get the parts vector. 75 * 76 * aPracticallyEqual will be set to true if the date times of the two 77 * Unix epoch times are equal. 78 */ 79 ICUResult TryFormatDateTime(double aStart, double aEnd, 80 AutoFormattedDateInterval& aFormatted, 81 bool* aPracticallyEqual) const; 82 83 /** 84 * Format a date-time range between two Unix epoch times in milliseconds. 85 * 86 * The result will be stored in aFormatted, caller can use 87 * AutoFormattedDateInterval::ToSpan() to get the formatted string, or pass 88 * the aFormatted to TryFormattedToParts to get the parts vector. 89 * 90 * aPracticallyEqual will be set to true if the date times of the two 91 * Unix epoch times are equal. 92 */ 93 ICUResult TryFormatDateTime(double aStart, double aEnd, 94 const DateTimeFormat* aDateTimeFormat, 95 AutoFormattedDateInterval& aFormatted, 96 bool* aPracticallyEqual) const; 97 98 /** 99 * Convert the formatted DateIntervalFormat into several parts. 100 * 101 * The caller get the formatted result from either TryFormatCalendar, or 102 * TryFormatDateTime methods, and instantiate the DateTimePartVector. This 103 * method will generate the parts and insert them into the vector. 104 * 105 * See: 106 * https://tc39.es/ecma402/#sec-formatdatetimerangetoparts 107 */ 108 ICUResult TryFormattedToParts(const AutoFormattedDateInterval& aFormatted, 109 DateTimePartVector& aParts) const; 110 111 private: 112 DateIntervalFormat() = delete; 113 explicit DateIntervalFormat(UDateIntervalFormat* aDif) 114 : mDateIntervalFormat(aDif) {} 115 DateIntervalFormat(const DateIntervalFormat&) = delete; 116 DateIntervalFormat& operator=(const DateIntervalFormat&) = delete; 117 118 ICUPointer<UDateIntervalFormat> mDateIntervalFormat = 119 ICUPointer<UDateIntervalFormat>(nullptr); 120 }; 121 } // namespace mozilla::intl 122 123 #endif