C localtime() Function

Beginner
7 min read
Updated: Nov 2026
3 examples
<time.h>

Definition

The C localtime() function converts a time_t calendar value into local broken-down time. The result reflects the implementation's configured time zone and daylight-saving rules.

Its returned struct tm * normally points to shared static storage. Check for NULL, and copy the structure if its values must survive another time-conversion call. Include <time.h>.

Syntax

Start with this concrete localtime() call:

C
localtime(&stamp);
CallWhat it does
localtime(&stamp);Converts stamp to local calendar fields.

Declare the required objects with the time types shown below, include <time.h>, and check the documented result before using output data.

Library declaration
C
struct tm *localtime(const time_t *timer);

Parameters

ParameterDescription
timerPointer to the calendar-time value to convert.

Return Value

Return ValueDescription
struct tm *Pointer to local broken-down time in static storage.
NULLThe local representation is unavailable.

C Examples

Example 1 — Show Current Local Time

This example demonstrates localtime() with a small, complete program and checks the relevant result before using it.

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

int main(void) {
    time_t now = time(NULL);
    struct tm *local = localtime(&now);
    if (local != NULL)
        printf("%02d:%02d:%02d\n", local->tm_hour, local->tm_min, local->tm_sec);
    return 0;
}

How It Works

  1. Step 1. Read the current calendar-time value.
  2. Step 2. Convert that instant according to the local time zone.
  3. Step 3. Verify the static result pointer.
  4. Step 4. Print the local hour, minute, and second fields.

Example 2 — Read Calendar Fields

This example demonstrates localtime() with a small, complete program and checks the relevant result before using it.

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

int main(void) {
    time_t stamp = (time_t)0;
    struct tm *local = localtime(&stamp);
    if (local != NULL)
        printf("%04d-%02d-%02d\n", local->tm_year+1900, local->tm_mon+1, local->tm_mday);
    return 0;
}

How It Works

  1. Step 1. Use a known sample calendar value.
  2. Step 2. localtime() applies local zone rules.
  3. Step 3. Translate the stored year and month conventions.
  4. Step 4. The date may differ around the epoch in zones west of UTC.

Example 3 — Copy the Result

This example demonstrates localtime() with a small, complete program and checks the relevant result before using it.

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

int main(void) {
    time_t now = time(NULL);
    struct tm *ptr = localtime(&now);
    if (ptr != NULL) {
        struct tm saved = *ptr;
        printf("day of year: %d\n", saved.tm_yday + 1);
    }
    return 0;
}

How It Works

  1. Step 1. Convert the current instant into shared local fields.
  2. Step 2. Dereference the pointer to copy all fields.
  3. Step 3. tm_yday starts at zero.
  4. Step 4. Add one to display the familiar one-based day of year.

Frequently Asked Questions

localtime() breaks calendar time into local date and clock fields.
Include <time.h> before calling localtime().
On success it returns a pointer to a static struct tm with local fields. On failure it returns NULL.
The result can be overwritten and is not generally thread-safe. POSIX programs can use localtime_r() for caller-owned storage.
It is declared by the C time library; exact representations, locale, time-zone data, and resolution can still vary by implementation.

Next: localtime_r()

Continue through the C time.h function tutorials.

localtime_r() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful