std::time_get::get, std::time_get::do_get
Defined in header <locale>
|
||
public: iter_type get(iter_type beg, iter_type end, std::ios_base& str, |
(1) | (since C++11) |
protected: virtual iter_type do_get(iter_type neg, iter_type end, std::ios_base& str, |
(2) | (since C++11) |
[beg, end)
according to the format provided in the character sequence [fmtbeg, fmtend). The format is expected to follow the format described below, although actual processing of each format specifier can be customized by overriding do_get
. The get
function performs the following:
First, clears the error bits in err
by executing err = std::ios_base::goodbit. Then enters a loop, which terminates whenever any of the following conditions becomes true (checked in this order):eofbit
and failbit
in err
- In the body of the loop, the following steps take place:
format
is the primary conversion specifier character, and modifier
is the optional modifier (which appears between %
and the format character, if present). 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 '%', eofbit
is set in err
and the loop is terminated. If, after the call to do_get
, no error bits are set in err
, the function increments fmtbeg
to point right after the conversion specifier and continues the loop.str
(i.e. std::isspace(*fmtbeg, str.getloc()) == true, the function keeps incrementing fmtbeg
until it either becomes equal to fmtend
or points to a non-whitespace character.failbit
in err
.[beg, end)
and updates the std::tm structure pointed to by t
accordingly.- First, clears the error bits in
err
by executing err = std::ios_base::goodbit. Then reads characters from the input sequence[beg, end)
that are expected by the std::time_get format specifier formed by combining '%',modifier
(if not '\0'), andformat
. If the characters do not combine to form a valid conversion specifier, setsfailbit
inerr
. If the end of the input stream is reached after reading a character, setseofbit
inerr
. If the input string was parsed successfully, updates the corresponding fields of *t.
- First, clears the error bits in
- 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 such case, it sets
eofbit
inerr
and leaves these fields in unspecified state.
- 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 such case, it sets
Contents |
[edit] Parameters
beg | - | iterator designating the start of the sequence to parse | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
end | - | one past the end iterator for the sequence to parse | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
str | - | a stream object that this function uses to obtain locale facets when needed, e.g. std::ctype to skip whitespace or std::collate to compare strings | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
err | - | stream error flags object that is modified by this function to indicate errors | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
t | - | pointer to the std::tm object that will hold the result of this function call | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
fmtbeg | - | pointer to the first character of a sequence of char_type characters specifying the conversion format
The format string consists of zero or more conversion specifiers, whitespace characters, and ordinary characters (except
Note:
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
fmtend | - | pointer one past the last character of a sequence of char_type characters specifying the conversion format | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
format | - | the character that names a conversion specifier | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
modifier | - | the optional modifier that may appear between % and the conversion specifier
|
[edit] Return value
Iterator pointing one past the last character in [beg, end)
that was parsed successfully.
[edit] 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()
.
[edit] Example
#include <iostream> #include <sstream> #include <locale> #include <iomanip> int main() { std::istringstream ss("2011-Februar-18 23:12:34"); ss.imbue(std::locale("de_DE.utf8")); auto& f = std::use_facet<std::time_get<char>>(std::locale("de_DE.utf8")); std::tm t{}; std::string s = "%Y-%b-%d %H:%M:%S"; std::ios_base::iostate err = std::ios_base::goodbit; std::istreambuf_iterator<char> ret = f.get(std::istreambuf_iterator<char>(ss), std::istreambuf_iterator<char>(), ss, err, &t, &s[0], &s[0] + s.size()); ss.setstate(err); if(ss) { std::cout << "Successfully parsed as " << std::put_time(&t, "%c"); if(ret != std::istreambuf_iterator<char>()) { std::cout << " Remaining content: "; std::copy(ret, std::istreambuf_iterator<char>(), std::ostreambuf_iterator<char>(std::cout)); } else { std::cout << " The input was fully consumed"; } } else { std::cout << "Parse failed. Unparsed string: "; std::copy(ret, std::istreambuf_iterator<char>(), std::ostreambuf_iterator<char>(std::cout)); } std::cout << '\n'; }
Output:
Successfully parsed, as Sun Feb 18 23:12:34 2011 The input was fully consumed
[edit] See also
(C++11) |
parses a date/time value of specified format (function template) |