C programming mktime time.h

In C programming the <time.h> mktime function calculate the local calendar time of the value assigned to the members of the struct tm.A detail explanation is given later.The declaration of the function is given below.

time_t mktime(struct tm *timeptr);

timeptr – A pointer to ‘struct tm’.The ‘struct tm’ is defined as,

struct tm
 int tm_sec; /* Seconds (0-60; 1 leap second) */
 int tm_min; /* Minutes (0-59) */
 int tm_hour; /* Hours (0-23) */
 int tm_mday; /* Day (1-31) */
 int tm_mon; /* Month (0-11) */
 int tm_year; /* Year (difference from 1900) */
 int tm_wday; /* Day of week (0-6) */
 int tm_yday; /* Day of year (0-365) */
 int tm_isdst; /* Daylight saving time (-1, 0, 1) */

Return type
time_t -Returns the specified calendar time encoded as a value of type time_t.If the calendar time cannot be represented,the function returns the value (time_t)(-1).

Note:The ‘tm_isdt’ is 0 if daylight saving time is not in effect,if it is the value is 1.The negative value indicate that the information is unavailable and in which case the function mktime() tries to calculate whether daylight saving time is applicable at the time represented by the other members.

The function ignores the member ‘tm_wday and tm_yday’,but use ‘tm_isdst’.Some of the values assigned to the members may be outside the range in which case the function will readjust the value to fit the range.How the function adjust the outside range values is shown below.For easier understanding let say the value assigned is ‘x’,

For ‘tm_sec’ if ‘x>60’ the value assigned is ‘x-60’ and the extra value is counted as minute.For instance say the second assigned is 100,which is in fact 1 minute 40 seconds so 40(100-60) is assign as seconds and the extra remaining 60 second(1 minute) is counted as minute.

For ‘tm_min’ if ‘x>59’ the value assigned is ‘x-59’,and the extra minutes is counted as hour.

For ‘tm_hour’ if ‘x>23’ the value assigned is ‘x-23’,but if ‘x<=23’ the value assigned is ‘x+1’.The extra hours will be added to the next day hour.

In case of ‘tm_mon'(month) it is affected by ‘tm_mday’ if the value is larger than 31 or 30 depending on the month having 30 or 31 days.For instance say the ‘tm_mon’ is assigned 5-which is June and have 30 days, and say the value assigned to ‘tm_mday’ is 31 but since June have only 30 days the extra 1 day will be added to the next month so the actual ‘tm_mon’ is 6-which is July and the ‘tm_mday’ is 1,so the correct encoded value is ‘1st July’ not ‘1st June’.And Of course all these adjustment is done automatically by the ‘mktime’ function.The explanation here is given to give you a brief idea of how the adjustment is made and it is possible that not knowing how things work may confuse you and you may even losing some sleep at night:trust me losing sleep for a programmer over some small matter is not an incredulous thing.

A code example is given below.

Code example

time_t ret;
struct tm when ;

when.tm_sec = 100;
when.tm_min = 10;
when.tm_hour = 21;
when.tm_mday = 31;
when.tm_mon = 5;
when.tm_year = 117;
when.tm_wday = 11;
when.tm_yday = 111;
when.tm_isdst = 1;

ret = mktime(&when);

printf(“ret=%d” , ret);
printf(“\nThe local time is=%s”, asctime(&when) ); /*the ‘asctime’ function decode the ‘struct tm’ members value to the appropriate time*/

Output in VS,

The local time is=Sat Jul 1 21:11:40 2017

Here you can see the second is 100 so the extra 1 minute is added to the minute value giving minute as 11,and of course the month is July and the date is 1,since the value assigned to ‘tm_mday’ is 31 and ‘tm_mon’ is 5.The week day is decoded from the ‘tm_mday’ and ‘tm_mon’ where 1st of July is Saturday,hence the ‘tm_wday’ is set as 6.

Related links

->C asctime time.h

->C ctime time.h