use ffi::TimeZoneInfo;
#[diplomat::bridge]
#[diplomat::abi_rename = "icu4x_{0}_mv1"]
#[diplomat::attr(auto, namespace = "icu4x")]
pub mod ffi {
use alloc::boxed::Box;
use core::fmt::Write;
use icu_timezone::{TimeZoneBcp47Id, UtcOffset, ZoneVariant};
use tinystr::TinyAsciiStr;
use crate::{datetime::ffi::IsoDateTime, errors::ffi::TimeZoneInvalidOffsetError};
#[diplomat::opaque]
#[diplomat::rust_link(icu::timezone::TimeZoneInfo, Struct)]
pub struct TimeZoneInfo {
pub(crate) time_zone_id: icu_timezone::TimeZoneBcp47Id,
pub(crate) offset: Option<icu_timezone::UtcOffset>,
pub(crate) zone_variant: Option<icu_timezone::ZoneVariant>,
pub(crate) local_time: Option<(icu_calendar::Date<icu_calendar::Iso>, icu_calendar::Time)>,
}
impl TimeZoneInfo {
#[diplomat::rust_link(icu::timezone::TimeZoneInfo::unknown, FnInStruct)]
#[diplomat::rust_link(icu::timezone::TimeZoneBcp47Id::unknown, FnInStruct, hidden)]
#[diplomat::attr(supports = fallible_constructors, named_constructor)]
pub fn unknown() -> Box<TimeZoneInfo> {
Box::new(icu_timezone::TimeZoneInfo::unknown().into())
}
#[diplomat::rust_link(icu::timezone::TimeZoneInfo::utc, FnInStruct)]
#[diplomat::rust_link(icu::timezone::UtcOffset::zero, FnInStruct, hidden)]
#[diplomat::attr(supports = fallible_constructors, named_constructor)]
pub fn utc() -> Box<TimeZoneInfo> {
Box::new(icu_timezone::TimeZoneInfo::utc().into())
}
#[diplomat::attr(auto, constructor)]
pub fn from_parts(
bcp47_id: &DiplomatStr,
offset_seconds: i32,
dst: bool,
) -> Box<TimeZoneInfo> {
Box::new(Self {
time_zone_id: TinyAsciiStr::try_from_utf8(bcp47_id)
.ok()
.map(TimeZoneBcp47Id)
.unwrap_or(TimeZoneBcp47Id::unknown()),
offset: UtcOffset::try_from_seconds(offset_seconds).ok(),
zone_variant: Some(if dst {
ZoneVariant::Daylight
} else {
ZoneVariant::Standard
}),
local_time: None,
})
}
#[diplomat::rust_link(icu::timezone::UtcOffset, Struct, compact)]
#[diplomat::rust_link(icu::timezone::TimeZoneBcp47Id::with_offset, FnInStruct)]
#[diplomat::rust_link(icu::timezone::UtcOffset::try_from_seconds, FnInStruct)]
#[diplomat::rust_link(icu::timezone::UtcOffset::from_seconds_unchecked, FnInStruct, hidden)]
pub fn try_set_offset_seconds(
&mut self,
offset_seconds: i32,
) -> Result<(), TimeZoneInvalidOffsetError> {
self.offset = Some(icu_timezone::UtcOffset::try_from_seconds(offset_seconds)?);
Ok(())
}
#[diplomat::rust_link(icu::timezone::UtcOffset, Struct, compact)]
#[diplomat::rust_link(icu::timezone::TimeZoneBcp47Id::with_offset, FnInStruct)]
#[diplomat::rust_link(icu::timezone::UtcOffset::from_eighths_of_hour, FnInStruct)]
pub fn set_offset_eighths_of_hour(&mut self, offset_eighths_of_hour: i8) {
self.offset = Some(icu_timezone::UtcOffset::from_eighths_of_hour(
offset_eighths_of_hour,
));
}
#[diplomat::rust_link(icu::timezone::UtcOffset, Struct, compact)]
#[diplomat::rust_link(icu::timezone::TimeZoneBcp47Id::with_offset, FnInStruct)]
#[diplomat::rust_link(icu::timezone::UtcOffset::try_from_str, FnInStruct)]
#[diplomat::rust_link(icu::timezone::UtcOffset::try_from_utf8, FnInStruct, hidden)]
#[diplomat::rust_link(icu::timezone::UtcOffset::from_str, FnInStruct, hidden)]
pub fn try_set_offset_str(
&mut self,
offset: &DiplomatStr,
) -> Result<(), TimeZoneInvalidOffsetError> {
self.offset = Some(
icu_timezone::UtcOffset::try_from_utf8(offset)
.map_err(|_| TimeZoneInvalidOffsetError)?,
);
Ok(())
}
#[diplomat::rust_link(icu::timezone::UtcOffset::to_eighths_of_hour, FnInStruct)]
pub fn offset_eighths_of_hour(&self) -> Option<i8> {
self.offset.map(icu_timezone::UtcOffset::to_eighths_of_hour)
}
#[diplomat::rust_link(icu::timezone::UtcOffset::offset_seconds, FnInStruct)]
#[diplomat::rust_link(icu::timezone::UtcOffset, Struct, compact)]
#[diplomat::rust_link(icu::timezone::TimeZoneBcp47Id::without_offset, FnInStruct, compact)]
pub fn clear_offset(&mut self) {
self.offset.take();
}
#[diplomat::rust_link(icu::timezone::TimeZoneInfo::offset, FnInStruct)]
#[diplomat::rust_link(icu::timezone::UtcOffset::to_seconds, FnInStruct)]
#[diplomat::rust_link(icu::timezone::UtcOffset, Struct, compact)]
#[diplomat::attr(auto, getter)]
pub fn offset_seconds(&self) -> Option<i32> {
self.offset.map(icu_timezone::UtcOffset::to_seconds)
}
#[diplomat::rust_link(icu::timezone::UtcOffset::is_non_negative, FnInStruct)]
#[diplomat::attr(auto, getter)]
pub fn is_offset_non_negative(&self) -> Option<bool> {
self.offset.map(icu_timezone::UtcOffset::is_non_negative)
}
#[diplomat::rust_link(icu::timezone::UtcOffset::is_zero, FnInStruct)]
#[diplomat::attr(auto, getter)]
pub fn is_offset_zero(&self) -> Option<bool> {
self.offset.map(icu_timezone::UtcOffset::is_zero)
}
#[diplomat::rust_link(icu::timezone::UtcOffset::hours_part, FnInStruct)]
#[diplomat::attr(auto, getter)]
pub fn offset_hours_part(&self) -> Option<i32> {
self.offset.map(|o| o.hours_part())
}
#[diplomat::rust_link(icu::timezone::UtcOffset::minutes_part, FnInStruct)]
#[diplomat::attr(auto, getter)]
pub fn offset_minutes_part(&self) -> Option<u32> {
self.offset.map(|o| o.minutes_part())
}
#[diplomat::rust_link(icu::timezone::UtcOffset::seconds_part, FnInStruct)]
#[diplomat::attr(auto, getter)]
pub fn offset_seconds_part(&self) -> Option<u32> {
self.offset.map(|o| o.seconds_part())
}
#[diplomat::rust_link(icu::timezone::TimeZoneBcp47Id, Struct, compact)]
#[diplomat::rust_link(icu::timezone::TimeZoneBcp47Id::from_str, FnInStruct, hidden)]
#[diplomat::rust_link(icu::timezone::TimeZoneBcp47Id::deref, FnInStruct, hidden)]
#[diplomat::rust_link(
icu::timezone::TimeZoneBcp47Id::Target,
AssociatedTypeInStruct,
hidden
)]
pub fn set_time_zone_id(&mut self, id: &DiplomatStr) {
self.time_zone_id = tinystr::TinyAsciiStr::try_from_utf8(id)
.map(TimeZoneBcp47Id)
.unwrap_or(TimeZoneBcp47Id::unknown());
}
pub fn set_iana_time_zone_id(
&mut self,
mapper: &crate::timezone_mapper::ffi::TimeZoneIdMapper,
id: &DiplomatStr,
) {
self.time_zone_id = mapper.0.as_borrowed().iana_bytes_to_bcp47(id);
}
#[diplomat::rust_link(icu::timezone::TimeZoneInfo::time_zone_id, FnInStruct)]
#[diplomat::rust_link(icu::timezone::TimeZoneBcp47Id, Struct, compact)]
#[diplomat::attr(auto, getter)]
pub fn time_zone_id(&self, write: &mut diplomat_runtime::DiplomatWrite) {
let _infallible = write.write_str(self.time_zone_id.0.as_str());
}
#[diplomat::rust_link(icu::timezone::ZoneVariant, Enum, compact)]
pub fn clear_zone_variant(&mut self) {
self.zone_variant.take();
}
#[diplomat::rust_link(icu::timezone::ZoneVariant::Standard, EnumVariant)]
#[diplomat::rust_link(icu::timezone::TimeZoneInfo::with_zone_variant, FnInStruct)]
pub fn set_standard_time(&mut self) {
self.zone_variant = Some(icu_timezone::ZoneVariant::Standard)
}
#[diplomat::rust_link(icu::timezone::ZoneVariant::Daylight, EnumVariant)]
#[diplomat::rust_link(icu::timezone::TimeZoneInfo::with_zone_variant, FnInStruct)]
pub fn set_daylight_time(&mut self) {
self.zone_variant = Some(icu_timezone::ZoneVariant::Daylight)
}
#[diplomat::rust_link(icu::timezone::ZoneVariant::Standard, EnumVariant)]
#[diplomat::rust_link(icu::timezone::TimeZoneInfo::zone_variant, FnInStruct, compact)]
#[diplomat::attr(auto, getter)]
pub fn is_standard_time(&self) -> Option<bool> {
Some(self.zone_variant? == icu_timezone::ZoneVariant::Standard)
}
#[diplomat::rust_link(icu::timezone::ZoneVariant::Daylight, EnumVariant)]
#[diplomat::rust_link(icu::timezone::TimeZoneInfo::zone_variant, FnInStruct, compact)]
#[diplomat::attr(auto, getter)]
pub fn is_daylight_time(&self) -> Option<bool> {
Some(self.zone_variant? == icu_timezone::ZoneVariant::Daylight)
}
#[diplomat::rust_link(icu::timezone::TimeZoneInfo::at_time, FnInStruct)]
#[diplomat::attr(auto, setter = "local_time")]
pub fn set_local_time(&mut self, datetime: &IsoDateTime) {
self.local_time = Some((datetime.0.date, datetime.0.time));
}
pub fn clear_local_time(&mut self) {
self.local_time.take();
}
#[diplomat::rust_link(icu::timezone::TimeZoneInfo::local_time, FnInStruct)]
#[diplomat::attr(auto, getter)]
pub fn get_local_time(&self) -> Option<Box<IsoDateTime>> {
self.local_time
.map(|(date, time)| Box::new(IsoDateTime(icu_calendar::DateTime { date, time })))
}
}
}
impl From<icu_timezone::TimeZoneInfo<icu_timezone::models::Base>> for TimeZoneInfo {
fn from(other: icu_timezone::TimeZoneInfo<icu_timezone::models::Base>) -> Self {
Self {
time_zone_id: other.time_zone_id(),
offset: other.offset(),
zone_variant: None,
local_time: None,
}
}
}