Home >>C Time Library Function >C mktime() function

C mktime() function

C mktime() function

C mktime() function is used to convert the local calendar time to the time since epoch. It returns the value as an object of type time_t. It is defined in <time.h> header file.

Syntax:
time_t mktime(struct tm *timeptr)

Parameters:-

timeptr− It is the pointer to a time_t value representing a calendar time, broken down into its components.

Here is an example of mktime() function:

#include <stdio.h>
#include <time.h>
int main () 
{
int ret;
struct tm info;
char buffer[80];

info.tm_year = 2001 - 1900;
info.tm_mon = 7 - 1;
info.tm_mday = 4;
info.tm_hour = 0;
info.tm_min = 0;
info.tm_sec = 1;
info.tm_isdst = -1;

ret = mktime(&info);
if( ret == -1 ) {
printf("Error: unable to make time using mktime\n");
} else {
strftime(buffer, sizeof(buffer), "%c", &info );
printf(buffer);
}
return(0);
}

Output:
Wed Jul 4 00:00:01 2001
Example-2

#include <stdio.h>
#include <time.h>
int main () 
{
int ret;
struct tm info;
char buffer[80];

info.tm_year = 2020 - 1987;
info.tm_mon = 5 - 1;
info.tm_mday = 5;
info.tm_hour = 0;
info.tm_min = 23;
info.tm_sec = 37;
info.tm_isdst = -1;

ret = mktime(&info);
if( ret == -1 ) {
  printf("Error: unable to make time using mktime\n");
} else {
  strftime(buffer, sizeof(buffer), "%c", &info );
  printf(buffer);
}

return(0);
}

Output:
Fri May 5 00:23:37 1933

No Sidebar ads