std::chrono...:year_month)(3)
β¨ 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:
- construct and check the sentry object (standard istream ritual, no notes) π§
- attempt to parse the input stream
isintoymaccording to the format stringfmt
and here's the part where you gotta pay attention, because it's the drama:
if the parse fails to decode a validyear_month, thenis.setstate(std::ios_base::failbit)gets called β andymis 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 ifabbrevis 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 ifoffsetis 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 resultabbrevβ if it's not null, it's a pointer to an object that will hold the time zone abbreviation or name corresponding to the%Zspecifieroffsetβ if it's not null, it's a pointer to an object that will hold the offset from UTC corresponding to the%zspecifier
πΈ 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: iffrom_streamfails 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, thenis.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%ECinterprets 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%Eyand%Oyinterpret 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%EYinterprets 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%Ominterprets 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%Odand%Oeinterpret 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%Owinterprets 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:
- Includes January 4
- 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%OUinterprets 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%OWinterprets 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%Exinterprets 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%OHinterprets 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%OIinterprets 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%OMinterprets 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%OSinterprets 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%EXinterprets the locale's alternate time representation.%cβ parses the locale's date and time representation β¨%Ecβ the modified command%Ecinterprets the locale's alternative date and time representation π%zβ parses the offset from UTC, in the format[+|-]hh[mm]. For example,-0430refers to 4 hours 30 minutes behind UTC, and04refers to 4 hours ahead of UTC π%Ez,%Ozβ the modified commands%Ezand%Ozparse 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 charactersAthroughZ,athroughz,0through9,-,+,_, 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
- 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