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