In this article, we will learn about the date and time formats in C++. There is no complete format in C++ for date and time so we inherit it from the c language. To use date and time in c++, <ctime> header file is added in the program.
<ctime>
This header file has four time-related types as follows -
- Clockt - It stands for clock type which is an alias of arithmetic type. It represents clock tick counts(units of a time of a constant with system-specific length). Clockt is the type returned by clock/.
- Timet - It stands for timetype. It represents the time that is returned by function time. It outputs an integral value as the number of seconds elapsed when time passes by 00:00 hours.
- Sizet - It is an alias for an unsigned integer type and represents the size of any object in bytes. Sizet is the result of the sizeof operator which prints sizes and counts.
- tm - The tm structure holds date and time in the C structure. It is defined as follows -
Example
struct tm {
int tm_sec; // seconds of minutes from 0 to 61
int tm_min; // minutes of hour from 0 to 59
int tm_hour; // hours of day from 0 to 24
int tm_mday; // day of month from 1 to 31
int tm_mon; // month of year from 0 to 11
int tm_year; // year since 1900
int tm_wday; // days since sunday
int tm_yday; // days since January 1st
int tm_isdst; // hours of daylight savings time
}
Date and time functions in c++
| Name of the function | Prototype of the function | Description About the function |
|---|---|---|
| mktime | time_t mktime(struct tm *time); | This function converts mktime to time_t or calendar date and time. |
| ctime | char ctime(const time_t time); | It returns the pointer to a string of the format - day month year hours: minutes: seconds year. |
| difftime | double difftime ( timet time2, timet time1 ); | It returns the difference of two-time objects t1 and t2. |
| gmtime | struct tm gmtime(const time_t time); | This function returns the pointer of the time in the format of a structure. The time is in UTC. |
| clock | clock_t clock(void); | It returns an approximated value for the amount of time the calling program is being run. The value .1 is returned if not available. |
| localtime | struct tm localtime(const time_t time); | This function returns the pointer to the tm structure representing local time. |
time |
timet time(timet *time); | It represents current time. |
| strftime | size_t strftime(); | With the help of this function, we can format date and time in a specific manner. |
| asctime | char asctime ( const struct tm time ); | The function converts the type object of tm to string and returns the pointer to that string. |
Example to print current date and time
Below is the example to print the current date and time in the UTC format.
Example
#include <ctime>
#include <iostream>
using namespace std;
int main()
{
time_t now = time(0); // get current dat/time with respect to system
char* dt = ctime(&now); // convert it into string
cout << "The local date and time is: " << dt << endl; // print local date and time
tm* gmtm = gmtime(&now); // for getting time to UTC convert to struct
dt = asctime(gmtm);
cout << "The UTC date and time is:" << dt << endl; // print UTC date and time
}
Output
Output
The local date and time is: Wed Sep 22 16:31:40 2021
The UTC date and time is: Wed Sep 22 16:31:40 2021
The below code tells how to break the tm structure and to print each attribute independently with the use of -> operator.
Example
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
time_t now = time(0); // get current date and time
cout << "Number of seconds since January 1,2021 is:: " << now << endl;
tm* ltm = localtime(&now);
// print various components of tm structure.
cout << "Year:" << 1900 + ltm->tm_year << endl; // print the year
cout << "Month: " << 1 + ltm->tm_mon << endl; // print month number
cout << "Day: " << ltm->tm_mday << endl; // print the day
// Print time in hour:minute:second
cout << "Time: " << 5 + ltm->tm_hour << ":";
cout << 30 + ltm->tm_min << ":";
cout << ltm->tm_sec << endl;
}
Output
Output
Number of seconds since January 1,2021 is:: 1632328553
Year:2021
Month: 9
Day: 22
Time: 21:65:53