chrono icon indicating copy to clipboard operation
chrono copied to clipboard

Unable to parse millisecond unix timestamps

Open jszwedko opened this issue 3 years ago • 3 comments

Hey all,

I might be missing something, but I'm not seeing a way to use parse_from_str with unix timestamps that include milliseconds.

I would have expected something like:

fn main() {
    use chrono::TimeZone;
    let dt = chrono::offset::Utc.datetime_from_str("1620309579123", "%s%3f");
    println!("{:?}", dt);
}

to work, but it gives:

Err(ParseError(TooShort))

Is this possible or could it be added? I could try taking a shot at a PR.

jszwedko avatar May 06 '21 14:05 jszwedko

@jszwedko Seems that Timestmap in fn parse_internal (format/parse.rs), try to consume all digits without taking into account next items/formats. I have changed it to allow for the next fixed item length, in case there is no separator between them (Nanosecond3NotDot in this case).

let dt = chrono::offset::Utc.datetime_from_str("1620309579123", "%s%3f");

should return now

Ok(2021-05-06T13:59:39.123)

Can make a PR if this is a valid approach (https://github.com/jgoday/chrono/commit/c44fd78e8f905ce6ab7d8824e2e4f7f9a06a628b).

jgoday avatar May 14 '21 18:05 jgoday

Nice, thanks @jgoday ! That seems like a reasonable approach to me.

jszwedko avatar May 14 '21 19:05 jszwedko

Hi! Would it be possible to include this fix in the next release? Thanks!

yalinglee avatar Nov 10 '21 17:11 yalinglee