#iso-8601 #date-time #parser #winnow

no-std winnow_iso8601

Parsing ISO8601 dates using winnow

4 releases (2 breaking)

new 0.3.0 Dec 17, 2024
0.2.1 Dec 15, 2024
0.2.0 Nov 17, 2024
0.1.0 Nov 15, 2024

#168 in Date and time

Download history 202/week @ 2024-11-14 37/week @ 2024-11-21 13/week @ 2024-11-28 14/week @ 2024-12-05 158/week @ 2024-12-12

232 downloads per month
Used in mysql-slowlog-parser

MIT license

72KB
1.5K SLoC

winnow-iso8601, making parsing ISO8601 dates a breeze

crates.io docs.rs docs

About

This library contains parsers for parsing ISO8601 dates and their various components.

Parsing

Complete

If you have all the data you need, you can just pass along the input directly.

let datetime = opt(parse_datetime)
    .parse_next(&mut "2015-06-26T16:43:23+0200"));

// the above will give you:
Some(DateTime {
    date: Date::YMD {
        year: 2015,
        month: 6,
        day: 26,
    },
    time: Time {
        hour: 16,
        minute: 43,
        second: 23,
        tz_offset_hours: 2,
        tz_offset_minutes: 0,
    },
});

Partial

For partial data the only difference is wrapping input in Partial and handling incomplete errors correctly, which is documented in winnow partial docs.

pub type Stream<'i> = Partial<&'i [u8]>;

let datetime = opt(parse_datetime)
    .parse_next(&mut Stream::new("2015-06-26T16:43:23+0200").as_bytes()));

// the above will give you:
Some(DateTime {
    date: Date::YMD {
        year: 2015,
        month: 6,
        day: 26,
    },
    time: Time {
        hour: 16,
        minute: 43,
        second: 23,
        tz_offset_hours: 2,
        tz_offset_minutes: 0,
    },
});

Serializing

If you have a datetime string handy you can use the helper methods such as datetime to get a DateTime object. This can be serialized into a chrono date object if the serde feature is enabled.

let datetime = winnow_iso8601::datetime("2015-06-26T16:43:23+0200").unwrap();

// the above will give you:
DateTime {
    date: Date::YMD {
        year: 2015,
        month: 6,
        day: 26,
    },
    time: Time {
        hour: 16,
        minute: 43,
        second: 23,
        tz_offset_hours: 2,
        tz_offset_minutes: 0,
    },
};

Contributors

winnow-iso8601 is the fruit of the work of many contributors over the years, many thanks for your help! In particular, thanks to badboy and hoodie for the original iso8601 crate and actually reading the standard.

Documentation

Documentation is online.

License

MIT Licensed. See LICENSE

Dependencies

~0.8–1.4MB
~27K SLoC