Skip to content

Commit

Permalink
add parser naive data function
Browse files Browse the repository at this point in the history
  • Loading branch information
baoyachi committed Nov 3, 2021
1 parent 50ca113 commit f09686b
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,13 +404,19 @@ pub fn parse_chrono<S: Into<String>>(input: S) -> anyhow::Result<chrono::Duratio
}

#[cfg(feature = "chrono")]
pub fn parse_naive_date<S: Into<String>>(input: S) -> anyhow::Result<chrono::NaiveDateTime> {
pub fn parse_naive_date_time<S: Into<String>>(input: S) -> anyhow::Result<chrono::NaiveDateTime> {
let std_duration = parse_std(input)?;
let duration = chrono::Duration::from_std(std_duration)?;
let time = (Utc::now() + duration).naive_utc();
Ok(time)
}

#[cfg(feature = "chrono")]
pub fn parse_naive_date<S: Into<String>>(input: S) -> anyhow::Result<chrono::NaiveDate> {
let date = parse_naive_date_time(input)?;
Ok(date.date())
}

macro_rules! des_duration {
($name:ident,$duration_type:ident,$fn_name:ident,$parse:ident) => {
struct $name;
Expand Down Expand Up @@ -612,6 +618,14 @@ mod tests {
assert_eq!(duration, Duration::new(3600, 0));
}

#[test]
fn test_parse_naive_date_time() {
let date = Utc::now().naive_utc().date();
let jd = date.num_days_from_ce() + 180;
let date = parse_naive_date_time("180d").unwrap();
assert_eq!(date.num_days_from_ce(), jd)
}

#[test]
fn test_parse_naive_date() {
let date = Utc::now().naive_utc().date();
Expand Down

0 comments on commit f09686b

Please sign in to comment.