C++ iomanip::put_time()














































C++ iomanip::put_time()



DESCRIPTION:


The put_time () function belongs to the <iomanip> header file which consists of functions that are used to manipulate the output of C++ program. This function is used to write a time value from a time structure to a stream by using a specified format.


It inserts the representation of the time and date information pointed by tmb, formatting it as specified by argument fmt.


The put_time () function basically returns an object that, when inserted into the stream str, behaves as a formatted output function. The output function calls the member function put for the locale facet time_put associated with str. The output function uses time_ptr to indicate the time structure and time_format to indicate the beginning of a null-terminated format string.


If successful, the call inserts literal text from the format string and converted values from the time structure. The manipulator then returns str.



Declaration:  

put_time(conststruct tm*tmb, constcharT* fmt);



The first parameter here will be a pointer where the date and time will be stored.
The second format will be the format of date and time in which the time is
desired.


Parameters: 


tmb: It is the pointer to an object of type struct tm with the
time and date information to format. struct tm is a class defined in header <ctime>.


    fmt: It is the pointer to a null-terminated CharT string specifying the          conversion format i.e. a C-string used by time_put::put as format string.
    It contains any combination of regular characters and special format
    specifiers. These format specifiers are replaced by the function to the
    corresponding values to represent the time specified in 
tmb.
    They all begin with a percentage (
%) sign.


The format string consists of zero or more conversion specifiers, whitespace characters, and ordinary characters (except %). Each ordinary character is expected to match one character in the input stream in case-insensitive comparison. Each whitespace character matches arbitrary whitespace in the input string. Each conversion specification begins with % character, optionally followed by E or O modifier (ignored if unsupported by the locale), followed by the character that determines the behavior of the specifier. The format specifiers match the POSIX function strptime().


Return Value:


It returns an object of unspecified.
Basically, it is used as a stream manipulator and hence does not return
anything.


Exceptions:

Basic Guarantee- The object is in a valid state, if an exception is thrown.


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.


Data Races:


Accesses the object pointed by tmb and the array pointed by fmt.
Modifies the stream object where it is inserted. Concurrent access to the same stream object may cause data races, except for the standard stream objects (cout, cerr, clog, wcout, wcerr and wclog)
when these are synchronized with stdio (in this case, no data
races are initiated, although no guarantees are given on the order in which characters from multiple threads are inserted).



Program 1: Example of put_time () function :


#include <iostream>

#include <iomanip>

#include <ctime>

#include <chrono>


using namespace std;


int main ()



{


       using chrono::system_clock;


       time_t t = system_clock::to_time_t (system_clock::now());


        struct tm * ptm = localtime(&t);


        cout << "Now (local time): " << put_time(ptm,"%c") << '\n';



      return 0;


}



                                                       Output: 



 

                           



Comments