localtime() function in C++


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

What is localtime()?

localtime() function is an inbuilt function in C++ STL, which is defined in the <ctime> header file. localtime() is used to convert the given time into the local time.

This function uses a value referred by the timer and fill the value of the structure and it converts the value into the given local timezone

Syntax

localtime(time_t *timer);

Parameters

The function accepts the following parameter(s) −

  • timer − It is a pointer to objects which have the time_t type value.

Return value

This function returns a pointer pointing to the structure of the time which is stored in the local timezone.

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int main(){
   time_t hold;
   hold = time(NULL);
   tm* hold_local = localtime(&hold);
   cout<<"Current local time of system is: "<< hold_local->tm_hour << ":"<<hold_local->tm_min << ":"<<hold_local->tm_sec;
   return 0;
}

Output

Current local time of system is: 8:28:57

Updated on: 17-Apr-2020

149 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements