C++ <ctime> - mktime() Function
The C++ <ctime> mktime() function returns the value of type time_t that represents the local time described by the tm structure pointed by timeptr.
The values of the members tm_wday and tm_yday of timeptr are ignored and the values of other members are permitted to be outside their normal ranges. For example, tm_mday may contain values above 31, which are interpreted accordingly as the days that follow the last day of the selected month. A negative value of tm_isdst causes this function to attempt to determine if Daylight Saving Time was in effect.
Syntax
time_t mktime (struct tm * timeptr);
Parameters
timeptr |
Specify pointer to a tm object specifying local calendar time to convert. |
Return Value
Returns a time_t value corresponding to the calendar time passed as argument. Returns -1 if timeptr cannot be represented as time_t object.
Example:
The example below shows the usage of mktime() function.
#include <iostream> #include <ctime> using namespace std; int main () { int ret; struct tm timeinfo; char buffer[80]; timeinfo.tm_year = 2019 - 1900; timeinfo.tm_mon = 7 - 1; timeinfo.tm_mday = 4; timeinfo.tm_hour = 10; timeinfo.tm_min = 28; timeinfo.tm_sec = 48; timeinfo.tm_isdst = -1; ret = mktime(&timeinfo); if( ret == -1 ) { cout<<"Error: Unable to make time using mktime\n"; } else { strftime(buffer, sizeof(buffer), "%c", &timeinfo ); cout<<buffer; } return 0; }
The output of the above code will be:
Thu Jul 4 10:28:48 2019
❮ C++ <ctime> Library