C Time Functions - C localtime






Converts time to local time.

Prototype

struct tm * localtime (const time_t * timer);

Parameter

This function has the following parameter.

timer
Pointer to time_t type containing a time value.

Return

A pointer to a tm structure for the local time representation of timer.

Example


#include <stdio.h> /* puts, printf */
#include <time.h> /* time_t, struct tm, time, localtime */
//  w w  w  . j  a  v  a 2 s  .co m
int main (){
  time_t rawtime;
  struct tm * timeinfo;

  time (&rawtime);
  timeinfo = localtime (&rawtime);
  printf ("Current local time and date: %s", asctime(timeinfo));

  return 0;
} 

       

The code above generates the following result.





Example 2


#include <time.h>
#include <stdio.h>
 
int main(void)
{
    time_t t = time(NULL);
    printf("UTC:   %s", asctime(gmtime(&t)));
    printf("local: %s", asctime(localtime(&t)));
}

The code above generates the following result.