function
<iomanip>

std::get_time

template <class charT>
/*unspecified*/ get_time (struct tm* tmb, const charT* fmt);
Get date and time
Extracts characters from the input stream it is applied to, and interprets them as time and date information as specified in argument fmt. The obtained data is stored at the struct tm object pointed by tmb.

Internally, the function accesses the input sequence by first constructing an object of type basic_istream::sentry. Then (if evaluating the sentry object is true), it calls time_get::get (using the stream's selected locale) to perform both the extraction and the parsing operations, and adjusts the stream's internal state flags accordingly. Finally, it destroys the sentry object before returning.

This manipulator is declared in header <iomanip>.

Parameters

tmb
Pointer to an object of type struct tm where the time and date information extracted is stored.
struct tm is a class defined in header <ctime>.
fmt
C-string used by time_get::get as format string (see time_get::get).
charT is the character type in the c-string.

Return Value

Unspecified. This function should only be used as a stream manipulator (see example).

Errors are signaled by modifying the stream's internal state flags:
flagerror
eofbitThe input sequence has no more characters available (end-of-file reached).
failbitEither no characters were extracted, or the characters extracted could not be interpreted as a valid monetary value.
badbitError on stream (such as when this function catches an exception thrown by an internal operation).
When set, the integrity of the stream may have been affected.
Multiple flags may be set by a single operation.

If the operation sets an internal state flag on the stream that was registered using its member exceptions, the function throws an exception of type ios_base::failure.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// get_time example
#include <iostream>     // std::cin, std::cout
#include <iomanip>      // std::get_time
#include <ctime>        // struct std::tm

int main ()
{
  struct std::tm when;
  std::cout << "Please, enter the time: ";
  std::cin >> std::get_time(&when,"%R");   // extract time (24H format)

  if (std::cin.fail()) std::cout << "Error reading time\n";
  else {
    std::cout << "The time entered is: ";
    std::cout << when.tm_hour << " hours and " << when.tm_min << " minutes\n";
  }

  return 0;
}


Data races

Accesses the array pointed by fmt Modifies the object pointed by tmb and the stream object from which it is extracted.
Concurrent access to the same stream object may cause data races, except for the standard stream objects cin and wcin when these are synchronized with stdio (in this case, no data races are initiated, although no guarantees are given on the order in which extracted characters are attributed to threads).

Exception safety

Basic guarantee: if an exception is thrown, the object is in a valid state.
It throws an exception of member type failure if the resulting error state flag is not goodbit and member exceptions was set to throw for that state.
Any exception thrown by an internal operation is caught and handled by the function, setting badbit. If badbit was set on the last call to exceptions, the function rethrows the caught exception.

Note that invalid arguments cause undefined behavior.

See also