Uploaded image for project: 'Rust Driver'
  1. Rust Driver
  2. RUST-798

DateTime silently loses precision when serialized to BSON

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • bson-2.0.0-beta
    • Affects Version/s: bson-1.1.0, bson-1.2.0, bson-1.2.1
    • Component/s: BSON
    • None

      In BSON, datetimes are represented in millisecond precision. When serializing a chrono::DateTime with greater precision than milliseconds to a BSON datetime, the extra information is silently discarded. Instead of doing so, we should return an error indicating that the date cannot be represented in BSON.

      repro:

      let doc = doc! { "date": chrono::Utc.timestamp(100, 5) };    
      let mut v: Vec<u8> = Vec::new();                             
      doc.to_writer(&mut v).unwrap();                              
                                                                   
      let dout = Document::from_reader(&mut v.as_slice()).unwrap();
      assert_eq!(doc, dout);                                       
      

      yields:

      thread 'main' panicked at 'assertion failed: `(left == right)`
        left: `Document({"date": DateTime(1970-01-01T00:01:40.000000005Z)})`,
       right: `Document({"date": DateTime(1970-01-01T00:01:40Z)})`', src/main.rs:222:5
      
      

            Assignee:
            patrick.freed@mongodb.com Patrick Freed
            Reporter:
            patrick.freed@mongodb.com Patrick Freed
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: