timezone_formatter.rs (22788B)
1 // This file is part of ICU4X. For terms of use, please see the file 2 // called LICENSE at the top level of the ICU4X source tree 3 // (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). 4 5 // @generated by tools/make/codegen/src/capi_datetime.rs 6 7 #[rustfmt::skip] 8 #[diplomat::bridge] 9 #[diplomat::abi_rename = "icu4x_{0}_mv1"] 10 #[diplomat::attr(auto, namespace = "icu4x")] 11 pub mod ffi { 12 use alloc::boxed::Box; 13 use writeable::TryWriteable; 14 15 #[allow(unused_imports)] 16 use crate::datetime_helpers::{self, map_or_default}; 17 18 #[allow(unused_imports)] 19 use crate::unstable::{ 20 date_formatter::ffi::{DateFormatter, DateFormatterGregorian}, 21 date_time_formatter::ffi::{DateTimeFormatter, DateTimeFormatterGregorian}, 22 date::ffi::IsoDate, 23 datetime_options::ffi::{DateTimeAlignment, DateTimeLength, TimePrecision}, 24 errors::ffi::DateTimeFormatterLoadError, 25 errors::ffi::DateTimeWriteError, 26 locale_core::ffi::Locale, 27 time_formatter::ffi::TimeFormatter, 28 time::ffi::Time, 29 timezone::ffi::TimeZoneInfo, 30 }; 31 32 #[cfg(feature = "buffer_provider")] 33 use crate::unstable::provider::ffi::DataProvider; 34 35 #[diplomat::opaque] 36 #[diplomat::rust_link(icu::datetime::NoCalendarFormatter, Typedef)] 37 pub struct TimeZoneFormatter( 38 pub icu_datetime::FixedCalendarDateTimeFormatter< 39 (), 40 icu_datetime::fieldsets::enums::ZoneFieldSet, 41 >, 42 ); 43 44 impl TimeZoneFormatter { 45 /// Creates a zoned formatter based on a non-zoned formatter. 46 /// 47 /// Caution: The locale provided here must match the locale used to construct the non-zoned formatter, 48 /// or else unexpected behavior may occur! 49 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "specific_long")] 50 #[diplomat::rust_link(icu::datetime::fieldsets::zone::SpecificLong, Struct)] 51 #[diplomat::rust_link(icu::datetime::fieldsets::Combo, Struct, hidden)] 52 #[cfg(feature = "compiled_data")] 53 pub fn create_specific_long( 54 locale: &Locale, 55 ) -> Result<Box<Self>, DateTimeFormatterLoadError> { 56 let zone = icu_datetime::fieldsets::zone::SpecificLong; 57 let prefs = (&locale.0).into(); 58 let options = zone; 59 Ok(Box::new(Self( 60 icu_datetime 61 ::FixedCalendarDateTimeFormatter 62 ::try_new( 63 prefs, 64 options 65 )? 66 .cast_into_fset(), 67 ))) 68 } 69 70 /// Creates a zoned formatter based on a non-zoned formatter. 71 /// 72 /// Caution: The locale provided here must match the locale used to construct the non-zoned formatter, 73 /// or else unexpected behavior may occur! 74 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "specific_long_with_provider")] 75 #[diplomat::rust_link(icu::datetime::fieldsets::zone::SpecificLong, Struct)] 76 #[diplomat::rust_link(icu::datetime::fieldsets::Combo, Struct, hidden)] 77 #[cfg(feature = "buffer_provider")] 78 pub fn create_specific_long_with_provider( 79 provider: &DataProvider, 80 locale: &Locale, 81 ) -> Result<Box<Self>, DateTimeFormatterLoadError> { 82 let provider = provider.get()?; 83 let zone = icu_datetime::fieldsets::zone::SpecificLong; 84 let prefs = (&locale.0).into(); 85 let options = zone; 86 Ok(Box::new(Self( 87 icu_datetime 88 ::FixedCalendarDateTimeFormatter 89 ::try_new_with_buffer_provider( 90 provider, 91 prefs, 92 options 93 )? 94 .cast_into_fset(), 95 ))) 96 } 97 98 /// Creates a zoned formatter based on a non-zoned formatter. 99 /// 100 /// Caution: The locale provided here must match the locale used to construct the non-zoned formatter, 101 /// or else unexpected behavior may occur! 102 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "specific_short")] 103 #[diplomat::rust_link(icu::datetime::fieldsets::zone::SpecificShort, Struct)] 104 #[diplomat::rust_link(icu::datetime::fieldsets::Combo, Struct, hidden)] 105 #[cfg(feature = "compiled_data")] 106 pub fn create_specific_short( 107 locale: &Locale, 108 ) -> Result<Box<Self>, DateTimeFormatterLoadError> { 109 let zone = icu_datetime::fieldsets::zone::SpecificShort; 110 let prefs = (&locale.0).into(); 111 let options = zone; 112 Ok(Box::new(Self( 113 icu_datetime 114 ::FixedCalendarDateTimeFormatter 115 ::try_new( 116 prefs, 117 options 118 )? 119 .cast_into_fset(), 120 ))) 121 } 122 123 /// Creates a zoned formatter based on a non-zoned formatter. 124 /// 125 /// Caution: The locale provided here must match the locale used to construct the non-zoned formatter, 126 /// or else unexpected behavior may occur! 127 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "specific_short_with_provider")] 128 #[diplomat::rust_link(icu::datetime::fieldsets::zone::SpecificShort, Struct)] 129 #[diplomat::rust_link(icu::datetime::fieldsets::Combo, Struct, hidden)] 130 #[cfg(feature = "buffer_provider")] 131 pub fn create_specific_short_with_provider( 132 provider: &DataProvider, 133 locale: &Locale, 134 ) -> Result<Box<Self>, DateTimeFormatterLoadError> { 135 let provider = provider.get()?; 136 let zone = icu_datetime::fieldsets::zone::SpecificShort; 137 let prefs = (&locale.0).into(); 138 let options = zone; 139 Ok(Box::new(Self( 140 icu_datetime 141 ::FixedCalendarDateTimeFormatter 142 ::try_new_with_buffer_provider( 143 provider, 144 prefs, 145 options 146 )? 147 .cast_into_fset(), 148 ))) 149 } 150 151 /// Creates a zoned formatter based on a non-zoned formatter. 152 /// 153 /// Caution: The locale provided here must match the locale used to construct the non-zoned formatter, 154 /// or else unexpected behavior may occur! 155 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "localized_offset_long")] 156 #[diplomat::rust_link(icu::datetime::fieldsets::zone::LocalizedOffsetLong, Struct)] 157 #[diplomat::rust_link(icu::datetime::fieldsets::Combo, Struct, hidden)] 158 #[cfg(feature = "compiled_data")] 159 pub fn create_localized_offset_long( 160 locale: &Locale, 161 ) -> Result<Box<Self>, DateTimeFormatterLoadError> { 162 let zone = icu_datetime::fieldsets::zone::LocalizedOffsetLong; 163 let prefs = (&locale.0).into(); 164 let options = zone; 165 Ok(Box::new(Self( 166 icu_datetime 167 ::FixedCalendarDateTimeFormatter 168 ::try_new( 169 prefs, 170 options 171 )? 172 .cast_into_fset(), 173 ))) 174 } 175 176 /// Creates a zoned formatter based on a non-zoned formatter. 177 /// 178 /// Caution: The locale provided here must match the locale used to construct the non-zoned formatter, 179 /// or else unexpected behavior may occur! 180 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "localized_offset_long_with_provider")] 181 #[diplomat::rust_link(icu::datetime::fieldsets::zone::LocalizedOffsetLong, Struct)] 182 #[diplomat::rust_link(icu::datetime::fieldsets::Combo, Struct, hidden)] 183 #[cfg(feature = "buffer_provider")] 184 pub fn create_localized_offset_long_with_provider( 185 provider: &DataProvider, 186 locale: &Locale, 187 ) -> Result<Box<Self>, DateTimeFormatterLoadError> { 188 let provider = provider.get()?; 189 let zone = icu_datetime::fieldsets::zone::LocalizedOffsetLong; 190 let prefs = (&locale.0).into(); 191 let options = zone; 192 Ok(Box::new(Self( 193 icu_datetime 194 ::FixedCalendarDateTimeFormatter 195 ::try_new_with_buffer_provider( 196 provider, 197 prefs, 198 options 199 )? 200 .cast_into_fset(), 201 ))) 202 } 203 204 /// Creates a zoned formatter based on a non-zoned formatter. 205 /// 206 /// Caution: The locale provided here must match the locale used to construct the non-zoned formatter, 207 /// or else unexpected behavior may occur! 208 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "localized_offset_short")] 209 #[diplomat::rust_link(icu::datetime::fieldsets::zone::LocalizedOffsetShort, Struct)] 210 #[diplomat::rust_link(icu::datetime::fieldsets::Combo, Struct, hidden)] 211 #[cfg(feature = "compiled_data")] 212 pub fn create_localized_offset_short( 213 locale: &Locale, 214 ) -> Result<Box<Self>, DateTimeFormatterLoadError> { 215 let zone = icu_datetime::fieldsets::zone::LocalizedOffsetShort; 216 let prefs = (&locale.0).into(); 217 let options = zone; 218 Ok(Box::new(Self( 219 icu_datetime 220 ::FixedCalendarDateTimeFormatter 221 ::try_new( 222 prefs, 223 options 224 )? 225 .cast_into_fset(), 226 ))) 227 } 228 229 /// Creates a zoned formatter based on a non-zoned formatter. 230 /// 231 /// Caution: The locale provided here must match the locale used to construct the non-zoned formatter, 232 /// or else unexpected behavior may occur! 233 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "localized_offset_short_with_provider")] 234 #[diplomat::rust_link(icu::datetime::fieldsets::zone::LocalizedOffsetShort, Struct)] 235 #[diplomat::rust_link(icu::datetime::fieldsets::Combo, Struct, hidden)] 236 #[cfg(feature = "buffer_provider")] 237 pub fn create_localized_offset_short_with_provider( 238 provider: &DataProvider, 239 locale: &Locale, 240 ) -> Result<Box<Self>, DateTimeFormatterLoadError> { 241 let provider = provider.get()?; 242 let zone = icu_datetime::fieldsets::zone::LocalizedOffsetShort; 243 let prefs = (&locale.0).into(); 244 let options = zone; 245 Ok(Box::new(Self( 246 icu_datetime 247 ::FixedCalendarDateTimeFormatter 248 ::try_new_with_buffer_provider( 249 provider, 250 prefs, 251 options 252 )? 253 .cast_into_fset(), 254 ))) 255 } 256 257 /// Creates a zoned formatter based on a non-zoned formatter. 258 /// 259 /// Caution: The locale provided here must match the locale used to construct the non-zoned formatter, 260 /// or else unexpected behavior may occur! 261 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "generic_long")] 262 #[diplomat::rust_link(icu::datetime::fieldsets::zone::GenericLong, Struct)] 263 #[diplomat::rust_link(icu::datetime::fieldsets::Combo, Struct, hidden)] 264 #[cfg(feature = "compiled_data")] 265 pub fn create_generic_long( 266 locale: &Locale, 267 ) -> Result<Box<Self>, DateTimeFormatterLoadError> { 268 let zone = icu_datetime::fieldsets::zone::GenericLong; 269 let prefs = (&locale.0).into(); 270 let options = zone; 271 Ok(Box::new(Self( 272 icu_datetime 273 ::FixedCalendarDateTimeFormatter 274 ::try_new( 275 prefs, 276 options 277 )? 278 .cast_into_fset(), 279 ))) 280 } 281 282 /// Creates a zoned formatter based on a non-zoned formatter. 283 /// 284 /// Caution: The locale provided here must match the locale used to construct the non-zoned formatter, 285 /// or else unexpected behavior may occur! 286 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "generic_long_with_provider")] 287 #[diplomat::rust_link(icu::datetime::fieldsets::zone::GenericLong, Struct)] 288 #[diplomat::rust_link(icu::datetime::fieldsets::Combo, Struct, hidden)] 289 #[cfg(feature = "buffer_provider")] 290 pub fn create_generic_long_with_provider( 291 provider: &DataProvider, 292 locale: &Locale, 293 ) -> Result<Box<Self>, DateTimeFormatterLoadError> { 294 let provider = provider.get()?; 295 let zone = icu_datetime::fieldsets::zone::GenericLong; 296 let prefs = (&locale.0).into(); 297 let options = zone; 298 Ok(Box::new(Self( 299 icu_datetime 300 ::FixedCalendarDateTimeFormatter 301 ::try_new_with_buffer_provider( 302 provider, 303 prefs, 304 options 305 )? 306 .cast_into_fset(), 307 ))) 308 } 309 310 /// Creates a zoned formatter based on a non-zoned formatter. 311 /// 312 /// Caution: The locale provided here must match the locale used to construct the non-zoned formatter, 313 /// or else unexpected behavior may occur! 314 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "generic_short")] 315 #[diplomat::rust_link(icu::datetime::fieldsets::zone::GenericShort, Struct)] 316 #[diplomat::rust_link(icu::datetime::fieldsets::Combo, Struct, hidden)] 317 #[diplomat::demo(default_constructor)] 318 #[cfg(feature = "compiled_data")] 319 pub fn create_generic_short( 320 locale: &Locale, 321 ) -> Result<Box<Self>, DateTimeFormatterLoadError> { 322 let zone = icu_datetime::fieldsets::zone::GenericShort; 323 let prefs = (&locale.0).into(); 324 let options = zone; 325 Ok(Box::new(Self( 326 icu_datetime 327 ::FixedCalendarDateTimeFormatter 328 ::try_new( 329 prefs, 330 options 331 )? 332 .cast_into_fset(), 333 ))) 334 } 335 336 /// Creates a zoned formatter based on a non-zoned formatter. 337 /// 338 /// Caution: The locale provided here must match the locale used to construct the non-zoned formatter, 339 /// or else unexpected behavior may occur! 340 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "generic_short_with_provider")] 341 #[diplomat::rust_link(icu::datetime::fieldsets::zone::GenericShort, Struct)] 342 #[diplomat::rust_link(icu::datetime::fieldsets::Combo, Struct, hidden)] 343 #[cfg(feature = "buffer_provider")] 344 pub fn create_generic_short_with_provider( 345 provider: &DataProvider, 346 locale: &Locale, 347 ) -> Result<Box<Self>, DateTimeFormatterLoadError> { 348 let provider = provider.get()?; 349 let zone = icu_datetime::fieldsets::zone::GenericShort; 350 let prefs = (&locale.0).into(); 351 let options = zone; 352 Ok(Box::new(Self( 353 icu_datetime 354 ::FixedCalendarDateTimeFormatter 355 ::try_new_with_buffer_provider( 356 provider, 357 prefs, 358 options 359 )? 360 .cast_into_fset(), 361 ))) 362 } 363 364 /// Creates a zoned formatter based on a non-zoned formatter. 365 /// 366 /// Caution: The locale provided here must match the locale used to construct the non-zoned formatter, 367 /// or else unexpected behavior may occur! 368 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "location")] 369 #[diplomat::rust_link(icu::datetime::fieldsets::zone::Location, Struct)] 370 #[diplomat::rust_link(icu::datetime::fieldsets::Combo, Struct, hidden)] 371 #[cfg(feature = "compiled_data")] 372 pub fn create_location( 373 locale: &Locale, 374 ) -> Result<Box<Self>, DateTimeFormatterLoadError> { 375 let zone = icu_datetime::fieldsets::zone::Location; 376 let prefs = (&locale.0).into(); 377 let options = zone; 378 Ok(Box::new(Self( 379 icu_datetime 380 ::FixedCalendarDateTimeFormatter 381 ::try_new( 382 prefs, 383 options 384 )? 385 .cast_into_fset(), 386 ))) 387 } 388 389 /// Creates a zoned formatter based on a non-zoned formatter. 390 /// 391 /// Caution: The locale provided here must match the locale used to construct the non-zoned formatter, 392 /// or else unexpected behavior may occur! 393 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "location_with_provider")] 394 #[diplomat::rust_link(icu::datetime::fieldsets::zone::Location, Struct)] 395 #[diplomat::rust_link(icu::datetime::fieldsets::Combo, Struct, hidden)] 396 #[cfg(feature = "buffer_provider")] 397 pub fn create_location_with_provider( 398 provider: &DataProvider, 399 locale: &Locale, 400 ) -> Result<Box<Self>, DateTimeFormatterLoadError> { 401 let provider = provider.get()?; 402 let zone = icu_datetime::fieldsets::zone::Location; 403 let prefs = (&locale.0).into(); 404 let options = zone; 405 Ok(Box::new(Self( 406 icu_datetime 407 ::FixedCalendarDateTimeFormatter 408 ::try_new_with_buffer_provider( 409 provider, 410 prefs, 411 options 412 )? 413 .cast_into_fset(), 414 ))) 415 } 416 417 /// Creates a zoned formatter based on a non-zoned formatter. 418 /// 419 /// Caution: The locale provided here must match the locale used to construct the non-zoned formatter, 420 /// or else unexpected behavior may occur! 421 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "exemplar_city")] 422 #[diplomat::rust_link(icu::datetime::fieldsets::zone::ExemplarCity, Struct)] 423 #[diplomat::rust_link(icu::datetime::fieldsets::Combo, Struct, hidden)] 424 #[cfg(feature = "compiled_data")] 425 pub fn create_exemplar_city( 426 locale: &Locale, 427 ) -> Result<Box<Self>, DateTimeFormatterLoadError> { 428 let zone = icu_datetime::fieldsets::zone::ExemplarCity; 429 let prefs = (&locale.0).into(); 430 let options = zone; 431 Ok(Box::new(Self( 432 icu_datetime 433 ::FixedCalendarDateTimeFormatter 434 ::try_new( 435 prefs, 436 options 437 )? 438 .cast_into_fset(), 439 ))) 440 } 441 442 /// Creates a zoned formatter based on a non-zoned formatter. 443 /// 444 /// Caution: The locale provided here must match the locale used to construct the non-zoned formatter, 445 /// or else unexpected behavior may occur! 446 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "exemplar_city_with_provider")] 447 #[diplomat::rust_link(icu::datetime::fieldsets::zone::ExemplarCity, Struct)] 448 #[diplomat::rust_link(icu::datetime::fieldsets::Combo, Struct, hidden)] 449 #[cfg(feature = "buffer_provider")] 450 pub fn create_exemplar_city_with_provider( 451 provider: &DataProvider, 452 locale: &Locale, 453 ) -> Result<Box<Self>, DateTimeFormatterLoadError> { 454 let provider = provider.get()?; 455 let zone = icu_datetime::fieldsets::zone::ExemplarCity; 456 let prefs = (&locale.0).into(); 457 let options = zone; 458 Ok(Box::new(Self( 459 icu_datetime 460 ::FixedCalendarDateTimeFormatter 461 ::try_new_with_buffer_provider( 462 provider, 463 prefs, 464 options 465 )? 466 .cast_into_fset(), 467 ))) 468 } 469 470 #[diplomat::rust_link(icu::datetime::FixedCalendarDateTimeFormatter::format, FnInStruct)] 471 #[diplomat::rust_link(icu::datetime::FormattedDateTime, Struct, hidden)] 472 #[diplomat::rust_link(icu::datetime::FormattedDateTime::to_string, FnInStruct, hidden)] 473 pub fn format( 474 &self, 475 zone: &TimeZoneInfo, 476 write: &mut diplomat_runtime::DiplomatWrite, 477 ) -> Result<(), DateTimeWriteError> { 478 let mut input = icu_datetime::unchecked::DateTimeInputUnchecked::default(); 479 input.set_time_zone_id(zone.id); 480 if let Some(offset) = zone.offset { 481 input.set_time_zone_utc_offset(offset); 482 } 483 if let Some(zone_name_timestamp) = zone.zone_name_timestamp { 484 input.set_time_zone_name_timestamp(zone_name_timestamp); 485 } 486 else { 487 input.set_time_zone_name_timestamp(icu_time::zone::ZoneNameTimestamp::far_in_future()) 488 } 489 if let Some(variant) = zone.variant { 490 input.set_time_zone_variant(variant); 491 } 492 let _infallible = self 493 .0 494 .format_unchecked(input) 495 .try_write_to(write) 496 .ok() 497 .transpose()?; 498 Ok(()) 499 } 500 } 501 502 }