Facebook Page Twitter Page LinkedIn Page
× C Standard Library


The C <time.h> 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 below example shows the usage of localtime() function.

#include <stdio.h>
#include <time.h>
 
int main (){
  time_t t = time(NULL);
  struct tm * timeinfo = localtime(&t);

  //displaying the result 
  printf("Current local time & date: %s",
       asctime(timeinfo));   
  
  return 0;
}

The output of the above code will be:

Current local time & date: Fri Apr 16 10:46:02 2021

❮ C <time.h> Library

5