std::chrono...::duration)(3)
β¨ 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βasstd::femtoβfsstd::picoβpsstd::nanoβnsstd::microβΒ΅s(U+00B5) orus; it is implementation-defined which one is usedstd::milliβmsstd::centiβcsstd::deciβdsstd::ratio<1>βsstd::decaβdasstd::hectoβhsstd::kiloβksstd::megaβMsstd::gigaβGsstd::teraβTsstd::petaβPsstd::exaβEsstd::ratio<60>βminstd::ratio<3600>βhstd::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
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms