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