C++ represents times and dates in two main ways:
- As an integer, of type time_t, traditionally the number of seconds since Jan 1, 1970 (the official birthday of unix).
- As a struct, of type tm , containing integers for the year, month, hour etc.
Here is an example:
#include <ctime> time_t mytt = time(0); // Current date and time as an int tm * mytm = localtime(&mytt); // convert to a tm struct cout << "year " << (mytm->tm_year + 1900) << endl; cout << "day " << mytm->tm_wday << endl; // Sunday = 0
Note that above, localtime() returns a pointer to a struct, hence the need for the -> operator to access its member variables. The library contains several other functions for manipulating times and dates.
A C++ class representing the date information you are interested in is frequently a good idea.
Here is the full declaration of struct tm:
struct tm { int tm_sec; /* Seconds: 0-59 (K&R says 0-61?) */ int tm_min; /* Minutes: 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 Jan. 1: 0-365 */ int tm_isdst; /* +1 Daylight Savings Time, 0 No DST, * -1 don't know */ };
The apparent uncertainty isn't mine; these are the precise contents of the mingw header file.