chrono
chrono copied to clipboard
Unable to parse millisecond unix timestamps
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 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).
Nice, thanks @jgoday ! That seems like a reasonable approach to me.
Hi! Would it be possible to include this fix in the next release? Thanks!