Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parsing negative NaiveDateTime timestamps with serde(with = "ts_milliseconds") fails #577

Closed
ousado opened this issue Jul 25, 2021 · 1 comment

Comments

@ousado
Copy link

ousado commented Jul 25, 2021

Parsing negative NaiveDateTime timestamps with #[serde(with = "ts_milliseconds")] (and other implementations) fails.

use chrono::{NaiveDateTime}; // 0.4.19
use chrono::naive::serde::ts_milliseconds::deserialize as from_milli_ts;
use serde::Deserialize;

#[derive(Deserialize)]
struct S {
  #[serde(deserialize_with = "from_milli_ts")]
  time: NaiveDateTime
}
        
fn example() -> Result<S, serde_json::Error> {
  let my_s: S = serde_json::from_str(r#"{ "time": -1 }"#)?;
  Ok(my_s)
}
        

fn main() {
    let value = -1;
    let r0 = value / 1000;
    let r1 = (value % 1000);
    // this is the implementation in chrono::naive::serde::ts_milliseconds
    let r = NaiveDateTime::from_timestamp_opt(   
        value / 1000, 
        ((value % 1000) * 1_000_000) as u32);  // conversion into u32 fails for negative values 
    println!("{:?} {} {}", r, r0, r1);
    example().unwrap();
}
@pitdicker
Copy link
Collaborator

Fixed in #1194.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants