Skip to content

Commit 5516eed

Browse files
committed
WIP: Add function to convert to chrono::DateTime
1 parent bf96790 commit 5516eed

File tree

9 files changed

+879
-83
lines changed

9 files changed

+879
-83
lines changed

Cargo.lock

+11-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
[package]
22
name = "parse_datetime"
3-
description = " parsing human-readable relative time strings and converting them to a Duration"
4-
version = "0.4.0"
3+
description = "parsing human-readable relative time strings and converting them to a Duration"
4+
version = "0.7.0"
55
edition = "2021"
66
license = "MIT"
77
repository = "https://github.com/uutils/parse_datetime"
88
readme = "README.md"
99

1010
[dependencies]
1111
chrono = { version="0.4", default-features=false, features=["std", "alloc", "clock"] }
12+
num-traits = "0.2.19"
1213
winnow = "0.5.34"
14+
15+
[dev-dependencies]
16+
anyhow = "1.0.86"

src/items/combined.rs

+35-3
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@
1414
//! > ISO 8601 fractional minutes and hours are not supported. Typically, hosts
1515
//! > support nanosecond timestamp resolution; excess precision is silently discarded.
1616
17+
use winnow::ascii::dec_uint;
18+
use winnow::token::take;
1719
use winnow::{combinator::alt, seq, PResult, Parser};
1820

21+
use crate::items::combined;
1922
use crate::items::space;
2023

2124
use super::{
@@ -24,13 +27,17 @@ use super::{
2427
time::{self, Time},
2528
};
2629

27-
#[derive(PartialEq, Debug, Clone)]
30+
#[derive(PartialEq, Debug, Clone, Default)]
2831
pub struct DateTime {
29-
date: Date,
30-
time: Time,
32+
pub(crate) date: Date,
33+
pub(crate) time: Time,
3134
}
3235

3336
pub fn parse(input: &mut &str) -> PResult<DateTime> {
37+
alt((parse_basic, parse_8digits)).parse_next(input)
38+
}
39+
40+
fn parse_basic(input: &mut &str) -> PResult<DateTime> {
3441
seq!(DateTime {
3542
date: date::iso,
3643
// Note: the `T` is lowercased by the main parse function
@@ -40,6 +47,31 @@ pub fn parse(input: &mut &str) -> PResult<DateTime> {
4047
.parse_next(input)
4148
}
4249

50+
fn parse_8digits(input: &mut &str) -> PResult<DateTime> {
51+
s((
52+
take(2usize).and_then(dec_uint),
53+
take(2usize).and_then(dec_uint),
54+
take(2usize).and_then(dec_uint),
55+
take(2usize).and_then(dec_uint),
56+
))
57+
.map(
58+
|(hour, minute, day, month): (u32, u32, u32, u32)| combined::DateTime {
59+
date: date::Date {
60+
day,
61+
month,
62+
year: None,
63+
},
64+
time: time::Time {
65+
hour,
66+
minute,
67+
second: 0.0,
68+
offset: None,
69+
},
70+
},
71+
)
72+
.parse_next(input)
73+
}
74+
4375
#[cfg(test)]
4476
mod tests {
4577
use super::{parse, DateTime};

src/items/date.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ use winnow::{
3737
use super::s;
3838
use crate::ParseDateTimeError;
3939

40-
#[derive(PartialEq, Eq, Clone, Debug)]
40+
#[derive(PartialEq, Eq, Clone, Debug, Default)]
4141
pub struct Date {
4242
pub day: u32,
4343
pub month: u32,
@@ -96,7 +96,7 @@ fn literal2(input: &mut &str) -> PResult<Date> {
9696
.parse_next(input)
9797
}
9898

99-
fn year(input: &mut &str) -> PResult<u32> {
99+
pub fn year(input: &mut &str) -> PResult<u32> {
100100
s(alt((
101101
take(4usize).try_map(|x: &str| x.parse()),
102102
take(3usize).try_map(|x: &str| x.parse()),

0 commit comments

Comments
 (0)