w3resource

C mktime() function

C mktime() function - Convert broken-down time into time since the Epoch

Syntax:

time_t mktime(struct tm *timeptr)

The mktime() function is used to convert a stored tm structure (assume to be in job local time) pointed to by time, into a time_t structure suitable for use with other time functions.

The fields of the tm structure include:

tm_sec Seconds (0-61)
tm_min Minutes (0-59)
tm_hour Hours (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 Zero if daylight saving time is not in effect; positive if daylight
saving time is in effect; negative if the information is not available.

Parameters:

Name Description Required /Optional
timeptr Pointer to time structure Required

Return value from mktime()

  • Universal Coordinate Time (UTC) having type time_t.
  • The value (time_t)(-1) is returned if the Universal Coordinate Time cannot be represented.

Example: mktime() function

The following example shows the usage of mktime() function.

#include <stdio.h>
#include <time.h>
 
char *weekday[] = { "Sunday", "Monday", "Tuesday", "Wednesday",
                 "Thursday", "Friday", "Saturday" };
 
int main(void)
{
  time_t dt1, dt3;
  struct tm *t2;
 
  dt1 = time(NULL);
  t2 = localtime(&dt1);
  t2 -> tm_mday += 110;
  t2 -> tm_hour += 20;
  dt3 = mktime(t2);
 
  printf("It will be a %s after 110 days and 20 hours from now.\n",
          weekday[t2 -> tm_wday]);
}

Output:

It will be a Monday after 110 days and 20 hours from now.

C Programming Code Editor:

Previous C Programming: C localtime()
Next C Programming: C strftime()



Follow us on Facebook and Twitter for latest update.