Outputting time and money

The put_time function in <iomanip> is passed a tm structure initialized with a time and date and a format string. The function returns an instance of the _Timeobj class. As the name suggests, you are not really expected to create variables of this class; instead, the function should be used to insert a time/date with a specific format into a stream. There is an insertion operator that will print a _Timeobj object. The function is used like this:

    time_t t = time(nullptr);     tm *pt = localtime(&t);     cout << put_time(pt, "time = %X date = %x") << "n";

The output from this is:

    time = 20:08:04 date = 01/02/17

The function will use the locale in the stream, so if you imbue a locale into the stream and then call put_time ...

Get Beginning C++ Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.