| 1 |
#ifndef ICU4XDate_HPP |
| 2 |
#define ICU4XDate_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 "ICU4XDate.h" |
| 13 |
|
| 14 |
class ICU4XCalendar; |
| 15 |
class ICU4XDate; |
| 16 |
#include "ICU4XError.hpp" |
| 17 |
class ICU4XIsoDate; |
| 18 |
#include "ICU4XIsoWeekday.hpp" |
| 19 |
class ICU4XWeekCalculator; |
| 20 |
struct ICU4XWeekOf; |
| 21 |
|
| 22 |
/** |
| 23 |
* A destruction policy for using ICU4XDate with std::unique_ptr. |
| 24 |
*/ |
| 25 |
struct ICU4XDateDeleter { |
| 26 |
void operator()(capi::ICU4XDate* l) const noexcept { |
| 27 |
capi::ICU4XDate_destroy(l); |
| 28 |
} |
| 29 |
}; |
| 30 |
|
| 31 |
/** |
| 32 |
* An ICU4X Date object capable of containing a date and time for any calendar. |
| 33 |
* |
| 34 |
* See the [Rust documentation for `Date`](https://docs.rs/icu/latest/icu/calendar/struct.Date.html) for more information. |
| 35 |
*/ |
| 36 |
class ICU4XDate { |
| 37 |
public: |
| 38 |
|
| 39 |
/** |
| 40 |
* Creates a new [`ICU4XDate`] representing the ISO date and time |
| 41 |
* given but in a given calendar |
| 42 |
* |
| 43 |
* See the [Rust documentation for `new_from_iso`](https://docs.rs/icu/latest/icu/calendar/struct.Date.html#method.new_from_iso) for more information. |
| 44 |
*/ |
| 45 |
static diplomat::result<ICU4XDate, ICU4XError> create_from_iso_in_calendar(int32_t year, uint8_t month, uint8_t day, const ICU4XCalendar& calendar); |
| 46 |
|
| 47 |
/** |
| 48 |
* Creates a new [`ICU4XDate`] from the given codes, which are interpreted in the given calendar system |
| 49 |
* |
| 50 |
* See the [Rust documentation for `try_new_from_codes`](https://docs.rs/icu/latest/icu/calendar/struct.Date.html#method.try_new_from_codes) for more information. |
| 51 |
*/ |
| 52 |
static diplomat::result<ICU4XDate, ICU4XError> create_from_codes_in_calendar(const std::string_view era_code, int32_t year, const std::string_view month_code, uint8_t day, const ICU4XCalendar& calendar); |
| 53 |
|
| 54 |
/** |
| 55 |
* Convert this date to one in a different calendar |
| 56 |
* |
| 57 |
* See the [Rust documentation for `to_calendar`](https://docs.rs/icu/latest/icu/calendar/struct.Date.html#method.to_calendar) for more information. |
| 58 |
*/ |
| 59 |
ICU4XDate to_calendar(const ICU4XCalendar& calendar) const; |
| 60 |
|
| 61 |
/** |
| 62 |
* Converts this date to ISO |
| 63 |
* |
| 64 |
* See the [Rust documentation for `to_iso`](https://docs.rs/icu/latest/icu/calendar/struct.Date.html#method.to_iso) for more information. |
| 65 |
*/ |
| 66 |
ICU4XIsoDate to_iso() const; |
| 67 |
|
| 68 |
/** |
| 69 |
* Returns the 1-indexed day in the year for this date |
| 70 |
* |
| 71 |
* 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. |
| 72 |
*/ |
| 73 |
uint16_t day_of_year() const; |
| 74 |
|
| 75 |
/** |
| 76 |
* Returns the 1-indexed day in the month for this date |
| 77 |
* |
| 78 |
* 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. |
| 79 |
*/ |
| 80 |
uint32_t day_of_month() const; |
| 81 |
|
| 82 |
/** |
| 83 |
* Returns the day in the week for this day |
| 84 |
* |
| 85 |
* 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. |
| 86 |
*/ |
| 87 |
ICU4XIsoWeekday day_of_week() const; |
| 88 |
|
| 89 |
/** |
| 90 |
* Returns the week number in this month, 1-indexed, based on what |
| 91 |
* is considered the first day of the week (often a locale preference). |
| 92 |
* |
| 93 |
* `first_weekday` can be obtained via `first_weekday()` on [`ICU4XWeekCalculator`] |
| 94 |
* |
| 95 |
* 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. |
| 96 |
*/ |
| 97 |
uint32_t week_of_month(ICU4XIsoWeekday first_weekday) const; |
| 98 |
|
| 99 |
/** |
| 100 |
* Returns the week number in this year, using week data |
| 101 |
* |
| 102 |
* 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. |
| 103 |
*/ |
| 104 |
diplomat::result<ICU4XWeekOf, ICU4XError> week_of_year(const ICU4XWeekCalculator& calculator) const; |
| 105 |
|
| 106 |
/** |
| 107 |
* Returns 1-indexed number of the month of this date in its year |
| 108 |
* |
| 109 |
* Note that for lunar calendars this may not lead to the same month |
| 110 |
* having the same ordinal month across years; use month_code if you care |
| 111 |
* about month identity. |
| 112 |
* |
| 113 |
* See the [Rust documentation for `month`](https://docs.rs/icu/latest/icu/calendar/struct.Date.html#method.month) for more information. |
| 114 |
*/ |
| 115 |
uint32_t ordinal_month() const; |
| 116 |
|
| 117 |
/** |
| 118 |
* Returns the month code for this date. Typically something |
| 119 |
* like "M01", "M02", but can be more complicated for lunar calendars. |
| 120 |
* |
| 121 |
* See the [Rust documentation for `month`](https://docs.rs/icu/latest/icu/calendar/struct.Date.html#method.month) for more information. |
| 122 |
*/ |
| 123 |
template<typename W> diplomat::result<std::monostate, ICU4XError> month_code_to_writeable(W& write) const; |
| 124 |
|
| 125 |
/** |
| 126 |
* Returns the month code for this date. Typically something |
| 127 |
* like "M01", "M02", but can be more complicated for lunar calendars. |
| 128 |
* |
| 129 |
* See the [Rust documentation for `month`](https://docs.rs/icu/latest/icu/calendar/struct.Date.html#method.month) for more information. |
| 130 |
*/ |
| 131 |
diplomat::result<std::string, ICU4XError> month_code() const; |
| 132 |
|
| 133 |
/** |
| 134 |
* Returns the year number in the current era for this date |
| 135 |
* |
| 136 |
* See the [Rust documentation for `year`](https://docs.rs/icu/latest/icu/calendar/struct.Date.html#method.year) for more information. |
| 137 |
*/ |
| 138 |
int32_t year_in_era() const; |
| 139 |
|
| 140 |
/** |
| 141 |
* Returns the era for this date, |
| 142 |
* |
| 143 |
* See the [Rust documentation for `year`](https://docs.rs/icu/latest/icu/struct.Date.html#method.year) for more information. |
| 144 |
* |
| 145 |
* Additional information: [1](https://docs.rs/icu/latest/icu/types/struct.Era.html) |
| 146 |
*/ |
| 147 |
template<typename W> diplomat::result<std::monostate, ICU4XError> era_to_writeable(W& write) const; |
| 148 |
|
| 149 |
/** |
| 150 |
* Returns the era for this date, |
| 151 |
* |
| 152 |
* See the [Rust documentation for `year`](https://docs.rs/icu/latest/icu/struct.Date.html#method.year) for more information. |
| 153 |
* |
| 154 |
* Additional information: [1](https://docs.rs/icu/latest/icu/types/struct.Era.html) |
| 155 |
*/ |
| 156 |
diplomat::result<std::string, ICU4XError> era() const; |
| 157 |
|
| 158 |
/** |
| 159 |
* Returns the number of months in the year represented by this date |
| 160 |
* |
| 161 |
* 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. |
| 162 |
*/ |
| 163 |
uint8_t months_in_year() const; |
| 164 |
|
| 165 |
/** |
| 166 |
* Returns the number of days in the month represented by this date |
| 167 |
* |
| 168 |
* 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. |
| 169 |
*/ |
| 170 |
uint8_t days_in_month() const; |
| 171 |
|
| 172 |
/** |
| 173 |
* Returns the number of days in the year represented by this date |
| 174 |
* |
| 175 |
* 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. |
| 176 |
*/ |
| 177 |
uint16_t days_in_year() const; |
| 178 |
|
| 179 |
/** |
| 180 |
* Returns the [`ICU4XCalendar`] object backing this date |
| 181 |
* |
| 182 |
* See the [Rust documentation for `calendar`](https://docs.rs/icu/latest/icu/calendar/struct.Date.html#method.calendar) for more information. |
| 183 |
*/ |
| 184 |
ICU4XCalendar calendar() const; |
| 185 |
inline const capi::ICU4XDate* AsFFI() const { return this->inner.get(); } |
| 186 |
inline capi::ICU4XDate* AsFFIMut() { return this->inner.get(); } |
| 187 |
inline explicit ICU4XDate(capi::ICU4XDate* i) : inner(i) {} |
| 188 |
ICU4XDate() = default; |
| 189 |
ICU4XDate(ICU4XDate&&) noexcept = default; |
| 190 |
ICU4XDate& operator=(ICU4XDate&& other) noexcept = default; |
| 191 |
private: |
| 192 |
std::unique_ptr<capi::ICU4XDate, ICU4XDateDeleter> inner; |
| 193 |
}; |
| 194 |
|
| 195 |
#include "ICU4XCalendar.hpp" |
| 196 |
#include "ICU4XIsoDate.hpp" |
| 197 |
#include "ICU4XWeekCalculator.hpp" |
| 198 |
#include "ICU4XWeekOf.hpp" |
| 199 |
|
| 200 |
inline diplomat::result<ICU4XDate, ICU4XError> ICU4XDate::create_from_iso_in_calendar(int32_t year, uint8_t month, uint8_t day, const ICU4XCalendar& calendar) { |
| 201 |
auto diplomat_result_raw_out_value = capi::ICU4XDate_create_from_iso_in_calendar(year, month, day, calendar.AsFFI()); |
| 202 |
diplomat::result<ICU4XDate, ICU4XError> diplomat_result_out_value; |
| 203 |
if (diplomat_result_raw_out_value.is_ok) { |
| 204 |
diplomat_result_out_value = diplomat::Ok<ICU4XDate>(ICU4XDate(diplomat_result_raw_out_value.ok)); |
| 205 |
} else { |
| 206 |
diplomat_result_out_value = diplomat::Err<ICU4XError>(static_cast<ICU4XError>(diplomat_result_raw_out_value.err)); |
| 207 |
} |
| 208 |
return diplomat_result_out_value; |
| 209 |
} |
| 210 |
inline diplomat::result<ICU4XDate, ICU4XError> ICU4XDate::create_from_codes_in_calendar(const std::string_view era_code, int32_t year, const std::string_view month_code, uint8_t day, const ICU4XCalendar& calendar) { |
| 211 |
auto diplomat_result_raw_out_value = capi::ICU4XDate_create_from_codes_in_calendar(era_code.data(), era_code.size(), year, month_code.data(), month_code.size(), day, calendar.AsFFI()); |
| 212 |
diplomat::result<ICU4XDate, ICU4XError> diplomat_result_out_value; |
| 213 |
if (diplomat_result_raw_out_value.is_ok) { |
| 214 |
diplomat_result_out_value = diplomat::Ok<ICU4XDate>(ICU4XDate(diplomat_result_raw_out_value.ok)); |
| 215 |
} else { |
| 216 |
diplomat_result_out_value = diplomat::Err<ICU4XError>(static_cast<ICU4XError>(diplomat_result_raw_out_value.err)); |
| 217 |
} |
| 218 |
return diplomat_result_out_value; |
| 219 |
} |
| 220 |
inline ICU4XDate ICU4XDate::to_calendar(const ICU4XCalendar& calendar) const { |
| 221 |
return ICU4XDate(capi::ICU4XDate_to_calendar(this->inner.get(), calendar.AsFFI())); |
| 222 |
} |
| 223 |
inline ICU4XIsoDate ICU4XDate::to_iso() const { |
| 224 |
return ICU4XIsoDate(capi::ICU4XDate_to_iso(this->inner.get())); |
| 225 |
} |
| 226 |
inline uint16_t ICU4XDate::day_of_year() const { |
| 227 |
return capi::ICU4XDate_day_of_year(this->inner.get()); |
| 228 |
} |
| 229 |
inline uint32_t ICU4XDate::day_of_month() const { |
| 230 |
return capi::ICU4XDate_day_of_month(this->inner.get()); |
| 231 |
} |
| 232 |
inline ICU4XIsoWeekday ICU4XDate::day_of_week() const { |
| 233 |
return static_cast<ICU4XIsoWeekday>(capi::ICU4XDate_day_of_week(this->inner.get())); |
| 234 |
} |
| 235 |
inline uint32_t ICU4XDate::week_of_month(ICU4XIsoWeekday first_weekday) const { |
| 236 |
return capi::ICU4XDate_week_of_month(this->inner.get(), static_cast<capi::ICU4XIsoWeekday>(first_weekday)); |
| 237 |
} |
| 238 |
inline diplomat::result<ICU4XWeekOf, ICU4XError> ICU4XDate::week_of_year(const ICU4XWeekCalculator& calculator) const { |
| 239 |
auto diplomat_result_raw_out_value = capi::ICU4XDate_week_of_year(this->inner.get(), calculator.AsFFI()); |
| 240 |
diplomat::result<ICU4XWeekOf, ICU4XError> diplomat_result_out_value; |
| 241 |
if (diplomat_result_raw_out_value.is_ok) { |
| 242 |
capi::ICU4XWeekOf diplomat_raw_struct_out_value = diplomat_result_raw_out_value.ok; |
| 243 |
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)) }); |
| 244 |
} else { |
| 245 |
diplomat_result_out_value = diplomat::Err<ICU4XError>(static_cast<ICU4XError>(diplomat_result_raw_out_value.err)); |
| 246 |
} |
| 247 |
return diplomat_result_out_value; |
| 248 |
} |
| 249 |
inline uint32_t ICU4XDate::ordinal_month() const { |
| 250 |
return capi::ICU4XDate_ordinal_month(this->inner.get()); |
| 251 |
} |
| 252 |
template<typename W> inline diplomat::result<std::monostate, ICU4XError> ICU4XDate::month_code_to_writeable(W& write) const { |
| 253 |
capi::DiplomatWriteable write_writer = diplomat::WriteableTrait<W>::Construct(write); |
| 254 |
auto diplomat_result_raw_out_value = capi::ICU4XDate_month_code(this->inner.get(), &write_writer); |
| 255 |
diplomat::result<std::monostate, ICU4XError> diplomat_result_out_value; |
| 256 |
if (diplomat_result_raw_out_value.is_ok) { |
| 257 |
diplomat_result_out_value = diplomat::Ok<std::monostate>(std::monostate()); |
| 258 |
} else { |
| 259 |
diplomat_result_out_value = diplomat::Err<ICU4XError>(static_cast<ICU4XError>(diplomat_result_raw_out_value.err)); |
| 260 |
} |
| 261 |
return diplomat_result_out_value; |
| 262 |
} |
| 263 |
inline diplomat::result<std::string, ICU4XError> ICU4XDate::month_code() const { |
| 264 |
std::string diplomat_writeable_string; |
| 265 |
capi::DiplomatWriteable diplomat_writeable_out = diplomat::WriteableFromString(diplomat_writeable_string); |
| 266 |
auto diplomat_result_raw_out_value = capi::ICU4XDate_month_code(this->inner.get(), &diplomat_writeable_out); |
| 267 |
diplomat::result<std::monostate, ICU4XError> diplomat_result_out_value; |
| 268 |
if (diplomat_result_raw_out_value.is_ok) { |
| 269 |
diplomat_result_out_value = diplomat::Ok<std::monostate>(std::monostate()); |
| 270 |
} else { |
| 271 |
diplomat_result_out_value = diplomat::Err<ICU4XError>(static_cast<ICU4XError>(diplomat_result_raw_out_value.err)); |
| 272 |
} |
| 273 |
return diplomat_result_out_value.replace_ok(std::move(diplomat_writeable_string)); |
| 274 |
} |
| 275 |
inline int32_t ICU4XDate::year_in_era() const { |
| 276 |
return capi::ICU4XDate_year_in_era(this->inner.get()); |
| 277 |
} |
| 278 |
template<typename W> inline diplomat::result<std::monostate, ICU4XError> ICU4XDate::era_to_writeable(W& write) const { |
| 279 |
capi::DiplomatWriteable write_writer = diplomat::WriteableTrait<W>::Construct(write); |
| 280 |
auto diplomat_result_raw_out_value = capi::ICU4XDate_era(this->inner.get(), &write_writer); |
| 281 |
diplomat::result<std::monostate, ICU4XError> diplomat_result_out_value; |
| 282 |
if (diplomat_result_raw_out_value.is_ok) { |
| 283 |
diplomat_result_out_value = diplomat::Ok<std::monostate>(std::monostate()); |
| 284 |
} else { |
| 285 |
diplomat_result_out_value = diplomat::Err<ICU4XError>(static_cast<ICU4XError>(diplomat_result_raw_out_value.err)); |
| 286 |
} |
| 287 |
return diplomat_result_out_value; |
| 288 |
} |
| 289 |
inline diplomat::result<std::string, ICU4XError> ICU4XDate::era() const { |
| 290 |
std::string diplomat_writeable_string; |
| 291 |
capi::DiplomatWriteable diplomat_writeable_out = diplomat::WriteableFromString(diplomat_writeable_string); |
| 292 |
auto diplomat_result_raw_out_value = capi::ICU4XDate_era(this->inner.get(), &diplomat_writeable_out); |
| 293 |
diplomat::result<std::monostate, ICU4XError> diplomat_result_out_value; |
| 294 |
if (diplomat_result_raw_out_value.is_ok) { |
| 295 |
diplomat_result_out_value = diplomat::Ok<std::monostate>(std::monostate()); |
| 296 |
} else { |
| 297 |
diplomat_result_out_value = diplomat::Err<ICU4XError>(static_cast<ICU4XError>(diplomat_result_raw_out_value.err)); |
| 298 |
} |
| 299 |
return diplomat_result_out_value.replace_ok(std::move(diplomat_writeable_string)); |
| 300 |
} |
| 301 |
inline uint8_t ICU4XDate::months_in_year() const { |
| 302 |
return capi::ICU4XDate_months_in_year(this->inner.get()); |
| 303 |
} |
| 304 |
inline uint8_t ICU4XDate::days_in_month() const { |
| 305 |
return capi::ICU4XDate_days_in_month(this->inner.get()); |
| 306 |
} |
| 307 |
inline uint16_t ICU4XDate::days_in_year() const { |
| 308 |
return capi::ICU4XDate_days_in_year(this->inner.get()); |
| 309 |
} |
| 310 |
inline ICU4XCalendar ICU4XDate::calendar() const { |
| 311 |
return ICU4XCalendar(capi::ICU4XDate_calendar(this->inner.get())); |
| 312 |
} |
| 313 |
#endif |