Before C++11, all date and time utilities were provided by the <ctime> header.
The first function that you usually use from this header is time, it is declared as:
std::time_t time(std::time_t* arg);
time returns the current time represented by the type std::time_t.
std::time_t is an opaque type, there is no guaranteed definition this type, but usually it is an alias for some unsigned integer.
If the argument supplied to arg is not a null pointer, then time will also store the std::time_t result at the address specified by arg.
There is not much you can do with a std::time_t by itself. With two std::time_t objects, you can compute the difference between them with the difftime function:
std::difftime(final, initial) returns a double value that represents the difference, in seconds, between the std::time_t values final and initial.
The expression std::ctime(&t) returns a null-terminated string (of type char*) containing a textual representation of the std::time_t object obtained from dereferencing &t.
std::localtime(&t) takes the address of some std::time_t object t, and returns a pointer to a structure of type std::tm which contains a breakdown of the information within that std::time_t object. std::tm is defined as:
struct tm {
int tm_sec; // Seconds after the minute: [0, 60]
int tm_min; // Minutes after the hour: [0, 59]
int tm_hour; // Hours since midnight: [0, 23]
int tm_mday; // Day of the month: [1, 31]
int tm_mon; // Months since January: [0, 11]
int tm_year; // Years since 1900
int tm_wday; // Days since Sunday: [0, 6]
int tm_yday; // Days since January 1: [0, 365]
int tm_isdst; // A positive value if Daylight Saving Time is in effect
};
std::mktime(&date) does the reverse of std::localtime, it takes a pointer to a std::tm object and returns the std::time_t value that corresponds it.
Here is a very simple program that displays the current date and time:
#include <iostream>
#include <ctime>
int main()
{
std::time_t time;
std::time(&time);
std::cout << std::ctime(&time) << std::endl;
return 0;
}
Here is another simple program that tells you if it's Friday:
#include <iostream>
#include <ctime>
int main()
{
std::time_t time;
std::tm date;
std::time(&time);
date = *std::localtime(&time);
if (date.tm_wday == 5) {
std::cout << "It's Friday" <<std::endl;
} else {
std::cout <<"It's not Friday" << std::endl;
}
return 0;
}
Notice that the return value from std::localtime is dereferenced. This is because std::localtime does not return a std::tm object, it returns a pointer to such an object. To save this object to our own local variable, we must dereference and copy-assign from it.
Expression | Meaning |
---|---|
std::time(&t) | Stores the current time into the std::time_t variable t |
t = std::time(0) | Same as above |
std::difftime(f, i) | Calculates the difference in seconds between the std::time_t values in f and i, the result is of type double. |
d = *std::localtime(&t) | Calculates the date corresponding to the std::time_t value in t and assigns it to the std::tm variable d. |
t = std::mktime(&d) | Calculates the time corresponding to the std::tm value in d and assigns it to the std::time_t variable t. |
std::ctime(&t) | Converts the std::time_t value in t to a null-terminated string. |
std::asctime(&d) | Converts the std::tm value in d to a null-terminated string. |
Some compilers, such as GCC, have been offering the I/O manipulators std::get_time and std::put_time from <iomanip> which were library extensions until they became standardized in C++11.
Expression | Meaning |
---|---|
std::cin >> std::get_time(&d, "%m/%d/%Y") | Extracts, from standard input, a character sequence representing a date in the form MM/DD/YYYY and parses that into the std::tm variable d |
std::cout << std::put_time(&d, "%m/%d/%Y") | Inserts a textual representation, in the form of MM/DD/YYYY, of the date stored in the std::tm variable d. |