mktime() function in C++ STL
Last Updated :
20 Jul, 2021
The mktime() is an inbuilt C++ function which converts the local calendar time to the time since epoch and returns the value as an object of type time_t.
Syntax :
time_t mktime( struct tm *time_ptr )
Parameters: The function accepts a mandatory parameter pointer time_ptr that points to a tm object structure that contains a calendar time which is to be converted.
Return Value: The function returns two type of values as described below:
- It returns the time since epoch as an object of type time_t if the parameter passed is a success.
- It returns -1 on failure.
Below program illustrates the mktime() function:
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
time_t tim;
tm * time_ptr;
char weekday[7][20] = { "Sunday" ,
"Monday" ,
"Tuesday" ,
"Wednesday" ,
"Thursday" ,
"Friday" ,
"Saturday" };
int year = 2018;
int month = 6;
int day = 18;
time (&tim);
time_ptr = localtime (&tim);
time_ptr->tm_year = year - 1900;
time_ptr->tm_mon = month - 1;
time_ptr->tm_mday = day;
mktime (time_ptr);
cout << "The Day on 18th June 2018 was "
<< weekday[time_ptr->tm_wday];
return 0;
}
|
Output
The Day on 18th June 2018 was Monday
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...