• Home
  • 5.5 localtime()

5.5 localtime()

View Categories

5.5 localtime()

3 min read

Overview #

The localtime() function converts a calendar time stored as a time_t value into a broken-down local date and time.

Instead of returning a formatted string like ctime(), localtime() returns a pointer to a struct tm containing individual date and time components such as the year, month, day, hour, minute, and second.

Header File #

#include <time.h>

Man Pages #

man 3 localtime

Function Prototype #

struct tm *localtime(const time_t *timer);

Parameters #

Parameter Description
timer Pointer to a time_t object containing the calendar time to be converted.

Return Value #

On success, localtime() returns a pointer to a struct tm containing the local date and time.

On failure, it returns:

NULL

Data Types #

struct tm #

struct tm stores the individual components of a date and time.

struct tm
{
    int tm_sec;
    int tm_min;
    int tm_hour;
    int tm_mday;
    int tm_mon;
    int tm_year;
    int tm_wday;
    int tm_yday;
    int tm_isdst;
};
Member Description
tm_sec Seconds (0โ€“60)
tm_min Minutes (0โ€“59)
tm_hour Hours (0โ€“23)
tm_mday Day of the month (1โ€“31)
tm_mon Month since January (0โ€“11)
tm_year Years since 1900
tm_wday Days since Sunday (0โ€“6)
tm_yday Days since January 1 (0โ€“365)
tm_isdst Daylight Saving Time flag

Examples #

Example 1 โ€“ Display Local Date and Time #

main.c #

#include <stdio.h>
#include <time.h>

int main(void)
{
    time_t current_time;
    struct tm *local;

    current_time = time(NULL);

    local = localtime(&current_time);

    printf("Date : %02d-%02d-%04d\n",
           local->tm_mday,
           local->tm_mon + 1,
           local->tm_year + 1900);

    printf("Time : %02d:%02d:%02d\n",
           local->tm_hour,
           local->tm_min,
           local->tm_sec);

    return 0;
}

Output #

Date : 27-07-2026
Time : 15:45:32

The output will be different on your system.

Explanation #

current_time = time(NULL);

Obtains the current calendar time.

local = localtime(&current_time);

Converts the calendar time into a struct tm representing the local date and time.

local->tm_mon + 1

tm_mon stores months in the range 0โ€“11, where January is 0. Adding 1 converts it to the familiar range 1โ€“12.

local->tm_year + 1900

tm_year stores the number of years since 1900. Adding 1900 gives the current year.


Example 2 โ€“ Display Individual Date and Time Components #

main.c #

#include <stdio.h>
#include <time.h>

int main(void)
{
    time_t current_time;
    struct tm *local;

    current_time = time(NULL);
    local = localtime(&current_time);

    printf("Hour   : %d\n", local->tm_hour);
    printf("Minute : %d\n", local->tm_min);
    printf("Second : %d\n", local->tm_sec);

    return 0;
}

Output #

Hour   : 15
Minute : 45
Second : 32

The output will be different on your system.

Explanation #

Each member of the struct tm can be accessed using the -> operator because localtime() returns a pointer to the structure.

Notes #

  • localtime() converts a time_t value into local date and time.
  • It returns a pointer to a struct tm.
  • tm_mon ranges from 0 to 11, so add 1 when displaying the month.
  • tm_year represents the number of years since 1900, so add 1900 to obtain the actual year.
  • The returned pointer refers to statically allocated memory managed by the C library.
  • Each call to localtime() may overwrite the result of a previous call.
  • Do not modify or free the returned structure.

Related Functions #

  • time()
  • ctime()
  • gmtime()
  • mktime()
  • strftime()

Powered by BetterDocs

Leave a Reply

Your email address will not be published. Required fields are marked *