w3resource

C localtime() function

C localtime() function - Convert a time value to a broken-down local time

Syntax:

struct tm *localtime(const time_t *timer)

The localtime() function is used to convert a time value, in seconds, to a structure of type tm.

The fields of the tm structure include:

tm_sec Seconds (0-61)
tm_min Minutes (0-59)
tm_hour Hours (0-23)
tm_mday Day of month (1-31)
tm_mon Month (0-11; January = 0)
tm_year Year (current year minus 1900)
tm_wday Day of week (0-6; Sunday = 0)
tm_yday Day of year (0-365; January 1 = 0)
tm_isdst Zero if daylight saving time is not in effect; positive if daylight
saving time is in effect; negative if the information is not available.

Parameters:

Name Description Required /Optional
timer Pointer to stored time. Required

Return value from localtime()

  • Returns a pointer to the structure result.
  • There is no error return value.

Example: localtime() function

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

#include <time.h>
#include <stdio.h>
 
int main(void)
{
   struct tm *new_time;
   time_t ltime;
 
   ltime = time(<ime);
   new_time = localtime(<ime);
   printf("The date and time is: %s", asctime(new_time));}

Output:

The date and time is: Tue Dec 20 21:50:05 2022

C Programming Code Editor:

Previous C Programming: C gmtime()
Next C Programming: C mktime()



Follow us on Facebook and Twitter for latest update.