1 /*	$OpenBSD: day.c,v 1.18 2004/12/10 20:47:30 mickey Exp $	*/
2 
3 /*
4  * Copyright (c) 1989, 1993, 1994
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __COPYRIGHT("@(#) Copyright (c) 1989, 1993\n\
34 	The Regents of the University of California.  All rights reserved.\n");
35 __SCCSID("@(#)calendar.c  8.3 (Berkeley) 3/25/94");
36 __RCSID("$MirOS: src/usr.bin/calendar/day.c,v 1.3 2010/09/21 21:24:33 tg Exp $");
37 
38 #include <sys/types.h>
39 #include <sys/uio.h>
40 
41 #include <ctype.h>
42 #include <err.h>
43 #include <locale.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <time.h>
48 #include <tzfile.h>
49 
50 #include "pathnames.h"
51 #include "calendar.h"
52 
53 #define WEEKLY 1
54 #define MONTHLY 2
55 #define YEARLY 3
56 
57 struct tm *tp;
58 int *cumdays, offset;
59 char dayname[10];
60 enum calendars calendar;
61 u_long julian;
62 
63 
64 /* 1-based month, 0-based days, cumulative */
65 int daytab[][14] = {
66 	{ 0, -1, 30, 58, 89, 119, 150, 180, 211, 242, 272, 303, 333, 364 },
67 	{ 0, -1, 30, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
68 };
69 
70 static char *days[] = {
71 	"sun", "mon", "tue", "wed", "thu", "fri", "sat", NULL,
72 };
73 
74 static char *months[] = {
75 	"jan", "feb", "mar", "apr", "may", "jun",
76 	"jul", "aug", "sep", "oct", "nov", "dec", NULL,
77 };
78 
79 static struct fixs fndays[8];         /* full national days names */
80 static struct fixs ndays[8];          /* short national days names */
81 
82 static struct fixs fnmonths[13];      /* full national months names */
83 static struct fixs nmonths[13];       /* short national month names */
84 
85 
setnnames(void)86 void setnnames(void)
87 {
88 	char buf[80];
89 	int i, l;
90 	struct tm tm;
91 
92 	for (i = 0; i < 7; i++) {
93 		tm.tm_wday = i;
94 		l = strftime(buf, sizeof(buf), "%a", &tm);
95 		for (; l > 0 && isspace((int)buf[l - 1]); l--)
96 			;
97 		buf[l] = '\0';
98 		if (ndays[i].name != NULL)
99 			free(ndays[i].name);
100 		if ((ndays[i].name = strdup(buf)) == NULL)
101 			err(1, NULL);
102 		ndays[i].len = strlen(buf);
103 
104 		l = strftime(buf, sizeof(buf), "%A", &tm);
105 		for (; l > 0 && isspace((int)buf[l - 1]); l--)
106 			;
107 		buf[l] = '\0';
108 		if (fndays[i].name != NULL)
109 			free(fndays[i].name);
110 		if ((fndays[i].name = strdup(buf)) == NULL)
111 			err(1, NULL);
112 		fndays[i].len = strlen(buf);
113 	}
114 
115 	for (i = 0; i < 12; i++) {
116 		tm.tm_mon = i;
117 		l = strftime(buf, sizeof(buf), "%b", &tm);
118 		for (; l > 0 && isspace((int)buf[l - 1]); l--)
119 			;
120 		buf[l] = '\0';
121 		if (nmonths[i].name != NULL)
122 			free(nmonths[i].name);
123 		if ((nmonths[i].name = strdup(buf)) == NULL)
124 			err(1, NULL);
125 		nmonths[i].len = strlen(buf);
126 
127 		l = strftime(buf, sizeof(buf), "%B", &tm);
128 		for (; l > 0 && isspace((int)buf[l - 1]); l--)
129 			;
130 		buf[l] = '\0';
131 		if (fnmonths[i].name != NULL)
132 			free(fnmonths[i].name);
133 		if ((fnmonths[i].name = strdup(buf)) == NULL)
134 			err(1, NULL);
135 		fnmonths[i].len = strlen(buf);
136 	}
137 	/* Hardwired special events */
138 	spev[0].name = strdup(PESACH);
139 	spev[0].nlen = PESACHLEN;
140 	spev[0].getev = pesach;
141 	spev[1].name = strdup(EASTER);
142 	spev[1].nlen = EASTERNAMELEN;
143 	spev[1].getev = easter;
144 	spev[2].name = strdup(PASKHA);
145 	spev[2].nlen = PASKHALEN;
146 	spev[2].getev = paskha;
147 	for (i = 0; i < NUMEV; i++) {
148 		if (spev[i].name == NULL)
149 			err(1, NULL);
150 		spev[i].uname = NULL;
151 	}
152 }
153 
154 void
settime(now)155 settime(now)
156 	time_t *now;
157 {
158 	tp = localtime(now);
159 	tp->tm_sec = 0;
160 	tp->tm_min = 0;
161 	/* Avoid getting caught by a timezone shift; set time to noon */
162 	tp->tm_isdst = 0;
163 	tp->tm_hour = 12;
164 	*now = mktime(tp);
165 	if (isleap(tp->tm_year + TM_YEAR_BASE))
166 		cumdays = daytab[1];
167 	else
168 		cumdays = daytab[0];
169 	/* Friday displays Monday's events */
170 	offset = tp->tm_wday == 5 ? 3 : 1;
171 	if (f_dayAfter)
172 		offset = 0;	/* Except not when range is set explicitly */
173 	header[5].iov_base = dayname;
174 
175 #ifndef __MirBSD__
176 	(void) setlocale(LC_TIME, "C");
177 #endif
178 	header[5].iov_len = strftime(dayname, sizeof(dayname), "%A", tp);
179 #ifndef __MirBSD__
180 	(void) setlocale(LC_TIME, "");
181 #endif
182 
183 	setnnames();
184 }
185 
186 /* convert [Year][Month]Day into unix time (since 1970)
187  * Year: two or four digits, Month: two digits, Day: two digits
188  */
Mktime(date)189 time_t Mktime (date)
190     char *date;
191 {
192     time_t t;
193     int len;
194     struct tm tm;
195 
196     (void)time(&t);
197     tp = localtime(&t);
198 
199     len = strlen(date);
200     if (len < 2)
201 	return((time_t)-1);
202     tm.tm_sec = 0;
203     tm.tm_min = 0;
204     /* Avoid getting caught by a timezone shift; set time to noon */
205     tm.tm_isdst = 0;
206     tm.tm_hour = 12;
207     tm.tm_wday = 0;
208     tm.tm_mday = tp->tm_mday;
209     tm.tm_mon = tp->tm_mon;
210     tm.tm_year = tp->tm_year;
211 
212     /* Day */
213     tm.tm_mday = atoi(date + len - 2);
214 
215     /* Month */
216     if (len >= 4) {
217 	*(date + len - 2) = '\0';
218 	tm.tm_mon = atoi(date + len - 4) - 1;
219     }
220 
221     /* Year */
222     if (len >= 6) {
223 		*(date + len - 4) = '\0';
224 		tm.tm_year = atoi(date);
225 
226 	/* tm_year up TM_YEAR_BASE ... */
227 	if (tm.tm_year < 69)		/* Y2K */
228 		tm.tm_year += 2000 - TM_YEAR_BASE;
229 	else if (tm.tm_year < 100)
230 		tm.tm_year += 1900 - TM_YEAR_BASE;
231 	else if (tm.tm_year > TM_YEAR_BASE)
232 		tm.tm_year -= TM_YEAR_BASE;
233     }
234 
235 #if DEBUG
236     printf("Mktime: %d %d %d %s\n", (int)mktime(&tm), (int)t, len,
237 	   asctime(&tm));
238 #endif
239     return(mktime(&tm));
240 }
241 
242 void
adjust_calendar(int * day,int * month)243 adjust_calendar(int *day, int *month)
244 {
245 	switch (calendar) {
246 	case GREGORIAN:
247 		break;
248 
249 	case JULIAN:
250 		*day += julian;
251 		if (*day > (cumdays[*month + 1] - cumdays[*month])) {
252 			*day -= (cumdays[*month + 1] - cumdays[*month]);
253 			if (++*month > 12)
254 				*month = 1;
255 		}
256 		break;
257 	case LUNAR:
258 		break;
259 	}
260 }
261 
262 /*
263  * Possible date formats include any combination of:
264  *	3-charmonth			(January, Jan, Jan)
265  *	3-charweekday			(Friday, Monday, mon.)
266  *	numeric month or day		(1, 2, 04)
267  *
268  * Any character except \t or '*' may separate them, or they may not be
269  * separated.  Any line following a line that is matched, that starts
270  * with \t, is shown along with the matched line.
271  */
272 struct match *
isnow(endp,bodun)273 isnow(endp, bodun)
274 	char	*endp;
275 	int	bodun;
276 {
277 	int day = 0, flags = 0, month = 0, v1, v2, i;
278 	int monthp, dayp, varp = 0;
279 	struct match *matches = NULL, *tmp, *tmp2;
280 	int interval = YEARLY;	/* how frequently the event repeats. */
281 	int vwd = 0;	/* Variable weekday */
282 	time_t tdiff, ttmp;
283 	struct tm tmtmp;
284 
285 	/*
286 	 * CONVENTION
287 	 *
288 	 * Month:     1-12
289 	 * Monthname: Jan .. Dec
290 	 * Day:       1-31
291 	 * Weekday:   Mon-Sun
292 	 *
293 	 */
294 
295 	/* read first field */
296 	/* didn't recognize anything, skip it */
297 	if (!(v1 = getfield(endp, &endp, &flags)))
298 		return (NULL);
299 
300 	/* adjust bodun rate */
301 	if (bodun && !bodun_always)
302 		bodun = !arc4random_uniform(3);
303 
304 	/* Easter or Easter depending days */
305 	if (flags & F_SPECIAL)
306 		vwd = v1;
307 
308 	 /*
309 	  * 1. {Weekday,Day} XYZ ...
310 	  *
311 	  *    where Day is > 12
312 	  */
313 	else if (flags & F_ISDAY || v1 > 12) {
314 
315 		/* found a day; day: 13-31 or weekday: 1-7 */
316 		day = v1;
317 
318 		/* {Day,Weekday} {Month,Monthname} ... */
319 		/* if no recognizable month, assume just a day alone -- this is
320 		 * very unlikely and can only happen after the first 12 days.
321 		 * --find month or use current month */
322 		if (!(month = getfield(endp, &endp, &flags))) {
323 			month = tp->tm_mon + 1;
324 			/* F_ISDAY is set only if a weekday was spelled out */
325 			/* F_ISDAY must be set if 0 < day < 8 */
326 			if ((day <= 7) && (day >= 1))
327 				interval = WEEKLY;
328 			else
329 				interval = MONTHLY;
330 		} else if ((day <= 7) && (day >= 1))
331 			day += 10;
332 			/* it's a weekday; make it the first one of the month */
333 		if (month == -1) {
334 			month = tp->tm_mon + 1;
335 			interval = MONTHLY;
336 		} else if (calendar)
337 			adjust_calendar(&day, &month);
338 		if ((month > 12) || (month < 1))
339 			return (NULL);
340 	}
341 
342 	/* 2. {Monthname} XYZ ... */
343 	else if (flags & F_ISMONTH) {
344 		month = v1;
345 		if (month == -1) {
346 			month = tp->tm_mon + 1;
347 			interval = MONTHLY;
348 		}
349 		/* Monthname {day,weekday} */
350 		/* if no recognizable day, assume the first day in month */
351 		if (!(day = getfield(endp, &endp, &flags)))
352 			day = 1;
353 		/* If a weekday was spelled out without an ordering,
354 		 * assume the first of that day in the month */
355 		if ((flags & F_ISDAY)) {
356 			if ((day >= 1) && (day <=7))
357 				day += 10;
358 		} else if (calendar)
359 			adjust_calendar(&day, &month);
360 	}
361 
362 	/* Hm ... */
363 	else {
364 		v2 = getfield(endp, &endp, &flags);
365 
366 		/*
367 		 * {Day} {Monthname} ...
368 		 * where Day <= 12
369 		 */
370 		if (flags & F_ISMONTH) {
371 			day = v1;
372 			month = v2;
373 			if (month == -1) {
374 				month = tp->tm_mon + 1;
375 				interval = MONTHLY;
376 			} else if (calendar)
377 				adjust_calendar(&day, &month);
378 		}
379 
380 		/* {Month} {Weekday,Day} ...  */
381 		else {
382 			/* F_ISDAY set, v2 > 12, or no way to tell */
383 			month = v1;
384 			/* if no recognizable day, assume the first */
385 			day = v2 ? v2 : 1;
386 			if ((flags & F_ISDAY)) {
387 				if ((day >= 1) && (day <= 7))
388 					day += 10;
389 			} else
390 				adjust_calendar(&day, &month);
391 		}
392 	}
393 
394 	/* convert Weekday into *next*  Day,
395 	 * e.g.: 'Sunday' -> 22
396 	 *       'SundayLast' -> ??
397 	 */
398 	if (flags & F_ISDAY) {
399 #if DEBUG
400 		fprintf(stderr, "\nday: %d %s month %d\n", day, endp, month);
401 #endif
402 
403 		varp = 1;
404 		/* variable weekday, SundayLast, MondayFirst ... */
405 		if (day < 0 || day >= 10)
406 			vwd = day;
407 		else {
408 			day = tp->tm_mday + (((day - 1) - tp->tm_wday + 7) % 7);
409 			interval = WEEKLY;
410 		}
411 	} else
412 	/* Check for silliness.  Note we still catch Feb 29 */
413 		if (!(flags & F_SPECIAL) &&
414 		    (day > (cumdays[month + 1] - cumdays[month]) || day < 1)) {
415 			if (!((month == 2 && day == 29) ||
416 			    (interval == MONTHLY && day <= 31)))
417 				return (NULL);
418 		}
419 
420 	if (!(flags & F_SPECIAL)) {
421 		monthp = month;
422 		dayp = day;
423 		day = cumdays[month] + day;
424 #if DEBUG
425 		fprintf(stderr, "day2: day %d(%d) yday %d\n", dayp, day, tp->tm_yday);
426 #endif
427 	/* Speed up processing for the most common situation:  yearly events
428 	 * when the interval being checked is less than a month or so (this
429 	 * could be less than a year, but then we have to start worrying about
430 	 * leap years).  Only one event can match, and it's easy to find.
431 	 * Note we can't check special events, because they can wander widely.
432 	 */
433 		if (((v1 = offset + f_dayAfter) < 50) && (interval == YEARLY)) {
434 			memcpy(&tmtmp, tp, sizeof(struct tm));
435 			tmtmp.tm_mday = dayp;
436 			tmtmp.tm_mon = monthp - 1;
437 			if (vwd) {
438 			/* We want the event next year if it's late now
439 			 * this year.  The 50-day limit means we don't have to
440 			 * worry if next year is or isn't a leap year.
441 			 */
442 				if (tp->tm_yday > 300 && tmtmp.tm_mon <= 1)
443 					variable_weekday(&vwd, tmtmp.tm_mon + 1,
444 					    tmtmp.tm_year + TM_YEAR_BASE + 1);
445 				else
446 					variable_weekday(&vwd, tmtmp.tm_mon + 1,
447 					    tmtmp.tm_year + TM_YEAR_BASE);
448 				day = cumdays[tmtmp.tm_mon + 1] + vwd;
449 				tmtmp.tm_mday = vwd;
450 			}
451 			v2 = day - tp->tm_yday;
452 			if ((v2 > v1) || (v2 < 0)) {
453 				if ((v2 += isleap(tp->tm_year + TM_YEAR_BASE) ? 366 : 365)
454 				    <= v1)
455 					tmtmp.tm_year++;
456 				else if(!bodun || (day - tp->tm_yday) != -1)
457 					return(NULL);
458 			}
459 			if ((tmp = malloc(sizeof(struct match))) == NULL)
460 				err(1, NULL);
461 
462 			if (bodun && (day - tp->tm_yday) == -1) {
463 				tmp->when = f_time - 1 * SECSPERDAY;
464 				tmtmp.tm_mday++;
465 				tmp->bodun = 1;
466 			} else {
467 				tmp->when = f_time + v2 * SECSPERDAY;
468 				tmp->bodun = 0;
469 			}
470 
471 			(void)mktime(&tmtmp);
472 			if (strftime(tmp->print_date,
473 			    sizeof(tmp->print_date),
474 			/*    "%a %b %d", &tm);  Skip weekdays */
475 			    "%b %d", &tmtmp) == 0)
476 				tmp->print_date[sizeof(tmp->print_date) - 1] = '\0';
477 
478 			tmp->var   = varp;
479 			tmp->next  = NULL;
480 			return(tmp);
481 		}
482 	} else {
483 		varp = 1;
484 		/* Set up v1 to the event number and ... */
485 		v1 = vwd % (NUMEV + 1) - 1;
486 		vwd /= (NUMEV + 1);
487 		if (v1 < 0) {
488 			v1 += NUMEV + 1;
489 			vwd--;
490 		}
491 		dayp = monthp = 1;	/* Why not */
492 	}
493 
494 	/* Compare to past and coming instances of the event.  The i == 0 part
495 	 * of the loop corresponds to this specific instance.  Note that we
496 	 * can leave things sort of higgledy-piggledy since a mktime() happens
497 	 * on this before anything gets printed.  Also note that even though
498 	 * we've effectively gotten rid of f_dayBefore, we still have to check
499 	 * the one prior event for situations like "the 31st of every month"
500 	 * and "yearly" events which could happen twice in one year but not in
501 	 * the next */
502 	tmp2 = matches;
503 	for (i = -1; i < 2; i++) {
504 		memcpy(&tmtmp, tp, sizeof(struct tm));
505 		tmtmp.tm_mday = dayp;
506 		tmtmp.tm_mon = month = monthp - 1;
507 		do {
508 			v2 = 0;
509 			switch (interval) {
510 			case WEEKLY:
511 				tmtmp.tm_mday += 7 * i;
512 				break;
513 			case MONTHLY:
514 				month += i;
515 				tmtmp.tm_mon = month;
516 				switch(tmtmp.tm_mon) {
517 				case -1:
518 					tmtmp.tm_mon = month = 11;
519 					tmtmp.tm_year--;
520 					break;
521 				case 12:
522 					tmtmp.tm_mon = month = 0;
523 					tmtmp.tm_year++;
524 					break;
525 				}
526 				if (vwd) {
527 					v1 = vwd;
528 					variable_weekday(&v1, tmtmp.tm_mon + 1,
529 					    tmtmp.tm_year + TM_YEAR_BASE);
530 					tmtmp.tm_mday = v1;
531 				} else
532 					tmtmp.tm_mday = dayp;
533 				break;
534 			case YEARLY:
535 			default:
536 				tmtmp.tm_year += i;
537 				if (flags & F_SPECIAL) {
538 					tmtmp.tm_mon = 0;	/* Gee, mktime() is nice */
539 					tmtmp.tm_mday = spev[v1].getev(tmtmp.tm_year +
540 					    TM_YEAR_BASE) + vwd;
541 				} else if (vwd) {
542 					v1 = vwd;
543 					variable_weekday(&v1, tmtmp.tm_mon + 1,
544 					    tmtmp.tm_year + TM_YEAR_BASE);
545 					tmtmp.tm_mday = v1;
546 				} else {
547 				/* Need the following to keep Feb 29 from
548 				 * becoming Mar 1 */
549 				tmtmp.tm_mday = dayp;
550 				tmtmp.tm_mon = monthp - 1;
551 				}
552 				break;
553 			}
554 			/* How many days apart are we */
555 			if ((ttmp = mktime(&tmtmp)) == -1)
556 				warnx("time out of range: %s", endp);
557 			else {
558 				tdiff = difftime(ttmp, f_time)/ SECSPERDAY;
559 				if (tdiff <= offset + f_dayAfter ||
560 				    (bodun && tdiff == -1)) {
561 					if (tdiff >=  0 ||
562 					    (bodun && tdiff == -1)) {
563 					if ((tmp = malloc(sizeof(struct match))) == NULL)
564 						err(1, NULL);
565 					tmp->when = ttmp;
566 					if (strftime(tmp->print_date,
567 					    sizeof(tmp->print_date),
568 					/*    "%a %b %d", &tm);  Skip weekdays */
569 					    "%b %d", &tmtmp) == 0)
570 						tmp->print_date[sizeof(tmp->print_date) - 1] = '\0';
571 					tmp->bodun = bodun && tdiff == -1;
572 					tmp->var   = varp;
573 					tmp->next  = NULL;
574 					if (tmp2)
575 						tmp2->next = tmp;
576 					else
577 						matches = tmp;
578 					tmp2 = tmp;
579 					v2 = (i == 1) ? 1 : 0;
580 					}
581 				} else
582 					i = 2; /* No point checking in the future */
583 			}
584 		} while (v2 != 0);
585 	}
586 	return (matches);
587 }
588 
589 
590 int
getmonth(s)591 getmonth(s)
592 	char *s;
593 {
594 	char **p;
595 	struct fixs *n;
596 
597 	for (n = fnmonths; n->name; ++n)
598 		if (!strncasecmp(s, n->name, n->len))
599 			return ((n - fnmonths) + 1);
600 	for (n = nmonths; n->name; ++n)
601 		if (!strncasecmp(s, n->name, n->len))
602 			return ((n - nmonths) + 1);
603 	for (p = months; *p; ++p)
604 		if (!strncasecmp(s, *p, 3))
605 			return ((p - months) + 1);
606 	return (0);
607 }
608 
609 
610 int
getday(s)611 getday(s)
612 	char *s;
613 {
614 	char **p;
615 	struct fixs *n;
616 
617 	for (n = fndays; n->name; ++n)
618 		if (!strncasecmp(s, n->name, n->len))
619 			return ((n - fndays) + 1);
620 	for (n = ndays; n->name; ++n)
621 		if (!strncasecmp(s, n->name, n->len))
622 			return ((n - ndays) + 1);
623 	for (p = days; *p; ++p)
624 		if (!strncasecmp(s, *p, 3))
625 			return ((p - days) + 1);
626 	return (0);
627 }
628 
629 /* return offset for variable weekdays
630  * -1 -> last weekday in month
631  * +1 -> first weekday in month
632  * ... etc ...
633  */
634 int
getdayvar(s)635 getdayvar(s)
636 	char *s;
637 {
638 	int offset;
639 
640 
641 	offset = strlen(s);
642 
643 	/* Sun+1 or Wednesday-2
644 	 *    ^              ^   */
645 
646 	/* printf ("x: %s %s %d\n", s, s + offset - 2, offset); */
647 	switch(*(s + offset - 2)) {
648 	case '-':
649 	case '+':
650 	    return(atoi(s + offset - 2));
651 	    break;
652 	}
653 
654 	/*
655 	 * some aliases: last, first, second, third, fourth
656 	 */
657 
658 	/* last */
659 	if      (offset > 4 && !strcasecmp(s + offset - 4, "last"))
660 	    return(-1);
661 	else if (offset > 5 && !strcasecmp(s + offset - 5, "first"))
662 	    return(+1);
663 	else if (offset > 6 && !strcasecmp(s + offset - 6, "second"))
664 	    return(+2);
665 	else if (offset > 5 && !strcasecmp(s + offset - 5, "third"))
666 	    return(+3);
667 	else if (offset > 6 && !strcasecmp(s + offset - 6, "fourth"))
668 	    return(+4);
669 
670 	/* no offset detected */
671 	return(0);
672 }
673 
674 
675 int
foy(year)676 foy(year)
677 	int year;
678 {
679 	/* 0-6; what weekday Jan 1 is */
680 	year--;
681 	return ((1 - year/100 + year/400 + (int)(365.25 * year)) % 7);
682 }
683 
684 
685 
686 void
variable_weekday(day,month,year)687 variable_weekday(day, month, year)
688 	int *day, month, year;
689 {
690 	int v1, v2;
691 	int *cumdays;
692 	int day1;
693 
694 	if (isleap(year))
695 		cumdays = daytab[1];
696 	else
697 		cumdays = daytab[0];
698 	day1 = foy(year);
699 	/* negative offset; last, -4 .. -1 */
700 	if (*day < 0) {
701 		v1 = *day/10 - 1;          /* offset -4 ... -1 */
702 		*day = 10 + (*day % 10);    /* day 1 ... 7 */
703 
704 		/* which weekday the end of the month is (1-7) */
705 		v2 = (cumdays[month + 1] + day1) % 7 + 1;
706 
707 		/* and subtract enough days */
708 		*day = cumdays[month + 1] - cumdays[month] +
709 		    (v1 + 1) * 7 - (v2 - *day + 7) % 7;
710 #if DEBUG
711 		fprintf(stderr, "\nMonth %d ends on weekday %d\n", month, v2);
712 #endif
713 	}
714 
715 	/* first, second ... +1 ... +5 */
716 	else {
717 		v1 = *day/10;        /* offset */
718 		*day = *day % 10;
719 
720 		/* which weekday the first of the month is (1-7) */
721 		v2 = (cumdays[month] + 1 + day1) % 7 + 1;
722 
723 		/* and add enough days */
724 		*day = 1 + (v1 - 1) * 7 + (*day - v2 + 7) % 7;
725 #if DEBUG
726 		fprintf(stderr, "\nMonth %d starts on weekday %d\n", month, v2);
727 #endif
728 	}
729 }
730