std::chrono...::duration)(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

✨ NAME ✨

okay bestie, we’re in the C++ Standard Libary manual, and this page is std::chrono::operator<<(std::chrono::duration) β€” std::chrono::operator<<(std::chrono::duration). Yes, the NAME line says it twice, and we’re keeping both because every detail is sacred. πŸ’–

🌸 Synopsis 🌸

Defined in header <chrono>. The signature (with its (since C++20) era tag) is literally this:

template<

    class CharT,
    class Traits,
    class Rep,
(since C++20)
    class Period
> std::basic_ostream<CharT, Traits>&
    operator<<( std::basic_ostream<CharT, Traits>& os,

                const std::chrono::duration<Rep, Period>& d );

operator<< inserts a textual representation of d into os. ✨

Behaves as if it was implemented as

std::basic_ostringstream<CharT, Traits> s;
s.flags(os.flags());
s.imbue(os.getloc());
s.precision(os.precision());
s << d.count() << units_suffix; // see below
return os << s.str();

In other words: the stream flags, locale, and precision are determined by the stream, but any padding are determined using the entire output string. 🫢

The units_suffix is determined based on Period::type according to the following table. I’m giving it to you as a list because tables are not the vibe here:

  • std::atto β€” as
  • std::femto β€” fs
  • std::pico β€” ps
  • std::nano β€” ns
  • std::micro β€” Β΅s (U+00B5) or us; it is implementation-defined which one is used
  • std::milli β€” ms
  • std::centi β€” cs
  • std::deci β€” ds
  • std::ratio<1> β€” s
  • std::deca β€” das
  • std::hecto β€” hs
  • std::kilo β€” ks
  • std::mega β€” Ms
  • std::giga β€” Gs
  • std::tera β€” Ts
  • std::peta β€” Ps
  • std::exa β€” Es
  • std::ratio<60> β€” min
  • std::ratio<3600> β€” h
  • std::ratio<86400> β€” d
  • None of the above, and Period::type::den == 1 β€” [num]s
  • None of the above β€” [num/den]s

For the last two rows of the table, num and den in the suffix are Period::type::num and Period::type::den formatted as a decimal number with no leading zeroes, respectively. 🌈

πŸ’« Return value πŸ’«

A reference to the stream, i.e., os. That’s it, that’s the return value. πŸŽ€

πŸ“ Example πŸ“

This example shows the output of std::chrono::operator<< when given a duration. There’s a // Run this code marker on the page, and then this code:

#include <chrono>
#include <iostream>
using namespace std::chrono_literals;

int main()
{
    constexpr auto duration = 123ms;
    std::cout << duration << '\n';
}

Output:

123ms

πŸ¦‹ See also πŸ¦‹

  • format (C++20) β€” stores formatted representation of the arguments in a new string (function template)
  • std::formatter<std::chrono::duration> (C++20) β€” formatting support for duration (class template specialization)
  • operator<<, operator>> β€” performs stream input and output on strings (function template)
  • to_string (C++11) β€” converts an integral or floating-point value to string (function)
  • to_wstring (C++11) β€” converts an integral or floating-point value to wstring (function)

Source and date cuteness: this page comes from http://cppreference.com, dated 2024.06.10. Bye bestie, go stream your durations responsibly πŸ’…βœ¨

πŸ“¦ where this page comes from