C LAB‎ > ‎Beyond‎ > ‎

### Date - Time Operations. How long can you last?

posted Jan 22, 2011, 12:11 AM by Neil Mathew   [ updated Jan 22, 2011, 3:41 AM ]
Dad asked me to try a question yesterday. Let's see how far I can reach here.

Q.  WAP to accept the starting date and time and the ending date and time and find the total time in minutes that has transpired in between.

Once that is done, I'll have to continue with two conditions.
One, Sunday should not be counted. No hours or minutes belonging to Sunday.
Two, No time between 6pm and 10am should be counted either.

A.  Under way...

Before I can start, I need to learn the date time functions available for use in the C library. I've taken the help of cplusplus.com. It has provided me with the following:

#### Fact #1:

The <time.h> headerfile has a number of functions dealing with time.

time_t is the derived datatype used to create variables storing seconds in decimal form.
It is almost universally expected to be an integral value representing the number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC

struct tm
The structure contains nine members of type int, which are (in any order):
 123456789 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;

The meaning of each is:
MemberMeaningRange
tm_secseconds after the minute0-61*
tm_minminutes after the hour0-59
tm_hourhours since midnight0-23
tm_mdayday of the month1-31
tm_monmonths since January0-11
tm_yearyears since 1900
tm_wdaydays since Sunday0-6
tm_ydaydays since January 10-365
tm_isdstDaylight Saving Time flag

tm_sec is generally 0-59. Extra range to accommodate for leap seconds in certain systems.

### Functions > - Functions I need to learn.

Time manipulation
 clock Clock program (function)
 >difftime Return difference between two times using time_t arguments (function)
 >mktime Convert tm structure to time_t (function)
 time Get current time (function)

Conversion:
 >asctime Convert tm structure to string (function)
 >ctime Convert time_t value to string (function)
 gmtime Convert time_t to tm as UTC time (function)
 >localtime Convert time_t to tm as local time (function)
 strftime Format time to string (function)