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
Returns an object of unspecified type such that if in
is the name of an input stream of type std::basic_istream<CharT, Traits>
, then the expression in >> get_time(tmb, fmt)
behaves as if the following code was executed:
typedef std::istreambuf_iterator<CharT, Traits> Iter;
typedef std::time_get<CharT, Iter> TimeGet;
std::ios_base::iostate err = std::ios_base::goodbit;
const TimeGet& tg = std::use_facet<TimeGet>(in.getloc());
tg.get(Iter(in.rdbuf()), Iter(), in, err, tmb, fmt, fmt + traits::length(fmt));
if (err != std::ios_base::goodbit)
in.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 to observe the output. libstdc++ does not correctly implement the %b specifier: bug 78714.
#include <iostream> #include <sstream> #include <locale> #include <iomanip> 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 struct std::tm (class template) |
|
(C++11) | formats and outputs a date/time value according to the specified format (function template) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
http://en.cppreference.com/w/cpp/io/manip/get_time