Debian Codegraph @

intl/icu_capi/bindings/cpp/ICU4XDateTime.hpp

raw
1 #ifndef ICU4XDateTime_HPP
2 #define ICU4XDateTime_HPP
3 #include <stdint.h>
4 #include <stddef.h>
5 #include <stdbool.h>
6 #include <algorithm>
7 #include <memory>
8 #include <variant>
9 #include <optional>
10 #include "diplomat_runtime.hpp"
11
12 #include "ICU4XDateTime.h"
13
14 class ICU4XCalendar;
15 class ICU4XDateTime;
16 #include "ICU4XError.hpp"
17 class ICU4XDate;
18 class ICU4XTime;
19 class ICU4XIsoDateTime;
20 #include "ICU4XIsoWeekday.hpp"
21 class ICU4XWeekCalculator;
22 struct ICU4XWeekOf;
23
24 /**
25 * A destruction policy for using ICU4XDateTime with std::unique_ptr.
26 */
27 struct ICU4XDateTimeDeleter {
28 void operator()(capi::ICU4XDateTime* l) const noexcept {
29 capi::ICU4XDateTime_destroy(l);
30 }
31 };
32
33 /**
34 * An ICU4X DateTime object capable of containing a date and time for any calendar.
35 *
36 * See the [Rust documentation for `DateTime`](https://docs.rs/icu/latest/icu/calendar/struct.DateTime.html) for more information.
37 */
38 class ICU4XDateTime {
39 public:
40
41 /**
42 * Creates a new [`ICU4XDateTime`] representing the ISO date and time
43 * given but in a given calendar
44 *
45 * See the [Rust documentation for `new_from_iso`](https://docs.rs/icu/latest/icu/struct.DateTime.html#method.new_from_iso) for more information.
46 */
47 static diplomat::result<ICU4XDateTime, ICU4XError> create_from_iso_in_calendar(int32_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t minute, uint8_t second, uint32_t nanosecond, const ICU4XCalendar& calendar);
48
49 /**
50 * Creates a new [`ICU4XDateTime`] from the given codes, which are interpreted in the given calendar system
51 *
52 * See the [Rust documentation for `try_new_from_codes`](https://docs.rs/icu/latest/icu/calendar/struct.DateTime.html#method.try_new_from_codes) for more information.
53 */
54 static diplomat::result<ICU4XDateTime, ICU4XError> create_from_codes_in_calendar(const std::string_view era_code, int32_t year, const std::string_view month_code, uint8_t day, uint8_t hour, uint8_t minute, uint8_t second, uint32_t nanosecond, const ICU4XCalendar& calendar);
55
56 /**
57 * Creates a new [`ICU4XDateTime`] from an [`ICU4XDate`] and [`ICU4XTime`] object
58 *
59 * See the [Rust documentation for `new`](https://docs.rs/icu/latest/icu/calendar/struct.DateTime.html#method.new) for more information.
60 */
61 static ICU4XDateTime create_from_date_and_time(const ICU4XDate& date, const ICU4XTime& time);
62
63 /**
64 * Gets a copy of the date contained in this object
65 *
66 * See the [Rust documentation for `date`](https://docs.rs/icu/latest/icu/calendar/struct.DateTime.html#structfield.date) for more information.
67 */
68 ICU4XDate date() const;
69
70 /**
71 * Gets the time contained in this object
72 *
73 * See the [Rust documentation for `time`](https://docs.rs/icu/latest/icu/calendar/struct.DateTime.html#structfield.time) for more information.
74 */
75 ICU4XTime time() const;
76
77 /**
78 * Converts this date to ISO
79 *
80 * See the [Rust documentation for `to_iso`](https://docs.rs/icu/latest/icu/calendar/struct.DateTime.html#method.to_iso) for more information.
81 */
82 ICU4XIsoDateTime to_iso() const;
83
84 /**
85 * Convert this datetime to one in a different calendar
86 *
87 * See the [Rust documentation for `to_calendar`](https://docs.rs/icu/latest/icu/calendar/struct.DateTime.html#method.to_calendar) for more information.
88 */
89 ICU4XDateTime to_calendar(const ICU4XCalendar& calendar) const;
90
91 /**
92 * Returns the hour in this time
93 *
94 * See the [Rust documentation for `hour`](https://docs.rs/icu/latest/icu/calendar/struct.Time.html#structfield.hour) for more information.
95 */
96 uint8_t hour() const;
97
98 /**
99 * Returns the minute in this time
100 *
101 * See the [Rust documentation for `minute`](https://docs.rs/icu/latest/icu/calendar/struct.Time.html#structfield.minute) for more information.
102 */
103 uint8_t minute() const;
104
105 /**
106 * Returns the second in this time
107 *
108 * See the [Rust documentation for `second`](https://docs.rs/icu/latest/icu/calendar/struct.Time.html#structfield.second) for more information.
109 */
110 uint8_t second() const;
111
112 /**
113 * Returns the nanosecond in this time
114 *
115 * See the [Rust documentation for `nanosecond`](https://docs.rs/icu/latest/icu/calendar/struct.Time.html#structfield.nanosecond) for more information.
116 */
117 uint32_t nanosecond() const;
118
119 /**
120 * Returns the 1-indexed day in the year for this date
121 *
122 * See the [Rust documentation for `day_of_year_info`](https://docs.rs/icu/latest/icu/calendar/struct.Date.html#method.day_of_year_info) for more information.
123 */
124 uint16_t day_of_year() const;
125
126 /**
127 * Returns the 1-indexed day in the month for this date
128 *
129 * See the [Rust documentation for `day_of_month`](https://docs.rs/icu/latest/icu/calendar/struct.Date.html#method.day_of_month) for more information.
130 */
131 uint32_t day_of_month() const;
132
133 /**
134 * Returns the day in the week for this day
135 *
136 * See the [Rust documentation for `day_of_week`](https://docs.rs/icu/latest/icu/calendar/struct.Date.html#method.day_of_week) for more information.
137 */
138 ICU4XIsoWeekday day_of_week() const;
139
140 /**
141 * Returns the week number in this month, 1-indexed, based on what
142 * is considered the first day of the week (often a locale preference).
143 *
144 * `first_weekday` can be obtained via `first_weekday()` on [`ICU4XWeekCalculator`]
145 *
146 * See the [Rust documentation for `week_of_month`](https://docs.rs/icu/latest/icu/calendar/struct.Date.html#method.week_of_month) for more information.
147 */
148 uint32_t week_of_month(ICU4XIsoWeekday first_weekday) const;
149
150 /**
151 * Returns the week number in this year, using week data
152 *
153 * See the [Rust documentation for `week_of_year`](https://docs.rs/icu/latest/icu/calendar/struct.Date.html#method.week_of_year) for more information.
154 */
155 diplomat::result<ICU4XWeekOf, ICU4XError> week_of_year(const ICU4XWeekCalculator& calculator) const;
156
157 /**
158 * Returns 1-indexed number of the month of this date in its year
159 *
160 * Note that for lunar calendars this may not lead to the same month
161 * having the same ordinal month across years; use month_code if you care
162 * about month identity.
163 *
164 * See the [Rust documentation for `month`](https://docs.rs/icu/latest/icu/calendar/struct.Date.html#method.month) for more information.
165 */
166 uint32_t ordinal_month() const;
167
168 /**
169 * Returns the month code for this date. Typically something
170 * like "M01", "M02", but can be more complicated for lunar calendars.
171 *
172 * See the [Rust documentation for `month`](https://docs.rs/icu/latest/icu/calendar/struct.Date.html#method.month) for more information.
173 */
174 template<typename W> diplomat::result<std::monostate, ICU4XError> month_code_to_writeable(W& write) const;
175
176 /**
177 * Returns the month code for this date. Typically something
178 * like "M01", "M02", but can be more complicated for lunar calendars.
179 *
180 * See the [Rust documentation for `month`](https://docs.rs/icu/latest/icu/calendar/struct.Date.html#method.month) for more information.
181 */
182 diplomat::result<std::string, ICU4XError> month_code() const;
183
184 /**
185 * Returns the year number in the current era for this date
186 *
187 * See the [Rust documentation for `year`](https://docs.rs/icu/latest/icu/calendar/struct.Date.html#method.year) for more information.
188 */
189 int32_t year_in_era() const;
190
191 /**
192 * Returns the era for this date,
193 *
194 * See the [Rust documentation for `year`](https://docs.rs/icu/latest/icu/calendar/struct.Date.html#method.year) for more information.
195 */
196 template<typename W> diplomat::result<std::monostate, ICU4XError> era_to_writeable(W& write) const;
197
198 /**
199 * Returns the era for this date,
200 *
201 * See the [Rust documentation for `year`](https://docs.rs/icu/latest/icu/calendar/struct.Date.html#method.year) for more information.
202 */
203 diplomat::result<std::string, ICU4XError> era() const;
204
205 /**
206 * Returns the number of months in the year represented by this date
207 *
208 * See the [Rust documentation for `months_in_year`](https://docs.rs/icu/latest/icu/calendar/struct.Date.html#method.months_in_year) for more information.
209 */
210 uint8_t months_in_year() const;
211
212 /**
213 * Returns the number of days in the month represented by this date
214 *
215 * See the [Rust documentation for `days_in_month`](https://docs.rs/icu/latest/icu/calendar/struct.Date.html#method.days_in_month) for more information.
216 */
217 uint8_t days_in_month() const;
218
219 /**
220 * Returns the number of days in the year represented by this date
221 *
222 * See the [Rust documentation for `days_in_year`](https://docs.rs/icu/latest/icu/calendar/struct.Date.html#method.days_in_year) for more information.
223 */
224 uint16_t days_in_year() const;
225
226 /**
227 * Returns the [`ICU4XCalendar`] object backing this date
228 *
229 * See the [Rust documentation for `calendar`](https://docs.rs/icu/latest/icu/calendar/struct.Date.html#method.calendar) for more information.
230 */
231 ICU4XCalendar calendar() const;
232 inline const capi::ICU4XDateTime* AsFFI() const { return this->inner.get(); }
233 inline capi::ICU4XDateTime* AsFFIMut() { return this->inner.get(); }
234 inline explicit ICU4XDateTime(capi::ICU4XDateTime* i) : inner(i) {}
235 ICU4XDateTime() = default;
236 ICU4XDateTime(ICU4XDateTime&&) noexcept = default;
237 ICU4XDateTime& operator=(ICU4XDateTime&& other) noexcept = default;
238 private:
239 std::unique_ptr<capi::ICU4XDateTime, ICU4XDateTimeDeleter> inner;
240 };
241
242 #include "ICU4XCalendar.hpp"
243 #include "ICU4XDate.hpp"
244 #include "ICU4XTime.hpp"
245 #include "ICU4XIsoDateTime.hpp"
246 #include "ICU4XWeekCalculator.hpp"
247 #include "ICU4XWeekOf.hpp"
248
249 inline diplomat::result<ICU4XDateTime, ICU4XError> ICU4XDateTime::create_from_iso_in_calendar(int32_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t minute, uint8_t second, uint32_t nanosecond, const ICU4XCalendar& calendar) {
250 auto diplomat_result_raw_out_value = capi::ICU4XDateTime_create_from_iso_in_calendar(year, month, day, hour, minute, second, nanosecond, calendar.AsFFI());
251 diplomat::result<ICU4XDateTime, ICU4XError> diplomat_result_out_value;
252 if (diplomat_result_raw_out_value.is_ok) {
253 diplomat_result_out_value = diplomat::Ok<ICU4XDateTime>(ICU4XDateTime(diplomat_result_raw_out_value.ok));
254 } else {
255 diplomat_result_out_value = diplomat::Err<ICU4XError>(static_cast<ICU4XError>(diplomat_result_raw_out_value.err));
256 }
257 return diplomat_result_out_value;
258 }
259 inline diplomat::result<ICU4XDateTime, ICU4XError> ICU4XDateTime::create_from_codes_in_calendar(const std::string_view era_code, int32_t year, const std::string_view month_code, uint8_t day, uint8_t hour, uint8_t minute, uint8_t second, uint32_t nanosecond, const ICU4XCalendar& calendar) {
260 auto diplomat_result_raw_out_value = capi::ICU4XDateTime_create_from_codes_in_calendar(era_code.data(), era_code.size(), year, month_code.data(), month_code.size(), day, hour, minute, second, nanosecond, calendar.AsFFI());
261 diplomat::result<ICU4XDateTime, ICU4XError> diplomat_result_out_value;
262 if (diplomat_result_raw_out_value.is_ok) {
263 diplomat_result_out_value = diplomat::Ok<ICU4XDateTime>(ICU4XDateTime(diplomat_result_raw_out_value.ok));
264 } else {
265 diplomat_result_out_value = diplomat::Err<ICU4XError>(static_cast<ICU4XError>(diplomat_result_raw_out_value.err));
266 }
267 return diplomat_result_out_value;
268 }
269 inline ICU4XDateTime ICU4XDateTime::create_from_date_and_time(const ICU4XDate& date, const ICU4XTime& time) {
270 return ICU4XDateTime(capi::ICU4XDateTime_create_from_date_and_time(date.AsFFI(), time.AsFFI()));
271 }
272 inline ICU4XDate ICU4XDateTime::date() const {
273 return ICU4XDate(capi::ICU4XDateTime_date(this->inner.get()));
274 }
275 inline ICU4XTime ICU4XDateTime::time() const {
276 return ICU4XTime(capi::ICU4XDateTime_time(this->inner.get()));
277 }
278 inline ICU4XIsoDateTime ICU4XDateTime::to_iso() const {
279 return ICU4XIsoDateTime(capi::ICU4XDateTime_to_iso(this->inner.get()));
280 }
281 inline ICU4XDateTime ICU4XDateTime::to_calendar(const ICU4XCalendar& calendar) const {
282 return ICU4XDateTime(capi::ICU4XDateTime_to_calendar(this->inner.get(), calendar.AsFFI()));
283 }
284 inline uint8_t ICU4XDateTime::hour() const {
285 return capi::ICU4XDateTime_hour(this->inner.get());
286 }
287 inline uint8_t ICU4XDateTime::minute() const {
288 return capi::ICU4XDateTime_minute(this->inner.get());
289 }
290 inline uint8_t ICU4XDateTime::second() const {
291 return capi::ICU4XDateTime_second(this->inner.get());
292 }
293 inline uint32_t ICU4XDateTime::nanosecond() const {
294 return capi::ICU4XDateTime_nanosecond(this->inner.get());
295 }
296 inline uint16_t ICU4XDateTime::day_of_year() const {
297 return capi::ICU4XDateTime_day_of_year(this->inner.get());
298 }
299 inline uint32_t ICU4XDateTime::day_of_month() const {
300 return capi::ICU4XDateTime_day_of_month(this->inner.get());
301 }
302 inline ICU4XIsoWeekday ICU4XDateTime::day_of_week() const {
303 return static_cast<ICU4XIsoWeekday>(capi::ICU4XDateTime_day_of_week(this->inner.get()));
304 }
305 inline uint32_t ICU4XDateTime::week_of_month(ICU4XIsoWeekday first_weekday) const {
306 return capi::ICU4XDateTime_week_of_month(this->inner.get(), static_cast<capi::ICU4XIsoWeekday>(first_weekday));
307 }
308 inline diplomat::result<ICU4XWeekOf, ICU4XError> ICU4XDateTime::week_of_year(const ICU4XWeekCalculator& calculator) const {
309 auto diplomat_result_raw_out_value = capi::ICU4XDateTime_week_of_year(this->inner.get(), calculator.AsFFI());
310 diplomat::result<ICU4XWeekOf, ICU4XError> diplomat_result_out_value;
311 if (diplomat_result_raw_out_value.is_ok) {
312 capi::ICU4XWeekOf diplomat_raw_struct_out_value = diplomat_result_raw_out_value.ok;
313 diplomat_result_out_value = diplomat::Ok<ICU4XWeekOf>(ICU4XWeekOf{ .week = std::move(diplomat_raw_struct_out_value.week), .unit = std::move(static_cast<ICU4XWeekRelativeUnit>(diplomat_raw_struct_out_value.unit)) });
314 } else {
315 diplomat_result_out_value = diplomat::Err<ICU4XError>(static_cast<ICU4XError>(diplomat_result_raw_out_value.err));
316 }
317 return diplomat_result_out_value;
318 }
319 inline uint32_t ICU4XDateTime::ordinal_month() const {
320 return capi::ICU4XDateTime_ordinal_month(this->inner.get());
321 }
322 template<typename W> inline diplomat::result<std::monostate, ICU4XError> ICU4XDateTime::month_code_to_writeable(W& write) const {
323 capi::DiplomatWriteable write_writer = diplomat::WriteableTrait<W>::Construct(write);
324 auto diplomat_result_raw_out_value = capi::ICU4XDateTime_month_code(this->inner.get(), &write_writer);
325 diplomat::result<std::monostate, ICU4XError> diplomat_result_out_value;
326 if (diplomat_result_raw_out_value.is_ok) {
327 diplomat_result_out_value = diplomat::Ok<std::monostate>(std::monostate());
328 } else {
329 diplomat_result_out_value = diplomat::Err<ICU4XError>(static_cast<ICU4XError>(diplomat_result_raw_out_value.err));
330 }
331 return diplomat_result_out_value;
332 }
333 inline diplomat::result<std::string, ICU4XError> ICU4XDateTime::month_code() const {
334 std::string diplomat_writeable_string;
335 capi::DiplomatWriteable diplomat_writeable_out = diplomat::WriteableFromString(diplomat_writeable_string);
336 auto diplomat_result_raw_out_value = capi::ICU4XDateTime_month_code(this->inner.get(), &diplomat_writeable_out);
337 diplomat::result<std::monostate, ICU4XError> diplomat_result_out_value;
338 if (diplomat_result_raw_out_value.is_ok) {
339 diplomat_result_out_value = diplomat::Ok<std::monostate>(std::monostate());
340 } else {
341 diplomat_result_out_value = diplomat::Err<ICU4XError>(static_cast<ICU4XError>(diplomat_result_raw_out_value.err));
342 }
343 return diplomat_result_out_value.replace_ok(std::move(diplomat_writeable_string));
344 }
345 inline int32_t ICU4XDateTime::year_in_era() const {
346 return capi::ICU4XDateTime_year_in_era(this->inner.get());
347 }
348 template<typename W> inline diplomat::result<std::monostate, ICU4XError> ICU4XDateTime::era_to_writeable(W& write) const {
349 capi::DiplomatWriteable write_writer = diplomat::WriteableTrait<W>::Construct(write);
350 auto diplomat_result_raw_out_value = capi::ICU4XDateTime_era(this->inner.get(), &write_writer);
351 diplomat::result<std::monostate, ICU4XError> diplomat_result_out_value;
352 if (diplomat_result_raw_out_value.is_ok) {
353 diplomat_result_out_value = diplomat::Ok<std::monostate>(std::monostate());
354 } else {
355 diplomat_result_out_value = diplomat::Err<ICU4XError>(static_cast<ICU4XError>(diplomat_result_raw_out_value.err));
356 }
357 return diplomat_result_out_value;
358 }
359 inline diplomat::result<std::string, ICU4XError> ICU4XDateTime::era() const {
360 std::string diplomat_writeable_string;
361 capi::DiplomatWriteable diplomat_writeable_out = diplomat::WriteableFromString(diplomat_writeable_string);
362 auto diplomat_result_raw_out_value = capi::ICU4XDateTime_era(this->inner.get(), &diplomat_writeable_out);
363 diplomat::result<std::monostate, ICU4XError> diplomat_result_out_value;
364 if (diplomat_result_raw_out_value.is_ok) {
365 diplomat_result_out_value = diplomat::Ok<std::monostate>(std::monostate());
366 } else {
367 diplomat_result_out_value = diplomat::Err<ICU4XError>(static_cast<ICU4XError>(diplomat_result_raw_out_value.err));
368 }
369 return diplomat_result_out_value.replace_ok(std::move(diplomat_writeable_string));
370 }
371 inline uint8_t ICU4XDateTime::months_in_year() const {
372 return capi::ICU4XDateTime_months_in_year(this->inner.get());
373 }
374 inline uint8_t ICU4XDateTime::days_in_month() const {
375 return capi::ICU4XDateTime_days_in_month(this->inner.get());
376 }
377 inline uint16_t ICU4XDateTime::days_in_year() const {
378 return capi::ICU4XDateTime_days_in_year(this->inner.get());
379 }
380 inline ICU4XCalendar ICU4XDateTime::calendar() const {
381 return ICU4XCalendar(capi::ICU4XDateTime_calendar(this->inner.get()));
382 }
383 #endif