|
|
1.1 root 1: /* /usr/lib/calendar produces an egrep -f file
2: that will select today's and tomorrow's
3: calendar entries, with special weekend provisions
4:
5: used by calendar command
6: */
7: #include <time.h>
8:
9: #define DAY (3600*24L)
10: #define NHOL (sizeof holidays / sizeof *holidays)
11:
12: char *month[] = {
13: "[Jj]an",
14: "[Ff]eb",
15: "[Mm]ar",
16: "[Aa]pr",
17: "[Mm]ay",
18: "[Jj]un",
19: "[Jj]ul",
20: "[Aa]ug",
21: "[Ss]ep",
22: "[Oo]ct",
23: "[Nn]ov",
24: "[Dd]ec"
25: };
26: /* Table of holidays gives inclusive months, inclusive
27: * days of month, and inclusive days of week on which a
28: * holiday may fall. Thus Thanksgiving, 4th Thursday of November,
29: * may happen as early as Nov 22 and as late as Nov 28.
30: * American holidays celebrated in more than two states
31: * are in table, but most are commented out as irrelevant to
32: * the schedule of the author's place of work
33: */
34: struct hol {
35: int h_mon0, h_mon1;
36: int h_mday0, h_mday1;
37: int h_wday0, h_wday1;
38: } holidays[] = {
39: 1,12, 1,31, 6,6, /* sat */
40: 1,12, 1,31, 0,0, /* sun */
41: 1,1, 1,1, 0,6, /* new year's */
42: /* 1,1, 8,14, 0,0, /* martin luther king's birthday */
43: /* 1,1, 15,15, 0,6, /* martin luther king's birthday */
44: /* 1,1, 15,21, 1,1, /* martin luther king's birthday */
45: /* 1,1, 19,19, 0,6, /* robert e lee's birthday */
46: /* 1,1, 15,21, 1,1, /* robert e lee's birthday */
47: /* 2,2, 1,7, 1,1, /* lincoln's birthday */
48: /* 2,2, 12,12, 0,6, /* lincoln's birthday */
49: /* 2,2, 15,21, 1,1, /* washington's birthday */
50: /* 2,2, 22,22, 0,6, /* washington's birthday */
51: /* 3,4, ??,??, 5,5, /* good fri, beyond calculation */
52: 5,5, 25,31, 1,1, /* memorial day */
53: /* 5,5, 30,30, 0,6, /* memorial day */
54: /* 6,6, 1,7, 1,1, /* jefferson davis's birthday */
55: /* 6,6, 3,3, 0,6, /* jefferson davis's birthday */
56: 7,7, 3,3, 6,6, /* fri before sat july 4th */
57: 7,7, 4,4, 0,6, /* independence day */
58: 7,7, 5,5, 1,1, /* mon after sun july 4th */
59: 9,9, 1,7, 1,1, /* labor day */
60: /* 10,10, 8,14, 1,1, /* columbus day */
61: /* 10,10, 12,12, 0,6, /* columbus day */
62: /* 10,10, 22,28, 1,1, /* veterans' day */
63: /* 11,11, 2,8, 2,2, /* election day */
64: /* 11,11, 11,11, 0,6, /* veterans' day */
65: 11,11, 22,28, 4,4, /* thanksgiving */
66: 11,11, 23,29, 5,5, /* fri after thanksgiving */
67: 12,12, 24,24, 1,1, /* mon before tue christmas */
68: 12,12, 25,25, 0,6, /* christmas */
69: 12,12, 26,26, 5,5, /* fri after thu christmas */
70: 12,12, 31,31, 1,1, /* mon before tue new years */
71: };
72: struct tm *localtime();
73: char *ctime();
74: char *date1, *date2;
75: #define year(date) (&date[20])
76: int hflg = 0;
77:
78: main(argc,argv)
79: char **argv;
80: {
81: long t;
82: int hidigit, lastday;
83: struct tm ctm, ntm;
84: int horiz = argc>1? atoi(argv[1]): 2;
85: if(horiz<=0) horiz = 1;
86: time(&t);
87: date1 = ctime(&t);
88: printf("(^|[ (,;])(");
89: for(ctm= *localtime(&t); horiz>0; ctm=ntm) {
90: hidigit = ctm.tm_mday/10;
91: ntm = ctm;
92: while(horiz>0) {
93: lastday = ntm.tm_mday;
94: if(horiz>1 || !dayoff(&ntm))
95: horiz--;
96: t += DAY;
97: ntm = *localtime(&t);
98: if(ntm.tm_mday/10!=hidigit)
99: break;
100: }
101: printf("((%s[^ ]* *|0*%d/|\\* */?)0*",
102: month[ctm.tm_mon], ctm.tm_mon+1);
103: if(hidigit!=0)
104: printf("%d",hidigit);
105: printf("[%d-%d])", ctm.tm_mday%10, lastday%10);
106: if(horiz>0)
107: printf("\n");
108: }
109: date2 = ctime(&t);
110: /* good until year 2000, but sloppy across year ends */
111: printf(")([^0-9,/ ]|$|/%c%c|",year(date1)[2],year(date1)[3]);
112: printf("([, ] *([^189 ]|1([^9]|$)|19[^89]|(19)?($|");
113: printf("%c%c|",year(date1)[2],year(date1)[3]);
114: printf("%c%c|",year(date2)[2],year(date2)[3]);
115: printf("[89]($|[^0-9])))))\n");
116: }
117:
118: dayoff(tm)
119: register struct tm *tm;
120: {
121: struct hol *hol;
122: for(hol= holidays; hol<&holidays[NHOL]; hol++) {
123: if(hol->h_mon0 <= tm->tm_mon+1 &&
124: tm->tm_mon+1 <= hol->h_mon1 &&
125: hol->h_mday0 <= tm->tm_mday &&
126: tm->tm_mday <= hol->h_mday1 &&
127: hol->h_wday0 <= tm->tm_wday &&
128: tm->tm_wday <= hol->h_wday1 )
129: return(1);
130: }
131: return(0);
132: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.