C++ Date and Time
The date and time formats in C++ will be covered in this article. Because C++ lacks a proper date and time format, we must rely on the c language. The < ctime > header file is
C++ Date and Time
The date and time formats in C++ will be covered in this article. Because C++ lacks a proper date and time format, we must rely on the c language. The < ctime > header file is included in the application to utilize date and time in C++.
The following are the four time-related types in this header file:
- The t-clock is an arithmetic type name and represents a type of clock. Represents a cold number in the clock (fixed time units with a certain system length). Retrieved type (clock /) t.
- Time t is an abbreviated form of time. Represents the time returned by the time function (). If the time exceeds 00:00 hours, we subtract the total value as the number of seconds exceeds.
- Size t indicates the size of any object by bytes and is a nickname for the type of the unsigned number. Task size () prints size and calculation, and output is size t.
- tm - In the C structure, the tm structure saves the day and time. Described as follows-
struct time {
int time_sec; // seconds of minutes from 0 to 61
int time_min; // minutes of hour from 0 to 59
int time_hour; // hours of day from 0 to 24
int time_monday; // day of month from 1 to 31
int time_month; // month of year from 0 to 11
int time_year; // year since 1900
int time_wday; // days since sunday
int time_yday; // days since January 1st
int time_isdst; // hours of daylight savings time
}
C++ date and time functions
The function name | The function prototype | The function description |
mktime | time_t mktime ( struct time *tm ) ; | This function converts mktime to time t, which is the calendar date and time format. |
ctime | char *ctime ( const time_t *tm ) ; | It gives you a reference to a string with the following format: day month year hours: minutes: seconds year. |
difftime | double difftime ( time_t time2, time_t time1 ) ; | The difference between two time objects t1 and t2 is returned. |
gmtime | struct time *gmtime ( const time_t *time ) ; | This method returns a reference to the time in structure format. The time is in UTC (Universal Coordinated Time). |
clock | clock_t clock ( void ) ; | It offers a rough estimate of how long the caller application has been running. If the value is not accessible, the value.1 is returned. |
localtime | struct time *localtime ( const time_t *time ) ; | The pointer to the tm structure, which represents local time, is returned by this method. |
time | time_t time ( time_t *time ) ; | It is the current time. |
strftime | size_t strftime ( ) ; | We may format date and time in a certain way with the aid of this function. |
asctime | char * asctime ( const struct tm * time ) ; | The function transforms the tm type object to a string and returns the string's pointer. |
The example below shows how to print the current date and time in UTC format:
#include < ctime >
#include < iostream >
#include < bits/stdc++.h >
#include < stdlib >
#include < stdio >
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
return 0;
}
OUTPUT:
The local date and time is: Wed Jun 15 20:19:40 2022
The UTC date and time is: Wed Jun 15 20:19:40 2022
………………………………………………………………………………..
Process executed in 2.33 seconds
Press any key to continue.
Explanation:
In the above example of a program in C++, we have used a header file named ctime to find the current date and time, time_t get our current date and time with re4spect to the system.
The code below shows how to split the tm structure and output each attribute separately using the -> operator:
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
time_t now = time(0); // get current date and time
cout << "Number of seconds sinceJune 1, 2022 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;
return 0;
}
OUTPUT:
Number of seconds since June 1, 2022 is:: 1632328
Year:2022
Month: 6
Day: 15
Time: 22:40:53
................................................
Process executed in 2.22 seconds
Press any key to continue.
Explanation:
In the above example of a code in C++, we have shown how to split the tm structure and output each attribute separately using the -> operator.
included in the application to utilize date and time in C++.The following are the four time-related types in this header file:
- The t-clock is an arithmetic type name and represents a type of clock. Represents a cold number in the clock (fixed time units with a certain system length). Retrieved type (clock /) t.
- Time t is an abbreviated form of time. Represents the time returned by the time function (). If the time exceeds 00:00 hours, we subtract the total value as the number of seconds exceeds.
- Size t indicates the size of any object by bytes and is a nickname for the type of the unsigned number. Task size () prints size and calculation, and output is size t.
- tm - In the C structure, the tm structure saves the day and time. Described as follows-
struct time {
int time_sec; // seconds of minutes from 0 to 61
int time_min; // minutes of hour from 0 to 59
int time_hour; // hours of day from 0 to 24
int time_monday; // day of month from 1 to 31
int time_month; // month of year from 0 to 11
int time_year; // year since 1900
int time_wday; // days since sunday
int time_yday; // days since January 1st
int time_isdst; // hours of daylight savings time
}
C++ date and time functions
The function name | The function prototype | The function description |
mktime | time_t mktime ( struct time *tm ) ; | This function converts mktime to time t, which is the calendar date and time format. |
ctime | char *ctime ( const time_t *tm ) ; | It gives you a reference to a string with the following format: day month year hours: minutes: seconds year. |
difftime | double difftime ( time_t time2, time_t time1 ) ; | The difference between two time objects t1 and t2 is returned. |
gmtime | struct time *gmtime ( const time_t *time ) ; | This method returns a reference to the time in structure format. The time is in UTC (Universal Coordinated Time). |
clock | clock_t clock ( void ) ; | It offers a rough estimate of how long the caller application has been running. If the value is not accessible, the value.1 is returned. |
localtime | struct time *localtime ( const time_t *time ) ; | The pointer to the tm structure, which represents local time, is returned by this method. |
time | time_t time ( time_t *time ) ; | It is the current time. |
strftime | size_t strftime ( ) ; | We may format date and time in a certain way with the aid of this function. |
asctime | char * asctime ( const struct tm * time ) ; | The function transforms the tm type object to a string and returns the string's pointer. |
The example below shows how to print the current date and time in UTC format:
#include < ctime >
#include < iostream >
#include < bits/stdc++.h >
#include < stdlib >
#include < stdio >
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
return 0;
}
OUTPUT:
The local date and time is: Wed Jun 15 20:19:40 2022
The UTC date and time is: Wed Jun 15 20:19:40 2022
………………………………………………………………………………..
Process executed in 2.33 seconds
Press any key to continue.
Explanation:
In the above example of a program in C++, we have used a header file named ctime to find the current date and time, time_t get our current date and time with re4spect to the system.
The code below shows how to split the tm structure and output each attribute separately using the -> operator:
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
time_t now = time(0); // get current date and time
cout << "Number of seconds sinceJune 1, 2022 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;
return 0;
}
OUTPUT:
Number of seconds since June 1, 2022 is:: 1632328
Year:2022
Month: 6
Day: 15
Time: 22:40:53
................................................
Process executed in 2.22 seconds
Press any key to continue.
Explanation:
In the above example of a code in C++, we have shown how to split the tm structure and output each attribute separately using the -> operator.