C++ Standard Library C++ STL Library

C++ <ctime> - localtime() Function



The C++ <ctime> localtime() function converts the value pointed by timer into calendar time, expressed in local time. The returned value a pointer to a tm object filled with the values representing local time for timer on success, or null pointer otherwise.

Syntax

struct tm * localtime (const time_t * timer);

Parameters

timer Specify pointer to a time_t object to convert.

Return Value

Returns a pointer to a tm object filled with the values representing local time for timer on success, or null pointer otherwise. The structure may be shared between gmtime(), localtime(), and ctime(), and may be overwritten on each invocation of any of these functions.

Example:

The example below shows the usage of localtime() function.

#include <iostream>
#include <ctime>
using namespace std;
 
int main (){
  time_t t = time(NULL);
  struct tm * timeinfo = localtime(&t);

  //displaying the result 
  cout<<"Current local time & date: "<<
       asctime(timeinfo);   
  
  return 0;
}

The output of the above code will be:

Current local time & date: Fri Apr 16 10:25:51 2021

❮ C++ <ctime> Library