Skip to main content
added 95 characters in body
Source Link
Drew Dormann
  • 65.8k
  • 14
  • 133
  • 202

I was expecting a duration of exactly 1 year without fractional seconds,

While {:%Q} could work, it is extracting the internal count() from a duration, which is dangerous and usually unnecessary with std::chrono types. If a code change alters the duration's period, you will start getting incorrect values.

The following will workgive you a year count 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.

While {:%Q} could work, it is extracting the internal count() from a duration, which is dangerous and usually unnecessary with std::chrono types. If a code change alters the duration's period, you will start getting incorrect values.

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.

I was expecting a duration of exactly 1 year without fractional seconds,

While {:%Q} could work, it is extracting the internal count() from a duration, which is dangerous and usually unnecessary with std::chrono types. If a code change alters the duration's period, you will start getting incorrect values.

The following will give you a year count 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.

added 91 characters in body
Source Link
Drew Dormann
  • 65.8k
  • 14
  • 133
  • 202

While {:%Q} willcould work, it is extracting the internal count() from a duration, which is usually unwanteddangerous and andusually unnecessary with std::chrono types. If a code change alters the duration's period, you will start getting incorrect values.

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.

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.

While {:%Q} could work, it is extracting the internal count() from a duration, which is dangerous and usually unnecessary with std::chrono types. If a code change alters the duration's period, you will start getting incorrect values.

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.

deleted 4 characters in body
Source Link
Drew Dormann
  • 65.8k
  • 14
  • 133
  • 202

While {:%Q} will workwill 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. I would also argue that the code's intention

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_castduration_cast) either duration's representation.

See it work.

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. I would also argue that the code's intention is 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.

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.

added 19 characters in body
Source Link
Drew Dormann
  • 65.8k
  • 14
  • 133
  • 202
Loading
Source Link
Drew Dormann
  • 65.8k
  • 14
  • 133
  • 202
Loading