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