1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
use std::fmt::Display;

use crate::Timestamp;

impl Display for Timestamp {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    match f.precision() {
      Some(p) => {
        let float = self.seconds as f64 + self.nanos as f64 / 1_000_000_000.0;
        write!(f, "{:.*}", p, float)
      }
      None => write!(f, "{}", self.seconds),
    }
  }
}

#[cfg(test)]
mod tests {
  use super::*;

  #[test]
  fn test_display() {
    let t = Timestamp::from(1335020400);
    assert_eq!(format!("{:.02}", t), "1335020400.00");
  }
}