What is localtime in C?

The localtime() function in C language is defined in the time.h header file and returns the local time of the computer being used to run a program.

Below is the definition of the localtime() function in the time.h header file:

tm* localtime(const time_t* t_ptr);

As seen in the code, the input parameter for the localtime() function is a pointer to variable time_t. This variable stores the current time of the machine and can be obtained using the time() function. The return type of the function localtime() is a pointer to struct tm.

Example

Below is an example that shows how you can use the localtime() function to display the local time of a machine.

The time() function returns calendar time according to seconds that have passed since 00:00:00 UTC, January 1, 1970 (Unix timestamp) and stores it in the t variable. The localtime() function then converts this calendar time to local time and displays it as such:

#include<stdio.h>
#include<time.h>
#include<iostream>
using namespace std;
int main() {
time_t t; // to store local time obtained from time() function
time(&t);
struct tm* loc_time = localtime(&t); // converting calendar time to local time
printf("time and date: %s\n",
asctime(loc_time));
return 0;
}

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved