[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.12

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