11.5 Sample Date/Time Scenario

The following example and explanation shows how to use the DEC C RTL time functions to print the current time:

#include <stdio.h>
#include <time.h>

main ()
{
  time_t t;

  t = time((time_t)0);
  printf ("The current time is: %s\n",asctime (localtime (&t)));
}

This example:

  1. Calls the time function to get the current time in seconds since the Epoch, in terms of UTC.

  2. Passes this value to the localtime function, which uses time-conversion information as specified by tzset to determine which time-zone conversion rules should be used to compute local time from UTC. By default, these rules are specified in the file defined by SYS$LOCALTIME:

    1. For a user in the Eastern United States interested in their local time, SYS$LOCALTIME would be defined during installation to SYS$COMMON:[SYS$ZONEINFO.US]EASTERN, the time- zone file containing conversion rules for the Eastern U.S. time zone..

    2. If the local time falls during daylight savings time (DST), SYS$COMMON:[SYS$ZONEINFO.US]EASTERN indicates that a time differential factor of -4 hours needs to be applied to UTC to get local time.

      If the local time falls during Eastern standard time (EST), SYS$COMMON:[SYS$ZONEINFO.US]EASTERN indicates that a time differential factor of -5 hours needs to be applied to UTC to get local time.

    3. The DEC C RTL applies -4 (DST) or -5 (EST) to UTC, and localtime returns the local time in terms of a tm structure.

  3. Pass this tm structure to the asctime function to print the local time in a readable format.


Previous Page | Next Page | Table of Contents | Index