1

If I print the current time directly, it includes many trailing decimals:

println!("{:?}", chrono::offset::Utc::now());
2022-12-01T07:56:54.242352517Z

How can I get it to print like this?

2022-12-01T07:56:54Z
kmdreko
  • 42,554
  • 6
  • 57
  • 106
GranBoh
  • 67
  • 8

1 Answers1

3

You can use to_rfc3339_opts. It accepts arguments for the format of the seconds and if the Z should be present.

let time = chrono::offset::Utc::now();
let formatted = time.to_rfc3339_opts(chrono::SecondsFormat::Secs, true);

println!("{:?}", time);    // 2022-12-01T08:32:20.580242150Z
println!("{}", formatted); // 2022-12-01T08:32:20Z

Rust Playground

kmdreko
  • 42,554
  • 6
  • 57
  • 106
Locke
  • 7,626
  • 2
  • 21
  • 41