Skip to content

Commit

Permalink
WIP: Add function to convert to chrono::DateTime
Browse files Browse the repository at this point in the history
  • Loading branch information
dhilst committed Aug 30, 2024
1 parent bf96790 commit 9388e45
Show file tree
Hide file tree
Showing 9 changed files with 878 additions and 83 deletions.
14 changes: 11 additions & 3 deletions Cargo.lock

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

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

[dependencies]
chrono = { version="0.4", default-features=false, features=["std", "alloc", "clock"] }
num-traits = "0.2.19"
winnow = "0.5.34"

[dev-dependencies]
anyhow = "1.0.86"
38 changes: 35 additions & 3 deletions src/items/combined.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@
//! > ISO 8601 fractional minutes and hours are not supported. Typically, hosts
//! > support nanosecond timestamp resolution; excess precision is silently discarded.

use winnow::ascii::dec_uint;
use winnow::token::take;
use winnow::{combinator::alt, seq, PResult, Parser};

use crate::items::combined;
use crate::items::space;

use super::{
Expand All @@ -24,13 +27,17 @@ use super::{
time::{self, Time},
};

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

pub fn parse(input: &mut &str) -> PResult<DateTime> {
alt((parse_basic, parse_8digits)).parse_next(input)
}

fn parse_basic(input: &mut &str) -> PResult<DateTime> {
seq!(DateTime {
date: date::iso,
// Note: the `T` is lowercased by the main parse function
Expand All @@ -40,6 +47,31 @@ pub fn parse(input: &mut &str) -> PResult<DateTime> {
.parse_next(input)
}

fn parse_8digits(input: &mut &str) -> PResult<DateTime> {
s((
take(2usize).and_then(dec_uint),
take(2usize).and_then(dec_uint),
take(2usize).and_then(dec_uint),
take(2usize).and_then(dec_uint),
))
.map(
|(hour, minute, day, month): (u32, u32, u32, u32)| combined::DateTime {
date: date::Date {
day,
month,
year: None,
},
time: time::Time {
hour,
minute,
second: 0.0,
offset: None,
},
},
)
.parse_next(input)
}

#[cfg(test)]
mod tests {
use super::{parse, DateTime};
Expand Down
4 changes: 2 additions & 2 deletions src/items/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use winnow::{
use super::s;
use crate::ParseDateTimeError;

#[derive(PartialEq, Eq, Clone, Debug)]
#[derive(PartialEq, Eq, Clone, Debug, Default)]
pub struct Date {
pub day: u32,
pub month: u32,
Expand Down Expand Up @@ -96,7 +96,7 @@ fn literal2(input: &mut &str) -> PResult<Date> {
.parse_next(input)
}

fn year(input: &mut &str) -> PResult<u32> {
pub fn year(input: &mut &str) -> PResult<u32> {
s(alt((
take(4usize).try_map(|x: &str| x.parse()),
take(3usize).try_map(|x: &str| x.parse()),
Expand Down
Loading

0 comments on commit 9388e45

Please sign in to comment.