public member function
<locale>

std::time_get::get_year

iter_type get_year (iter_type s, iter_type end, ios_base& str,
                    ios_base::iostate& err, tm* t) const;
Read year
Parses the sequence of characters between s and end and interprets it as a year, storing its value into the tm object pointed by t.

The function reads characters until the character read cannot be part of a valid year expression or end is reached. The next character in the sequence is pointed by the iterator returned by the function.

If successful, the function sets the relevant members of the tm structure t. The remaining members are left unchanged.

Internally, this function simply calls the virtual protected member do_get_year, which by default parses characters for a sequence representing a year. It is unspecified whether two-digit years are accepted, and -if so- what centery they lie to.

The function updates err with the result of the operation if necessary:
- If the format of the sequence is not valid, the function sets failbit.
- Otherwise, it is unspecified, although some implementations set err to eofbit if the function exhausts the sequence of characters (i.e., it reaches end, both in case of success and failure), or to goodbit otherwise.
The function updates err with the result of the operation if necessary:
- If the format of the sequence is not valid, the function sets failbit.
- If the function exhausts the sequence of characters (i.e., it reaches end) during its operations, it sets eofbit (both failbit and eofbit may be set by a single operation).
- Otherwise, it is unspecified, although some implementations set err to goodbit.

Parameters

s, end
Iterators pointing to the beginning and end characters of the sequence. The range used is [s,end), which contains all the characters between s and end, including the character pointed by s but not the character pointed by end.
Member type iter_type is the facet's iterator type (defined as an alias of time_get's second template parameter, InputIterator). By default, this is an istreambuf_iterator, allowing implicit conversions from basic_istream objects.
str
Object of a class derived from ios_base (generally an input stream object). It is only used to obtain formatting information.
err
Stream error state object, of type ios_base::iostate where the error states are stored.
t
Pointer to an object of type struct tm (defined in header <ctime>), whose members are set by a successful call to this member function.

Return value

The next character in the sequence right after the last character used by the operation.
Member type iter_type is the facet's iterator type (defined as an alias of time_get's second template parameter, InputIterator).

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// time_get::get_year example
#include <iostream>       // std::cout, std::ios
#include <sstream>        // std::istringstream
#include <ctime>          // std::tm
#include <locale>         // std::locale, std::time_get, std::use_facet

int main ()
{
  std::locale loc;        // classic "C" locale

  // get time_get facet:
  const std::time_get<char>& tmget = std::use_facet <std::time_get<char> > (loc);

  std::ios::iostate state;
  std::istringstream iss ("2013");
  std::tm when;

  tmget.get_year (iss, std::time_get<char>::iter_type(), iss, state, &when);

  std::cout << "year: " << when.tm_year << '\n';
  return 0;
}


Possible output:

tm_year: 113
Notice that 113 in tm_year is interpreted as the year 2013 (see struct tm for more info).

Data races

The object, and up to the entire range between s and end, are accessed.
Arguments str, err and t may be modified.

Exception safety

If an exception is thrown, there are no changes in the facet object, although some of arguments may have been affected.

See also