
- C Library - Home
- C Library - <assert.h>
- C Library - <complex.h>
- C Library - <ctype.h>
- C Library - <errno.h>
- C Library - <fenv.h>
- C Library - <float.h>
- C Library - <inttypes.h>
- C Library - <iso646.h>
- C Library - <limits.h>
- C Library - <locale.h>
- C Library - <math.h>
- C Library - <setjmp.h>
- C Library - <signal.h>
- C Library - <stdalign.h>
- C Library - <stdarg.h>
- C Library - <stdbool.h>
- C Library - <stddef.h>
- C Library - <stdio.h>
- C Library - <stdlib.h>
- C Library - <string.h>
- C Library - <tgmath.h>
- C Library - <time.h>
- C Library - <wctype.h>
- C Programming Resources
- C Programming - Tutorial
- C - Useful Resources
C library - localtime() function
The C library localtime() function is used to handle the time-relevant tasks, especially when the user wants to display the time information in a human-readable format.
Here, struct tm *localtime(const time_t *timer) uses the time pointed by timer to fill a tm structure with the values that represent the corresponding local time. The value of timer is broken up into the structure tm and expressed in the local time zone.
Syntax
Following is the syntax of the C library localtime() function −
struct tm *localtime(const time_t *timer)
Parameters
This function accepts only a single parameters −
- timer − This is the pointer to a time_t value representing a calendar time.
Return Value
This function returns a pointer to a tm structure with the time information filled in.
Following is the tm structure information −struct tm { int tm_sec; /* seconds, range 0 to 59 */ int tm_min; /* minutes, range 0 to 59 */ int tm_hour; /* hours, range 0 to 23 */ int tm_mday; /* day of the month, range 1 to 31 */ int tm_mon; /* month, range 0 to 11 */ int tm_year; /* The number of years since 1900 */ int tm_wday; /* day of the week, range 0 to 6 */ int tm_yday; /* day in the year, range 0 to 365 */ int tm_isdst; /* daylight saving time */ };
Example 1
Following is the basic C library program to see the demontration of localtime() function.
#include <stdio.h> #include <time.h> int main () { time_t rawtime; struct tm *info; time( &rawtime ); info = localtime( &rawtime ); printf("Current local time and date: %s", asctime(info)); return(0); }
Output
On execution of above code, we get the following result −
Current local time and date: Tue May 14 10:01:48 2024
Example 2
Below the program illustrate how to use the time() function to obtain the current time and the localtime() function to show the result.
#include <stdio.h> #include <time.h> int main() { time_t current_time; // get the current time using time() current_time = time(NULL); // call the function localtime() that takes timestamp and convert it into localtime representation struct tm *tm_local = localtime(¤t_time); // corresponding component of the local time printf("Current local time : %d:%d:%d", tm_local -> tm_hour, tm_local -> tm_min, tm_local -> tm_sec); return 0; }
Output
The above code produces the following result −
Current local time : 11:36:56
Example 3
While operating the task over the current date, it uses localtime() to provide the date and strftime() to display the formatted result.
#include <stdio.h> #include <time.h> int main(void) { size_t maxsize = 80; char dest[maxsize]; time_t now; struct tm *tm_struct; time(&now); tm_struct = localtime(&now); strftime(dest, maxsize, "%D", tm_struct); printf("Current date: %s\n", dest); return 0; }
Output
After executing the code, we get the following result −
Current date: 05/17/24