#date-time #literals #macro

datetime_literal

A macro for creating chrono::DateTime instances

4 releases

0.1.3 Jan 21, 2025
0.1.2 Jan 21, 2025
0.1.1 Jan 9, 2025
0.1.0 Jan 9, 2025

#136 in Date and time

Download history 255/week @ 2025-01-08 65/week @ 2025-01-15 121/week @ 2025-01-22

441 downloads per month

MIT license

6KB

Datetime Literal

This is an extremely simple crate that provides a macro for easily creating instances of chrono's DateTime:

Examples:

assert_eq!(
    datetime!(2024-01-02 13:14:15),
    "2024-01-02T13:14:15".parse().unwrap()
);
assert_eq!(
    datetime!(2024-01-02 13:14:15 Z),
    "2024-01-02T13:14:15Z".parse::<chrono::DateTime<chrono::Utc>>().unwrap()
);

assert_eq!(
    datetime!(2024-01-02 T 13:14:15),
    "2024-01-02T13:14:15".parse().unwrap()
);
assert_eq!(
    datetime!(2024-01-02 T 13:14:15 Z),
    "2024-01-02T13:14:15Z".parse::<chrono::DateTime<chrono::Utc>>().unwrap()
);

assert_eq!(
    datetime!(2024-01-02),
    "2024-01-02T00:00:00".parse().unwrap()
);
assert_eq!(
    datetime!(2024-01-02 Z),
    "2024-01-02T00:00:00Z".parse::<chrono::DateTime<chrono::Utc>>().unwrap()
);

Note! Because of how rust macros work, you must leave space before/after 'T', and before 'Z'.

Dependencies

~1MB
~18K SLoC