std::chrono::parse(3)
π std::chrono::parse β C++ Standard Library
okay bestie, sit down, because we're about to get into std::chrono::parse, and it is so much. this is the C++ Standard Library manual page for it, and yes it's long, which is why it comes in three parts. this is part one. hydrate. π
β¨ NAME β¨
std::chrono::parse β literally std::chrono::parse. iconic, self-titled, no notes. π
β¨ Synopsis β¨
so first things first: this whole situation lives in the header <chrono>. get familiar. π
there are eight overloads, and every single one of them is (since C++20). yes, all of them. we love a consistent queen.
Overload (1) β (since C++20)
template< class CharT, class Parsable >
/* unspecified */ parse( const CharT* fmt, Parsable& tp );
Overload (2) β (since C++20)
template< class CharT, class Traits, class Alloc, class Parsable >
/* unspecified */ parse( const std::basic_string<CharT, Traits, Alloc>& fmt,
Parsable& tp );
Overload (3) β (since C++20)
template< class CharT, class Traits, class Alloc, class Parsable >
/* unspecified */ parse( const CharT* fmt, Parsable& tp,
std::basic_string<CharT, Traits, Alloc>& abbrev );
Overload (4) β (since C++20)
template< class CharT, class Traits, class Alloc, class Parsable >
/* unspecified */ parse( const std::basic_string<CharT, Traits, Alloc>& fmt,
Parsable& tp,
std::basic_string<CharT, Traits, Alloc>& abbrev );
Overload (5) β (since C++20)
template< class CharT, class Parsable >
/* unspecified */ parse( const CharT* fmt, Parsable& tp,
std::chrono::minutes& offset );
Overload (6) β (since C++20)
template< class CharT, class Traits, class Alloc, class Parsable >
/* unspecified */ parse( const std::basic_string<CharT, Traits, Alloc>& fmt,
Parsable& tp, std::chrono::minutes& offset );
Overload (7) β (since C++20)
template< class CharT, class Traits, class Alloc, class Parsable >
/* unspecified */ parse( const CharT* fmt, Parsable& tp,
std::basic_string<CharT, Traits, Alloc>& abbrev,
std::chrono::minutes& offset );
Overload (8) β (since C++20)
template< class CharT, class Traits, class Alloc, class Parsable >
/* unspecified */ parse( const std::basic_string<CharT, Traits, Alloc>& fmt,
Parsable& tp,
std::basic_string<CharT, Traits, Alloc>& abbrev,
std::chrono::minutes& offset );
π¦ what it actually returns (the plot twist)
so every one of these bad boys returns an object manip of unspecified type. unspecified type means we're not telling you the type, don't @ us. π
the deal is: given a std::basic_istream<CharT, Traits> object named is, the expression is >> manip calls from_stream β and it calls it unqualified, specifically so that argument-dependent lookup is enabled. iykyk. β¨
here's exactly how each overload maps out:
from_stream(is, fmt, tp)from_stream(is, fmt.c_str(), tp)from_stream(is, fmt, tp, std::addressof(abbrev))from_stream(is, fmt.c_str(), tp, std::addressof(abbrev))from_stream(is, fmt, tp, static_cast<std::basic_string<CharT, Traits, Alloc>*>(nullptr), &offset)from_stream(is, fmt.c_str(), tp, static_cast<std::basic_string<CharT, Traits, Alloc>*>(nullptr), &offset)from_stream(is, fmt, tp, std::addressof(abbrev), &offset)from_stream(is, fmt.c_str(), tp, std::addressof(abbrev), &offset)
periodt. that's the whole mapping. π
π«Ά the lil details that matter
the expression is >> manip is an lvalue of type std::basic_istream<CharT, Traits> whose value is is. say it with me: value is is. very "the way that it just returns the stream" of it all. π
these overloads only participate in overload resolution if the corresponding from_stream expression is well-formed. so if it don't compile, it don't exist. vibe check passed or it's not even in the building. π«
ok and here's the serious one, bestie, don't skip it: implementations are recommended to make it difficult to use potentially dangling references to the format string β for example, by making the return types non-movable and preventing operator>> from accepting lvalues of the return types. that's a recommendation to implementers, not you, but it's there for a reason and it's giving "we're protecting you from yourself" and honestly? valid. π§
part 1 of 3, keep scrolling for the rest π
π«Ά PARAMETERS π«Ά
fmtβ a format string (see below)tpβ object to hold the parse resultabbrevβ string to hold the time zone abbreviation or name corresponding to the%Zspecifieroffsetβ duration to represent the offset from UTC corresponding to the%zspecifier
β¨ Format string β¨
Okay so bestie, the format string is literally made of zero or more conversion specifiers and ordinary characters. 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. No cap.
Each whitespace character matches zero or more whitespace characters in the input stream. Slay.
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. (So like, don't stress if you see a stray % β it's just a regular character then. π«Ά)
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. Big yikes, but that's the rule. π
The following conversion specifiers are available:
%%β 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 widthNspecifies the maximum number of characters to read. The default width is2. 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 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 widthNspecifies the maximum number of characters to read. The default width is2. 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 widthNspecifies the maximum number of characters to read. The default width is4. 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 is1). The widthNspecifies the maximum number of characters to read. The default width is2. 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 widthNspecifies the maximum number of characters to read. The default width is2. Leading zeroes are permitted but not required.%e,%Ne,%Oeβ 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 is1. The widthNspecifies the maximum number of characters to read. The default width is1. Leading zeroes are permitted but not required.%w,%Nw,%Owβ parses the weekday as a decimal number (0-6), where Sunday is0. The widthNspecifies the maximum number of characters to read. The default width is1. Leading zeroes are permitted but not required. The modified command%Owinterprets the locale's alternative representation.
πΈ ISO 8601 week-based year πΈ
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 widthNspecifies the maximum number of characters to read. The default width is2. Leading zeroes are permitted but not required.%G,%NGβ parses the ISO 8601 week-based year as a decimal number. The widthNspecifies the maximum number of characters to read. The default width is4. Leading zeroes are permitted but not required.%V,%NVβ parses the ISO 8601 week of the year as a decimal number. The widthNspecifies the maximum number of characters to read. The default width is2. 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 is1). The widthNspecifies the maximum number of characters to read. The default width is3. 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 week01. Days of the same year prior to that are in week00. The widthNspecifies the maximum number of characters to read. The default width is2. 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 week01. Days of the same year prior to that are in week00. The widthNspecifies the maximum number of characters to read. The default width is2. 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 widthNspecifies the maximum number of characters to read. The default width is2. 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 widthNspecifies the maximum number of characters to read. The default width is2. 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 widthNspecifies the maximum number of characters to read. The default width is2. 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 widthNspecifies the maximum number of characters to read. The default width is2. 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.
π Miscellaneous π
%cβ parses the locale's date and time representation. Classic, no notes π%Ecβ the modified command here; it interprets the locale's alternative date and time representation. Lowkey the cooler twin π¦%zβ parses the offset from UTC, and the format is[+|-]hh[mm]. So like,-0430means 4 hours 30 minutes behind UTC, and04means 4 hours ahead of UTC. Easy β¨%Ez,%Ozβ the modified commands, and they parse the format[+|-]h[h][:mm]. Translation: the:between hours and minutes is required, and the leading zero on the hour is optional. She's flexible but she does have standards π%Zβ parses the time zone abbreviation or name, taken as the longest sequence of characters that only contains the charactersAthroughZ,athroughz,0through9,-,+,_, and/. That's the whole guest list, nobody else gets in π
πΈ EXAMPLE πΈ
This section is incomplete Reason: no example
Not me giving you a section that's literally just a "no example yet" placeholder π the vibes are immaculate but the code sample is missing. Tragic. If you need a working example bestie, that's a you-and-the-compiler moment fr fr π
π DEFECT REPORTS π
The following behavior-changing defect reports were applied retroactively to previously published C++ standards. So yes, the past got edited. Main character energy π
LWG 3554β applied to C++20. What was published: overloads for plain null-terminated character type sequences were missing. Correct behavior now: added. She said "actually no" and they fixed it π«Ά
π«Ά SEE ALSO π«Ά
Okay so there are a lot of sibling functions here, all of them from_stream overloads living their best lives, plus one old-school friend. Every one of these is a function template β noted for each, no cap β¨
from_stream(std::chrono::sys_time)(C++20) β parses asys_timefrom a stream according to the provided formatfrom_stream(std::chrono::utc_time)(C++20) β parses autc_timefrom a stream according to the provided formatfrom_stream(std::chrono::tai_time)(C++20) β parses atai_timefrom a stream according to the provided formatfrom_stream(std::chrono::gps_time)(C++20) β parses agps_timefrom a stream according to the provided formatfrom_stream(std::chrono::file_time)(C++20) β parses afile_timefrom a stream according to the provided formatfrom_stream(std::chrono::local_time)(C++20) β parses alocal_timefrom a stream according to the provided formatfrom_stream(C++20) β parses ayearfrom a stream according to the provided formatfrom_stream(C++20) β parses amonthfrom a stream according to the provided formatfrom_stream(C++20) β parses adayfrom a stream according to the provided formatfrom_stream(C++20) β parses aweekdayfrom a stream according to the provided formatfrom_stream(C++20) β parses amonth_dayfrom a stream according to the provided formatfrom_stream(C++20) β parses ayear_monthfrom a stream according to the provided formatfrom_stream(C++20) β parses ayear_month_dayfrom a stream according to the provided formatget_time(C++11) β parses a date/time value of specified format. The vintage one, still iconic π
One last little sidebar before we go π¦ β under the hood this page is filed under:
- Category:
* Todo no example
So yeah, the to-do list literally says "no example." She's aware. She's working on it. Give her a minute π
And that's a wrap on std::chrono::parse(3) bestie!! π Source is http://cppreference.com, version 2024.06.10 β go drink some water, go parse a timestamp, and remember: the format string is not a suggestion, it's a lifestyle. Periodt πβ¨π«ΆπΈ
π¦ 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