C Library Function - localtime()
Description
The C library function struct tm *localtime(const time_t *timer) uses the value of timer to fill the tm structure. The value of timer is broken down into the tm structure and represented in local time.
Declaration
Following is the declaration for the localtime() function.
struct tm *localtime(const time_t *timer)
Parameters
- timer -- This is a pointer to a time_t value representing the calendar time.
Return Value
The function returns a pointer to the tm structure filled with the time information. Below are the details of the tm structure:
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; /* 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
The following example demonstrates the use of the localtime() function.
#include <stdio.h>
#include <time.h>
int main ()
{
time_t rawtime;
struct tm *info;
char buffer[80];
time( &rawtime );
info = localtime( &rawtime );
printf("Current local time and date: %s", asctime(info));
return(0);
}
Let's compile and run the above program, which will produce the following result:
Current local time and date: Thu Aug 23 09:12:05 2012