-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ba19b94
commit bf96790
Showing
12 changed files
with
1,351 additions
and
567 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}") | ||
} | ||
} | ||
} |
Oops, something went wrong.