home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / e / e051 / 2.ddi / LIB / CALENDAR.C next >
Encoding:
C/C++ Source or Header  |  1985-07-14  |  4.7 KB  |  191 lines

  1. /************************************************************************
  2. Title: Calendar
  3. ************************************************************************/
  4.  
  5. #include <stdio.h>
  6. #include <dos.h>
  7.  
  8. /* Months */
  9. #define    JANUARY    1
  10. #define    FEBUARY    2
  11. #define MARCH    3
  12. #define APRIL    4
  13. #define    MAY    5
  14. #define    JUNE    6
  15. #define    JULY    7
  16. #define AUGUST    8
  17. #define    SEPTEMBER 9
  18. #define OCTOBER    10
  19. #define NOVEMBER 11
  20. #define DECEMBER 12
  21.  
  22. /* days */
  23. #define    MONDAY    1
  24. #define    TUESDAY    2
  25. #define    WEDNESDAY 3
  26. #define THURSDAY 4
  27. #define FRIDAY    5
  28. #define SATURDAY 6
  29. #define SUNDAY    7
  30.  
  31. struct date {
  32.   unsigned dyear  : 15;
  33.   unsigned dmonth : 4;
  34.   unsigned dday   : 5; };
  35.  
  36. static int daymon[2][13] = {
  37.    {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
  38.    {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
  39. };
  40.  
  41. static char *day_names[] = {
  42.   "day-error", "Mon", "Tue", "Wed", "Thur", "Fri", "Sat", "Sun" };
  43.  
  44. static char *month_names[] = {
  45.   "month-error",
  46.   "January", "Febuary", "March", "April", "May", "June", "July",
  47.   "August", "September", "October", "November", "December" };
  48.  
  49. struct date fmonday = { 1950,6,5 };
  50.  
  51. /* leapyear(year) -- if year is a year, returns 0 if not a 
  52.                       leap year 1 otherwise */
  53. leapyear(year)
  54. int year;
  55. {  return ( year%4 == 0 && year%100 != 0 || year%400 == 0 ); }
  56.  
  57. /* dayofyear(adate) -- if adate is a 'date' record, returns day of year
  58.                          for the given date. (a function of day and month */
  59. dayofyear(adate)
  60. struct date *adate;
  61. { int d,i,leapp;
  62.   d = adate->dday;
  63.   leapp = leapyear(adate->dyear);
  64.   for (i = 1; i < adate->dmonth ; i++)
  65.     d += daymon[leapp][i];
  66.   return(d); }
  67.  
  68. double daysinperiod(adate,bdate)
  69. struct date *adate,*bdate;
  70. { int adays,bdays,year;
  71.   double perdays;
  72.   perdays = 0.0;
  73.   adays = dayofyear(adate)-1;
  74.   bdays = dayofyear(bdate)-1;
  75.   for (year = adate->dyear; year < bdate->dyear; year++)
  76.     perdays += (leapyear(year)?366.0:365.0);
  77.   return (perdays - (double)adays + (double)bdays); }
  78.  
  79. dayofweek(adate)
  80. struct date *adate;
  81. { double dip;
  82.   dip = daysinperiod(&fmonday,adate);
  83.   return ( 1 + (int)(dip-(7.0*((double)((int)(dip/7.0))))) ); }
  84.  
  85. todays_date(adate)
  86. struct date *adate;
  87. { int ret;
  88.   union REGS res;
  89.   res.h.ah = 0x2A;            /* get date */
  90.   ret = intdos(&res,&res);
  91.   adate->dday = res.h.dl;
  92.   adate->dmonth = res.h.dh;
  93.   adate->dyear = res.x.cx;
  94.   return (ret); }
  95.  
  96. /************************************************************************
  97. *              INTERFACE TO OPS5+                *
  98. ************************************************************************/
  99. #include "o5udefs.h"
  100.  
  101. u_date()
  102. { struct date today;
  103.   todays_date(&today);
  104.   o_reset();
  105.   o_value(o_intern("todays-date"));
  106.   o_value(o_cvna((long)today.dday));  
  107.   o_value(o_cvna((long)today.dmonth));  
  108.   o_value(o_cvna((long)today.dyear)); 
  109.   o_assert(); }
  110.  
  111. u_day()
  112. { struct date today;
  113.   todays_date(&today);
  114.   o_value(o_cvna((long)today.dday)); }
  115.  
  116. u_month()
  117. { struct date today;
  118.   todays_date(&today);
  119.   o_value(o_cvna((long)today.dmonth));  }
  120.  
  121. u_year()
  122. { struct date today;
  123.   todays_date(&today);
  124.   o_value(o_cvna((long)today.dyear)); }
  125.  
  126. u_leapyear(year)
  127. RSULTP year;
  128. { o_value(o_cvna((long)leapyear(o_cvan(year)))); }
  129.  
  130. u_doy(dd,mm,yy)
  131. RSULTP dd,mm,yy;
  132. { struct date adate;
  133.   adate.dday = o_cvan(dd);
  134.   adate.dmonth = o_cvan(mm);
  135.   adate.dyear = o_cvan(yy);
  136.   o_value(o_cvna((long)dayofyear(&adate))); }
  137.  
  138. u_dayname(day)
  139. RSULTP day;
  140. { int dd = o_cvan(day);
  141.   dd = (dd - 1) % 7 + 1;    /* remove multiple weeks. */
  142.   o_value(o_intern(day_names[dd])); }
  143.  
  144. u_monthname(month)
  145. RSULTP month;
  146. { int mm = o_cvan(month);
  147.   mm = (mm - 1) % 12 + 1;    /* remove multiple years. */
  148.   o_value(o_intern(month_names[mm])); }
  149.  
  150. u_dip(ad,am,ay,bd,bm,by)
  151. RSULTP ad,am,ay,bd,bm,by;
  152. { struct date adate,bdate;
  153.   adate.dday = o_cvan(ad);
  154.   adate.dmonth = o_cvan(am);
  155.   adate.dyear = o_cvan(ay);
  156.   bdate.dday = o_cvan(bd);
  157.   bdate.dmonth = o_cvan(bm);
  158.   bdate.dyear = o_cvan(by);
  159.   o_value(o_cvfa(daysinperiod(&adate,&bdate))); }
  160.  
  161. u_dow(ad,am,ay)
  162. RSULTP ad,am,ay;
  163. { struct date adate;
  164.   adate.dday = o_cvan(ad);
  165.   adate.dmonth = o_cvan(am);
  166.   adate.dyear = o_cvan(ay);
  167.   o_value(o_cvna((long)dayofweek(&adate))); }
  168.  
  169. u_explode(nam)
  170. RSULTP nam;
  171. { char *rep = o_pname(nam);
  172.   char tmp[2];
  173.   tmp[1]=0;
  174.   while (*rep) {
  175.     tmp[0] = *(rep++);
  176.     o_value(o_intern(tmp)); }; }
  177.  
  178. initudefs()
  179. { udefact ("date",u_date); 
  180.   udeffn  ("day",u_day);
  181.   udeffn  ("month",u_month);
  182.   udeffn  ("year",u_year);
  183.   udeffn  ("leapyear",u_leapyear);
  184.   udeffn  ("dayofyear",u_doy);
  185.   udeffn  ("day-name",u_dayname);
  186.   udeffn  ("month-name",u_monthname);
  187.   udeffn  ("days-in-period",u_dip);
  188.   udeffn  ("day-of-week",u_dow);
  189.   udeffn  ("explode",u_explode);
  190. }
  191.