Home C time.h Functions localtime_r() C localtime_r() Function 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:
localtime_r(&stamp, &local); Call What 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 struct tm *localtime_r(const time_t *restrict timer, struct tm *restrict result); Parameters Parameter Description timer Pointer to the calendar-time value. result Pointer to caller-owned struct tm storage.
Return Value Return Value Description resultConversion succeeded and the destination pointer is returned. NULLConversion failed.
C Examples 1. Use a Stack Result 2. Keep Two Local Dates 3. Format Local Time Safely Example 1 — Use a Stack Result This example demonstrates localtime_r() with a small, complete program and checks the relevant result before using it.
#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;
} local hour: 20
(example value varies) How It Works Step 1. Request the current calendar time.Step 2. Reserve a local struct tm on the stack.Step 3. Pass its address as the conversion destination.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.
#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;
} 1 -> 2
(output depends on local time zone) How It Works Step 1. Prepare two sample instants.Step 2. Convert each into a distinct structure.Step 3. The second conversion does not replace the first result.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.
#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;
} Tuesday, November 17 at 20:15
(example value varies) How It Works Step 1. Convert the current instant into caller-owned local fields.Step 2. Provide a fixed-size character destination.Step 3. strftime() expands weekday, month, date, and clock directives.Step 4. Print the buffer only when formatting returns a nonzero count.Frequently Asked Questions What does localtime_r() do in C? localtime_r() converts calendar time to local fields in caller-owned storage.
Which header declares localtime_r()? Include <time.h> before calling localtime_r().
What does localtime_r() return? On success it returns the destination pointer after filling your struct tm. On failure it returns NULL.
What should beginners check when using localtime_r()? localtime_r() is POSIX, not ISO C. Microsoft environments usually offer localtime_s() with a different signature.
Is localtime_r() portable? 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 Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion