C localtime_r() Function

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

Definition

The POSIX localtime_r() function converts a time_t calendar value to local broken-down time and stores the fields in a caller-provided struct tm.

It performs the same local-zone conversion as localtime() without returning shared static storage. This makes separate result objects practical in threaded or nested code. Include <time.h>.

Syntax

Start with this concrete localtime_r() call:

C
localtime_r(&stamp, &local);
CallWhat it does
localtime_r(&stamp, &local);Writes local fields for stamp into local.

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_r(const time_t *restrict timer, struct tm *restrict result);

Parameters

ParameterDescription
timerPointer to the calendar-time value.
resultPointer to caller-owned struct tm storage.

Return Value

Return ValueDescription
resultConversion succeeded and the destination pointer is returned.
NULLConversion failed.

C Examples

Example 1 — Use a Stack Result

This example demonstrates localtime_r() 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;
    if (now != (time_t)-1 && localtime_r(&now, &local) != NULL)
        printf("local hour: %d\n", local.tm_hour);
    return 0;
}

How It Works

  1. Step 1. Request the current calendar time.
  2. Step 2. Reserve a local struct tm on the stack.
  3. Step 3. Pass its address as the conversion destination.
  4. Step 4. Read fields directly from caller-owned storage after success.

Example 2 — Keep Two Local Dates

This example demonstrates localtime_r() 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 first_t = (time_t)0, second_t = (time_t)86400;
    struct tm first, second;
    if (localtime_r(&first_t, &first) && localtime_r(&second_t, &second))
        printf("%d -> %d\n", first.tm_mday, second.tm_mday);
    return 0;
}

How It Works

  1. Step 1. Prepare two sample instants.
  2. Step 2. Convert each into a distinct structure.
  3. Step 3. The second conversion does not replace the first result.
  4. Step 4. Compare both local day fields after conversion.

Example 3 — Format Local Time Safely

This example demonstrates localtime_r() 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;
    char text[40];
    if (localtime_r(&now, &local) != NULL &&
        strftime(text, sizeof text, "%A, %B %d at %H:%M", &local) > 0)
        printf("%s\n", text);
    return 0;
}

How It Works

  1. Step 1. Convert the current instant into caller-owned local fields.
  2. Step 2. Provide a fixed-size character destination.
  3. Step 3. strftime() expands weekday, month, date, and clock directives.
  4. Step 4. Print the buffer only when formatting returns a nonzero count.

Frequently Asked Questions

localtime_r() converts calendar time to local fields in caller-owned storage.
Include <time.h> before calling localtime_r().
On success it returns the destination pointer after filling your struct tm. On failure it returns NULL.
localtime_r() is POSIX, not ISO C. Microsoft environments usually offer localtime_s() with a different signature.
It is specified by POSIX rather than ISO C, so check the target platform.

Next: mktime()

Continue through the C time.h function tutorials.

mktime() →

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