mktime() function in C++ STL


In this article we will be discussing the working, syntax and examples of mktime() function in C++ STL.

What is mktime()?

mktime() function is an inbuilt function in C++ STL, which is defined in the <ctime> header file. mktime() function is used to convert the local time to and object time_t.

This function is like the reverse of the function localtime(), which converts an input to the local timezone of the machine.

This function automatically modifies the values of the member timeptr if they are off the range or there is tm_day and tm_yday which are not allowed.

Syntax

time_t mktime( struct tm* tptr );

Parameters

The function accepts following parameter(s) −

  • tptr − The pointer to the structure which contains the local time.

Return value

This function returns the time_t value corresponding to tptr.

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int main(){
   time_t hold;
   tm* hold_ptr;
   char days[7][20] = {"Sunday", "Monday", "tuesday","Wednesday","Thursday","Friday","Saturday" };
   int year = 1996;
   int month = 9;
   int day = 25;
   time(&hold);
   hold_ptr = localtime(&hold);
   hold_ptr->tm_year = year - 1900;
   hold_ptr->tm_mon = month - 1;
   hold_ptr->tm_mday = day;
   mktime(hold_ptr);
   cout<<"Day on 25th September 1996 was "<<days[hold_ptr->tm_wday];
   return 0;
}

Output

Day on 25th September 1996 was Wednesday

Updated on: 17-Apr-2020

534 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements