Debian Codegraph @

debian/rust-vendor/icu_calendar/src/lib.rs

raw
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 // https://github.com/unicode-org/icu4x/blob/main/documents/process/boilerplate.md#library-annotations
6 #![cfg_attr(not(any(test, doc)), no_std)]
7 #![cfg_attr(
8 not(test),
9 deny(
10 clippy::indexing_slicing,
11 clippy::unwrap_used,
12 clippy::expect_used,
13 clippy::panic,
14 )
15 )]
16 #![warn(missing_docs)]
17
18 //! Types for dealing with dates and custom calendars.
19 //!
20 //! This module is published as its own crate ([`icu_calendar`](https://docs.rs/icu_calendar/latest/icu_calendar/))
21 //! and as part of the [`icu`](https://docs.rs/icu/latest/icu/) crate. See the latter for more details on the ICU4X project.
22 //! The [`types`] module has a lot of common types for dealing with dates.
23 //!
24 //! [`Calendar`] is a trait that allows one to define custom calendars, and [`Date`]
25 //! can represent dates for arbitrary calendars.
26 //!
27 //! The [`Iso`] and [`Gregorian`] types are implementations for the ISO and
28 //! Gregorian calendars respectively. Further calendars can be found in the [`cal`] module.
29 //!
30 //! Most interaction with this crate will be done via the [`Date`] type.
31 //!
32 //! Some of the algorithms implemented here are based on
33 //! Dershowitz, Nachum, and Edward M. Reingold. _Calendrical calculations_. Cambridge University Press, 2008.
34 //! with associated Lisp code found at <https://github.com/EdReingold/calendar-code2>.
35 //!
36 //! # Examples
37 //!
38 //! Examples of date manipulation using `Date` object. `Date` objects are useful
39 //! for working with dates, encompassing information about the day, month, year,
40 //! as well as the calendar type.
41 //!
42 //! ```rust
43 //! use icu::calendar::{Date, types::Weekday};
44 //!
45 //! // Creating ISO date: 1992-09-02.
46 //! let mut date_iso = Date::try_new_iso(1992, 9, 2)
47 //! .expect("Failed to initialize ISO Date instance.");
48 //!
49 //! assert_eq!(date_iso.weekday(), Weekday::Wednesday);
50 //! assert_eq!(date_iso.era_year().year, 1992);
51 //! assert_eq!(date_iso.month().ordinal, 9);
52 //! assert_eq!(date_iso.day_of_month().0, 2);
53 //!
54 //! // Answering questions about days in month and year.
55 //! assert_eq!(date_iso.days_in_year(), 366);
56 //! assert_eq!(date_iso.days_in_month(), 30);
57 //! ```
58 //!
59 //! Example of converting an ISO date across Indian and Buddhist calendars.
60 //!
61 //! ```rust
62 //! use icu::calendar::Date;
63 //! use icu::calendar::cal::{Buddhist, Indian};
64 //!
65 //! // Creating ISO date: 1992-09-02.
66 //! let mut date_iso = Date::try_new_iso(1992, 9, 2)
67 //! .expect("Failed to initialize ISO Date instance.");
68 //!
69 //! assert_eq!(date_iso.era_year().year, 1992);
70 //! assert_eq!(date_iso.month().ordinal, 9);
71 //! assert_eq!(date_iso.day_of_month().0, 2);
72 //!
73 //! // Conversion into Indian calendar: 1914-08-02.
74 //! let date_indian = date_iso.to_calendar(Indian);
75 //! assert_eq!(date_indian.era_year().year, 1914);
76 //! assert_eq!(date_indian.month().ordinal, 6);
77 //! assert_eq!(date_indian.day_of_month().0, 11);
78 //!
79 //! // Conversion into Buddhist calendar: 2535-09-02.
80 //! let date_buddhist = date_iso.to_calendar(Buddhist);
81 //! assert_eq!(date_buddhist.era_year().year, 2535);
82 //! assert_eq!(date_buddhist.month().ordinal, 9);
83 //! assert_eq!(date_buddhist.day_of_month().0, 2);
84 //! ```
85 //!
86 //! [`ICU4X`]: ../icu/index.html
87
88 // It's not worth it to try and maintain clean import lists for both
89 // stable and unstable mode when the code is so entangled.
90 #![cfg_attr(not(feature = "unstable"), allow(unused))]
91
92 #[cfg(feature = "alloc")]
93 extern crate alloc;
94
95 // Make sure inherent docs go first
96 mod date;
97
98 // Public modules
99 mod any_calendar;
100 pub mod cal;
101 pub mod options;
102 pub mod provider;
103 pub mod types;
104 #[path = "third_party.rs"]
105 pub mod unstable_third_party;
106 pub mod week;
107
108 mod calendar;
109 mod calendar_arithmetic;
110 #[cfg(feature = "unstable_chrono_0_4")]
111 mod chrono;
112 mod duration;
113 pub mod error;
114 #[cfg(feature = "ixdtf")]
115 mod ixdtf;
116 #[cfg(feature = "unstable_jiff_0_2")]
117 mod jiff;
118 #[cfg(feature = "unstable_time_0_3")]
119 mod time_crate;
120
121 // Top-level types
122 pub use any_calendar::IntoAnyCalendar;
123 pub use calendar::Calendar;
124 pub use date::{AsCalendar, Date, Ref};
125 pub use error::{DateError, RangeError};
126 #[cfg(feature = "ixdtf")]
127 pub use ixdtf::ParseError;
128
129 // Reexports
130 #[doc(no_inline)]
131 pub use cal::{AnyCalendar, AnyCalendarKind, Gregorian, Iso};
132
133 pub mod preferences;
134
135 #[cfg(test)]
136 mod tests;