Backed out changeset 2450366cf7ca (bug 1891629) for causing win msix mochitest failures
[gecko.git] / intl / icu_capi / src / time.rs
blob2a6e6e4d6e40647d6d3a4900d6f7c098f4715175
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 ).
5 #[diplomat::bridge]
6 pub mod ffi {
7     use alloc::boxed::Box;
9     use icu_calendar::types::Time;
11     use crate::errors::ffi::ICU4XError;
13     #[diplomat::opaque]
14     /// An ICU4X Time object representing a time in terms of hour, minute, second, nanosecond
15     #[diplomat::rust_link(icu::calendar::types::Time, Struct)]
16     pub struct ICU4XTime(pub Time);
18     impl ICU4XTime {
19         /// Creates a new [`ICU4XTime`] given field values
20         #[diplomat::rust_link(icu::calendar::types::Time, Struct)]
21         pub fn create(
22             hour: u8,
23             minute: u8,
24             second: u8,
25             nanosecond: u32,
26         ) -> Result<Box<ICU4XTime>, ICU4XError> {
27             let hour = hour.try_into()?;
28             let minute = minute.try_into()?;
29             let second = second.try_into()?;
30             let nanosecond = nanosecond.try_into()?;
31             let time = Time {
32                 hour,
33                 minute,
34                 second,
35                 nanosecond,
36             };
37             Ok(Box::new(ICU4XTime(time)))
38         }
40         /// Creates a new [`ICU4XTime`] representing midnight (00:00.000).
41         #[diplomat::rust_link(icu::calendar::types::Time, Struct)]
42         pub fn create_midnight() -> Result<Box<ICU4XTime>, ICU4XError> {
43             let time = Time::midnight();
44             Ok(Box::new(ICU4XTime(time)))
45         }
47         /// Returns the hour in this time
48         #[diplomat::rust_link(icu::calendar::types::Time::hour, StructField)]
49         pub fn hour(&self) -> u8 {
50             self.0.hour.into()
51         }
52         /// Returns the minute in this time
53         #[diplomat::rust_link(icu::calendar::types::Time::minute, StructField)]
54         pub fn minute(&self) -> u8 {
55             self.0.minute.into()
56         }
57         /// Returns the second in this time
58         #[diplomat::rust_link(icu::calendar::types::Time::second, StructField)]
59         pub fn second(&self) -> u8 {
60             self.0.second.into()
61         }
62         /// Returns the nanosecond in this time
63         #[diplomat::rust_link(icu::calendar::types::Time::nanosecond, StructField)]
64         pub fn nanosecond(&self) -> u32 {
65             self.0.nanosecond.into()
66         }
67     }