The mktime() function is defined in <ctime> header file.
mktime() prototype
time_t mktime(tm* time);
The mktime function takes a pointer to a tm object as its argument and returns the time since epoch as a value of type time_t
. The values, time->tm_wday and time->tm_yday are ignored.
If the value of time->tm_isdst
is negative, it causes mktime to attempt to determine if Daylight Saving Time was in effect.
mktime() Parameters
- time: A pointer to a tm object that represents the local calendar time to convert.
mktime() Return value
- On success, the mktime() function returns the time since epoch as an object of type time_t.
- On failure it returns -1.
Example: How mktime() function works?
#include <iostream>
#include <ctime>
using namespace std;
int main ()
{
time_t tim;
tm *ptr;
int y = 2017, m = 4, d = 20;
char weekday[7][20] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
time(&tim);
ptr = localtime(&tim);
// tm_year is time since 1900
ptr->tm_year = y - 1900;
ptr->tm_mon = m - 1;
ptr->tm_mday = d;
mktime (ptr);
cout << "April 20, 2017 was " << weekday[ptr->tm_wday];
return 0;
}
When you run the program, the output will be:
April 4, 2017 was a Thursday