waltzofpearls / dateparser

Parse dates in commonly used string formats with Rust.
MIT License
41 stars 8 forks source link

parse returns datetime with time set to current local time #19

Closed deankarn closed 2 years ago

deankarn commented 2 years ago

Reviving https://github.com/waltzofpearls/belt/issues/16

I was wondering if you would consider adding a feature or accepting a PR to allow zeroing out of unknown components?

My use case is I wish to use this parser to parse arbitrary dates where adding the local time makes the dates incorrect. Because I'll be parsing arbitrary data where the input is not under my direct control I will be unable to implement the workaround provided in the previous issue.

waltzofpearls commented 2 years ago

Would this work you? https://github.com/waltzofpearls/belt/blob/main/dateparser/examples/parse_with.rs

use chrono::{
    naive::NaiveTime,
    offset::{Local, Utc},
};
use dateparser::parse_with;
use std::error::Error;

fn main() -> Result<(), Box<dyn Error>> {
    let parsed_in_local = parse_with("2021-10-09", &Local, NaiveTime::from_hms(0, 0, 0))?;
    println!("{:#?}", parsed_in_local);

    let parsed_in_utc = parse_with("2021-10-09", &Utc, NaiveTime::from_hms(0, 0, 0))?;
    println!("{:#?}", parsed_in_utc);

    Ok(())
}

It printed the following at the time I ran it.

2021-10-10T00:00:00Z
2021-10-09T00:00:00Z

Both zero out time. The difference is the first one assumes it's a local date, and the second parses the date with UTC.

deankarn commented 2 years ago

@waltzofpearls unfortunately this would not resolve my issue, the core of the issue is setting unknown parts of a DateTime with the current Local or UTC time rather than zeroing them out.

Thanks for your time, I think I'm going to invest some time in a more custom solution for now.