home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 108_01 / datedemo.c < prev    next >
Text File  |  1985-11-14  |  7KB  |  319 lines

  1. #include    CLOCK.H
  2.  
  3. #define    BASE_YEAR    1978
  4.  
  5. /* Example program for Mountain Hardware "100,000 day clock" S-100 board.
  6.    This main program simply calls the date function and time function,
  7.    displaying the returned strings. */
  8.  
  9. main()
  10.  
  11. {
  12.     char    date_str[35];    /* string (character array) for date */
  13.     char    time_str[8];    /* string for time */
  14.  
  15.     if (date(date_str,0)){
  16.         printf("Exiting to CP/M\07\n");
  17.         exit();
  18.     }
  19.     printf("Demonstration of 'C' functions for the ");
  20.     printf("Mountain Hardware 100,000 day clock.\n\n");
  21.     printf("\tBy Bill Bolton, Software Tools\n");
  22.     printf("\tP.O. Box 80,\n\tNewport Beach,\n");
  23.     printf("\tNSW, 2106, AUSTRALIA\n\n");
  24.     printf("\Date format 0 is %s\n\n",date_str);
  25.     date(date_str,1);
  26.     printf("Date format 1 is %s\n\n",date_str);
  27.     date(date_str,2);
  28.     printf("Date format 2 is %s\n\n",date_str);
  29.     date(date_str,3);
  30.     printf("Date format 3 is %s\n\n",date_str);
  31.     date(date_str,4);
  32.     time(time_str,0);
  33.     printf("Time of day format 0 = %s\n\n",time_str);
  34.     time(time_str,1);
  35.     printf("Time of day format 1 = %s\n\n",time_str);
  36.     time(time_str,2);
  37.     printf("Time of day format 2 = %s\n\n",time_str);
  38.     time(time_str,3);
  39.     date(date_str,1);
  40.     time(time_str,0);
  41.     printf("Printed at %s Hours on %s for Dr.Dobbs Journal.\n\n",
  42.             time_str,date_str);
  43.  }
  44.  
  45. /* date(srt,format) will fill a string with date formatted as follows:
  46.  
  47.     format = 0    "May 11, 1981"
  48.     format = 1    "Monday, May 11, 1981"
  49.     format = 2    "11/5/1981"
  50.     format = 3    "Monday, 11/5/1981"
  51. */
  52.  
  53. date(str,format)
  54.  
  55. char    *str;        /* pointer to date string */
  56. int    format;        /* format identifier */
  57.  
  58. {
  59.     char    wname[10];    /* string for week day proper name */
  60.     char    mname[12];    /* string for month proper name */
  61.     int    year[1];    /* year, range 1978 to ????? */
  62.     int    month[1];    /* month of the year, range 1 to 12 */
  63.     int    mday[1];    /* day of the current month, range 0 to 31 */
  64.     int    wday[1];    /* day of the week, range 0 to 6 */ 
  65.  
  66.     if (get_date(year,month,mday,wday)){
  67.         printf("No clock board present in system\07\n");
  68.         return(-1);
  69.     } 
  70.     name_month(mname,month);
  71.     name_week(wname,wday);
  72.     switch(format){
  73.     case 0:
  74.         sprintf(str,"%s %d, %d",mname,*mday,*year);
  75.         return(0);
  76.     case 1:
  77.         sprintf(str,"%s, %s %d, %d",wname,mname,*mday,*year);
  78.         return(0);
  79.     case 2:
  80.         sprintf(str,"%d/%d/%d",*mday,*month,*year);
  81.         return(0);
  82.     case 3:
  83.         sprintf(str,"%s, %d/%d/%d",wname,*mday,*month,*year);
  84.         return(0);
  85.     default:
  86.         printf("Date format argument ERROR !\07\n\n");
  87.         return(-1);
  88.     }
  89. }
  90.  
  91. /* get_date(year,month,mday,wday) provides the basic data for formatting
  92.    a date string, fetched from the clock board and converted to a useable
  93.    set of values */
  94.  
  95. int get_date(year,month,mday,wday)
  96.  
  97. int    *year;        /* pointer to current year */
  98. int    *month;        /* pointer to current month */
  99. int    *mday;        /* pointer to day of the month */
  100. int    *wday;        /* pointer to day of the week */
  101.  
  102. {
  103.     int    tdays;    
  104.     int    port;    /* input port number */
  105.     int    pos;    /* digit decimal position */ 
  106.     int    tyear;
  107.  
  108.     tdays = 0;
  109.  
  110.     if (inp(CLOCK) == 0XFF )    /* no clock board present */
  111.         return(-1);
  112.     for (port = DAY1, pos = 1; port <= DAY10K; port++, pos *= 10){
  113.         tdays += (inp(port) & MASK) * pos;
  114.     }
  115.     *wday = tdays%7;
  116.     for (*year = BASE_YEAR; (tyear = tdays - ndays(year)) > 0;(*year)++){
  117.         tdays = tyear;
  118.     }
  119.     month_day(year,tdays,month,mday);
  120.     return (0);
  121. }
  122.  
  123. /* month_day(year,yday,month,mday) is straight out of Kernighan and Ritchie
  124.    page 104, except that BDS C doesn't support intialisers so the special
  125.    BDS C "intw" kludge is used to initialise the day_tab array. Also the
  126.    leap year calaculation has been split off into a separate function as
  127.    it is generally useful.
  128. */
  129.  
  130. int month_day(year,yday,month,mday)
  131.  
  132. int    *year;        /* pointer to current year */
  133. int    yday;        /* day of the year, range 1 to 366 */
  134. int    *month;        /* pointer to month of the year */
  135. int    *mday;        /* pointer to day of the month */
  136. {
  137.     int    i;        
  138.     int    lyear;
  139.     int    day_tab[2][13];
  140.  
  141.     initw(day_tab[0],"0,31,28,31,30,31,30,31,31,30,31,30,31");
  142.     initw(day_tab[1],"0,31,29,31,30,31,30,31,31,30,31,30,31");
  143.     lyear = leap(*year);
  144.     for (i = 1; yday > day_tab[lyear][i]; i++){
  145.         yday -= day_tab[lyear][i];
  146.     }
  147.     *month = i;
  148.     *mday = yday;
  149. }
  150.  
  151. /* ndays(year) returns the number of days in the current year.
  152. */
  153.  
  154. int ndays(year)
  155.  
  156. int    *year;        /* pointer to current year */
  157.  
  158. {
  159.     return(leap(*year) ?  366 : 365);
  160. }
  161.  
  162. /* leap(year) returns a flag to indicate if current year is a leap year.
  163. */
  164.  
  165. int leap(year)
  166.  
  167. int    year;        /* current year */
  168.  
  169. {
  170.     return (year%4 == 0 && year%100 != 0 || year%400 == 0);
  171. }
  172.  
  173. /* name_month(mname,month) fills a string with the name of the the
  174.    current month.
  175. */
  176.  
  177. int name_month(mname,month)
  178.  
  179. char    *mname;        /* pointer to month name string */
  180. int    *month;        /* pointer to current month */
  181.  
  182. {
  183.     switch(*month){
  184.     case 1:
  185.         strcpy(mname,"January");
  186.         return(0);
  187.     case 2:
  188.         strcpy(mname,"February");
  189.         return(0);
  190.     case 3:
  191.         strcpy(mname,"March");
  192.         return(0);
  193.     case 4:
  194.         strcpy(mname,"April");
  195.         return(0);
  196.     case 5:
  197.         strcpy(mname,"May");
  198.         return(0);
  199.     case 6:
  200.         strcpy(mname,"June");
  201.         return(0);
  202.     case 7:
  203.         strcpy(mname,"July");
  204.         return(0);
  205.     case 8:
  206.         strcpy(mname,"August");
  207.         return(0);
  208.     case 9:
  209.         strcpy(mname,"September");
  210.         return(0);
  211.     case 10:
  212.         strcpy(mname,"October");
  213.         return(0);
  214.     case 11:
  215.         strcpy(mname,"November");
  216.         return(0);
  217.     case 12:
  218.         strcpy(mname,"December");
  219.         return(0);
  220.     default:
  221.         printf("Month name ERROR !\n");
  222.         return(-1);
  223.     }
  224. }
  225.  
  226. /* name_week(wname,wday) fills a string with the name of the current
  227.    week day.
  228. */
  229.  
  230. int name_week(wname,wday)
  231.  
  232. char    *wname;        /* pointer to week name string */
  233. int    *wday;        /* pointer to current week day */
  234.  
  235. {
  236.     switch(*wday){
  237.     case 1:
  238.         strcpy(wname,"Sunday");
  239.         return(0);
  240.     case 2:
  241.         strcpy(wname,"Monday");
  242.         return(0);
  243.     case 3:
  244.         strcpy(wname,"Tuesday");
  245.         return(0);
  246.     case 4:
  247.         strcpy(wname,"Wednesday");
  248.         return(0);
  249.     case 5:
  250.         strcpy(wname,"Thursday");
  251.         return(0);
  252.     case 6:
  253.         strcpy(wname,"Friday");
  254.         return(0);
  255.     case 0:
  256.         strcpy(wname,"Saturday");
  257.         return(0);
  258.     default:
  259.         printf("Weekday name ERROR !\n");
  260.         return(-1);
  261.     }
  262. }
  263.  
  264. /* time(str,format) fills a string with the time of day in the 
  265.    following formats :
  266.  
  267.     format 0     1800:15
  268.     format 1    18:00:15
  269.     format 2    18:00
  270. */
  271.  
  272. time(str,format)
  273.  
  274. char    *str;        /* string to fill with time */
  275. int    format;        /* flag for format of string */
  276.  
  277. {
  278.     int    t[6];
  279.  
  280.     if (read_clock(t)){
  281.         printf("No clock board present in system !\07\n");
  282.         return(-1);
  283.     } 
  284.     switch(format){
  285.     case 0:
  286.         sprintf(str,"%d%d%d%d:%d%d",t[0],t[1],t[2],t[3],t[4],t[5]);
  287.         return(0);
  288.     case 1:
  289.         sprintf(str,"%d%d:%d%d:%d%d",t[0],t[1],t[2],t[3],t[4],t[5]);
  290.         return(0);
  291.     case 2:
  292.         sprintf(str,"%d%d:%d%d",t[0],t[1],t[2],t[3]);
  293.         return(0);
  294.     default:
  295.         printf("Time of day format argument ERROR !\07\n\n");
  296.         return(-1);
  297.     }
  298. }
  299.  
  300. /* read_clock(t) fills an array with the time of day digits read from
  301.    the clock board
  302. */
  303.  
  304. int read_clock(t)
  305.  
  306. int    *t;        /* array to store clock digits */
  307.  
  308. {
  309.     int     port;    /* clock port to read */ 
  310.     int    ptr;    /* pointer into digit array */
  311.  
  312.     if (inp(CLOCK) == 0XFF )    /* no clock board present */
  313.         return(-1);
  314.     for (port = HOUR10, ptr = 0; port >= SEC1; port--, ptr++)
  315.         t[ptr] = inp(port) & MASK;
  316.     return(0);
  317. }
  318.  
  319.