Debian Codegraph @

intl/components/src/calendar/ICU4XCalendar.h

raw
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_calendar_ICU4XCalendar_h_
5 #define intl_components_calendar_ICU4XCalendar_h_
6
7 #include "mozilla/intl/calendar/ICU4XUniquePtr.h"
8 #include "mozilla/intl/calendar/ISODate.h"
9 #include "mozilla/intl/calendar/MonthCode.h"
10
11 #include <memory>
12 #include <mutex>
13 #include <stdint.h>
14 #include <string_view>
15
16 #include "unicode/calendar.h"
17 #include "unicode/locid.h"
18 #include "unicode/timezone.h"
19 #include "unicode/utypes.h"
20
21 #include "ICU4XAnyCalendarKind.h"
22
23 namespace mozilla::intl::calendar {
24
25 /**
26 * Abstract class to implement icu::Calendar using ICU4X.
27 */
28 class ICU4XCalendar : public icu::Calendar {
29 mutable UniqueICU4XCalendar calendar_{};
30 mutable std::unique_ptr<icu::Calendar> fallback_{};
31 capi::ICU4XAnyCalendarKind kind_;
32
33 protected:
34 ICU4XCalendar(capi::ICU4XAnyCalendarKind kind, const icu::Locale& locale,
35 UErrorCode& success);
36 ICU4XCalendar(capi::ICU4XAnyCalendarKind kind, const icu::TimeZone& timeZone,
37 const icu::Locale& locale, UErrorCode& success);
38 ICU4XCalendar(const ICU4XCalendar& other);
39
40 /**
41 * Get or create the underlying ICU4X calendar.
42 */
43 capi::ICU4XCalendar* getICU4XCalendar(UErrorCode& status) const;
44
45 /**
46 * Get or create the ICU4C fallback calendar implementation.
47 */
48 icu::Calendar* getFallbackCalendar(UErrorCode& status) const;
49
50 protected:
51 /**
52 * Return the ICU4X era name for the given extended year.
53 */
54 virtual std::string_view eraName(int32_t extendedYear) const = 0;
55
56 /**
57 * Return true if this calendar contains any leap months.
58 */
59 virtual bool hasLeapMonths() const = 0;
60
61 /**
62 * Return true if this calendar contains the requested month code.
63 */
64 virtual bool hasMonthCode(MonthCode monthCode) const = 0;
65
66 /**
67 * Subclasses can request to use the ICU4C fallback calendar.
68 *
69 * Can be removed when <https://github.com/unicode-org/icu4x/issues/4917> is
70 * fixed.
71 */
72 virtual bool requiresFallbackForExtendedYear(int32_t year) const = 0;
73 virtual bool requiresFallbackForGregorianYear(int32_t year) const = 0;
74
75 protected:
76 static constexpr int32_t kEpochStartAsJulianDay =
77 2440588; // January 1, 1970 (Gregorian)
78
79 /**
80 * Return the month code of |date|.
81 */
82 static MonthCode monthCodeFrom(const capi::ICU4XDate* date,
83 UErrorCode& status);
84
85 /**
86 * Create a new ICU4X date object from an ISO date.
87 */
88 UniqueICU4XDate createICU4XDate(const ISODate& date,
89 UErrorCode& status) const;
90
91 /**
92 * Create a new ICU4X date object from a calendar date.
93 */
94 UniqueICU4XDate createICU4XDate(const CalendarDate& date,
95 UErrorCode& status) const;
96
97 public:
98 ICU4XCalendar() = delete;
99 virtual ~ICU4XCalendar();
100
101 const char* getTemporalMonthCode(UErrorCode& status) const override;
102 void setTemporalMonthCode(const char* code, UErrorCode& status) override;
103
104 void add(UCalendarDateFields field, int32_t amount,
105 UErrorCode& status) override;
106 void add(EDateFields field, int32_t amount, UErrorCode& status) override;
107 void roll(UCalendarDateFields field, int32_t amount,
108 UErrorCode& status) override;
109 void roll(EDateFields field, int32_t amount, UErrorCode& status) override;
110
111 protected:
112 int32_t internalGetMonth(int32_t defaultValue,
113 UErrorCode& status) const override;
114 int32_t internalGetMonth(UErrorCode& status) const override;
115
116 int64_t handleComputeMonthStart(int32_t extendedYear, int32_t month,
117 UBool useMonth,
118 UErrorCode& status) const override;
119 int32_t handleGetMonthLength(int32_t extendedYear, int32_t month,
120 UErrorCode& status) const override;
121 int32_t handleGetYearLength(int32_t extendedYear,
122 UErrorCode& status) const override;
123
124 int32_t handleGetExtendedYear(UErrorCode& status) override;
125
126 protected:
127 /**
128 * handleComputeFields implementation using the ICU4C fallback calendar.
129 */
130 void handleComputeFieldsFromFallback(int32_t julianDay, UErrorCode& status);
131 };
132
133 /**
134 * `IMPL_SYSTEM_DEFAULT_CENTURY` is internal to "i18n/gregoimp.h", so we have
135 * to provider our own helper class to implement default centuries.
136 */
137 template <class Calendar, class Locale>
138 class SystemDefaultCentury {
139 mutable UDate start_ = DBL_MIN;
140 mutable int32_t startYear_ = -1;
141 mutable std::once_flag init_{};
142
143 void initialize() const {
144 UErrorCode status = U_ZERO_ERROR;
145 Calendar calendar(Locale::identifier, status);
146 if (U_FAILURE(status)) {
147 return;
148 }
149 calendar.setTime(icu::Calendar::getNow(), status);
150 calendar.add(UCAL_EXTENDED_YEAR, -80, status);
151 start_ = calendar.getTime(status);
152 startYear_ = calendar.get(UCAL_YEAR, status);
153 }
154
155 public:
156 UDate start() const {
157 std::call_once(init_, [this] { initialize(); });
158 return start_;
159 }
160 int32_t startYear() const {
161 std::call_once(init_, [this] { initialize(); });
162 return startYear_;
163 }
164 };
165
166 } // namespace mozilla::intl::calendar
167
168 #endif