std::time_g...get::do_get(3)
πΈ std::time_get::get, std::time_get::do_get β NAME πΈ
okay bestie, we're in section 3 of the C++ Standard Library manual, and today's main characters are std::time_get::get and std::time_get::do_get π
no cap, they're literally the same name twice because one calls the other, iykyk.
β¨ Synopsis β¨
These bad boys live in the header <locale>, and here's the whole shape of them:
Defined in header <locale>
public:
iter_type get( iter_type beg, iter_type end, std::ios_base& str,
std::ios_base::iostate& err, std::tm* t,
const char_type* fmtbeg, const char_type* fmtend ) const;
(1) (since C++11)
protected:
virtual iter_type do_get( iter_type beg, iter_type end,
std::ios_base& str,
std::ios_base::iostate& err, std::tm *t,
char format, char modifier ) const;
(2) (since C++11)
so like, (1) is the public one you actually call, and (2) is the protected virtual one you can override if you're feeling main character energy. π«Ά
π (1) β the get one, aka the public face
It parses the date and time out of the input character sequence [beg, end) according to the format given to you in the character sequence [fmtbeg, fmtend). The format is supposed to follow the format described below (in part 2, bestie, hold on), although the actual processing of each format specifier can totally be customized by overriding do_get. π
Here's what get actually does, step by step:
First, it clears the error bits in err by executing err = std::ios_base::goodbit. Clean slate, fresh start, glowing skin. β¨
Then it enters a loop, and that loop terminates whenever any of these conditions becomes true β and yes, they're checked in this exact order:
- All characters have been read from the format string (
fmtbeg == fmtend). - There was a parsing error (
err != std::ios_base::goodbit). - All characters have been read from the input sequence (
beg == end). If this condition is the one that terminates the loop, the function sets botheofbitandfailbitinerr.
In the body of the loop, these steps take place:
- a) If the next character in the format string is
'%', followed by one or two characters that form a validstd::get_timeconversion specifier (see below, in the next part), those characters are used in the calldo_get(beg, end, str, err, t, format, modifier), whereformatis the primary conversion specifier character andmodifieris the optional modifier (the thing that appears between%and the format character, if it's there at all). If there is no modifier, the value'\0'is used. π If the format string is ambiguous or ends too early to determine the conversion specifier after%,eofbitis set inerrand the loop is terminated. If, after the call todo_get, no error bits are set inerr, the function incrementsfmtbegto point right after the conversion specifier and continues the loop. π« - b) If the next character is whitespace, as indicated by the locale provided in the stream
str(i.e.std::isspace(*fmtbeg, str.getloc()) == true), the function keeps incrementingfmtbeguntil it either becomes equal tofmtendor points to a non-whitespace character. Skips the space, iconic behaviour. π - c) If the next character in the format string is equivalent to the next character in the input stream according to case-insensitive comparison, the function advances both sequences by one character,
++fmtbeg, ++beg;, and continues the loop. Otherwise, it sets thefailbitinerr. π
π (2) β the do_get one, aka the overrideable inner workings
It parses one conversion specifier from the input sequence [beg, end) and updates the std::tm structure pointed to by t accordingly. One specifier per call, we love a focused queen. π¦
First, it clears the error bits in err by executing err = std::ios_base::goodbit. Then it reads characters from the input sequence [beg, end) that are expected by the std::time_get format specifier formed by combining '%', modifier (if it isn't '\0'), and format. πΈ
- If the characters do not combine to form a valid conversion specifier, it sets
failbitinerr. - If the end of the input stream is reached after reading a character, it sets
eofbitinerr. - If the input string was parsed successfully, it updates the corresponding fields of
*t. slay. π
β οΈ For complex conversion specifiers, such as'%x'or'%c', or the directives that use the modifiers'E'and'O', the function may fail to determine some of the values to store in*t. In that case, it setseofbitinerrand leaves these fields in an unspecified state. not to be dramatic but π that's your cue to not trust those fields afterward, fr fr.
π Parameters π
okay bestie, hereβs the cast of characters for this function call β every single one matters, no skips π
begβ iterator designating the start of the sequence to parseendβ one past the end iterator for the sequence to parsestrβ a stream object that this function uses to obtain locale facets when needed, e.g.std::ctypeto skip whitespace orstd::collateto compare stringserrβ stream error flags object that is modified by this function to indicate errorstβ pointer to thestd::tmobject that will hold the result of this function callfmtbegβ pointer to the first character of a sequence ofchar_typecharacters specifying the conversion format (see below)fmtendβ pointer one past the last character of a sequence ofchar_typecharacters specifying the conversion formatformatβ the character that names a conversion specifiermodifierβ the optional modifier that may appear between%and the conversion specifier
The format string consists of zero or more conversion specifiers, whitespace characters, and ordinary characters (except %). Each ordinary character is expected to match one character in the input stream in case-insensitive comparison. Each whitespace character matches arbitrary whitespace in the input string. Each conversion specification begins with % character, optionally followed by E or O modifier (ignored if unsupported by the locale), followed by the character that determines the behavior of the specifier. The format specifiers match the POSIX function strptime():
πΈ Year πΈ
%β matches a literal%. The full conversion specification must be%%. (writes to: none)tβ matches any whitespace. (writes to: none)nβ matches any whitespace. (writes to: none)Yβ parses full year as a 4 digit decimal number, leading zeroes permitted but not required. (writes to:tm_year)EYβ parses year in the alternative representation, e.g.εΉ³ζ23εΉ΄(year Heisei 23) which writes 2011 totm_yearinja_JPlocale. (writes to:tm_year)yβ parses last 2 digits of year as a decimal number. Range[69,99]results in values 1969 to 1999, range[00,68]results in 2000-2068. (writes to:tm_year)Oyβ parses last 2 digits of year using the alternative numeric system, e.g.εδΈis parsed as 11 inja_JPlocale. (writes to:tm_year)Eyβ parses year as offset from locale's alternative calendar period%EC. (writes to:tm_year)Cβ parses the first 2 digits of year as a decimal number (range[00,99]). (writes to:tm_year)ECβ parses the name of the base year (period) in the locale's alternative representation, e.g.εΉ³ζ(Heisei era) inja_JP. (writes to:tm_year)
π Month π
bβ parses the month name, either full or abbreviated, e.g.Oct. (writes to:tm_mon)hβ synonym ofb. (writes to:tm_mon)Bβ synonym ofb. (writes to:tm_mon)mβ parses the month as a decimal number (range[01,12]), leading zeroes permitted but not required. (writes to:tm_mon)Omβ parses the month using the alternative numeric system, e.g.εδΊparses as 12 inja_JPlocale. (writes to:tm_mon)
π¦ Week π¦
Uβ parses the week of the year as a decimal number (Sunday is the first day of the week) (range[00,53]), leading zeroes permitted but not required. (writes to:tm_year,tm_wday,tm_yday)OUβ parses the week of the year, as by%U, using the alternative numeric system, e.g.δΊεδΊparses as 52 inja_JPlocale. (writes to:tm_year,tm_wday,tm_yday)Wβ parses the week of the year as a decimal number (Monday is the first day of the week) (range[00,53]), leading zeroes permitted but not required. (writes to:tm_year,tm_wday,tm_yday)OWβ parses the week of the year, as by%W, using the alternative numeric system, e.g.δΊεδΊparses as 52 inja_JPlocale. (writes to:tm_year,tm_wday,tm_yday)
π Day of the year/month π
jβ parses day of the year as a decimal number (range[001,366]), leading zeroes permitted but not required. (writes to:tm_yday)dβ parses the day of the month as a decimal number (range[01,31]), leading zeroes permitted but not required. (writes to:tm_mday)Odβ parses the day of the month using the alternative numeric system, e.g.δΊεδΈparses as 27 inja_JPlocale, leading zeroes permitted but not required. (writes to:tm_mday)eβ synonym ofd. (writes to:tm_mday)Oeβ synonym ofOd. (writes to:tm_mday)
β Day of the week β
aβ parses the name of the day of the week, either full or abbreviated, e.g.Fri. (writes to:tm_wday)Aβ synonym ofa. (writes to:tm_wday)wβ parses weekday as a decimal number, where Sunday is 0 (range[0-6]). (writes to:tm_wday)Owβ parses weekday as a decimal number, where Sunday is 0, using the alternative numeric system, e.g.δΊparses as 2 inja_JPlocale. (writes to:tm_wday)
π Hour, minute, second π
Hβ parses the hour as a decimal number, 24 hour clock (range[00-23]), leading zeroes permitted but not required. (writes to:tm_hour)OHβ parses hour from 24-hour clock using the alternative numeric system, e.g.εε «parses as 18 inja_JPlocale. (writes to:tm_hour)Iβ parses hour as a decimal number, 12 hour clock (range[01,12]), leading zeroes permitted but not required. (writes to:tm_hour)OIβ parses hour from 12-hour clock using the alternative numeric system, e.g.ε reads as 06 inja_JPlocale. (writes to:tm_hour)Mβ parses minute as a decimal number (range[00,59]), leading zeroes permitted but not required. (writes to:tm_min)OMβ parses minute using the alternative numeric system, e.g.δΊεδΊparses as 25 inja_JPlocale. (writes to:tm_min)Sβ parses second as a decimal number (range[00,60]), leading zeroes permitted but not required. (writes to:tm_sec)OSβ parses second using the alternative numeric system, e.g.δΊεεparses as 24 inja_JPlocale. (writes to:tm_sec)
π Other π
cβ parses the locale's standard date and time string format, e.g.Sun Oct 17 04:41:13 2010(locale dependent). (writes to: all)Ecβ parses the locale's alternative date and time string format, e.g. expectingεΉ³ζ23εΉ΄(year Heisei 23) instead of2011εΉ΄(year 2011) inja_JPlocale. (writes to: all)xβ parses the locale's standard date representation. (writes to: all)Exβ parses the locale's alternative date representation, e.g. expectingεΉ³ζ23εΉ΄(year Heisei 23) instead of2011εΉ΄(year 2011) inja_JPlocale. (writes to: all)Xβ parses the locale's standard time representation. (writes to: all)EXβ parses the locale's alternative time representation. (writes to: all)Dβ equivalent to"%m / %d / %y ". (writes to:tm_mon,tm_mday,tm_year)rβ parses locale's standard 12-hour clock time (in POSIX,"%I : %M : %S %p"). (writes to:tm_hour,tm_min,tm_sec)Rβ equivalent to"%H : %M". (writes to:tm_hour,tm_min)Tβ equivalent to"%H : %M : %S". (writes to:tm_hour,tm_min,tm_sec)pβ parses the locale's equivalent of a.m. or p.m. (writes to:tm_hour)
Note:tm_isdstis not written to, and needs to be set explicitly for use with functions such asmktime.
π Return value π
Iterator pointing one past the last character in [beg, end) that was parsed successfully.
β¨ Notes β¨
The case-insensitive comparison for the non-whitespace non-'%' characters in the format string, the std::collate facet of the locale provided by str is typically, but not necessarily, used.
If a parsing error is encountered, many implementations of this function leave *t completely untouched.
It's unspecified if these functions zero out the fields in *t that they do not set directly: portable programs should initialize every field to zero before calling get().
π Example π
// Run this code
#include <iomanip>
#include <iostream>
#include <locale>
#include <sstream>
int main()
{
std::istringstream ss("2026-MΓ€rz-12 23:45:56");
ss.imbue(std::locale("de_DE.utf8"));
auto& f = std::use_facet<std::time_get<char>>(ss.getloc());
std::tm t{};
std::string s = "%Y-%b-%d %H:%M:%S";
std::ios_base::iostate err = std::ios_base::goodbit;
auto ret = f.get({ss}, {}, ss, err, &t, &s[0], &s[0] + s.size());
ss.setstate(err);
std::istreambuf_iterator<char> last{};
if (ss)
{
std::cout << "Successfully parsed as " << std::put_time(&t, "%c") << '\n';
if (ret != last)
{
std::cout << "Remaining content: ";
std::copy(ret, last, std::ostreambuf_iterator<char>(std::cout));
}
else
std::cout << "The input was fully consumed.";
}
else
{
std::cout << "Parse failed.\nUnparsed string: ";
std::copy(ret, last, std::ostreambuf_iterator<char>(std::cout));
}
std::cout << '\n';
}
Output:
Successfully parsed as Sun Mar 12 23:45:56 2026
The input was fully consumed.
π«Ά See also π«Ά
get_timeβ parses a date/time value of specified format (C++11) (function template)
okay thatβs the whole vibe π
source + version: cppreference.com, the 2024.06.10 version, for std::time_g...get::do_get(3) β go parse something iconic β¨π¦
π¦ 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