localtime() Time Function localtime()
Convert system time to calendar structure
#include <time.h>
#include <sys/types.h>
tm *localtime(timep) time_t *timep;
localtime converts the COHERENT internal time into the form
described in the structure tm.
timep points to the system time. It is of type time_t, which is
defined in the header file types.h.
localtime returns a pointer to the structure tm, which is also
defined in types.h. The function asctime turns tm into an ASCII
string.
Unlike its cousin gmtime, localtime returns the local time, in-
cluding conversion to daylight saving time, if applicable. The
daylight saving time flag indicates whether daylight saving time
is now in effect, not whether it is in effect during some part of
the year. Note, too, that the time zone is set by localtime
every time the value returned by
getenv("TIMEZONE")
changes. See the Lexicon entry for TIMEZONE for more information
on how COHERENT handles time zone settings.
***** Example *****
The following example recreates the function asctime. It builds
a string somewhat different from that returned by asctime to
demonstrate how to manipulate the tm structure.
#include <time.h>
#include <sys/types.h>
char *month[] = {
"January", "February", "March", "April",
"May", "June", "July", "August", "September",
"October", "November", "December"
};
COHERENT Lexicon Page 1
localtime() Time Function localtime()
char *weekday[] = {
"Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"
};
main()
{
char buf[20];
time_t tnum;
struct tm *ts;
int hour = 0;
time(&tnum); /* get time from system */
/* convert time to tm struct */
ts=localtime(&tnum);
if (ts->tm_hour == 0)
sprintf(buf,"12:%02d:%02d A.M.",
ts->tm_min, ts->tm_sec);
else
if(ts->tm_hour>=12) {
hour=ts->tm_hour-12;
if (hour==0)
hour=12;
sprintf(buf,"%02d:%02d:%02d P.M.",
hour, ts->tm_min,ts->tm_sec);
} else
sprintf(buf,"%02d:%02d:%02d A.M.", ts->tm_hour,
ts->tm_min,ts->tm_sec);
printf("\n%s %d %s 19%d %s\n",
weekday[ts->tm_wday], ts->tm_mday,
month[ts->tm_mon], ts->tm_year, buf);
COHERENT Lexicon Page 2
localtime() Time Function localtime()
printf("Today is the %d day of 19%d\n",
ts->tm_yday, ts->tm_year);
printf("Daylight Saving Time %s in effect\n",
ts->tm_isdst ? "is" : "is not");
}
***** See Also *****
gmtime(), time, TIMEZONE
***** Notes *****
localtime returns a pointer to a statically allocated data area
that is overwritten by successive calls.
COHERENT Lexicon Page 3