Skip to main content

datetime_literal/
lib.rs

1pub use chrono;
2
3/// Creates chrono::DateTime values.
4///
5/// The syntax is inspired by ISO 8601, but because of how rust macros work it cannot
6/// support the exact same format.
7///
8/// Examples:
9///
10/// ```rust
11///     use datetime_literal::datetime;
12///
13///     assert_eq!(
14///         datetime!(2024-01-02 13:14:15),
15///         "2024-01-02T13:14:15".parse().unwrap()
16///     );
17///     assert_eq!(
18///         datetime!(2024-01-02 13:14:15 Z),
19///         "2024-01-02T13:14:15Z".parse::<chrono::DateTime<chrono::Utc>>().unwrap()
20///     );
21///
22///     assert_eq!(
23///         datetime!(2024-01-02 T 13:14:15),
24///         "2024-01-02T13:14:15".parse().unwrap()
25///     );
26///     assert_eq!(
27///         datetime!(2024-01-02 T 13:14:15 Z),
28///         "2024-01-02T13:14:15Z".parse::<chrono::DateTime<chrono::Utc>>().unwrap()
29///     );
30///
31///     assert_eq!(
32///         datetime!(2024-01-02),
33///         "2024-01-02T00:00:00".parse().unwrap()
34///     );
35///     assert_eq!(
36///         datetime!(2024-01-02 Z),
37///         "2024-01-02T00:00:00Z".parse::<chrono::DateTime<chrono::Utc>>().unwrap()
38///     );
39///
40/// ```
41/// Note! Because of how rust macros work, you must leave space before/after 'T', and before 'Z'.
42///
43#[macro_export]
44macro_rules! datetime {
45    ( $year:literal-$month:literal-$day:literal Z)  => {
46
47        const {
48            #[allow(clippy::zero_prefixed_literal)]
49            $crate::chrono::DateTime::<$crate::chrono::Utc>::from_naive_utc_and_offset(
50            datetime!($year - $month - $day),
51            $crate::chrono::Utc
52        ) }
53    };
54    ( $year:literal-$month:literal-$day:literal)  => {
55
56        const {
57            #[allow(clippy::zero_prefixed_literal)]
58            $crate::chrono::NaiveDateTime::new(
59
60            $crate::chrono::NaiveDate::from_ymd_opt($year, $month, $day).expect("date values must be in valid range"),
61            $crate::chrono::NaiveTime::from_hms_opt(0,0,0).unwrap()
62        ) }
63    };
64    ( $year:literal-$month:literal-$day:literal $(T)? $hour:literal:$min:literal:$second:literal Z)  => {
65
66        const {
67            #[allow(clippy::zero_prefixed_literal)]
68            $crate::chrono::DateTime::<$crate::chrono::Utc>::from_naive_utc_and_offset(
69
70            datetime!($year - $month - $day $hour:$min:$second),
71            $crate::chrono::Utc
72        ) }
73    };
74    ( $year:literal-$month:literal-$day:literal $(T)? $hour:literal:$min:literal:$second:literal)  => {
75
76        const {
77            #[allow(clippy::zero_prefixed_literal)]
78            $crate::chrono::NaiveDateTime::new(
79            $crate::chrono::NaiveDate::from_ymd_opt($year, $month, $day).expect("date values must be in valid range"),
80            $crate::chrono::NaiveTime::from_hms_opt($hour, $min, $second).expect("time values must be in valid range")
81        ) }
82    };
83}
84
85#[cfg(test)]
86mod tests {
87    #[test]
88    fn test() {
89        assert_eq!(datetime!(1970-01-01 0:00:00 Z).timestamp(), 0);
90    }
91}