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);
Call
What 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
Parameter
Description
timer
Pointer to the calendar-time value to convert.
Return Value
Return Value
Description
struct tm *
Pointer to local broken-down time in static storage.
NULL
The 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;
}
Output
20:15:00
(example value varies)
How It Works
Step 1. Read the current calendar-time value.
Step 2. Convert that instant according to the local time zone.
Step 3. Verify the static result pointer.
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.