home *** CD-ROM | disk | FTP | other *** search
/ ftp.barnyard.co.uk / 2015.02.ftp.barnyard.co.uk.tar / ftp.barnyard.co.uk / cpm / walnut-creek-CDROM / CPM / AZTEC-C / COMND004.ARK / DATE.CPM < prev    next >
Text File  |  1986-06-17  |  4KB  |  206 lines

  1. /*    date.cpm    Date/Time support for CP/M-80 v 3.0
  2.  
  3.     Copyright (C) 1985 Mark E. Mallett
  4.  
  5.     Permission is hereby granted to distribute this file indiscriminately.
  6.  
  7. Edit history
  8.  
  9. When    Who    What
  10. ------    ---    --------------------------------
  11. 84xxxx    MEM    Create file.
  12.  
  13.  
  14.     Included are
  15.  
  16.         curdtm        Get current date/time
  17.         cvedid        Convert external to internal date
  18.         cvided        Convert internal to external date
  19.  
  20. */
  21.  
  22. #include    "stdio.h"
  23. #include    "mem.h"            /* Get my standard defs */
  24. #include    "cpm.h"            /* Get CP/M call definitions */
  25.  
  26. /* Local definitions */
  27.  
  28. typedef                    /* date/time structure */
  29.   struct
  30.     {
  31.     int        DAT_DAT;        /* Days since Jan 1 1978 */
  32.     BYTE    DAT_HRS;        /* BCD hours */
  33.     BYTE    DAT_MIN;        /* BCD minutes */
  34.     }
  35.   DAT;                    /* Date */
  36.  
  37. /* External variables */
  38.  
  39.  
  40. /* External routines */
  41.  
  42.  
  43.  
  44. /* Locals */
  45.  
  46.  
  47. static    int mdtbl[] = {31,28,31,30,31,30,31,31,30,31,30,31};
  48.                     /* Days in each month */
  49. /*
  50.  
  51. *//* curdtm (id, it)
  52.  
  53.     Get date/time from operating system
  54.  
  55. Accepts :
  56.  
  57.     id        Address of where to store the date in internal form
  58.     it        Address of where to store the time in internal form
  59.  
  60. Returns :
  61.  
  62.     *id        Date as an int.
  63.     *it        Time as an int.
  64.  
  65. */
  66.  
  67.  
  68. curdtm (id, it)
  69.  
  70. int        *id;            /* Address of date variable */
  71. int        *it;            /* Address of time variable */
  72.  
  73. {
  74. IND    DAT    date;            /* Date value */
  75. IND    int    i;            /* Scratch */
  76.  
  77. CPM (_MRGDT, &date);            /* Call system to get date */
  78. *id = date.DAT_DAT;            /* Get days since Jan 1 1978 */
  79.  
  80. i = (date.DAT_HRS>>4)&0x0F;
  81. i = i*10 + (date.DAT_HRS&0x0F);
  82. i = i*6 + ((date.DAT_MIN>>4)&0x0F);
  83. i = i*10 + (date.DAT_MIN&0x0F);
  84. *it = i;                /* Return the time. */
  85. }
  86. /*
  87.  
  88. *//* cvedid (id, it, m, d, y, hh, mm)
  89.  
  90.     Convert external date/time to internal date/time
  91.  
  92.  
  93. Accepts :
  94.  
  95.     id        Address of internal date variable (int)
  96.     it        Address of internal time variable (int)
  97.     m        Month number (1-12)
  98.     d        Day number (1-31)
  99.     y        Year number (1978-??)
  100.     hh        Hour (0 - 23)
  101.     mm        Minutes (0 - 59)
  102.  
  103. Returns :
  104.  
  105.     <value>        0 if ok inputs
  106.             -1 = bad year
  107.             -2 = bad month
  108.             -3 = bad day of month
  109.     *id        Internal date value
  110.     *it        Internal time value
  111.  
  112. */
  113.  
  114. cvedid (id, it, m, d, y, hh, mm)
  115.  
  116. int        *id, *it;        /* Addr of date and time variables */
  117. int        m, d, y, hh, mm;    /* Date and time component values */
  118.  
  119. {
  120. IND    int    i,j;            /* Scratch */
  121.  
  122. *it = hh*60+mm;                /* Takes care of time */
  123.  
  124. if ((y < 1978) || (y > 2070))
  125.     return (-1);
  126.  
  127. mdtbl[1] = 28;                /* Setup February */
  128. if (y%4 == 0)                /* if leap year */
  129.     mdtbl[1] = 29;
  130.  
  131. m--;                    /* Make month and day zero-based */
  132. d--;
  133. if ((m < 0) || (m > 11))        /* Check out month */
  134.     return (-2);
  135. if ((d < 0) || (d >= mdtbl[m]))        /* Check out day of month */
  136.     return (-3);
  137.  
  138. i = 0;                    /* Calculate days */
  139. for (j=0; j < m; j++)
  140.     i += mdtbl[j];
  141. i += d;
  142. i += (y-1978)*365;
  143. i += (y-1976)/4;
  144.  
  145. *id = i+1;                /* Return day */
  146.  
  147. return (0);                /* Give good status */
  148. }
  149. /*
  150.  
  151. *//* cvided (id, it, m, d, y, hh, mm)
  152.  
  153.     Convert internal date/time values to external values
  154.  
  155.  
  156. Accepts :
  157.  
  158.     id        Date in internal form
  159.     it        Time in internal form
  160.     *m        Where to put month number
  161.     *d        Where to put day number
  162.     *y        Where to put year number
  163.     *hh        Where to put hours
  164.     *mm        Where to put minutes
  165.  
  166.  
  167. Returns :
  168.  
  169.     *m        Month number (1-12)
  170.     *d        Day number (1-31)
  171.     *y        Year number (1978-??)
  172.     *hh        Hour (0-23)
  173.     *mm        Minutes (0-59)
  174.  
  175. */
  176.  
  177. cvided (id, it, m, d, y, hh, mm)
  178.  
  179. int        id, it;            /* Internal date/time */
  180. int        *m, *d, *y, *hh, *mm;    /* Where to put components */
  181.  
  182. {
  183. IND    int    i,j;            /* Scratch */
  184. IND    int    yr,ly,mo,day;        /* Various values */
  185.  
  186. i = id-1;                /* day 1 = Jan 1 1978 */
  187. ly = (i+672)/1461;            /* Number of leap days passed. */
  188. yr = (i-ly)/365;            /* Number of full years passed */
  189. i = i-(yr*365)-((yr+1)/4);        /* Number of day in this year */
  190. yr = yr+1978;                /* What year it is */
  191. mdtbl[1] = 28;                /* Presume not leap year */
  192. if (yr%4 == 0)                /* If is */
  193.     mdtbl[1] = 29;            /*  then set feb days */
  194. for (j=0,mo=0; j <= i; mo++)        /* Find month */
  195.     j = j+mdtbl[mo];            /* Add days */
  196.  
  197. j = j-mdtbl[--mo];            /* Subtract off last month */
  198. day = (i-j)+1;                /* Get real day. */
  199.  
  200. *y = yr;                /* Return component values */
  201. *m = mo+1;
  202. *d = day;
  203. *hh = it/60;
  204. *mm = it%60;
  205. }
  206.