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