Debian Codegraph @

third_party/rust/time/src/macros.rs

raw
1 //! Macros to construct statically known values.
2
3 /// Construct a [`Date`](crate::Date) with a statically known value.
4 ///
5 /// The resulting expression can be used in `const` or `static` declarations.
6 ///
7 /// Three formats are supported: year-week-weekday, year-ordinal, and year-month-day.
8 ///
9 /// ```rust
10 /// # use time::{Date, Weekday::*, Month, macros::date};
11 /// assert_eq!(
12 /// date!(2020 - W 01 - 3),
13 /// Date::from_iso_week_date(2020, 1, Wednesday)?
14 /// );
15 /// assert_eq!(date!(2020 - 001), Date::from_ordinal_date(2020, 1)?);
16 /// assert_eq!(
17 /// date!(2020 - 01 - 01),
18 /// Date::from_calendar_date(2020, Month::January, 1)?
19 /// );
20 /// # Ok::<_, time::Error>(())
21 /// ```
22 pub use time_macros::date;
23 /// Construct a [`PrimitiveDateTime`] or [`OffsetDateTime`] with a statically known value.
24 ///
25 /// The resulting expression can be used in `const` or `static` declarations.
26 ///
27 /// The syntax accepted by this macro is the same as [`date!`] and [`time!`], with an optional
28 /// [`offset!`], all space-separated. If an [`offset!`] is provided, the resulting value will
29 /// be an [`OffsetDateTime`]; otherwise it will be a [`PrimitiveDateTime`].
30 ///
31 /// [`OffsetDateTime`]: crate::OffsetDateTime
32 /// [`PrimitiveDateTime`]: crate::PrimitiveDateTime
33 ///
34 /// ```rust
35 /// # use time::{Date, Month, macros::datetime, UtcOffset};
36 /// assert_eq!(
37 /// datetime!(2020-01-01 0:00),
38 /// Date::from_calendar_date(2020, Month::January, 1)?.midnight()
39 /// );
40 /// assert_eq!(
41 /// datetime!(2020-01-01 0:00 UTC),
42 /// Date::from_calendar_date(2020, Month::January, 1)?.midnight().assume_utc()
43 /// );
44 /// assert_eq!(
45 /// datetime!(2020-01-01 0:00 -1),
46 /// Date::from_calendar_date(2020, Month::January, 1)?.midnight()
47 /// .assume_offset(UtcOffset::from_hms(-1, 0, 0)?)
48 /// );
49 /// # Ok::<_, time::Error>(())
50 /// ```
51 pub use time_macros::datetime;
52 /// Equivalent of performing [`format_description::parse()`] at compile time.
53 ///
54 /// Using the macro instead of the function results in a static slice rather than a
55 /// [`Vec`](alloc::vec::Vec), such that it can be used in `#![no_alloc]` situations.
56 ///
57 /// The resulting expression can be used in `const` or `static` declarations, and implements
58 /// the sealed traits required for both formatting and parsing.
59 #[cfg_attr(feature = "alloc", doc = "```rust")]
60 #[cfg_attr(not(feature = "alloc"), doc = "```rust,ignore")]
61 /// # use time::{format_description, macros::format_description};
62 /// assert_eq!(
63 /// format_description!("[hour]:[minute]:[second]"),
64 /// format_description::parse("[hour]:[minute]:[second]")?
65 /// );
66 /// # Ok::<_, time::Error>(())
67 /// ```
68 ///
69 /// The syntax accepted by this macro is the same as [`format_description::parse()`], which can
70 /// be found in [the book](https://time-rs.github.io/book/api/format-description.html).
71 ///
72 /// [`format_description::parse()`]: crate::format_description::parse()
73 #[cfg(any(feature = "formatting", feature = "parsing"))]
74 pub use time_macros::format_description;
75 /// Construct a [`UtcOffset`](crate::UtcOffset) with a statically known value.
76 ///
77 /// The resulting expression can be used in `const` or `static` declarations.
78 ///
79 /// A sign and the hour must be provided; minutes and seconds default to zero. `UTC` (both
80 /// uppercase and lowercase) is also allowed.
81 ///
82 /// ```rust
83 /// # use time::{UtcOffset, macros::offset};
84 /// assert_eq!(offset!(UTC), UtcOffset::from_hms(0, 0, 0)?);
85 /// assert_eq!(offset!(utc), UtcOffset::from_hms(0, 0, 0)?);
86 /// assert_eq!(offset!(+0), UtcOffset::from_hms(0, 0, 0)?);
87 /// assert_eq!(offset!(+1), UtcOffset::from_hms(1, 0, 0)?);
88 /// assert_eq!(offset!(-1), UtcOffset::from_hms(-1, 0, 0)?);
89 /// assert_eq!(offset!(+1:30), UtcOffset::from_hms(1, 30, 0)?);
90 /// assert_eq!(offset!(-1:30), UtcOffset::from_hms(-1, -30, 0)?);
91 /// assert_eq!(offset!(+1:30:59), UtcOffset::from_hms(1, 30, 59)?);
92 /// assert_eq!(offset!(-1:30:59), UtcOffset::from_hms(-1, -30, -59)?);
93 /// assert_eq!(offset!(+23:59:59), UtcOffset::from_hms(23, 59, 59)?);
94 /// assert_eq!(offset!(-23:59:59), UtcOffset::from_hms(-23, -59, -59)?);
95 /// # Ok::<_, time::Error>(())
96 /// ```
97 pub use time_macros::offset;
98 /// Construct a [`Time`](crate::Time) with a statically known value.
99 ///
100 /// The resulting expression can be used in `const` or `static` declarations.
101 ///
102 /// Hours and minutes must be provided, while seconds defaults to zero. AM/PM is allowed
103 /// (either uppercase or lowercase). Any number of subsecond digits may be provided (though any
104 /// past nine will be discarded).
105 ///
106 /// All components are validated at compile-time. An error will be raised if any value is
107 /// invalid.
108 ///
109 /// ```rust
110 /// # use time::{Time, macros::time};
111 /// assert_eq!(time!(0:00), Time::from_hms(0, 0, 0)?);
112 /// assert_eq!(time!(1:02:03), Time::from_hms(1, 2, 3)?);
113 /// assert_eq!(
114 /// time!(1:02:03.004_005_006),
115 /// Time::from_hms_nano(1, 2, 3, 4_005_006)?
116 /// );
117 /// assert_eq!(time!(12:00 am), Time::from_hms(0, 0, 0)?);
118 /// assert_eq!(time!(1:02:03 am), Time::from_hms(1, 2, 3)?);
119 /// assert_eq!(
120 /// time!(1:02:03.004_005_006 am),
121 /// Time::from_hms_nano(1, 2, 3, 4_005_006)?
122 /// );
123 /// assert_eq!(time!(12 pm), Time::from_hms(12, 0, 0)?);
124 /// assert_eq!(time!(12:00 pm), Time::from_hms(12, 0, 0)?);
125 /// assert_eq!(time!(1:02:03 pm), Time::from_hms(13, 2, 3)?);
126 /// assert_eq!(
127 /// time!(1:02:03.004_005_006 pm),
128 /// Time::from_hms_nano(13, 2, 3, 4_005_006)?
129 /// );
130 /// # Ok::<_, time::Error>(())
131 /// ```
132 pub use time_macros::time;