std::chrono...hrono::year)(3)

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

πŸ‘‘ C++ Standard Library πŸ‘‘ β€” a lil page about std::chrono::from_stream(std::chrono::year)

okay so bestie, welcome to the group chat. this is the manual page for std::chrono::from_stream(std::chrono::year), straight out of the C++ Standard Library, and it's giving "niche but iconic." part 1 of 3, so buckle up β€” more coming after this, no sign-off, we're not done yet πŸ’…


✨ NAME ✨

std::chrono::from_stream(std::chrono::year) β€” like, that's the whole name, twice, because the manpage said it twice and I would never gatekeep a single crumb of info from you 🫢

std::chrono::from_stream(std::chrono::year)


🌸 Synopsis 🌸

so first things first, it lives here:

Defined in header <chrono>

and the template situation is this, literally copy-paste it exactly, don't freestyle:

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,
                 std::chrono::year& y,
                 std::basic_string<CharT, Traits, Alloc>* abbrev = nullptr,

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

(since C++20) β€” yes queen, she's a modern girlie ✨


πŸ¦‹ okay but what does it DO πŸ¦‹

So basically: it attempts to parse the input stream is into the std::chrono::year y, according to the format string fmt. that's the whole vibe. parse β†’ year β†’ slay.

It behaves as an UnformattedInputFunction, except β€” and this is the important lil caveat β€” it has an unspecified effect on is.gcount(). like, don't count on gcount, bestie, it's not that kind of function πŸ™ˆ

After constructing and checking the sentry object, it attempts to parse the input stream is into y according to the format string fmt. and if the parse fails to decode a valid year? tragic. is.setstate(std::ios_base::failbit) gets called and y is not modified. so your year stays exactly where it was, untouched, unbothered, main character energy πŸ’€


πŸŽ€ the %Z and %z situation πŸŽ€

this part is lowkey the most important, so pay attention πŸ”₯

  • If the format specifier %Z is used and successfully parsed, the parsed value is assigned to *abbrev β€” but only if abbrev is not null. no abbrev, no assign. simple as.
  • If the format specifier %z or a modified variant is used and successfully parsed, the parsed value is assigned to *offset β€” but only if offset is not null. same rules, bestie.
⚠️ reminder: "if abbrev is not null" and "if offset is not null" are doing a LOT of heavy lifting in those sentences. the default for both is nullptr, so if you don't pass them, nothing gets written. periodt.

k that's part 1, more coming, don't go anywhere πŸ’–πŸ§š

πŸ’– Parameters πŸ’–

okay bestie, here’s the whole intake list β€” no cap, every single one:

  • is β€” an input stream πŸ’…
  • fmt β€” a format string (peep the next section πŸ‘€)
  • y β€” the object that holds the parse result
  • abbrev β€” if it’s not null, 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, a pointer to an object that will hold the offset from UTC corresponding to the %z specifier

πŸŽ€ Format string πŸŽ€

The format string is literally zero or more conversion specifiers plus ordinary characters. It’s giving build-your-own-parser, and honestly? iconic.

  • Each ordinary character β€” excluding whitespace characters and the terminating null character β€” matches one identical character from the input stream, or causes the function to fail if the next character on the stream does not compare equal.
  • Each whitespace character matches zero or more whitespace characters in the input stream.
  • Each unmodified conversion specifier begins with a % character followed by a character that determines the behavior of the specifier.
  • Some conversion specifiers have a modified form in which an E or O modifier character is inserted after the % character.
  • Some conversion specifiers have a modified form in which a width parameter given as a positive decimal integer (shown as N below) is inserted after the % character.
  • Each conversion specifier causes the matched characters to be interpreted as parts of date and time types according to the table below.

A character sequence in the format string that begins with a % but does not match one of the conversion specifiers below is interpreted as ordinary characters. slay, it just vibes as text.

🚨 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, is.setstate(std::ios_base::failbit) is called.

✨ The conversion specifiers ✨

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

Year era πŸ‘‘

  • %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 is not otherwise specified (e.g. 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 moment 🌸

  • %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 energy πŸ“

  • %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. The modified command %Od interprets the locale’s alternative representation.
  • %e, %Ne, %Oe β€” these sit in the day-of-month group too. The modified commands %Od and %Oe interpret the locale’s alternative representation.

Day of the week files πŸ—“οΈ

  • %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, periodt πŸ¦‹

In ISO 8601 weeks begin with Monday and the first week of the year must satisfy the following requirements:

  • Includes January 4
  • Includes first Thursday of the year
  • %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 vibes 🌈

  • %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 section, we’re so back πŸ’…

  • %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, main character energy ⏰

  • %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.

🌈 Miscellaneous 🌈

These format-command besties are all about parsing date/time vibes, and every little character matters πŸ’…

  • %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 πŸ’–

The return-value situation is literally just:

is

That's the whole thing, bestie β€” no extra lore in this part 😭


πŸ¦‹ See also πŸ¦‹

  • parse β€” parses a chrono object from a stream (C++20) (function template). πŸ‘‘

Okay, part 3 wrap-up time πŸ’… Big source shoutout to cppreference.com, version/date 2024.06.10, and the footer vibe std::chrono...hrono::year)(3) β€” mwah πŸ’–πŸŽ€

πŸ“¦ where this page comes from