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