std::chrono...:year_month)(3)

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

✨ std::chrono::from_stream (std::chrono::year_month) ✨

okay bestie, buckle up πŸ’… this one lives in the C++ Standard Library manual, and it's literally the std::chrono::from_stream(std::chrono::year_month) page β€” yes that title is a mouthful, no we are not renaming it, iykyk. it's giving "glow-up for your date parsing" and honestly? obsessed πŸŽ€


🌸 NAME 🌸

std::chrono::from_stream(std::chrono::year_month) β€” the one that pulls a year-and-month out of a stream, fr fr πŸ’–


⭐ Synopsis ⭐

first things first, she's defined in header <chrono>, and she's been here since C++20 πŸ‘‘

template< class CharT, class Traits, class Alloc =
std::allocator<CharT> >

std::basic_istream<CharT, Traits>&
    from_stream( std::basic_istream<CharT, Traits>& is, const CharT* fmt,
(since C++20)
                 std::chrono::year_month& ym,
                 std::basic_string<CharT, Traits, Alloc>* abbrev = nullptr,

                 std::chrono::minutes* offset = nullptr );

like, look at that signature. four-and-a-bit template params, a format string, a year_month reference, and two optional pointers that are basically "hey, want the extras?" πŸ™ˆ


πŸ’œ the vibe check (what it actually does) πŸ’œ

so. what does she do? she attempts to parse the input stream is into the std::chrono::year_month ym, according to the format string fmt. that's the whole assignment, and she executes it flawlessly πŸ’…

she behaves as an UnformattedInputFunction, with one lil exception: she has an unspecified effect on is.gcount() β€” so don't go counting on that count, bestie, it's a mystery on purpose πŸ•΅οΈβ€β™€οΈ

the actual sequence is iconic:

  1. construct and check the sentry object (standard istream ritual, no notes) 🧚
  2. attempt to parse the input stream is into ym according to the format string fmt

and here's the part where you gotta pay attention, because it's the drama:

if the parse fails to decode a valid year_month, then is.setstate(std::ios_base::failbit) gets called β€” and ym is not modified. no half-parsed monstrosity, no corrupted value, she just ✨ refuses ✨ and leaves your variable exactly as she found it 🫢

πŸ¦‹ the format specifier tea β˜•

two special little guys get extra treatment:

  • %Z β€” if this specifier is used and successfully parsed, the parsed value gets assigned to *abbrev, but only if abbrev is not null. null pointer? she simply does not bother πŸ’€
  • %z (or a modified variant of it) β€” if it's used and successfully parsed, the parsed value gets assigned to *offset, again only if offset is not null 🌈

that's it, that's the whole extras system. optional pointers, optional data, zero pressure 😌


part 2 is coming, don't go anywhere πŸ‘€

✨ Parameters ✨

okay so here's what you're feeding this thing, bestie β€” five little slots, and they all matter:

  • is β€” an input stream πŸ’–
  • fmt β€” a format string (see below, and yes, "below" means right down there πŸ‘‡)
  • ym β€” the object that holds the parse result
  • abbrev β€” if it's not null, it's a pointer to an object that will hold the time zone abbreviation or name corresponding to the %Z specifier
  • offset β€” if it's not null, it's a pointer to an object that will hold the offset from UTC corresponding to the %z specifier

🌸 Format string 🌸

Your format string is made of zero or more conversion specifiers plus ordinary characters. That's the whole vibe.

Every ordinary character β€” meaning anything that isn't a whitespace character and isn't the terminating null character β€” has to match one identical character from the input stream. If the next character on the stream doesn't compare equal, the function just fails. No cap, that's the rule. 😭

Every whitespace character in the format matches zero or more whitespace characters in the input stream. Generous queen behavior tbh. 🫢

Each unmodified conversion specifier begins with a % character followed by a character that decides what that specifier actually does. Some of them have a modified form where an E or O modifier character gets inserted right after the %. Some have a modified form where a width parameter β€” written as a positive decimal integer, shown as N down below β€” gets inserted after the % character. Each conversion specifier takes the characters it matched and interprets them as parts of date and time types, per the table below. πŸ’…

And here's a lil gotcha: a character sequence in the format string that begins with a % but does not match one of the conversion specifiers below? That's interpreted as ordinary characters. Cute, iconic, slightly chaotic. πŸŽ€

⚠️ real talk: if from_stream fails to parse everything specified by the format string, or if insufficient information is parsed to specify a complete result, or if parsing discloses contradictory information, then is.setstate(std::ios_base::failbit) gets called. All three. Any of them. Failbit. πŸ’€

Here are the conversion specifiers you've got:

  • %% β€” matches a literal % character.
  • %n β€” matches one whitespace character.
  • %t β€” matches zero or one whitespace character.

🌟 Year 🌟

  • %C, %NC, %EC β€” parses the century as a decimal number. The width N specifies the maximum number of characters to read. The default width is 2. Leading zeroes are permitted but not required. The modified command %EC interprets the locale's alternative representation of the century.
  • %y, %Ny, %Ey, %Oy β€” parses the last two decimal digits of the year. If the century isn't otherwise specified (like, say, with %C), values in the range [69, 99] are presumed to refer to the years 1969 to 1999, and values in the range [00, 68] are presumed to refer to the years 2000 to 2068. The width N specifies the maximum number of characters to read. The default width is 2. Leading zeroes are permitted but not required. The modified commands %Ey and %Oy interpret the locale's alternative representation.
  • %Y, %NY, %EY β€” parses the year as a decimal number. The width N specifies the maximum number of characters to read. The default width is 4. Leading zeroes are permitted but not required. The modified command %EY interprets the locale's alternative representation.

🌸 Month 🌸

  • %b, %B, %h β€” parses the locale's full or abbreviated case-insensitive month name.
  • %m, %Nm, %Om β€” parses the month as a decimal number (January is 1). The width N specifies the maximum number of characters to read. The default width is 2. Leading zeroes are permitted but not required. The modified command %Om interprets the locale's alternative representation.

πŸ¦‹ Day πŸ¦‹

  • %d, %Nd, %Od β€” parses the day of month as a decimal number. The width N specifies the maximum number of characters to read. The default width is 2. Leading zeroes are permitted but not required.
  • %e, %Ne, %Oe β€” grouped right here with the day crew, and for the record: the modified commands %Od and %Oe interpret the locale's alternative representation.

πŸŽ€ Day of the week πŸŽ€

  • %a, %A β€” parses the locale's full or abbreviated case-insensitive weekday name.
  • %u, %Nu β€” parses the ISO weekday as a decimal number (1-7), where Monday is 1. The width N specifies the maximum number of characters to read. The default width is 1. Leading zeroes are permitted but not required.
  • %w, %Nw, %Ow β€” parses the weekday as a decimal number (0-6), where Sunday is 0. The width N specifies the maximum number of characters to read. The default width is 1. Leading zeroes are permitted but not required. The modified command %Ow interprets the locale's alternative representation.

🌈 ISO 8601 week-based year 🌈

quick side quest, bestie β€” in ISO 8601, weeks begin with Monday, and the first week of the year has to satisfy these requirements:

  1. Includes January 4
  2. Includes first Thursday of the year

okay back to the specifiers:

  • %g, %Ng β€” parses the last two decimal digits of the ISO 8601 week-based year. The width N specifies the maximum number of characters to read. The default width is 2. Leading zeroes are permitted but not required.
  • %G, %NG β€” parses the ISO 8601 week-based year as a decimal number. The width N specifies the maximum number of characters to read. The default width is 4. Leading zeroes are permitted but not required.
  • %V, %NV β€” parses the ISO 8601 week of the year as a decimal number. The width N specifies the maximum number of characters to read. The default width is 2. Leading zeroes are permitted but not required.

⭐ Week/day of the year ⭐

  • %j, %Nj β€” parses the day of the year as a decimal number (January 1 is 1). The width N specifies the maximum number of characters to read. The default width is 3. Leading zeroes are permitted but not required.
  • %U, %NU, %OU β€” parses the week number of the year as a decimal number. The first Sunday of the year is the first day of week 01. Days of the same year prior to that are in week 00. The width N specifies the maximum number of characters to read. The default width is 2. Leading zeroes are permitted but not required. The modified command %OU interprets the locale's alternative representation.
  • %W, %NW, %OW β€” parses the week number of the year as a decimal number. The first Monday of the year is the first day of week 01. Days of the same year prior to that are in week 00. The width N specifies the maximum number of characters to read. The default width is 2. Leading zeroes are permitted but not required. The modified command %OW interprets the locale's alternative representation.

πŸ’œ Date πŸ’œ

  • %D β€” equivalent to "%m/%d/%y".
  • %F, %NF β€” equivalent to "%Y-%m-%d". If the width is specified, it is only applied to the %Y.
  • %x, %Ex β€” parses the locale's date representation. The modified command %Ex interprets the locale's alternate date representation.

πŸ”₯ Time of day πŸ”₯

  • %H, %NH, %OH β€” parses the hour (24-hour clock) as a decimal number. The width N specifies the maximum number of characters to read. The default width is 2. Leading zeroes are permitted but not required. The modified command %OH interprets the locale's alternative representation.
  • %I, %NI, %OI β€” parses the hour (12-hour clock) as a decimal number. The width N specifies the maximum number of characters to read. The default width is 2. Leading zeroes are permitted but not required. The modified command %OI interprets the locale's alternative representation.
  • %M, %NM, %OM β€” parses the minute as a decimal number. The width N specifies the maximum number of characters to read. The default width is 2. Leading zeroes are permitted but not required. The modified command %OM interprets the locale's alternative representation.
  • %S, %NS, %OS β€” parses the second as a decimal number. The width N specifies the maximum number of characters to read. The default width is 2. Leading zeroes are permitted but not required. The modified command %OS interprets the locale's alternative representation.
  • %p β€” parses the locale's equivalent of the AM/PM designations associated with a 12-hour clock.
  • %R β€” equivalent to "%H:%M".
  • %T β€” equivalent to "%H:%M:%S".
  • %r β€” parses the locale's 12-hour clock time.
  • %X, %EX β€” parses the locale's time representation. The modified command %EX interprets the locale's alternate time representation.
  • %c β€” parses the locale's date and time representation ✨
  • %Ec β€” the modified command %Ec interprets the locale's alternative date and time representation πŸŽ€
  • %z β€” parses the offset from UTC, in the format [+|-]hh[mm]. For example, -0430 refers to 4 hours 30 minutes behind UTC, and 04 refers to 4 hours ahead of UTC πŸ’…
  • %Ez, %Oz β€” the modified commands %Ez and %Oz parse the format [+|-]h[h][:mm] (i.e., requiring a : between the hours and minutes and making the leading zero for hour optional) πŸ¦‹
  • %Z β€” parses the time zone abbreviation or name, taken as the longest sequence of characters that only contains the characters A through Z, a through z, 0 through 9, -, +, _, and / ⭐

πŸ’– RETURN VALUE πŸ’–

Ok so bestie, the page literally just says this:

is

That's it. That's the whole return value section 😭 is. Ngl that's iconic in a feral way β€” go read it as "whatever the rest of the sentence says," lmao. πŸ™ˆ


🌈 SEE ALSO 🌈

  • parse β€” parses a chrono object from a stream 🫢 (C++20) (function template)

fr fr read the parse page next, it's the whole vibe check for this one πŸ‘‘

And that's a wrap on part 3, bestie! πŸ’œ This lil beauty is from cppreference.com, dated 2024.06.10, and it's the man page for std::chrono...:year_month β€” mwah, go parse something slay βœ¨πŸŒΈπŸŽ€

πŸ“¦ where this page comes from