Coverage Report

Created: 2025-07-18 06:22

/rust/registry/src/index.crates.io-6f17d22bba15001f/icu_capi-1.5.1/src/date.rs
Line
Count
Source (jump to first uncovered line)
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
0
#[diplomat::bridge]
Unexecuted instantiation: ICU4XDate_create_from_iso_in_calendar
Unexecuted instantiation: ICU4XDate_create_from_codes_in_calendar
Unexecuted instantiation: ICU4XDate_to_calendar
Unexecuted instantiation: ICU4XDate_to_iso
Unexecuted instantiation: ICU4XDate_day_of_year
Unexecuted instantiation: ICU4XDate_day_of_month
Unexecuted instantiation: ICU4XDate_day_of_week
Unexecuted instantiation: ICU4XDate_week_of_month
Unexecuted instantiation: ICU4XDate_week_of_year
Unexecuted instantiation: ICU4XDate_ordinal_month
Unexecuted instantiation: ICU4XDate_month_code
Unexecuted instantiation: ICU4XDate_year_in_era
Unexecuted instantiation: ICU4XDate_era
Unexecuted instantiation: ICU4XDate_months_in_year
Unexecuted instantiation: ICU4XDate_days_in_month
Unexecuted instantiation: ICU4XDate_days_in_year
Unexecuted instantiation: ICU4XDate_calendar
Unexecuted instantiation: ICU4XDate_destroy
Unexecuted instantiation: ICU4XIsoDate_create
Unexecuted instantiation: ICU4XIsoDate_create_for_unix_epoch
Unexecuted instantiation: ICU4XIsoDate_to_calendar
Unexecuted instantiation: ICU4XIsoDate_to_any
Unexecuted instantiation: ICU4XIsoDate_day_of_year
Unexecuted instantiation: ICU4XIsoDate_day_of_month
Unexecuted instantiation: ICU4XIsoDate_day_of_week
Unexecuted instantiation: ICU4XIsoDate_week_of_month
Unexecuted instantiation: ICU4XIsoDate_week_of_year
Unexecuted instantiation: ICU4XIsoDate_month
Unexecuted instantiation: ICU4XIsoDate_year
Unexecuted instantiation: ICU4XIsoDate_is_in_leap_year
Unexecuted instantiation: ICU4XIsoDate_months_in_year
Unexecuted instantiation: ICU4XIsoDate_days_in_month
Unexecuted instantiation: ICU4XIsoDate_days_in_year
Unexecuted instantiation: ICU4XIsoDate_destroy
Unexecuted instantiation: ICU4XIsoWeekday_destroy
6
pub mod ffi {
7
    use alloc::boxed::Box;
8
    use alloc::sync::Arc;
9
    use core::fmt::Write;
10
    use icu_calendar::types::IsoWeekday;
11
    use icu_calendar::AnyCalendar;
12
    use icu_calendar::{Date, Iso};
13
    use tinystr::TinyAsciiStr;
14
15
    use crate::calendar::ffi::ICU4XCalendar;
16
    use crate::errors::ffi::ICU4XError;
17
18
    #[cfg(feature = "icu_calendar")]
19
    use crate::week::ffi::ICU4XWeekCalculator;
20
21
0
    #[diplomat::enum_convert(IsoWeekday)]
Unexecuted instantiation: <icu_calendar::types::IsoWeekday as core::convert::From<icu_capi::date::ffi::ICU4XIsoWeekday>>::from
Unexecuted instantiation: <icu_capi::date::ffi::ICU4XIsoWeekday as core::convert::From<icu_calendar::types::IsoWeekday>>::from
22
    pub enum ICU4XIsoWeekday {
23
        Monday = 1,
24
        Tuesday,
25
        Wednesday,
26
        Thursday,
27
        Friday,
28
        Saturday,
29
        Sunday,
30
    }
31
    #[diplomat::opaque]
32
0
    #[diplomat::transparent_convert]
33
    /// An ICU4X Date object capable of containing a ISO-8601 date
34
    #[diplomat::rust_link(icu::calendar::Date, Struct)]
35
    pub struct ICU4XIsoDate(pub Date<Iso>);
36
37
    impl ICU4XIsoDate {
38
        /// Creates a new [`ICU4XIsoDate`] from the specified date and time.
39
        #[diplomat::rust_link(icu::calendar::Date::try_new_iso_date, FnInStruct)]
40
        #[diplomat::attr(all(supports = constructors, supports = fallible_constructors), constructor)]
41
0
        pub fn create(year: i32, month: u8, day: u8) -> Result<Box<ICU4XIsoDate>, ICU4XError> {
42
0
            Ok(Box::new(ICU4XIsoDate(Date::try_new_iso_date(
43
0
                year, month, day,
44
0
            )?)))
45
0
        }
46
47
        /// Creates a new [`ICU4XIsoDate`] representing January 1, 1970.
48
        #[diplomat::rust_link(icu::calendar::Date::unix_epoch, FnInStruct)]
49
        #[diplomat::attr(all(supports = constructors, supports = fallible_constructors, supports = named_constructors), named_constructor = "for_unix_epoch")]
50
0
        pub fn create_for_unix_epoch() -> Box<ICU4XIsoDate> {
51
0
            Box::new(ICU4XIsoDate(Date::unix_epoch()))
52
0
        }
53
54
        /// Convert this date to one in a different calendar
55
        #[diplomat::rust_link(icu::calendar::Date::to_calendar, FnInStruct)]
56
0
        pub fn to_calendar(&self, calendar: &ICU4XCalendar) -> Box<ICU4XDate> {
57
0
            Box::new(ICU4XDate(self.0.to_calendar(calendar.0.clone())))
58
0
        }
59
60
        #[diplomat::rust_link(icu::calendar::Date::to_any, FnInStruct)]
61
0
        pub fn to_any(&self) -> Box<ICU4XDate> {
62
0
            Box::new(ICU4XDate(self.0.to_any().wrap_calendar_in_arc()))
63
0
        }
64
65
        /// Returns the 1-indexed day in the year for this date
66
        #[diplomat::rust_link(icu::calendar::Date::day_of_year_info, FnInStruct)]
67
        #[diplomat::attr(supports = accessors, getter)]
68
0
        pub fn day_of_year(&self) -> u16 {
69
0
            self.0.day_of_year_info().day_of_year
70
0
        }
71
72
        /// Returns the 1-indexed day in the month for this date
73
        #[diplomat::rust_link(icu::calendar::Date::day_of_month, FnInStruct)]
74
        #[diplomat::attr(supports = accessors, getter)]
75
0
        pub fn day_of_month(&self) -> u32 {
76
0
            self.0.day_of_month().0
77
0
        }
78
79
        /// Returns the day in the week for this day
80
        #[diplomat::rust_link(icu::calendar::Date::day_of_week, FnInStruct)]
81
        #[diplomat::attr(supports = accessors, getter)]
82
0
        pub fn day_of_week(&self) -> ICU4XIsoWeekday {
83
0
            self.0.day_of_week().into()
84
0
        }
85
86
        /// Returns the week number in this month, 1-indexed, based on what
87
        /// is considered the first day of the week (often a locale preference).
88
        ///
89
        /// `first_weekday` can be obtained via `first_weekday()` on [`ICU4XWeekCalculator`]
90
        #[diplomat::rust_link(icu::calendar::Date::week_of_month, FnInStruct)]
91
        #[diplomat::rust_link(
92
            icu::calendar::week::WeekCalculator::week_of_month,
93
            FnInStruct,
94
            hidden
95
        )]
96
0
        pub fn week_of_month(&self, first_weekday: ICU4XIsoWeekday) -> u32 {
97
0
            self.0.week_of_month(first_weekday.into()).0
98
0
        }
99
100
        /// Returns the week number in this year, using week data
101
        #[diplomat::rust_link(icu::calendar::Date::week_of_year, FnInStruct)]
102
        #[diplomat::rust_link(
103
            icu::calendar::week::WeekCalculator::week_of_year,
104
            FnInStruct,
105
            hidden
106
        )]
107
        #[cfg(feature = "icu_calendar")]
108
0
        pub fn week_of_year(
109
0
            &self,
110
0
            calculator: &ICU4XWeekCalculator,
111
0
        ) -> Result<crate::week::ffi::ICU4XWeekOf, ICU4XError> {
112
0
            Ok(self.0.week_of_year(&calculator.0)?.into())
113
0
        }
114
115
        /// Returns 1-indexed number of the month of this date in its year
116
        #[diplomat::rust_link(icu::calendar::Date::month, FnInStruct)]
117
        #[diplomat::attr(supports = accessors, getter)]
118
0
        pub fn month(&self) -> u32 {
119
0
            self.0.month().ordinal
120
0
        }
121
122
        /// Returns the year number for this date
123
        #[diplomat::rust_link(icu::calendar::Date::year, FnInStruct)]
124
        #[diplomat::attr(supports = accessors, getter)]
125
0
        pub fn year(&self) -> i32 {
126
0
            self.0.year().number
127
0
        }
128
129
        /// Returns if the year is a leap year for this date
130
        #[diplomat::rust_link(icu::calendar::Date::is_in_leap_year, FnInStruct)]
131
        #[diplomat::attr(supports = accessors, getter)]
132
0
        pub fn is_in_leap_year(&self) -> bool {
133
0
            self.0.is_in_leap_year()
134
0
        }
135
136
        /// Returns the number of months in the year represented by this date
137
        #[diplomat::rust_link(icu::calendar::Date::months_in_year, FnInStruct)]
138
        #[diplomat::attr(supports = accessors, getter)]
139
0
        pub fn months_in_year(&self) -> u8 {
140
0
            self.0.months_in_year()
141
0
        }
142
143
        /// Returns the number of days in the month represented by this date
144
        #[diplomat::rust_link(icu::calendar::Date::days_in_month, FnInStruct)]
145
        #[diplomat::attr(supports = accessors, getter)]
146
0
        pub fn days_in_month(&self) -> u8 {
147
0
            self.0.days_in_month()
148
0
        }
149
150
        /// Returns the number of days in the year represented by this date
151
        #[diplomat::rust_link(icu::calendar::Date::days_in_year, FnInStruct)]
152
        #[diplomat::attr(supports = accessors, getter)]
153
0
        pub fn days_in_year(&self) -> u16 {
154
0
            self.0.days_in_year()
155
0
        }
156
    }
157
158
    #[diplomat::opaque]
159
0
    #[diplomat::transparent_convert]
160
    /// An ICU4X Date object capable of containing a date and time for any calendar.
161
    #[diplomat::rust_link(icu::calendar::Date, Struct)]
162
    pub struct ICU4XDate(pub Date<Arc<AnyCalendar>>);
163
164
    impl ICU4XDate {
165
        /// Creates a new [`ICU4XDate`] representing the ISO date and time
166
        /// given but in a given calendar
167
        #[diplomat::rust_link(icu::calendar::Date::new_from_iso, FnInStruct)]
168
        #[diplomat::attr(all(supports = constructors, supports = fallible_constructors, supports = named_constructors), named_constructor = "from_iso_in_calendar")]
169
0
        pub fn create_from_iso_in_calendar(
170
0
            year: i32,
171
0
            month: u8,
172
0
            day: u8,
173
0
            calendar: &ICU4XCalendar,
174
0
        ) -> Result<Box<ICU4XDate>, ICU4XError> {
175
0
            let cal = calendar.0.clone();
176
0
            Ok(Box::new(ICU4XDate(
177
0
                Date::try_new_iso_date(year, month, day)?.to_calendar(cal),
178
            )))
179
0
        }
180
181
        /// Creates a new [`ICU4XDate`] from the given codes, which are interpreted in the given calendar system
182
        #[diplomat::rust_link(icu::calendar::Date::try_new_from_codes, FnInStruct)]
183
        #[diplomat::attr(all(supports = constructors, supports = fallible_constructors, supports = named_constructors), named_constructor = "from_codes_in_calendar")]
184
0
        pub fn create_from_codes_in_calendar(
185
0
            era_code: &DiplomatStr,
186
0
            year: i32,
187
0
            month_code: &DiplomatStr,
188
0
            day: u8,
189
0
            calendar: &ICU4XCalendar,
190
0
        ) -> Result<Box<ICU4XDate>, ICU4XError> {
191
0
            let era = TinyAsciiStr::from_bytes(era_code)
192
0
                .map_err(|_| ICU4XError::CalendarUnknownEraError)?
193
0
                .into();
194
0
            let month = TinyAsciiStr::from_bytes(month_code)
195
0
                .map_err(|_| ICU4XError::CalendarUnknownMonthCodeError)?
196
0
                .into();
197
0
            let cal = calendar.0.clone();
198
0
            Ok(Box::new(ICU4XDate(Date::try_new_from_codes(
199
0
                era, year, month, day, cal,
200
0
            )?)))
201
0
        }
202
203
        /// Convert this date to one in a different calendar
204
        #[diplomat::rust_link(icu::calendar::Date::to_calendar, FnInStruct)]
205
0
        pub fn to_calendar(&self, calendar: &ICU4XCalendar) -> Box<ICU4XDate> {
206
0
            Box::new(ICU4XDate(self.0.to_calendar(calendar.0.clone())))
207
0
        }
208
209
        /// Converts this date to ISO
210
        #[diplomat::rust_link(icu::calendar::Date::to_iso, FnInStruct)]
211
0
        pub fn to_iso(&self) -> Box<ICU4XIsoDate> {
212
0
            Box::new(ICU4XIsoDate(self.0.to_iso()))
213
0
        }
214
215
        /// Returns the 1-indexed day in the year for this date
216
        #[diplomat::rust_link(icu::calendar::Date::day_of_year_info, FnInStruct)]
217
        #[diplomat::attr(supports = accessors, getter)]
218
0
        pub fn day_of_year(&self) -> u16 {
219
0
            self.0.day_of_year_info().day_of_year
220
0
        }
221
222
        /// Returns the 1-indexed day in the month for this date
223
        #[diplomat::rust_link(icu::calendar::Date::day_of_month, FnInStruct)]
224
        #[diplomat::attr(supports = accessors, getter)]
225
0
        pub fn day_of_month(&self) -> u32 {
226
0
            self.0.day_of_month().0
227
0
        }
228
229
        /// Returns the day in the week for this day
230
        #[diplomat::rust_link(icu::calendar::Date::day_of_week, FnInStruct)]
231
        #[diplomat::attr(supports = accessors, getter)]
232
0
        pub fn day_of_week(&self) -> ICU4XIsoWeekday {
233
0
            self.0.day_of_week().into()
234
0
        }
235
236
        /// Returns the week number in this month, 1-indexed, based on what
237
        /// is considered the first day of the week (often a locale preference).
238
        ///
239
        /// `first_weekday` can be obtained via `first_weekday()` on [`ICU4XWeekCalculator`]
240
        #[diplomat::rust_link(icu::calendar::Date::week_of_month, FnInStruct)]
241
        #[diplomat::rust_link(
242
            icu::calendar::week::WeekCalculator::week_of_month,
243
            FnInStruct,
244
            hidden
245
        )]
246
0
        pub fn week_of_month(&self, first_weekday: ICU4XIsoWeekday) -> u32 {
247
0
            self.0.week_of_month(first_weekday.into()).0
248
0
        }
249
250
        /// Returns the week number in this year, using week data
251
        #[diplomat::rust_link(icu::calendar::Date::week_of_year, FnInStruct)]
252
        #[diplomat::rust_link(
253
            icu::calendar::week::WeekCalculator::week_of_year,
254
            FnInStruct,
255
            hidden
256
        )]
257
        #[cfg(feature = "icu_calendar")]
258
0
        pub fn week_of_year(
259
0
            &self,
260
0
            calculator: &ICU4XWeekCalculator,
261
0
        ) -> Result<crate::week::ffi::ICU4XWeekOf, ICU4XError> {
262
0
            Ok(self.0.week_of_year(&calculator.0)?.into())
263
0
        }
264
265
        /// Returns 1-indexed number of the month of this date in its year
266
        ///
267
        /// Note that for lunar calendars this may not lead to the same month
268
        /// having the same ordinal month across years; use month_code if you care
269
        /// about month identity.
270
        #[diplomat::rust_link(icu::calendar::Date::month, FnInStruct)]
271
        #[diplomat::attr(supports = accessors, getter)]
272
0
        pub fn ordinal_month(&self) -> u32 {
273
0
            self.0.month().ordinal
274
0
        }
275
276
        /// Returns the month code for this date. Typically something
277
        /// like "M01", "M02", but can be more complicated for lunar calendars.
278
        #[diplomat::rust_link(icu::calendar::Date::month, FnInStruct)]
279
        #[diplomat::attr(supports = accessors, getter)]
280
0
        pub fn month_code(
281
0
            &self,
282
0
            write: &mut diplomat_runtime::DiplomatWriteable,
283
0
        ) -> Result<(), ICU4XError> {
284
0
            let code = self.0.month().code;
285
0
            write.write_str(&code.0)?;
286
0
            Ok(())
287
0
        }
288
289
        /// Returns the year number in the current era for this date
290
        #[diplomat::rust_link(icu::calendar::Date::year, FnInStruct)]
291
        #[diplomat::attr(supports = accessors, getter)]
292
0
        pub fn year_in_era(&self) -> i32 {
293
0
            self.0.year().number
294
0
        }
295
296
        /// Returns the era for this date,
297
        #[diplomat::rust_link(icu::Date::year, FnInStruct)]
298
        #[diplomat::rust_link(icu::types::Era, Struct, compact)]
299
        #[diplomat::attr(supports = accessors, getter)]
300
0
        pub fn era(
301
0
            &self,
302
0
            write: &mut diplomat_runtime::DiplomatWriteable,
303
0
        ) -> Result<(), ICU4XError> {
304
0
            let era = self.0.year().era;
305
0
            write.write_str(&era.0)?;
306
0
            Ok(())
307
0
        }
308
309
        /// Returns the number of months in the year represented by this date
310
        #[diplomat::rust_link(icu::calendar::Date::months_in_year, FnInStruct)]
311
        #[diplomat::attr(supports = accessors, getter)]
312
0
        pub fn months_in_year(&self) -> u8 {
313
0
            self.0.months_in_year()
314
0
        }
315
316
        /// Returns the number of days in the month represented by this date
317
        #[diplomat::rust_link(icu::calendar::Date::days_in_month, FnInStruct)]
318
        #[diplomat::attr(supports = accessors, getter)]
319
0
        pub fn days_in_month(&self) -> u8 {
320
0
            self.0.days_in_month()
321
0
        }
322
323
        /// Returns the number of days in the year represented by this date
324
        #[diplomat::rust_link(icu::calendar::Date::days_in_year, FnInStruct)]
325
        #[diplomat::attr(supports = accessors, getter)]
326
0
        pub fn days_in_year(&self) -> u16 {
327
0
            self.0.days_in_year()
328
0
        }
329
330
        /// Returns the [`ICU4XCalendar`] object backing this date
331
        #[diplomat::rust_link(icu::calendar::Date::calendar, FnInStruct)]
332
        #[diplomat::rust_link(icu::calendar::Date::calendar_wrapper, FnInStruct, hidden)]
333
        #[diplomat::attr(supports = accessors, getter)]
334
0
        pub fn calendar(&self) -> Box<ICU4XCalendar> {
335
0
            Box::new(ICU4XCalendar(self.0.calendar_wrapper().clone()))
336
0
        }
337
    }
338
}