[BACK]Return to day.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / calendar

Annotation of src/usr.bin/calendar/day.c, Revision 1.15

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