How to retrieve the date and time info (C/C++)
In order to get the current date and time in C / C++, we do the following steps
(1) #include "time.h"
(2) Declare two structure of type tm* and time_t
(3) Call time() by passing it the address of a time_t variable. When time() returns, the current time will be stored in time_tTime. time_tTime will contain the number of seconds elapsed since midnight (00:00:00) of January 1, 1970, coordinated universal time (UTC), according to the system clock.
(4) Call localtime()by passing it the address of a time_t variable which has been passed to time(). This function converts the time_t information to the current local time and extracting the time information (such as sec, min, day, etc)
currentTimePtr = localtime( &time_tTime);
The tm structure contains the following members (taken from the MSDN Library)
tm_sec
tm_min
tm_hour
tm_mday
tm_mon
tm_year
tm_wday
tm_yday
tm_isdst
(1) #include "time.h"
(2) Declare two structure of type tm* and time_t
tm* currentTimePtr;
time_t time_tTime;
(3) Call time() by passing it the address of a time_t variable. When time() returns, the current time will be stored in time_tTime. time_tTime will contain the number of seconds elapsed since midnight (00:00:00) of January 1, 1970, coordinated universal time (UTC), according to the system clock.
time(&time_tTime);
(4) Call localtime()by passing it the address of a time_t variable which has been passed to time(). This function converts the time_t information to the current local time and extracting the time information (such as sec, min, day, etc)
currentTimePtr = localtime( &time_tTime);
The tm structure contains the following members (taken from the MSDN Library)
tm_sec
Seconds after minute (0 – 59).
tm_min
Minutes after hour (0 – 59).
tm_hour
Hours after midnight (0 – 23).
tm_mday
Day of month (1 – 31).
tm_mon
Month (0 – 11; January = 0).
tm_year
Year (current year minus 1900).
tm_wday
Day of week (0 – 6; Sunday = 0).
tm_yday
Day of year (0 – 365; January 1 = 0).
tm_isdst
Positive value if daylight saving time is in effect;
0 if daylight saving time is not in effect;
negative value if status of daylight saving time is unknown.
If the TZ environment variable is set, the C run-time library assumes rules appropriate to the United States for implementing the calculation of daylight-saving time (DST).

0 Comments:
Post a Comment
<< Home