| 1 |
#ifndef ICU4XZonedDateTimeFormatter_HPP |
| 2 |
#define ICU4XZonedDateTimeFormatter_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 "ICU4XZonedDateTimeFormatter.h" |
| 13 |
|
| 14 |
class ICU4XDataProvider; |
| 15 |
class ICU4XLocale; |
| 16 |
#include "ICU4XDateLength.hpp" |
| 17 |
#include "ICU4XTimeLength.hpp" |
| 18 |
class ICU4XZonedDateTimeFormatter; |
| 19 |
#include "ICU4XError.hpp" |
| 20 |
struct ICU4XIsoTimeZoneOptions; |
| 21 |
class ICU4XDateTime; |
| 22 |
class ICU4XCustomTimeZone; |
| 23 |
class ICU4XIsoDateTime; |
| 24 |
|
| 25 |
/** |
| 26 |
* A destruction policy for using ICU4XZonedDateTimeFormatter with std::unique_ptr. |
| 27 |
*/ |
| 28 |
struct ICU4XZonedDateTimeFormatterDeleter { |
| 29 |
void operator()(capi::ICU4XZonedDateTimeFormatter* l) const noexcept { |
| 30 |
capi::ICU4XZonedDateTimeFormatter_destroy(l); |
| 31 |
} |
| 32 |
}; |
| 33 |
|
| 34 |
/** |
| 35 |
* An object capable of formatting a date time with time zone to a string. |
| 36 |
* |
| 37 |
* See the [Rust documentation for `ZonedDateTimeFormatter`](https://docs.rs/icu/latest/icu/datetime/struct.ZonedDateTimeFormatter.html) for more information. |
| 38 |
*/ |
| 39 |
class ICU4XZonedDateTimeFormatter { |
| 40 |
public: |
| 41 |
|
| 42 |
/** |
| 43 |
* Creates a new [`ICU4XZonedDateTimeFormatter`] from locale data. |
| 44 |
* |
| 45 |
* This function has `date_length` and `time_length` arguments and uses default options |
| 46 |
* for the time zone. |
| 47 |
* |
| 48 |
* See the [Rust documentation for `try_new`](https://docs.rs/icu/latest/icu/datetime/struct.ZonedDateTimeFormatter.html#method.try_new) for more information. |
| 49 |
*/ |
| 50 |
static diplomat::result<ICU4XZonedDateTimeFormatter, ICU4XError> create_with_lengths(const ICU4XDataProvider& provider, const ICU4XLocale& locale, ICU4XDateLength date_length, ICU4XTimeLength time_length); |
| 51 |
|
| 52 |
/** |
| 53 |
* Creates a new [`ICU4XZonedDateTimeFormatter`] from locale data. |
| 54 |
* |
| 55 |
* This function has `date_length` and `time_length` arguments and uses an ISO-8601 style |
| 56 |
* fallback for the time zone with the given configurations. |
| 57 |
* |
| 58 |
* See the [Rust documentation for `try_new`](https://docs.rs/icu/latest/icu/datetime/struct.ZonedDateTimeFormatter.html#method.try_new) for more information. |
| 59 |
*/ |
| 60 |
static diplomat::result<ICU4XZonedDateTimeFormatter, ICU4XError> create_with_lengths_and_iso_8601_time_zone_fallback(const ICU4XDataProvider& provider, const ICU4XLocale& locale, ICU4XDateLength date_length, ICU4XTimeLength time_length, ICU4XIsoTimeZoneOptions zone_options); |
| 61 |
|
| 62 |
/** |
| 63 |
* Formats a [`ICU4XDateTime`] and [`ICU4XCustomTimeZone`] to a string. |
| 64 |
* |
| 65 |
* See the [Rust documentation for `format`](https://docs.rs/icu/latest/icu/datetime/struct.ZonedDateTimeFormatter.html#method.format) for more information. |
| 66 |
*/ |
| 67 |
template<typename W> diplomat::result<std::monostate, ICU4XError> format_datetime_with_custom_time_zone_to_writeable(const ICU4XDateTime& datetime, const ICU4XCustomTimeZone& time_zone, W& write) const; |
| 68 |
|
| 69 |
/** |
| 70 |
* Formats a [`ICU4XDateTime`] and [`ICU4XCustomTimeZone`] to a string. |
| 71 |
* |
| 72 |
* See the [Rust documentation for `format`](https://docs.rs/icu/latest/icu/datetime/struct.ZonedDateTimeFormatter.html#method.format) for more information. |
| 73 |
*/ |
| 74 |
diplomat::result<std::string, ICU4XError> format_datetime_with_custom_time_zone(const ICU4XDateTime& datetime, const ICU4XCustomTimeZone& time_zone) const; |
| 75 |
|
| 76 |
/** |
| 77 |
* Formats a [`ICU4XIsoDateTime`] and [`ICU4XCustomTimeZone`] to a string. |
| 78 |
* |
| 79 |
* See the [Rust documentation for `format`](https://docs.rs/icu/latest/icu/datetime/struct.ZonedDateTimeFormatter.html#method.format) for more information. |
| 80 |
*/ |
| 81 |
template<typename W> diplomat::result<std::monostate, ICU4XError> format_iso_datetime_with_custom_time_zone_to_writeable(const ICU4XIsoDateTime& datetime, const ICU4XCustomTimeZone& time_zone, W& write) const; |
| 82 |
|
| 83 |
/** |
| 84 |
* Formats a [`ICU4XIsoDateTime`] and [`ICU4XCustomTimeZone`] to a string. |
| 85 |
* |
| 86 |
* See the [Rust documentation for `format`](https://docs.rs/icu/latest/icu/datetime/struct.ZonedDateTimeFormatter.html#method.format) for more information. |
| 87 |
*/ |
| 88 |
diplomat::result<std::string, ICU4XError> format_iso_datetime_with_custom_time_zone(const ICU4XIsoDateTime& datetime, const ICU4XCustomTimeZone& time_zone) const; |
| 89 |
inline const capi::ICU4XZonedDateTimeFormatter* AsFFI() const { return this->inner.get(); } |
| 90 |
inline capi::ICU4XZonedDateTimeFormatter* AsFFIMut() { return this->inner.get(); } |
| 91 |
inline explicit ICU4XZonedDateTimeFormatter(capi::ICU4XZonedDateTimeFormatter* i) : inner(i) {} |
| 92 |
ICU4XZonedDateTimeFormatter() = default; |
| 93 |
ICU4XZonedDateTimeFormatter(ICU4XZonedDateTimeFormatter&&) noexcept = default; |
| 94 |
ICU4XZonedDateTimeFormatter& operator=(ICU4XZonedDateTimeFormatter&& other) noexcept = default; |
| 95 |
private: |
| 96 |
std::unique_ptr<capi::ICU4XZonedDateTimeFormatter, ICU4XZonedDateTimeFormatterDeleter> inner; |
| 97 |
}; |
| 98 |
|
| 99 |
#include "ICU4XDataProvider.hpp" |
| 100 |
#include "ICU4XLocale.hpp" |
| 101 |
#include "ICU4XIsoTimeZoneOptions.hpp" |
| 102 |
#include "ICU4XDateTime.hpp" |
| 103 |
#include "ICU4XCustomTimeZone.hpp" |
| 104 |
#include "ICU4XIsoDateTime.hpp" |
| 105 |
|
| 106 |
inline diplomat::result<ICU4XZonedDateTimeFormatter, ICU4XError> ICU4XZonedDateTimeFormatter::create_with_lengths(const ICU4XDataProvider& provider, const ICU4XLocale& locale, ICU4XDateLength date_length, ICU4XTimeLength time_length) { |
| 107 |
auto diplomat_result_raw_out_value = capi::ICU4XZonedDateTimeFormatter_create_with_lengths(provider.AsFFI(), locale.AsFFI(), static_cast<capi::ICU4XDateLength>(date_length), static_cast<capi::ICU4XTimeLength>(time_length)); |
| 108 |
diplomat::result<ICU4XZonedDateTimeFormatter, ICU4XError> diplomat_result_out_value; |
| 109 |
if (diplomat_result_raw_out_value.is_ok) { |
| 110 |
diplomat_result_out_value = diplomat::Ok<ICU4XZonedDateTimeFormatter>(ICU4XZonedDateTimeFormatter(diplomat_result_raw_out_value.ok)); |
| 111 |
} else { |
| 112 |
diplomat_result_out_value = diplomat::Err<ICU4XError>(static_cast<ICU4XError>(diplomat_result_raw_out_value.err)); |
| 113 |
} |
| 114 |
return diplomat_result_out_value; |
| 115 |
} |
| 116 |
inline diplomat::result<ICU4XZonedDateTimeFormatter, ICU4XError> ICU4XZonedDateTimeFormatter::create_with_lengths_and_iso_8601_time_zone_fallback(const ICU4XDataProvider& provider, const ICU4XLocale& locale, ICU4XDateLength date_length, ICU4XTimeLength time_length, ICU4XIsoTimeZoneOptions zone_options) { |
| 117 |
ICU4XIsoTimeZoneOptions diplomat_wrapped_struct_zone_options = zone_options; |
| 118 |
auto diplomat_result_raw_out_value = capi::ICU4XZonedDateTimeFormatter_create_with_lengths_and_iso_8601_time_zone_fallback(provider.AsFFI(), locale.AsFFI(), static_cast<capi::ICU4XDateLength>(date_length), static_cast<capi::ICU4XTimeLength>(time_length), capi::ICU4XIsoTimeZoneOptions{ .format = static_cast<capi::ICU4XIsoTimeZoneFormat>(diplomat_wrapped_struct_zone_options.format), .minutes = static_cast<capi::ICU4XIsoTimeZoneMinuteDisplay>(diplomat_wrapped_struct_zone_options.minutes), .seconds = static_cast<capi::ICU4XIsoTimeZoneSecondDisplay>(diplomat_wrapped_struct_zone_options.seconds) }); |
| 119 |
diplomat::result<ICU4XZonedDateTimeFormatter, ICU4XError> diplomat_result_out_value; |
| 120 |
if (diplomat_result_raw_out_value.is_ok) { |
| 121 |
diplomat_result_out_value = diplomat::Ok<ICU4XZonedDateTimeFormatter>(ICU4XZonedDateTimeFormatter(diplomat_result_raw_out_value.ok)); |
| 122 |
} else { |
| 123 |
diplomat_result_out_value = diplomat::Err<ICU4XError>(static_cast<ICU4XError>(diplomat_result_raw_out_value.err)); |
| 124 |
} |
| 125 |
return diplomat_result_out_value; |
| 126 |
} |
| 127 |
template<typename W> inline diplomat::result<std::monostate, ICU4XError> ICU4XZonedDateTimeFormatter::format_datetime_with_custom_time_zone_to_writeable(const ICU4XDateTime& datetime, const ICU4XCustomTimeZone& time_zone, W& write) const { |
| 128 |
capi::DiplomatWriteable write_writer = diplomat::WriteableTrait<W>::Construct(write); |
| 129 |
auto diplomat_result_raw_out_value = capi::ICU4XZonedDateTimeFormatter_format_datetime_with_custom_time_zone(this->inner.get(), datetime.AsFFI(), time_zone.AsFFI(), &write_writer); |
| 130 |
diplomat::result<std::monostate, ICU4XError> diplomat_result_out_value; |
| 131 |
if (diplomat_result_raw_out_value.is_ok) { |
| 132 |
diplomat_result_out_value = diplomat::Ok<std::monostate>(std::monostate()); |
| 133 |
} else { |
| 134 |
diplomat_result_out_value = diplomat::Err<ICU4XError>(static_cast<ICU4XError>(diplomat_result_raw_out_value.err)); |
| 135 |
} |
| 136 |
return diplomat_result_out_value; |
| 137 |
} |
| 138 |
inline diplomat::result<std::string, ICU4XError> ICU4XZonedDateTimeFormatter::format_datetime_with_custom_time_zone(const ICU4XDateTime& datetime, const ICU4XCustomTimeZone& time_zone) const { |
| 139 |
std::string diplomat_writeable_string; |
| 140 |
capi::DiplomatWriteable diplomat_writeable_out = diplomat::WriteableFromString(diplomat_writeable_string); |
| 141 |
auto diplomat_result_raw_out_value = capi::ICU4XZonedDateTimeFormatter_format_datetime_with_custom_time_zone(this->inner.get(), datetime.AsFFI(), time_zone.AsFFI(), &diplomat_writeable_out); |
| 142 |
diplomat::result<std::monostate, ICU4XError> diplomat_result_out_value; |
| 143 |
if (diplomat_result_raw_out_value.is_ok) { |
| 144 |
diplomat_result_out_value = diplomat::Ok<std::monostate>(std::monostate()); |
| 145 |
} else { |
| 146 |
diplomat_result_out_value = diplomat::Err<ICU4XError>(static_cast<ICU4XError>(diplomat_result_raw_out_value.err)); |
| 147 |
} |
| 148 |
return diplomat_result_out_value.replace_ok(std::move(diplomat_writeable_string)); |
| 149 |
} |
| 150 |
template<typename W> inline diplomat::result<std::monostate, ICU4XError> ICU4XZonedDateTimeFormatter::format_iso_datetime_with_custom_time_zone_to_writeable(const ICU4XIsoDateTime& datetime, const ICU4XCustomTimeZone& time_zone, W& write) const { |
| 151 |
capi::DiplomatWriteable write_writer = diplomat::WriteableTrait<W>::Construct(write); |
| 152 |
auto diplomat_result_raw_out_value = capi::ICU4XZonedDateTimeFormatter_format_iso_datetime_with_custom_time_zone(this->inner.get(), datetime.AsFFI(), time_zone.AsFFI(), &write_writer); |
| 153 |
diplomat::result<std::monostate, ICU4XError> diplomat_result_out_value; |
| 154 |
if (diplomat_result_raw_out_value.is_ok) { |
| 155 |
diplomat_result_out_value = diplomat::Ok<std::monostate>(std::monostate()); |
| 156 |
} else { |
| 157 |
diplomat_result_out_value = diplomat::Err<ICU4XError>(static_cast<ICU4XError>(diplomat_result_raw_out_value.err)); |
| 158 |
} |
| 159 |
return diplomat_result_out_value; |
| 160 |
} |
| 161 |
inline diplomat::result<std::string, ICU4XError> ICU4XZonedDateTimeFormatter::format_iso_datetime_with_custom_time_zone(const ICU4XIsoDateTime& datetime, const ICU4XCustomTimeZone& time_zone) const { |
| 162 |
std::string diplomat_writeable_string; |
| 163 |
capi::DiplomatWriteable diplomat_writeable_out = diplomat::WriteableFromString(diplomat_writeable_string); |
| 164 |
auto diplomat_result_raw_out_value = capi::ICU4XZonedDateTimeFormatter_format_iso_datetime_with_custom_time_zone(this->inner.get(), datetime.AsFFI(), time_zone.AsFFI(), &diplomat_writeable_out); |
| 165 |
diplomat::result<std::monostate, ICU4XError> diplomat_result_out_value; |
| 166 |
if (diplomat_result_raw_out_value.is_ok) { |
| 167 |
diplomat_result_out_value = diplomat::Ok<std::monostate>(std::monostate()); |
| 168 |
} else { |
| 169 |
diplomat_result_out_value = diplomat::Err<ICU4XError>(static_cast<ICU4XError>(diplomat_result_raw_out_value.err)); |
| 170 |
} |
| 171 |
return diplomat_result_out_value.replace_ok(std::move(diplomat_writeable_string)); |
| 172 |
} |
| 173 |
#endif |