home *** CD-ROM | disk | FTP | other *** search
- /************************************************************************
- Title: Calendar
- ************************************************************************/
-
- #include <stdio.h>
- #include <dos.h>
-
- /* Months */
- #define JANUARY 1
- #define FEBUARY 2
- #define MARCH 3
- #define APRIL 4
- #define MAY 5
- #define JUNE 6
- #define JULY 7
- #define AUGUST 8
- #define SEPTEMBER 9
- #define OCTOBER 10
- #define NOVEMBER 11
- #define DECEMBER 12
-
- /* days */
- #define MONDAY 1
- #define TUESDAY 2
- #define WEDNESDAY 3
- #define THURSDAY 4
- #define FRIDAY 5
- #define SATURDAY 6
- #define SUNDAY 7
-
- struct date {
- unsigned dyear : 15;
- unsigned dmonth : 4;
- unsigned dday : 5; };
-
- static int daymon[2][13] = {
- {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
- {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
- };
-
- static char *day_names[] = {
- "day-error", "Mon", "Tue", "Wed", "Thur", "Fri", "Sat", "Sun" };
-
- static char *month_names[] = {
- "month-error",
- "January", "Febuary", "March", "April", "May", "June", "July",
- "August", "September", "October", "November", "December" };
-
- struct date fmonday = { 1950,6,5 };
-
- /* leapyear(year) -- if year is a year, returns 0 if not a
- leap year 1 otherwise */
- leapyear(year)
- int year;
- { return ( year%4 == 0 && year%100 != 0 || year%400 == 0 ); }
-
- /* dayofyear(adate) -- if adate is a 'date' record, returns day of year
- for the given date. (a function of day and month */
- dayofyear(adate)
- struct date *adate;
- { int d,i,leapp;
- d = adate->dday;
- leapp = leapyear(adate->dyear);
- for (i = 1; i < adate->dmonth ; i++)
- d += daymon[leapp][i];
- return(d); }
-
- double daysinperiod(adate,bdate)
- struct date *adate,*bdate;
- { int adays,bdays,year;
- double perdays;
- perdays = 0.0;
- adays = dayofyear(adate)-1;
- bdays = dayofyear(bdate)-1;
- for (year = adate->dyear; year < bdate->dyear; year++)
- perdays += (leapyear(year)?366.0:365.0);
- return (perdays - (double)adays + (double)bdays); }
-
- dayofweek(adate)
- struct date *adate;
- { double dip;
- dip = daysinperiod(&fmonday,adate);
- return ( 1 + (int)(dip-(7.0*((double)((int)(dip/7.0))))) ); }
-
- todays_date(adate)
- struct date *adate;
- { int ret;
- union REGS res;
- res.h.ah = 0x2A; /* get date */
- ret = intdos(&res,&res);
- adate->dday = res.h.dl;
- adate->dmonth = res.h.dh;
- adate->dyear = res.x.cx;
- return (ret); }
-
- /************************************************************************
- * INTERFACE TO OPS5+ *
- ************************************************************************/
- #include "o5udefs.h"
-
- u_date()
- { struct date today;
- todays_date(&today);
- o_reset();
- o_value(o_intern("todays-date"));
- o_value(o_cvna((long)today.dday));
- o_value(o_cvna((long)today.dmonth));
- o_value(o_cvna((long)today.dyear));
- o_assert(); }
-
- u_day()
- { struct date today;
- todays_date(&today);
- o_value(o_cvna((long)today.dday)); }
-
- u_month()
- { struct date today;
- todays_date(&today);
- o_value(o_cvna((long)today.dmonth)); }
-
- u_year()
- { struct date today;
- todays_date(&today);
- o_value(o_cvna((long)today.dyear)); }
-
- u_leapyear(year)
- RSULTP year;
- { o_value(o_cvna((long)leapyear(o_cvan(year)))); }
-
- u_doy(dd,mm,yy)
- RSULTP dd,mm,yy;
- { struct date adate;
- adate.dday = o_cvan(dd);
- adate.dmonth = o_cvan(mm);
- adate.dyear = o_cvan(yy);
- o_value(o_cvna((long)dayofyear(&adate))); }
-
- u_dayname(day)
- RSULTP day;
- { int dd = o_cvan(day);
- dd = (dd - 1) % 7 + 1; /* remove multiple weeks. */
- o_value(o_intern(day_names[dd])); }
-
- u_monthname(month)
- RSULTP month;
- { int mm = o_cvan(month);
- mm = (mm - 1) % 12 + 1; /* remove multiple years. */
- o_value(o_intern(month_names[mm])); }
-
- u_dip(ad,am,ay,bd,bm,by)
- RSULTP ad,am,ay,bd,bm,by;
- { struct date adate,bdate;
- adate.dday = o_cvan(ad);
- adate.dmonth = o_cvan(am);
- adate.dyear = o_cvan(ay);
- bdate.dday = o_cvan(bd);
- bdate.dmonth = o_cvan(bm);
- bdate.dyear = o_cvan(by);
- o_value(o_cvfa(daysinperiod(&adate,&bdate))); }
-
- u_dow(ad,am,ay)
- RSULTP ad,am,ay;
- { struct date adate;
- adate.dday = o_cvan(ad);
- adate.dmonth = o_cvan(am);
- adate.dyear = o_cvan(ay);
- o_value(o_cvna((long)dayofweek(&adate))); }
-
- u_explode(nam)
- RSULTP nam;
- { char *rep = o_pname(nam);
- char tmp[2];
- tmp[1]=0;
- while (*rep) {
- tmp[0] = *(rep++);
- o_value(o_intern(tmp)); }; }
-
- initudefs()
- { udefact ("date",u_date);
- udeffn ("day",u_day);
- udeffn ("month",u_month);
- udeffn ("year",u_year);
- udeffn ("leapyear",u_leapyear);
- udeffn ("dayofyear",u_doy);
- udeffn ("day-name",u_dayname);
- udeffn ("month-name",u_monthname);
- udeffn ("days-in-period",u_dip);
- udeffn ("day-of-week",u_dow);
- udeffn ("explode",u_explode);
- }
-