localtime

Description

Converts a time value and corrects for the local time zone.

#include <time.h>

struct tm *localtime( const time_t *timer );

timer Pointer to stored time  

Remarks

The localtime function converts a time stored as a time_t value and stores the result in a structure of type tm. The long value timer represents the seconds elapsed since midnight (00:00:00), December 31, 1899, Universal Coordinated Time. This value is usually obtained from the time function.

The fields of the structure type tm store the following values:

Element Value Stored

int tm_sec Seconds
int tm_min Minutes
int tm_hour Hours (0–24)
int tm_mday Day of month (1–31)
int tm_mon Month (0–11; January = 0)
int tm_year Year (current year minus 1900)
int tm_wday Day of week (0–6; Sunday = 0)
int tm_yday Day of year (0–365; January 1 = 0)
int tm_isdst Nonzero if daylight saving time is in effect, otherwise 0

Note that the gmtime, mktime, and localtime functions use a single statically allocated tm structure for the conversion. Each call to one of these routines destroys the result of the previous call.

The localtime function makes corrections for the local time zone if the user first sets the environment variable TZ. When TZ is set, three other environment variables (_timezone, _daylight, and _tzname) are automatically set as well. See _tzset for a description of these variables.

The TZ variable is not part of the ANSI standard definition of localtime but is a Microsoft extension.

Return Value

The localtime function returns a pointer to the structure result. If the value in timer represents a date before midnight, December 31, 1899, the function returns NULL.

Compatibility

Standards:ANSI, UNIX

16-Bit:DOS, QWIN, WIN, WIN DLL

32-Bit:DOS32X

See Also

asctime, ctime, _ftime, gmtime, time, _tzset

Example

/* LOCALTIM.C: This program uses time to get the current time and

* then uses localtime to convert this time to a structure representing

* the local time. The program converts the result from a 24-hour clock

* to a 12-hour clock and determines the proper extension (AM or PM).

*/

#include <stdio.h>

#include <string.h>

#include <time.h>

void main( void )

{

struct tm *newtime;

char am_pm[] = "AM";

time_t long_time;

time( &long_time ); /* Get time as long integer. */

newtime = localtime( &long_time ); /* Convert to local time. */

if( newtime->tm_hour < 12 ) /* Set up extension. */

strcpy( am_pm, "AM" );

if( newtime->tm_hour > 12 ) /* Convert from 24-hour */

newtime->tm_hour -=12; /* to 12-hour clock. */

printf( "%.19s %s\n", asctime( newtime ), am_pm );

}

Output

Fri Jun 16 06:27:02 AM