std::get_time(3)
π std::get_time π
okay bestie, welcome to std::get_time β part of the C++ Standard Library, and honestly? lowkey iconic. she's serving parse a date out of a stream and she does it with the std::time_get facet energy. no cap, we're gonna go through the whole thing together, every single specifier, so grab a boba π
β¨ SYNOPSIS β¨
Defined in header <iomanip>
template< class CharT > (since C++11)
/*unspecified*/ get_time( std::tm* tmb, const CharT* fmt );
so here's the vibe π
β when you use it inside an expression like in >> get_time(tmb, fmt), it reads the character input and parses it as a date/time value according to the format string fmt, and it does that according to the std::time_get facet of the locale that's currently imbued in the input stream in. the resultant value gets stored in the std::tm object pointed to by tmb. simple as that, periodt.
πΈ PARAMETERS πΈ
tmbβ a valid pointer to thestd::tmobject where the result will be stored π«Άfmtβ pointer to a null-terminatedCharTstring that specifies the conversion format
okay so the format string is made of zero or more conversion specifiers, whitespace characters, and ordinary characters (everything except %). let's break that down because this ate:
- each ordinary character is expected to match one character in the input stream, and the comparison is case-insensitive (yes, she's chill like that)
- each whitespace character matches arbitrary whitespace in the input string
- each conversion specification begins with the
%character, optionally followed by anEorOmodifier (which is ignored if the locale doesn't support it β not me being flexible π ), followed by the character that determines the behavior of the specifier - the format specifiers match the POSIX function
strptime()β iykyk
π the conversion specifiers, in all their glory π
π the misc girlies (year-agnostic)
%%β matches a literal%. tbh the full conversion specification must be%%. Writes to: none.%tβ matches any whitespace. Writes to: none.%nβ matches any whitespace. Writes to: none.
ποΈ Year
%Yβ parses full year as a 4 digit decimal number, leading zeroes permitted but not required. Writes totm_year.%EYβ parses year in the alternative representation, e.g.εΉ³ζ23εΉ΄(year Heisei 23) which writes 2011 totm_yearin theja_JPlocale. Writes totm_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 totm_year.%Oyβ parses last 2 digits of year using the alternative numeric system, e.g.εδΈis parsed as 11 in theja_JPlocale. Writes totm_year.%Eyβ parses year as offset from locale's alternative calendar period%EC. Writes totm_year.%Cβ parses the first 2 digits of year as a decimal number (range[00,99]). Writes totm_year.%ECβ parses the name of the base year (period) in the locale's alternative representation, e.g.εΉ³ζ(Heisei era) inja_JP. Writes totm_year.
π Month
%bβ parses the month name, either full or abbreviated, e.g.Oct. Writes totm_mon.%hβ synonym of%b. Writes totm_mon.%Bβ synonym of%b. Writes totm_mon.%mβ parses the month as a decimal number (range[01,12]), leading zeroes permitted but not required. Writes totm_mon.%Omβ parses the month using the alternative numeric system, e.g.εδΊparses as 12 in theja_JPlocale. Writes totm_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 totm_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 in theja_JPlocale. Writes totm_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 totm_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 in theja_JPlocale. Writes totm_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 totm_yday.%dβ parses the day of the month as a decimal number (range[01,31]), leading zeroes permitted but not required. Writes totm_mday.%Odβ parses the day of the month using the alternative numeric system, e.g.δΊεδΈparses as 27 in theja_JPlocale, leading zeroes permitted but not required. Writes totm_mday.%eβ synonym of%d. Writes totm_mday.%Oeβ synonym of%Od. Writes totm_mday.
πΈ Day of the week
%aβ parses the name of the day of the week, either full or abbreviated, e.g.Fri. Writes totm_wday.%Aβ synonym of%a. Writes totm_wday.%wβ parses weekday as a decimal number, where Sunday is 0 (range[0-6]). Writes totm_wday.%Owβ parses weekday as a decimal number, where Sunday is 0, using the alternative numeric system, e.g.δΊparses as 2 in theja_JPlocale. Writes totm_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 totm_hour.%OHβ parses hour from 24-hour clock using the alternative numeric system, e.g.εε «parses as 18 in theja_JPlocale. Writes totm_hour.%Iβ parses hour as a decimal number, 12 hour clock (range[01,12]), leading zeroes permitted but not required. Writes totm_hour.%OIβ parses hour from 12-hour clock using the alternative numeric system, e.g.ε reads as 06 in theja_JPlocale. Writes totm_hour.%Mβ parses minute as a decimal number (range[00,59]), leading zeroes permitted but not required. Writes totm_min.%OMβ parses minute using the alternative numeric system, e.g.δΊεδΊparses as 25 in theja_JPlocale. Writes totm_min.%Sβ parses second as a decimal number (range[00,60]), leading zeroes permitted but not required. Writes totm_sec.%OSβ parses second using the alternative numeric system, e.g.δΊεεparses as 24 in theja_JPlocale. Writes totm_sec.
π Other (the combo-meal specifiers, obsessed)
%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) in theja_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) in theja_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 totm_mon,tm_mday,tm_year.%rβ parses locale's standard 12-hour clock time (in POSIX,"%I : %M : %S %p"). Writes totm_hour,tm_min,tm_sec.%Rβ equivalent to"%H : %M". Writes totm_hour,tm_min.%Tβ equivalent to"%H : %M : %S". Writes totm_hour,tm_min,tm_sec.%pβ parses the locale's equivalent of a.m. or p.m. Writes totm_hour.
π note bestie:tm_isdstis not written to, and it needs to be set explicitly for use with functions such asmktime. don't forget or she'll be mad at you.
π RETURN VALUE π
so she returns an object of unspecified type such that:
- if
inis an object of typestd::basic_istream<CharT, Traits>, the expressionin >> get_time(tmb, fmt)- has type
std::basic_istream<CharT, Traits>& - has value
in - behaves as if it called
f(in, tmb, fmt)
- has type
where the function f is defined as:
template<class CharT, class Traits>
void f(std::basic_ios<CharT, Traits>& str, std::tm* tmb, const CharT* fmt)
{
using Iter = std::istreambuf_iterator<CharT, Traits>;
using TimeGet = time_get<CharT, Iter>;
std::ios_base::iostate err = std::ios_base::goodbit;
const TimeGet& tg = std::use_facet<TimeGet>(str.getloc());
tg.get(Iter(str.rdbuf()), Iter(), str, err, tmb,
fmt, fmt + Traits::length(fmt));
if (err != std::ios_base::goodbit)
str.setstate(err);
}
like, she's literally just handing you the whole implementation, no gatekeeping β€οΈ
π¦ NOTES π¦
as specified in std::time_get::do_get, which this function calls, it's unspecified if this function zero out the fields in *tmb that are not set directly by the conversion specifiers that appear in fmt π. so portable programs should initialize every field of *tmb to zero before calling std::get_time. that's not optional if you want to be portable, fr fr β zero her out first.
and that's the first half of the glow-up, bestie β part 2 is right behind me π«Άβ¨
π Example π
okay bestie, here's the vibe check on this one β and yes, there's a compiler note you need before you run it:
Note: choose clang or gcc >= 12.1 to observe the output. libstdc++ before 12.1 does not correctly implement the %b specifier: bug #78714.
So likeβ¦ don't come for me if you're on an ancient libstdc++ and %b is acting up π that's literally bug #78714, not you.
Here's the code, no cap:
// Run this code
#include <iomanip>
#include <iostream>
#include <locale>
#include <sstream>
int main()
{
std::tm t = {};
std::istringstream ss("2011-Februar-18 23:12:34");
ss.imbue(std::locale("de_DE.utf-8"));
ss >> std::get_time(&t, "%Y-%b-%d %H:%M:%S");
if (ss.fail())
std::cout << "Parse failed\n";
else
std::cout << std::put_time(&t, "%c") << '\n';
}
Possible output:
Sun Feb 18 23:12:34 2011
she ate that fr fr π β¨
π See also π
time_getβ parses time/date values from an input character sequence intostd::tmβ (class template) π°οΈput_timeβ formats and outputs a date/time value according to the specified format β (C++11) (function template) πparseβ parses a chrono object from a stream β (C++20) (function template) πΈ
that's the whole page, bestie π«Ά go forth and parse those timestamps with main character energy π
source/version: http://cppreference.com Β· date: 2024.06.10 Β· page: std::get_time(3) πβ¨
π¦ 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