Home >>C++ Tutorial >C++ Date and Time

C++ Date and Time

C++ Date and Time

As a matter of fact we know that C++ is an upgraded version of C hence, there are many functions and structures that are inherited by the C++ from C language. Date and time in C++ is one of structure that has been inherited by the C language in order to manipulate date and time. <ctime> header file is included in the C++ program in order to access date and time regulated functions and structures.

Here are the types of time-related:

  • Clock_t
  • Time_t
  • Size_t
  • Tm

The system time and date is represented as some sort of integer by the types clock_t, size_t and time_t.

Here is the list of the elements that are held by the structure type tm. The structure type tm generally holds the date and time in the form of a C structure that has these 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; 
}

Here is the list of the very important functions that are used when working with the date and time in C or C++.
These below mentioned functions are basically a part of the standard C++ or C library.

Fucntion Description
time_t time(time_t *time) This function is generally used to return the current calendar time of the system in number of seconds elapsed since January 1, 1970. In case, the system has no time, .1 is returned.
char *ctime(const time_t *time) This function is used to return a pointer to a string in the form of day month year hours:minutes:seconds year\n\0.
struct tm *localtime(const time_t *time) This function is used to return a pointer to the tm structure that generally represents the local time.
clock_t clock(void); This function is used to return a value that approximates the amount of time the calling program has been running. In case the time is not available then the value of .1 is returned.
char * asctime ( const struct tm * time ) This function is used to return a pointer to a string containing the information stored in the structure pointed towards by time that is converted into the form of: day month date hours:minutes:seconds year\n\0
struct tm *gmtime(const time_t *time) This function is used to return a pointer to the time in the form of a tm structure. The time is generally represented in Coordinated Universal Time (UTC), which is essentially Greenwich Mean Time (GMT).
time_t mktime(struct tm *time) This function is used to return the calendar-time that is equivalent of the time that is found in the structure pointed to by time.
double difftime ( time_t time2, time_t time1 ) This function is used to calculate the difference that happened in seconds between time1 and time2.
size_t strftime() This function is used to format date and time in to a specific format.

Current Date and Time

Whether the programmer wants to retrieve the current date and time of the system either in the format of local time or in UTC that is basically the Coordinated Universal Time, here is an example that will give you the discussed output:

#include <iostream>
#include <ctime>
using namespace std;
int main() 
{
   // Get current date and time
   time_t now = time(0);
   
   // convert into string format of now
   char* dt = ctime(&now);

   cout << "The current date and time = " << dt << endl;

   // convert to  tm struct for UTC
   tm *gmtm = gmtime(&now);
   dt = asctime(gmtm);
   cout << "The  date and time of UTC is ="<< dt << endl;
} 
Output :
The current date and time = Tue Dec 17 10:22:26 2019
The date and time of UTC is =Tue Dec 17 10:22:26 2019

Format Time using struct tm

The tm structure generally holds the date and time just in the form of a C structure. It is known to be the most important structure while working with the date and time either in C or C++. This function is generally used by the most of the time related functions.

Here is an example that will make the use of various time-related functions and tm structure.

#include <iostream>
#include <ctime>
using namespace std;
int main() 
{
   // Get date and time of current system
   time_t now = time(0);

   cout << "Total Number of sec From 1 January 1970:" << now << endl;

   tm *ltm = localtime(&now);

   // print various components of tm structure.
   cout << "Current Year" << 1900 + ltm->tm_year << endl;
   cout << "Current  Month: "<< 1 + ltm->tm_mon<< endl;
   cout << "Current  Day: "<<  ltm->tm_mday << endl;
   cout << "Current  Time: "<< 1 + ltm->tm_hour << ":";
   cout << 1 + ltm->tm_min << ":";
   cout << 1 + ltm->tm_sec << endl;
} 
Output :
Total Number of sec From 1 January 1970:1576578629
Current Year2019
Current Month: 12
Current Day: 17
Current Time: 11:31:30

No Sidebar ads