Topic :Date and Time in C++ Programming Language:

Date & Time:

The C++ standard library does not provide a proper data type. C++ Inherits the structs and functions for date and time manipulation from C. To access date and time related functions and structures, we would need to include <ctime> header file in our c++ program. This header provides four data types used for time representation.



  • » Clock_t
    » time_t
    » size_t
    » tm



The First three data types represent time as integers and we will need to convert these integers to get commonly used a representation of time. The structure type tm holds the date and time in the form of a C structure having the following elements:



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
}


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 we can check their details using reference to C++ standard library given below.



=========Function=========purpose
time-t  time(time_t *time);This function returns the current calendar time of the system in number of seconds elapsed since January 1, 1970. if 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 of the form day month year hours:minutes:seconds year \n\0.
struct tm *localtime(const time_t *time); This function is used to returns a pointer to the tm structure representing 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. A value of .1 us returned if the time is not available.
char *asctime(const struct tm *time); This function return a pointer to a string that contains the information stored in the structure pointed to by time converted into the form: day monh date Hours:minutes:second year \n\0
struct tm *gmtime(const time_t *time); This function is used to returns a pointer to the time in the form of a tm structure. The time is represented in Coordinated Universal time (UTC), which is essentially Greenwich mean time (GMT).
double difftime(time_t   time2, time_t   time1); This function calculates the difference in seconds between time1 and time2.
size-t strftime(); This function can be used to format data and time a specific format.
time_t mktime(struct tm *time); This function is used to returns the calendar-time equivalent of the time found in the structure pointed to by time.


The most user friendly way of time representation has Struct tm. what is a structure is discussed in the C++ Data Structure tutorial. The tm has the following fields that represent time.



FieldTypeMeaningRange
tm_secintseconds after the minute0-61
tm_minintminutes after the hour0-59
tm_hourinthours since midnight0-23
tm_mdayintDay of the month1-31
tm_monintMonths since January0-11
tm_yearintYears since 19000-6
tm_ydayintDays since January 10-365
tm_isdstintDaylight saving Time flag....

The Daylight Saving Time Flag (tm_isdst) is greater than zero if Daylight Saving Time is in Effect, zero if Daylight saving time is not in effect, and less than zero if the information is not available.



Current Data and Time:

Consider we want to retrieve the current system date and time, either as a local time or as a Coordinated Universal Time (UTC). Following is the example to achieve the same:



/* Current Date & Time: InfoBrother:*/
 
#include <iostream>
#include <ctime>
using namespace std;
 
main( ) 
{
	// current date/time based on current system
	time_t now = time(0);
 
	// convert now to string form
	char* dt = ctime(&now);
 
	cout << "The local date and time is: " << dt << endl;
 
	// convert now to tm struct for UTC
	tm *gmtm = gmtime(&now);
	dt = asctime(gmtm);
	cout << "The UTC date and time is:"<< dt << endl;
 
	return 0;
}


Format Time using Struct tm:

The tm structure is very important while working with date and time in either C or C++. This structure holds the date and time in the form of a C structure as mentioned above. Most of the time related functions makes use of tm structure. following is an example which makes use of various date and time related functions and tm structure:



While using structure in this Tutorial, I'm making an assumption that you have basic understanding on C structure and how to access structure members using arrow -> operator.




/*Format time using struct tm: InfoBrother:*/
 
#include <iostream>
#include <ctime>
using namespace std;
 
main( ) 
{
	// current date/time based on current system
	time_t now = time(0);
 
	cout << "Number of sec since January 1,1970:" << now << endl;
 
	tm *ltm = localtime(&now);
 
	// print various components of tm structure.
	cout << "Year: " << 1900 + ltm->tm_year<<endl;
	cout << "Month: "<< 1 + ltm->tm_mon<< endl;
	cout << "Day: "<< ltm->tm_mday << endl;
	cout << "Time: "<< 1 + ltm->tm_hour << ":";
	cout << 1 + ltm->tm_min << ":";
	cout << 1 + ltm->tm_sec << endl;
 
	return 0;
}








I Tried my Best to Provide you complete Information regarding this topic in very easy and conceptual way. but still if you have any Problem to understand this topic, or do you have any Questions, Feel Free to Ask Question. i'll do my best to Provide you what you need.

Sardar Omar.
InfoBrother





WRITE FOR INFOBROTHER

Advertising






Advertisement