Skip to content

Commit

Permalink
start parsing date with winnow
Browse files Browse the repository at this point in the history
  • Loading branch information
tertsdiepraam authored and dhilst committed Aug 29, 2024
1 parent ba19b94 commit bf96790
Show file tree
Hide file tree
Showing 12 changed files with 1,351 additions and 567 deletions.
72 changes: 13 additions & 59 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
[package]
name = "parse_datetime"
description = "parsing human-readable time strings and converting them to a DateTime"
version = "0.6.0"
description = " parsing human-readable relative time strings and converting them to a Duration"
version = "0.4.0"
edition = "2021"
license = "MIT"
repository = "https://github.com/uutils/parse_datetime"
readme = "README.md"

[dependencies]
regex = "1.10.4"
chrono = { version="0.4", default-features=false, features=["std", "alloc", "clock"] }
nom = "7.1.3"
winnow = "0.5.34"
76 changes: 76 additions & 0 deletions src/items/combined.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.

//! Parse an ISO 8601 date and time item
//!
//! The GNU docs state:
//!
//! > The ISO 8601 date and time of day extended format consists of an ISO 8601
//! > date, a ‘T’ character separator, and an ISO 8601 time of day. This format
//! > is also recognized if the ‘T’ is replaced by a space.
//! >
//! > In this format, the time of day should use 24-hour notation. Fractional
//! > seconds are allowed, with either comma or period preceding the fraction.
//! > ISO 8601 fractional minutes and hours are not supported. Typically, hosts
//! > support nanosecond timestamp resolution; excess precision is silently discarded.
use winnow::{combinator::alt, seq, PResult, Parser};

use crate::items::space;

use super::{
date::{self, Date},
s,
time::{self, Time},
};

#[derive(PartialEq, Debug, Clone)]
pub struct DateTime {
date: Date,
time: Time,
}

pub fn parse(input: &mut &str) -> PResult<DateTime> {
seq!(DateTime {
date: date::iso,
// Note: the `T` is lowercased by the main parse function
_: alt((s('t').void(), (' ', space).void())),
time: time::iso,
})
.parse_next(input)
}

#[cfg(test)]
mod tests {
use super::{parse, DateTime};
use crate::items::{date::Date, time::Time};

#[test]
fn some_date() {
let reference = Some(DateTime {
date: Date {
day: 10,
month: 10,
year: Some(2022),
},
time: Time {
hour: 10,
minute: 10,
second: 55.0,
offset: None,
},
});

for mut s in [
"2022-10-10t10:10:55",
"2022-10-10 10:10:55",
"2022-10-10 t 10:10:55",
"2022-10-10 10:10:55",
"2022-10-10 (A comment!) t 10:10:55",
"2022-10-10 (A comment!) 10:10:55",
] {
let old_s = s.to_owned();
assert_eq!(parse(&mut s).ok(), reference, "Failed string: {old_s}")
}
}
}
Loading

0 comments on commit bf96790

Please sign in to comment.