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