std::chrono...chrono::day)(3)

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

πŸ’… NAME πŸ’…

okay so this is the manual page for std::chrono::operator<<(std::chrono::day) β€” the page title doodle is std::chrono...chrono::day)(3), and it lives in the C++ Standard Libary manual (spelling is theirs, not mine πŸ’€). The NAME line is literally std::chrono::operator<<(std::chrono::day) - and then it says std::chrono::operator<<(std::chrono::day) again. Repetition? Iconic. We stan a consistent name. πŸ’–


🌸 Synopsis 🌸

Defined in header <chrono>:

template< class CharT, class Traits >
std::basic_ostream<CharT, Traits>&
operator<<( std::basic_ostream<CharT, Traits>& os, const std::chrono::day& d );

This overload is (since C++20), bestie. ✨

So what does it do? It forms a std::basic_string<CharT> s consisting of the day value stored in d, formatted as a decimal number, with a leading zero if the result would otherwise be a single decimal digit. Then, if !d.ok(), it appends the string " is not a valid day" β€” yes, with that leading space β€” to the formatted string. Finally, it inserts that string into os. Slay. πŸ’…

Equivalent to:

return os << (d.ok() ?
    std::format(STATICALLY_WIDEN<CharT>("{:%d}"), d) :
    std::format(STATICALLY_WIDEN<CharT>("{:%d} is not a valid day"), d));

where STATICALLY_WIDEN<CharT>("...") is "..." if CharT is char, and L"..." if CharT is wchar_t. 🫢


πŸ’– Return value πŸ’–

It returns os. That's the whole return value. No cap. 🌈


πŸŽ€ Example πŸŽ€

// Run this code

#include <chrono>
#include <iostream>

int main()
{
    constexpr std::chrono::day d1{31}, d2{7}, d3{42}, d4{};
    std::cout << d1 << '\n'
              << d2 << '\n'
              << d3 << '\n'
              << d4 << '\n';
}

Possible output, bestie:

31
07
42 is not a valid day
00 is not a valid day

πŸ¦‹ See also πŸ¦‹

  • format (C++20) β€” stores formatted representation of the arguments in a new string (function template).
  • std::formatter<std::chrono::day> (C++20) β€” formatting support for day (class template specialization).

made with πŸ’– by your Unix bestie, sourced from http://cppreference.com, dated 2024.06.10. πŸŽ€

πŸ“¦ where this page comes from