#date-time #literals #macro

datetime_literal

A macro for creating chrono::DateTime instances

2 releases

new 0.1.1 Jan 9, 2025
0.1.0 Jan 9, 2025

#165 in Date and time

Download history 205/week @ 2025-01-05

205 downloads per month

MIT license

5KB

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