Skip to main content
3 of 5
deleted 4 characters in body
Drew Dormann
  • 65.8k
  • 14
  • 133
  • 202

While {:%Q} will work, it is extracting the internal count() from a duration, which is usually unwanted and unnecessary with std::chrono types.

The following will work regardless of the internal representation of your duration.

The code is also smaller and arguably clearer.

    auto duration = tp2 - tp1;
    std::println("duration: {} year(s)", duration / years{1});

The lesson: dividing two durations gives you a correct count without needing to know (or duration_cast) either duration's representation.

See it work.

Drew Dormann
  • 65.8k
  • 14
  • 133
  • 202