C++ Date & Time
The C++ Standard Library does not provide a built-in date type. C++ inherits the structures and functions for date and time operations from the C language. To use date and time related functions and structures, you need to include the <ctime>
header file in your C++ program.
There are four time-related types: clockt, timet, sizet, and tm. The types clock_t, size_t, and timet can represent the system time and date as some form of integer.
The structure type tm holds the date and time in the form of a C structure. The definition of the tm structure is as follows:
struct tm {
int tm_sec; // seconds, normally 0-59, but can be up to 61
int tm_min; // minutes, range 0-59
int tm_hour; // hours, range 0-23
int tm_mday; // day of the month, range 1-31
int tm_mon; // month, range 0-11
int tm_year; // years since 1900
int tm_wday; // day of the week, range 0-6, starting from Sunday
int tm_yday; // day of the year, range 0-365, starting from January 1
int tm_isdst; // Daylight Saving Time flag
};
Below are some important functions related to date and time in C/C++. All these functions are part of the C/C++ standard library. You can find the details of each function in the C++ standard library.
No. | Function & Description |
---|---|
1 | time_t time(time_t *time); <br>This function returns the current calendar time of the system as the number of seconds that have elapsed since 1 January 1970. If the system has no time, it returns -1. |
2 | char *ctime(const time_t *time); <br>This returns a pointer to a string of the form day month year hours:minutes:seconds year\n\0 representing the local time. |
3 | struct tm *localtime(const time_t *time); <br>This function returns a pointer to a tm structure representing the local time. |
4 | clock_t clock(void); <br>This function returns the processor time consumed by the program since its beginning. If the time is not available, it returns -1. |
5 | char * asctime ( const struct tm * time ); <br>This function returns a pointer to a string that contains the information stored in the structure pointed to by time, formatted as: day month date hours:minutes:seconds year\n\0. |
6 | struct tm *gmtime(const time_t *time); <br>This function returns a pointer to a tm structure representing the time in Coordinated Universal Time (UTC), also known as Greenwich Mean Time (GMT). |
7 | time_t mktime(struct tm *time); <br>This function returns the calendar time equivalent to the time stored in the structure pointed to by time. |
8 | double difftime ( time_t time2, time_t time1 ); <br>This function returns the difference in seconds between time1 and time2. |
9 | size_t strftime(); <br>This function can be used to format date and time in a specified format. |
Current Date and Time
The following example retrieves the current system date and time, including local time and Coordinated Universal Time (UTC).
Example
#include <iostream>
#include <ctime>
using namespace std;
int main( )
{
// Get the current system date/time
time_t now = time(0);
// Convert now to string form
char* dt = ctime(&now);
cout << "Local date and time: " << dt << endl;
// Convert now to tm struct
tm *gmtm = gmtime(&now);
dt = asctime(gmtm);
cout << "UTC date and time: " << dt << endl;
}
When the above code is compiled and executed, it produces the following result:
Local date and time: Sat Jan 8 20:07:41 2011
UTC date and time: Sun Jan 9 03:07:41 2011
Formatting Time Using Struct tm
The tm structure is particularly important in C/C++ for operations related to date and time. The tm structure stores date and time in a C structure format. Most time-related functions use the tm structure. The following example uses the tm structure and various date and time related functions.
Before practicing with structures, you need a basic understanding of C structures and how to use the arrow -> operator to access structure members.
Example
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
// Based on the current system's current date/time
time_t now = time(0);
cout << "Seconds since 1970: " << now << endl;
tm *ltm = localtime(&now);
// Output the various components of the tm structure
cout << "Year: " << 1900 + ltm->tm_year << endl;
cout << "Month: " << 1 + ltm->tm_mon << endl;
cout << "Day: " << ltm->tm_mday << endl;
cout << "Time: " << ltm->tm_hour << ":";
cout << ltm->tm_min << ":";
cout << ltm->tm_sec << endl;
}
When the above code is compiled and executed, it produces the following result:
Seconds since 1970: 1503564157
Year: 2017
Month: 8
Day: 24
Time: 16:42:37