std:: get_time
Defined in header
<iomanip>
|
||
template
<
class
CharT
>
/*unspecified*/ get_time ( std:: tm * tmb, const CharT * fmt ) ; |
(since C++11) | |
When used in an expression in >> get_time ( tmb, fmt ) , parses the character input as a date/time value according to format string fmt according to the std::time_get facet of the locale currently imbued in the input stream in . The resultant value is stored in a std::tm object pointed to by tmb .
Parameters
tmb | - | valid pointer to the std::tm object where the result will be stored | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
fmt | - |
pointer to a null-terminated
CharT
string specifying the conversion format
The format string consists of zero or more conversion specifiers, whitespace characters, and ordinary characters (except
Note:
|
Return value
An object of unspecified type such that
-
if
in
is an object of type
std::
basic_istream
<
CharT, Traits
>
, the expression
in
>>
get_time
(
tmb, fmt
)
- has type std:: basic_istream < CharT, Traits > &
- has value in
- behaves as if it called f ( in, tmb, fmt )
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); }
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
: portable programs should initialize every field of
*
tmb
to zero before calling
std::get_time
.
Example
Note: choose clang or gcc >= 12.1 to observe the output. libstdc++ before 12.1 does not correctly implement the % b specifier: bug #78714 .
#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
See also
parses time/date values from an input character sequence into
std::tm
(class template) |
|
(C++11)
|
formats and outputs a date/time value according to the specified format
(function template) |
(C++20)
|
parses a
chrono
object from a stream
(function template) |