home *** CD-ROM | disk | FTP | other *** search
/ Dream 48 / Amiga_Dream_48.iso / Atari / c / sozobon-v2 / dlibsrc.lha / TIME.C < prev    next >
C/C++ Source or Header  |  1988-02-07  |  4KB  |  147 lines

  1. /*
  2.  * DATE/TIME FUNCTIONS:
  3.  *
  4.  *    To use the functions in this section, you must include "TIME.H"
  5.  *    in your source file.
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <time.h>
  10.  
  11. static struct tm    the_time;
  12. static struct tm    jan_1;
  13. static char        timebuf[26] =
  14.             "Day Mon dd hh:mm:ss yyyy\n";
  15. static char        *day[] =
  16.             {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
  17. static char        *month[] =
  18.             {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
  19.             "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
  20.  
  21. long julian_date(time)
  22.     register struct tm *time;
  23. /*
  24.  *    Number of days since the base date of the Julian calendar.
  25.  */
  26.     {
  27.     register long c, y, m, d;
  28.  
  29.     y = time->tm_year + 1900;    /* year - 1900 */
  30.     m = time->tm_mon + 1;        /* month, 0..11 */
  31.     d = time->tm_mday;        /* day, 1..31 */
  32.     if(m > 2)
  33.         m -= 3L;
  34.     else
  35.         {
  36.         m += 9L;
  37.         y -= 1L;
  38.         }
  39.     c = y / 100L;
  40.     y %= 100L;
  41.     return(    ((146097L * c) >> 2) +
  42.         ((1461L * y) >> 2) +
  43.         (((153L * m) + 2) / 5) +
  44.         d +
  45.         1721119L );
  46.     }
  47.  
  48. time_t time(rawtime)
  49.     register long *rawtime;
  50. /*
  51.  *    Get the current system clock date/time value.  Under many systems,
  52.  *    this function returns the number of seconds since 00:00:00 GMT on
  53.  *    Jan 1, 1970.  This implementation returns an encoded date/time
  54.  *    value instead.  Therefore any programs which depend on this value
  55.  *    being a number of seconds will not work properly.  However, other
  56.  *    functions in this section which make use of the raw time value
  57.  *    returned by time() are implemented to be compatible with this
  58.  *    encoding, and will work properly.  In addition to returning the
  59.  *    raw time value, if the <rawtime> pointer in not NULL, the value
  60.  *    is stored in the long <rawtime> points to.
  61.  */
  62.     {
  63.     register time_t t;
  64.  
  65.     t = (gemdos(0x2C) << 16) | (gemdos(0x2A) & 0xFFFFL);
  66.     if(rawtime)
  67.         *rawtime = t;
  68.     return(t);
  69.     }
  70.  
  71. struct tm *gmtime()
  72. /*
  73.  *    Can't determine Greenwich Mean Time, so return NULL
  74.  *    as specified by ANSI standard.
  75.  */
  76.     {
  77.     return(NULL);
  78.     }
  79.  
  80. struct tm *localtime(rawtime)
  81.     time_t *rawtime;
  82. /*
  83.  *    Convert <rawtime> to fill time structure fields.  A pointer to an
  84.  *    internal structure is returned.  Refer to "TIME.H" for the values
  85.  *    of the various structure fields.
  86.  */
  87.     {
  88.     register time_t time, jdate, jjan1;
  89.     register struct tm *t, *j;
  90.  
  91.     time = *rawtime;
  92.     t = &the_time;
  93.     j = &jan_1;
  94.     t->tm_mday = (time & 0x1F);
  95.     time >>= 5;
  96.     t->tm_mon = (time & 0x0F) - 1;
  97.     time >>= 4;
  98.     t->tm_year = (time & 0x7F) + 80;
  99.     time >>= 7;
  100.     t->tm_sec = (time & 0x1F) << 1;
  101.     time >>= 5;
  102.     t->tm_min = (time & 0x3F);
  103.     time >>= 6;
  104.     t->tm_hour = (time & 0x1F);
  105.     jdate = julian_date(t);
  106.     *j = *t;
  107.     j->tm_mon = 0;        /* set up Jan 1 */
  108.     j->tm_mday = 1;
  109.     jjan1 = julian_date(j);
  110.     t->tm_wday = (jdate + 1) % 7;
  111.     t->tm_yday = jdate - jjan1;
  112.     t->tm_isdst = (-1);
  113.     return(t);
  114.     }
  115.  
  116. char *asctime(time)
  117.     register struct tm *time;
  118. /*
  119.  *    Convert <time> structure value to a string.  The same format, and
  120.  *    the same internal buffer, as for ctime() is used for this function.
  121.  */
  122.     {
  123.     sprintf(timebuf, "%.3s %.3s%3d %02d:%02d:%02d %04d\n",
  124.         day[time->tm_wday], month[time->tm_mon], time->tm_mday,
  125.         time->tm_hour, time->tm_min, time->tm_sec, 1900+time->tm_year);
  126.     return(timebuf);
  127.     }
  128.  
  129. char *ctime(rawtime)
  130.     time_t *rawtime;
  131. /*
  132.  *    Convert <rawtime> to a string.  A 26 character fixed field string
  133.  *    is created from the raw time value.  The following is an example
  134.  *    of what this string might look like:
  135.  *        "Wed Jul 08 18:43:07 1987\n\0"
  136.  *    A 24-hour clock is used, and due to a limitation in the ST system
  137.  *    clock value, only a resolution of 2 seconds is possible.  A pointer
  138.  *    to the formatted string, which is held in an internal buffer, is
  139.  *    returned.
  140.  */
  141.     {
  142.     char *asctime();
  143.     struct tm *localtime();
  144.  
  145.     return(asctime(localtime(rawtime)));
  146.     }
  147.