COHERENT manpages

This page displays the COHERENT manpage for localtime() [Convert system time to calendar structure].

List of available manpages
Index


localtime() -- Time Function (libc)

Convert system time to calendar structure
#include <time.h>
#include <sys/types.h>
struct tm *localtime(timep)
time_t *timep;

localtime() converts  COHERENT's internal time  into the form  described in
the structure tm, which is defined in the header file <time.h>.

timep points to the system time.  It is of type time_t, which is defined in
the header file <sys/types.h>.

localtime() returns  a pointer to the structure  tm. The function asctime()
turns tm into an ASCII string.

Unlike its  cousin gmtime(), localtime() returns  the local time, including
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"
};

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);

    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(),
libc,
time [overview],
TIMEZONE
ANSI Standard, §7.12.3.4
POSIX Standard, §8.1

Notes

localtime() returns  a pointer to a statically allocated  data area that is
overwritten by successive calls.