CPP Date Time
CPP Date Time
T he C++ standard library does not provide a proper date type. C++ inherits the structs and functions for date
and time manipulation from C. T o access date and time related functions and structures, you would need to
include <ctime> header file in your C++ prog ram.
T here are four time-related types: c loc k_t, time_t, size_t, and tm. T he types clock_t, size_t and time_t are
capable of representing the system time and date as some sort of integ er.
T he structure type tm holds the date and time in the form of a C structure having the following elements:
struct tm {
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;
}
//
//
//
//
//
//
//
//
//
Following are the important functions, which we use while working with date and time in C or C++. All these
functions are part of standard C and C++ library and you can check their detail using reference to C++ standard
library g iven below.
SN
size_t strftime();
T his function can be used to format date and time a specific format.
When the above code is compiled and executed, it produces the following result:
The local date and time is: Sat Jan
The UTC date and time is:Sun Jan
8 20:07:41 2011
9 03:07:41 2011
When the above code is compiled and executed, it produces the following result:
Number of sec since January 1, 1970:1294548238
Year: 2011
Month: 1
Day: 8
Time: 22: 44:59