std::chrono...no::weekday)(3)

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

🌸 NAME 🌸

std::chrono::operator<<(std::chrono::weekday) β€” okay so this is the lil overload that lets you just stream a weekday straight into an output stream, bestie. That's it. That's the whole vibe. πŸ’…

This page lives in the C++ Standard Library manual, section 3 (library functions), iykyk.


✨ Synopsis ✨

First things first, she's defined in header <chrono> πŸŽ€

template< class CharT, class Traits >

std::basic_ostream<CharT, Traits>&

(since C++20) ⭐

operator<<( std::basic_ostream<CharT, Traits>& os, const std::chrono::weekday& wd );

So like, here's the tea on what she actually does β˜•:

  • If !wd.ok(), she inserts wd.c_encoding() followed by " is not a valid weekday" into os. Basically she's calling out your invalid weekday in front of everyone 😭
  • Otherwise, she forms a std::basic_string<CharT> named s consisting of the abbreviated weekday name for the weekday represented by wd, determined using the locale associated with os, and inserts s into os. Slay πŸ’–

And she's equivalent to this whole thing:

return os << (wd.ok() ?
    std::format(os.getloc(), STATICALLY_WIDEN<CharT>("{:L%a}"), wd) :
    std::format(os.getloc(), STATICALLY_WIDEN<CharT>("{} is not a valid weekday"),
                wd.c_encoding()));

Where STATICALLY_WIDEN<CharT>("...") is "..." if CharT is char, and L"..." if CharT is wchar_t. Little type-dependent glow-up, we love to see it 🌈


πŸ¦‹ Return value πŸ¦‹

She returns os. That's it. Same stream you handed her, bestie β€” she just decorated it πŸŽ€


πŸ“ Example πŸ“

This section is incomplete πŸ™ˆ Reason: no example

Yeah… no example. Not me showing up to the group chat with nothing to demo πŸ’€ (There's literally a category tag for it down below, see? Called out in the official record.)


πŸ‘‘ Defect reports πŸ‘‘

The following behavior-changing defect reports were applied retroactively to previously published C++ standards. (Retroactive glow-ups, honestly iconic ✨)

  • P2372R3 β€” applied to C++20
    • Behavior as published: the given locale was used by default
    • Correct behavior: L is needed to use the given locale

Periodt. That's the whole fix 🫢


πŸ”₯ See also πŸ”₯

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

🌸 Category 🌸

  • Todo no example β€” yeah she's flagged, someone please come write her a demo 😭

Source/version and date, per the footer: this one's straight from http://cppreference.com, dated 2024.06.10 β€” so go touch some grass, hydrate, and remember: abbreviated weekday names only, no cap πŸ’œπŸ§šβ€β™€οΈ

πŸ“¦ where this page comes from