[BACK]Return to date.y CVS log [TXT][DIR] Up to [local] / src / usr.bin / cvs

Annotation of src/usr.bin/cvs/date.y, Revision 1.4

1.1       jfb         1: %{
1.4     ! jfb         2: /*     $OpenBSD: date.y,v 1.3 2005/03/24 15:40:43 jfb Exp $    */
1.1       jfb         3:
                      4: /*
                      5: **  Originally written by Steven M. Bellovin <smb@research.att.com> while
                      6: **  at the University of North Carolina at Chapel Hill.  Later tweaked by
                      7: **  a couple of people on Usenet.  Completely overhauled by Rich $alz
                      8: **  <rsalz@bbn.com> and Jim Berets <jberets@bbn.com> in August, 1990;
                      9: **
                     10: **  This grammar has 10 shift/reduce conflicts.
                     11: **
                     12: **  This code is in the public domain and has no copyright.
                     13: */
                     14: /* SUPPRESS 287 on yaccpar_sccsid *//* Unused static variable */
                     15: /* SUPPRESS 288 on yyerrlab *//* Label unused */
                     16:
                     17: #include <sys/types.h>
                     18: #include <sys/timeb.h>
                     19:
                     20: #include <ctype.h>
                     21: #include <stdio.h>
1.2       jfb        22: #include <stdarg.h>
1.1       jfb        23: #include <stdlib.h>
                     24: #include <string.h>
                     25: #include <time.h>
                     26:
                     27: #include "log.h"
                     28:
1.3       jfb        29: #define YEAR_EPOCH     1970
                     30: #define YEAR_TMORIGIN  1900
1.1       jfb        31: #define HOUR(x)                ((time_t)(x) * 60)
                     32: #define SECSPERDAY     (24L * 60L * 60L)
                     33:
                     34:
                     35: /* An entry in the lexical lookup table */
                     36: typedef struct _TABLE {
                     37:        char    *name;
                     38:        int     type;
                     39:        time_t  value;
                     40: } TABLE;
                     41:
                     42:
1.4     ! jfb        43: /*  Daylight-savings mode:  on, off, or not yet known. */
1.1       jfb        44: typedef enum _DSTMODE {
                     45:        DSTon, DSToff, DSTmaybe
                     46: } DSTMODE;
                     47:
1.4     ! jfb        48: /*  Meridian:  am, pm, or 24-hour style. */
1.1       jfb        49: typedef enum _MERIDIAN {
                     50:        MERam, MERpm, MER24
                     51: } MERIDIAN;
                     52:
                     53:
                     54: /*
1.4     ! jfb        55:  *  Global variables.  We could get rid of most of these by using a good
        !            56:  *  union as the yacc stack.  (This routine was originally written before
        !            57:  *  yacc had the %union construct.)  Maybe someday; right now we only use
        !            58:  *  the %union very rarely.
        !            59:  */
1.1       jfb        60: static char    *yyInput;
                     61: static DSTMODE yyDSTmode;
                     62: static time_t  yyDayOrdinal;
                     63: static time_t  yyDayNumber;
                     64: static int     yyHaveDate;
                     65: static int     yyHaveDay;
                     66: static int     yyHaveRel;
                     67: static int     yyHaveTime;
                     68: static int     yyHaveZone;
                     69: static time_t  yyTimezone;
                     70: static time_t  yyDay;
                     71: static time_t  yyHour;
                     72: static time_t  yyMinutes;
                     73: static time_t  yyMonth;
                     74: static time_t  yySeconds;
                     75: static time_t  yyYear;
                     76: static MERIDIAN        yyMeridian;
                     77: static time_t  yyRelMonth;
                     78: static time_t  yyRelSeconds;
                     79:
1.2       jfb        80:
                     81: static int   yyerror   (const char *, ...);
                     82: static int   yylex     (void);
                     83: static int   yyparse   (void);
                     84:
                     85:
1.1       jfb        86: %}
                     87:
                     88: %union {
                     89:        time_t          Number;
                     90:        enum _MERIDIAN  Meridian;
                     91: }
                     92:
                     93: %token tAGO tDAY tDAYZONE tID tMERIDIAN tMINUTE_UNIT tMONTH tMONTH_UNIT
                     94: %token tSEC_UNIT tSNUMBER tUNUMBER tZONE tDST
                     95:
                     96: %type  <Number>        tDAY tDAYZONE tMINUTE_UNIT tMONTH tMONTH_UNIT
                     97: %type  <Number>        tSEC_UNIT tSNUMBER tUNUMBER tZONE
                     98: %type  <Meridian>      tMERIDIAN o_merid
                     99:
                    100: %%
                    101:
                    102: spec   : /* NULL */
                    103:        | spec item
                    104:        ;
                    105:
                    106: item   : time {
1.3       jfb       107:                yyHaveTime++;
1.1       jfb       108:        }
                    109:        | zone {
1.3       jfb       110:                yyHaveZone++;
1.1       jfb       111:        }
                    112:        | date {
1.3       jfb       113:                yyHaveDate++;
1.1       jfb       114:        }
                    115:        | day {
1.3       jfb       116:                yyHaveDay++;
1.1       jfb       117:        }
                    118:        | rel {
1.3       jfb       119:                yyHaveRel++;
1.1       jfb       120:        }
                    121:        | number
                    122:        ;
                    123:
                    124: time   : tUNUMBER tMERIDIAN {
1.3       jfb       125:                yyHour = $1;
                    126:                yyMinutes = 0;
                    127:                yySeconds = 0;
                    128:                yyMeridian = $2;
1.1       jfb       129:        }
                    130:        | tUNUMBER ':' tUNUMBER o_merid {
1.3       jfb       131:                yyHour = $1;
                    132:                yyMinutes = $3;
                    133:                yySeconds = 0;
                    134:                yyMeridian = $4;
1.1       jfb       135:        }
                    136:        | tUNUMBER ':' tUNUMBER tSNUMBER {
1.3       jfb       137:                yyHour = $1;
                    138:                yyMinutes = $3;
                    139:                yyMeridian = MER24;
                    140:                yyDSTmode = DSToff;
                    141:                yyTimezone = - ($4 % 100 + ($4 / 100) * 60);
1.1       jfb       142:        }
                    143:        | tUNUMBER ':' tUNUMBER ':' tUNUMBER o_merid {
1.3       jfb       144:                yyHour = $1;
                    145:                yyMinutes = $3;
                    146:                yySeconds = $5;
                    147:                yyMeridian = $6;
1.1       jfb       148:        }
                    149:        | tUNUMBER ':' tUNUMBER ':' tUNUMBER tSNUMBER {
1.3       jfb       150:                yyHour = $1;
                    151:                yyMinutes = $3;
                    152:                yySeconds = $5;
                    153:                yyMeridian = MER24;
                    154:                yyDSTmode = DSToff;
                    155:                yyTimezone = - ($6 % 100 + ($6 / 100) * 60);
1.1       jfb       156:        }
                    157:        ;
                    158:
                    159: zone   : tZONE {
1.3       jfb       160:                yyTimezone = $1;
                    161:                yyDSTmode = DSToff;
1.1       jfb       162:        }
                    163:        | tDAYZONE {
1.3       jfb       164:                yyTimezone = $1;
                    165:                yyDSTmode = DSTon;
1.1       jfb       166:        }
1.3       jfb       167:        | tZONE tDST {
                    168:                yyTimezone = $1;
                    169:                yyDSTmode = DSTon;
1.1       jfb       170:        }
                    171:        ;
                    172:
                    173: day    : tDAY {
1.3       jfb       174:                yyDayOrdinal = 1;
                    175:                yyDayNumber = $1;
1.1       jfb       176:        }
                    177:        | tDAY ',' {
1.3       jfb       178:                yyDayOrdinal = 1;
                    179:                yyDayNumber = $1;
1.1       jfb       180:        }
                    181:        | tUNUMBER tDAY {
1.3       jfb       182:                yyDayOrdinal = $1;
                    183:                yyDayNumber = $2;
1.1       jfb       184:        }
                    185:        ;
                    186:
                    187: date   : tUNUMBER '/' tUNUMBER {
1.3       jfb       188:                yyMonth = $1;
                    189:                yyDay = $3;
                    190:        }
                    191:        | tUNUMBER '/' tUNUMBER '/' tUNUMBER {
                    192:                if ($1 >= 100) {
                    193:                        yyYear = $1;
                    194:                        yyMonth = $3;
                    195:                        yyDay = $5;
                    196:                } else {
1.1       jfb       197:                        yyMonth = $1;
                    198:                        yyDay = $3;
1.3       jfb       199:                        yyYear = $5;
                    200:                }
1.1       jfb       201:        }
1.3       jfb       202:        | tUNUMBER tSNUMBER tSNUMBER {
                    203:                /* ISO 8601 format.  yyyy-mm-dd.  */
1.1       jfb       204:                yyYear = $1;
1.3       jfb       205:                yyMonth = -$2;
                    206:                yyDay = -$3;
1.1       jfb       207:        }
                    208:        | tUNUMBER tMONTH tSNUMBER {
1.3       jfb       209:                /* e.g. 17-JUN-1992.  */
                    210:                yyDay = $1;
                    211:                yyMonth = $2;
                    212:                yyYear = -$3;
1.1       jfb       213:        }
                    214:        | tMONTH tUNUMBER {
1.3       jfb       215:                yyMonth = $1;
                    216:                yyDay = $2;
1.1       jfb       217:        }
                    218:        | tMONTH tUNUMBER ',' tUNUMBER {
1.3       jfb       219:                yyMonth = $1;
                    220:                yyDay = $2;
                    221:                yyYear = $4;
1.1       jfb       222:        }
                    223:        | tUNUMBER tMONTH {
1.3       jfb       224:                yyMonth = $2;
                    225:                yyDay = $1;
1.1       jfb       226:        }
                    227:        | tUNUMBER tMONTH tUNUMBER {
1.3       jfb       228:                yyMonth = $2;
                    229:                yyDay = $1;
                    230:                yyYear = $3;
1.1       jfb       231:        }
                    232:        ;
                    233:
                    234: rel    : relunit tAGO {
1.3       jfb       235:                yyRelSeconds = -yyRelSeconds;
                    236:                yyRelMonth = -yyRelMonth;
1.1       jfb       237:        }
                    238:        | relunit
                    239:        ;
                    240:
                    241: relunit        : tUNUMBER tMINUTE_UNIT {
1.3       jfb       242:                yyRelSeconds += $1 * $2 * 60L;
1.1       jfb       243:        }
                    244:        | tSNUMBER tMINUTE_UNIT {
1.3       jfb       245:                yyRelSeconds += $1 * $2 * 60L;
1.1       jfb       246:        }
                    247:        | tMINUTE_UNIT {
1.3       jfb       248:                yyRelSeconds += $1 * 60L;
1.1       jfb       249:        }
                    250:        | tSNUMBER tSEC_UNIT {
1.3       jfb       251:                yyRelSeconds += $1;
1.1       jfb       252:        }
                    253:        | tUNUMBER tSEC_UNIT {
1.3       jfb       254:                yyRelSeconds += $1;
1.1       jfb       255:        }
                    256:        | tSEC_UNIT {
1.3       jfb       257:                yyRelSeconds++;
1.1       jfb       258:        }
                    259:        | tSNUMBER tMONTH_UNIT {
1.3       jfb       260:                yyRelMonth += $1 * $2;
1.1       jfb       261:        }
                    262:        | tUNUMBER tMONTH_UNIT {
1.3       jfb       263:                yyRelMonth += $1 * $2;
1.1       jfb       264:        }
                    265:        | tMONTH_UNIT {
1.3       jfb       266:                yyRelMonth += $1;
1.1       jfb       267:        }
                    268:        ;
                    269:
                    270: number : tUNUMBER {
1.3       jfb       271:                if (yyHaveTime && yyHaveDate && !yyHaveRel)
                    272:                        yyYear = $1;
                    273:                else {
                    274:                        if ($1 > 10000) {
1.1       jfb       275:                                yyHaveDate++;
                    276:                                yyDay= ($1)%100;
                    277:                                yyMonth= ($1/100)%100;
                    278:                                yyYear = $1/10000;
1.3       jfb       279:                        } else {
1.1       jfb       280:                                yyHaveTime++;
                    281:                                if ($1 < 100) {
1.3       jfb       282:                                        yyHour = $1;
                    283:                                        yyMinutes = 0;
                    284:                                } else {
1.1       jfb       285:                                        yyHour = $1 / 100;
                    286:                                        yyMinutes = $1 % 100;
                    287:                                }
                    288:                                yySeconds = 0;
                    289:                                yyMeridian = MER24;
                    290:                        }
1.3       jfb       291:                }
1.1       jfb       292:        }
                    293:        ;
                    294:
                    295: o_merid        : /* NULL */ {
                    296:                $$ = MER24;
                    297:        }
                    298:        | tMERIDIAN {
                    299:                $$ = $1;
                    300:        }
                    301:        ;
                    302:
                    303: %%
                    304:
                    305: /* Month and day table. */
                    306: static TABLE const MonthDayTable[] = {
                    307:        { "january",    tMONTH, 1 },
                    308:        { "february",   tMONTH, 2 },
                    309:        { "march",      tMONTH, 3 },
                    310:        { "april",      tMONTH, 4 },
                    311:        { "may",        tMONTH, 5 },
                    312:        { "june",       tMONTH, 6 },
                    313:        { "july",       tMONTH, 7 },
                    314:        { "august",     tMONTH, 8 },
                    315:        { "september",  tMONTH, 9 },
                    316:        { "sept",       tMONTH, 9 },
                    317:        { "october",    tMONTH, 10 },
                    318:        { "november",   tMONTH, 11 },
                    319:        { "december",   tMONTH, 12 },
                    320:        { "sunday",     tDAY,   0 },
                    321:        { "monday",     tDAY,   1 },
                    322:        { "tuesday",    tDAY,   2 },
                    323:        { "tues",       tDAY,   2 },
                    324:        { "wednesday",  tDAY,   3 },
                    325:        { "wednes",     tDAY,   3 },
                    326:        { "thursday",   tDAY,   4 },
                    327:        { "thur",       tDAY,   4 },
                    328:        { "thurs",      tDAY,   4 },
                    329:        { "friday",     tDAY,   5 },
                    330:        { "saturday",   tDAY,   6 },
                    331:        { NULL }
                    332: };
                    333:
                    334: /* Time units table. */
                    335: static TABLE const UnitsTable[] = {
                    336:        { "year",       tMONTH_UNIT,    12 },
                    337:        { "month",      tMONTH_UNIT,    1 },
                    338:        { "fortnight",  tMINUTE_UNIT,   14 * 24 * 60 },
                    339:        { "week",       tMINUTE_UNIT,   7 * 24 * 60 },
                    340:        { "day",        tMINUTE_UNIT,   1 * 24 * 60 },
                    341:        { "hour",       tMINUTE_UNIT,   60 },
                    342:        { "minute",     tMINUTE_UNIT,   1 },
                    343:        { "min",        tMINUTE_UNIT,   1 },
                    344:        { "second",     tSEC_UNIT,      1 },
                    345:        { "sec",        tSEC_UNIT,      1 },
                    346:        { NULL }
                    347: };
                    348:
                    349: /* Assorted relative-time words. */
                    350: static TABLE const OtherTable[] = {
                    351:        { "tomorrow",   tMINUTE_UNIT,   1 * 24 * 60 },
                    352:        { "yesterday",  tMINUTE_UNIT,   -1 * 24 * 60 },
                    353:        { "today",      tMINUTE_UNIT,   0 },
                    354:        { "now",        tMINUTE_UNIT,   0 },
                    355:        { "last",       tUNUMBER,       -1 },
                    356:        { "this",       tMINUTE_UNIT,   0 },
                    357:        { "next",       tUNUMBER,       2 },
                    358:        { "first",      tUNUMBER,       1 },
                    359: /*  { "second",                tUNUMBER,       2 }, */
                    360:        { "third",      tUNUMBER,       3 },
                    361:        { "fourth",     tUNUMBER,       4 },
                    362:        { "fifth",      tUNUMBER,       5 },
                    363:        { "sixth",      tUNUMBER,       6 },
                    364:        { "seventh",    tUNUMBER,       7 },
                    365:        { "eighth",     tUNUMBER,       8 },
                    366:        { "ninth",      tUNUMBER,       9 },
                    367:        { "tenth",      tUNUMBER,       10 },
                    368:        { "eleventh",   tUNUMBER,       11 },
                    369:        { "twelfth",    tUNUMBER,       12 },
                    370:        { "ago",        tAGO,   1 },
                    371:        { NULL }
                    372: };
                    373:
                    374: /* The timezone table. */
                    375: /* Some of these are commented out because a time_t can't store a float. */
                    376: static TABLE const TimezoneTable[] = {
                    377:        { "gmt",        tZONE,     HOUR( 0) },  /* Greenwich Mean */
1.4     ! jfb       378:        { "ut",         tZONE,     HOUR( 0) },  /* Universal (Coordinated) */
1.1       jfb       379:        { "utc",        tZONE,     HOUR( 0) },
                    380:        { "wet",        tZONE,     HOUR( 0) },  /* Western European */
                    381:        { "bst",        tDAYZONE,  HOUR( 0) },  /* British Summer */
                    382:        { "wat",        tZONE,     HOUR( 1) },  /* West Africa */
1.4     ! jfb       383:        { "at",         tZONE,     HOUR( 2) },  /* Azores */
1.1       jfb       384: #if    0
                    385:        /* For completeness.  BST is also British Summer, and GST is
                    386:         * also Guam Standard. */
                    387:        { "bst",        tZONE,     HOUR( 3) },  /* Brazil Standard */
                    388:        { "gst",        tZONE,     HOUR( 3) },  /* Greenland Standard */
                    389: #endif
                    390: #if 0
                    391:        { "nft",        tZONE,     HOUR(3.5) }, /* Newfoundland */
                    392:        { "nst",        tZONE,     HOUR(3.5) }, /* Newfoundland Standard */
                    393:        { "ndt",        tDAYZONE,  HOUR(3.5) }, /* Newfoundland Daylight */
                    394: #endif
                    395:        { "ast",        tZONE,     HOUR( 4) },  /* Atlantic Standard */
                    396:        { "adt",        tDAYZONE,  HOUR( 4) },  /* Atlantic Daylight */
                    397:        { "est",        tZONE,     HOUR( 5) },  /* Eastern Standard */
                    398:        { "edt",        tDAYZONE,  HOUR( 5) },  /* Eastern Daylight */
                    399:        { "cst",        tZONE,     HOUR( 6) },  /* Central Standard */
                    400:        { "cdt",        tDAYZONE,  HOUR( 6) },  /* Central Daylight */
                    401:        { "mst",        tZONE,     HOUR( 7) },  /* Mountain Standard */
                    402:        { "mdt",        tDAYZONE,  HOUR( 7) },  /* Mountain Daylight */
                    403:        { "pst",        tZONE,     HOUR( 8) },  /* Pacific Standard */
                    404:        { "pdt",        tDAYZONE,  HOUR( 8) },  /* Pacific Daylight */
                    405:        { "yst",        tZONE,     HOUR( 9) },  /* Yukon Standard */
                    406:        { "ydt",        tDAYZONE,  HOUR( 9) },  /* Yukon Daylight */
                    407:        { "hst",        tZONE,     HOUR(10) },  /* Hawaii Standard */
                    408:        { "hdt",        tDAYZONE,  HOUR(10) },  /* Hawaii Daylight */
                    409:        { "cat",        tZONE,     HOUR(10) },  /* Central Alaska */
                    410:        { "ahst",       tZONE,     HOUR(10) },  /* Alaska-Hawaii Standard */
1.4     ! jfb       411:        { "nt",         tZONE,     HOUR(11) },  /* Nome */
1.1       jfb       412:        { "idlw",       tZONE,     HOUR(12) },  /* International Date Line West */
                    413:        { "cet",        tZONE,     -HOUR(1) },  /* Central European */
                    414:        { "met",        tZONE,     -HOUR(1) },  /* Middle European */
                    415:        { "mewt",       tZONE,     -HOUR(1) },  /* Middle European Winter */
                    416:        { "mest",       tDAYZONE,  -HOUR(1) },  /* Middle European Summer */
                    417:        { "swt",        tZONE,     -HOUR(1) },  /* Swedish Winter */
                    418:        { "sst",        tDAYZONE,  -HOUR(1) },  /* Swedish Summer */
                    419:        { "fwt",        tZONE,     -HOUR(1) },  /* French Winter */
                    420:        { "fst",        tDAYZONE,  -HOUR(1) },  /* French Summer */
                    421:        { "eet",        tZONE,     -HOUR(2) },  /* Eastern Europe, USSR Zone 1 */
1.4     ! jfb       422:        { "bt",         tZONE,     -HOUR(3) },  /* Baghdad, USSR Zone 2 */
1.1       jfb       423: #if 0
1.4     ! jfb       424:        { "it",         tZONE,     -HOUR(3.5) },/* Iran */
1.1       jfb       425: #endif
                    426:        { "zp4",        tZONE,     -HOUR(4) },  /* USSR Zone 3 */
                    427:        { "zp5",        tZONE,     -HOUR(5) },  /* USSR Zone 4 */
                    428: #if 0
                    429:        { "ist",        tZONE,     -HOUR(5.5) },/* Indian Standard */
                    430: #endif
                    431:        { "zp6",        tZONE,     -HOUR(6) },  /* USSR Zone 5 */
                    432: #if    0
                    433:        /* For completeness.  NST is also Newfoundland Stanard, and SST is
                    434:         * also Swedish Summer. */
                    435:        { "nst",        tZONE,     -HOUR(6.5) },/* North Sumatra */
                    436:        { "sst",        tZONE,     -HOUR(7) },  /* South Sumatra, USSR Zone 6 */
                    437: #endif /* 0 */
                    438:        { "wast",       tZONE,     -HOUR(7) },  /* West Australian Standard */
                    439:        { "wadt",       tDAYZONE,  -HOUR(7) },  /* West Australian Daylight */
                    440: #if 0
1.4     ! jfb       441:        { "jt",         tZONE,     -HOUR(7.5) },/* Java (3pm in Cronusland!) */
1.1       jfb       442: #endif
                    443:        { "cct",        tZONE,     -HOUR(8) },  /* China Coast, USSR Zone 7 */
                    444:        { "jst",        tZONE,     -HOUR(9) },  /* Japan Standard, USSR Zone 8 */
                    445: #if 0
                    446:        { "cast",       tZONE,     -HOUR(9.5) },/* Central Australian Standard */
                    447:        { "cadt",       tDAYZONE,  -HOUR(9.5) },/* Central Australian Daylight */
                    448: #endif
                    449:        { "east",       tZONE,     -HOUR(10) }, /* Eastern Australian Standard */
                    450:        { "eadt",       tDAYZONE,  -HOUR(10) }, /* Eastern Australian Daylight */
                    451:        { "gst",        tZONE,     -HOUR(10) }, /* Guam Standard, USSR Zone 9 */
                    452:        { "nzt",        tZONE,     -HOUR(12) }, /* New Zealand */
                    453:        { "nzst",       tZONE,     -HOUR(12) }, /* New Zealand Standard */
                    454:        { "nzdt",       tDAYZONE,  -HOUR(12) }, /* New Zealand Daylight */
                    455:        { "idle",       tZONE,     -HOUR(12) }, /* International Date Line East */
                    456:        {  NULL  }
                    457: };
                    458:
                    459: /* Military timezone table. */
                    460: static TABLE const MilitaryTable[] = {
                    461:        { "a",  tZONE,  HOUR(  1) },
                    462:        { "b",  tZONE,  HOUR(  2) },
                    463:        { "c",  tZONE,  HOUR(  3) },
                    464:        { "d",  tZONE,  HOUR(  4) },
                    465:        { "e",  tZONE,  HOUR(  5) },
                    466:        { "f",  tZONE,  HOUR(  6) },
                    467:        { "g",  tZONE,  HOUR(  7) },
                    468:        { "h",  tZONE,  HOUR(  8) },
                    469:        { "i",  tZONE,  HOUR(  9) },
                    470:        { "k",  tZONE,  HOUR( 10) },
                    471:        { "l",  tZONE,  HOUR( 11) },
                    472:        { "m",  tZONE,  HOUR( 12) },
                    473:        { "n",  tZONE,  HOUR(- 1) },
                    474:        { "o",  tZONE,  HOUR(- 2) },
                    475:        { "p",  tZONE,  HOUR(- 3) },
                    476:        { "q",  tZONE,  HOUR(- 4) },
                    477:        { "r",  tZONE,  HOUR(- 5) },
                    478:        { "s",  tZONE,  HOUR(- 6) },
                    479:        { "t",  tZONE,  HOUR(- 7) },
                    480:        { "u",  tZONE,  HOUR(- 8) },
                    481:        { "v",  tZONE,  HOUR(- 9) },
                    482:        { "w",  tZONE,  HOUR(-10) },
                    483:        { "x",  tZONE,  HOUR(-11) },
                    484:        { "y",  tZONE,  HOUR(-12) },
                    485:        { "z",  tZONE,  HOUR(  0) },
                    486:        { NULL }
                    487: };
                    488:
                    489:
                    490: /* ARGSUSED */
                    491: static int
1.2       jfb       492: yyerror(const char *fmt, ...)
1.1       jfb       493: {
1.2       jfb       494:        va_list vap;
                    495:
                    496:        va_start(vap, fmt);
                    497:        cvs_vlog(LP_ERR, fmt, vap);
                    498:        va_end(vap);
                    499:
1.1       jfb       500:        return (0);
                    501: }
                    502:
                    503:
                    504: static time_t
                    505: ToSeconds(time_t Hours, time_t Minutes, time_t Seconds, MERIDIAN Meridian)
                    506: {
                    507:        if (Minutes < 0 || Minutes > 59 || Seconds < 0 || Seconds > 59)
                    508:                return (-1);
                    509:
                    510:        switch (Meridian) {
                    511:        case MER24:
                    512:                if (Hours < 0 || Hours > 23)
                    513:                        return (-1);
                    514:                return (Hours * 60L + Minutes) * 60L + Seconds;
                    515:        case MERam:
                    516:                if (Hours < 1 || Hours > 12)
                    517:                        return (-1);
                    518:                if (Hours == 12)
                    519:                        Hours = 0;
                    520:                return (Hours * 60L + Minutes) * 60L + Seconds;
                    521:        case MERpm:
                    522:                if (Hours < 1 || Hours > 12)
                    523:                        return (-1);
                    524:                if (Hours == 12)
                    525:                        Hours = 0;
                    526:                return ((Hours + 12) * 60L + Minutes) * 60L + Seconds;
                    527:        default:
                    528:                abort ();
                    529:        }
                    530:        /* NOTREACHED */
                    531: }
                    532:
                    533:
                    534: /* Year is either
                    535:  * A negative number, which means to use its absolute value (why?)
                    536:  * A number from 0 to 99, which means a year from 1900 to 1999, or
                    537:  * The actual year (>=100).
                    538:  */
                    539: static time_t
                    540: Convert(time_t Month, time_t Day, time_t Year, time_t Hours, time_t Minutes,
                    541:     time_t Seconds, MERIDIAN Meridian, DSTMODE DSTmode)
                    542: {
                    543:        static int DaysInMonth[12] = {
                    544:                31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
                    545:        };
                    546:        time_t  tod;
                    547:        time_t  julian;
                    548:        int     i;
                    549:
                    550:        if (Year < 0)
                    551:                Year = -Year;
                    552:        if (Year < 69)
                    553:                Year += 2000;
                    554:        else if (Year < 100) {
                    555:                Year += 1900;
1.3       jfb       556:                if (Year < YEAR_EPOCH)
1.1       jfb       557:                        Year += 100;
                    558:        }
                    559:        DaysInMonth[1] = Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0)
                    560:            ? 29 : 28;
                    561:        /* Checking for 2038 bogusly assumes that time_t is 32 bits.  But
                    562:           I'm too lazy to try to check for time_t overflow in another way.  */
1.3       jfb       563:        if (Year < YEAR_EPOCH || Year > 2038 || Month < 1 || Month > 12 ||
1.1       jfb       564:            /* Lint fluff:  "conversion from long may lose accuracy" */
                    565:             Day < 1 || Day > DaysInMonth[(int)--Month])
                    566:                return (-1);
                    567:
                    568:        for (julian = Day - 1, i = 0; i < Month; i++)
                    569:                julian += DaysInMonth[i];
                    570:
1.3       jfb       571:        for (i = YEAR_EPOCH; i < Year; i++)
1.1       jfb       572:                julian += 365 + (i % 4 == 0);
                    573:        julian *= SECSPERDAY;
                    574:        julian += yyTimezone * 60L;
                    575:
                    576:        if ((tod = ToSeconds(Hours, Minutes, Seconds, Meridian)) < 0)
                    577:                return (-1);
                    578:        julian += tod;
                    579:        if ((DSTmode == DSTon) ||
                    580:            (DSTmode == DSTmaybe && localtime(&julian)->tm_isdst))
                    581:        julian -= 60 * 60;
                    582:        return (julian);
                    583: }
                    584:
                    585:
                    586: static time_t
                    587: DSTcorrect(time_t Start, time_t Future)
                    588: {
                    589:        time_t  StartDay;
                    590:        time_t  FutureDay;
                    591:
                    592:        StartDay = (localtime(&Start)->tm_hour + 1) % 24;
                    593:        FutureDay = (localtime(&Future)->tm_hour + 1) % 24;
                    594:        return (Future - Start) + (StartDay - FutureDay) * 60L * 60L;
                    595: }
                    596:
                    597:
                    598: static time_t
                    599: RelativeDate(time_t Start, time_t DayOrdinal, time_t DayNumber)
                    600: {
                    601:        struct tm       *tm;
                    602:        time_t  now;
                    603:
                    604:        now = Start;
                    605:        tm = localtime(&now);
                    606:        now += SECSPERDAY * ((DayNumber - tm->tm_wday + 7) % 7);
                    607:        now += 7 * SECSPERDAY * (DayOrdinal <= 0 ? DayOrdinal : DayOrdinal - 1);
                    608:        return DSTcorrect(Start, now);
                    609: }
                    610:
                    611:
                    612: static time_t
                    613: RelativeMonth(time_t Start, time_t RelMonth)
                    614: {
                    615:        struct tm       *tm;
                    616:        time_t  Month;
                    617:        time_t  Year;
                    618:
                    619:        if (RelMonth == 0)
                    620:                return (0);
                    621:        tm = localtime(&Start);
                    622:        Month = 12 * (tm->tm_year + 1900) + tm->tm_mon + RelMonth;
                    623:        Year = Month / 12;
                    624:        Month = Month % 12 + 1;
                    625:        return DSTcorrect(Start,
                    626:            Convert(Month, (time_t)tm->tm_mday, Year,
                    627:            (time_t)tm->tm_hour, (time_t)tm->tm_min, (time_t)tm->tm_sec,
                    628:            MER24, DSTmaybe));
                    629: }
                    630:
                    631:
                    632: static int
                    633: LookupWord(char *buff)
                    634: {
                    635:        char            *p, *q;
                    636:        int             i, abbrev;
                    637:        const TABLE     *tp;
                    638:
                    639:        /* Make it lowercase. */
                    640:        for (p = buff; *p; p++)
                    641:                if (isupper(*p))
                    642:                        *p = tolower(*p);
                    643:
                    644:        if (strcmp(buff, "am") == 0 || strcmp(buff, "a.m.") == 0) {
                    645:                yylval.Meridian = MERam;
                    646:                return (tMERIDIAN);
                    647:        }
                    648:        if (strcmp(buff, "pm") == 0 || strcmp(buff, "p.m.") == 0) {
                    649:                yylval.Meridian = MERpm;
                    650:                return (tMERIDIAN);
                    651:        }
                    652:
                    653:        /* See if we have an abbreviation for a month. */
                    654:        if (strlen(buff) == 3)
                    655:                abbrev = 1;
                    656:        else if (strlen(buff) == 4 && buff[3] == '.') {
                    657:                abbrev = 1;
                    658:                buff[3] = '\0';
                    659:        } else
                    660:                abbrev = 0;
                    661:
                    662:        for (tp = MonthDayTable; tp->name; tp++) {
                    663:                if (abbrev) {
                    664:                        if (strncmp(buff, tp->name, 3) == 0) {
                    665:                                yylval.Number = tp->value;
                    666:                                return (tp->type);
                    667:                        }
                    668:                } else if (strcmp(buff, tp->name) == 0) {
                    669:                        yylval.Number = tp->value;
                    670:                        return (tp->type);
                    671:                }
                    672:        }
                    673:
                    674:        for (tp = TimezoneTable; tp->name; tp++)
                    675:                if (strcmp(buff, tp->name) == 0) {
                    676:                        yylval.Number = tp->value;
                    677:                        return (tp->type);
                    678:                }
                    679:
                    680:        if (strcmp(buff, "dst") == 0)
                    681:                return (tDST);
                    682:
                    683:        for (tp = UnitsTable; tp->name; tp++)
                    684:                if (strcmp(buff, tp->name) == 0) {
                    685:                        yylval.Number = tp->value;
                    686:                        return (tp->type);
                    687:                }
                    688:
                    689:        /* Strip off any plural and try the units table again. */
                    690:        i = strlen(buff) - 1;
                    691:        if (buff[i] == 's') {
                    692:                buff[i] = '\0';
                    693:                for (tp = UnitsTable; tp->name; tp++)
                    694:                        if (strcmp(buff, tp->name) == 0) {
                    695:                                yylval.Number = tp->value;
                    696:                                return (tp->type);
                    697:                        }
                    698:                buff[i] = 's';  /* Put back for "this" in OtherTable. */
                    699:        }
                    700:
                    701:        for (tp = OtherTable; tp->name; tp++)
                    702:                if (strcmp(buff, tp->name) == 0) {
                    703:                        yylval.Number = tp->value;
                    704:                        return (tp->type);
                    705:                }
                    706:
                    707:        /* Military timezones. */
                    708:        if (buff[1] == '\0' && isalpha(*buff)) {
                    709:                for (tp = MilitaryTable; tp->name; tp++)
                    710:                        if (strcmp(buff, tp->name) == 0) {
                    711:                                yylval.Number = tp->value;
                    712:                                return (tp->type);
                    713:                        }
                    714:        }
                    715:
                    716:        /* Drop out any periods and try the timezone table again. */
                    717:        for (i = 0, p = q = buff; *q; q++)
                    718:                if (*q != '.')
                    719:                        *p++ = *q;
                    720:                else
                    721:                        i++;
                    722:        *p = '\0';
                    723:        if (i)
                    724:                for (tp = TimezoneTable; tp->name; tp++)
                    725:                        if (strcmp(buff, tp->name) == 0) {
                    726:                                yylval.Number = tp->value;
                    727:                                return (tp->type);
                    728:                        }
                    729:
                    730:        return (tID);
                    731: }
                    732:
                    733:
                    734: static int
                    735: yylex(void)
                    736: {
                    737:        char    c, *p, buff[20];
                    738:        int     count, sign;
                    739:
                    740:        for (;;) {
                    741:                while (isspace(*yyInput))
                    742:                        yyInput++;
                    743:
                    744:                if (isdigit(c = *yyInput) || c == '-' || c == '+') {
                    745:                        if (c == '-' || c == '+') {
                    746:                                sign = c == '-' ? -1 : 1;
                    747:                                if (!isdigit(*++yyInput))
                    748:                                        /* skip the '-' sign */
                    749:                                        continue;
                    750:                        }
                    751:                        else
                    752:                                sign = 0;
                    753:
                    754:                        for (yylval.Number = 0; isdigit(c = *yyInput++); )
                    755:                                yylval.Number = 10 * yylval.Number + c - '0';
                    756:                        yyInput--;
                    757:                        if (sign < 0)
                    758:                                yylval.Number = -yylval.Number;
                    759:                        return sign ? tSNUMBER : tUNUMBER;
                    760:                }
                    761:
                    762:                if (isalpha(c)) {
                    763:                        for (p = buff; isalpha(c = *yyInput++) || c == '.'; )
                    764:                                if (p < &buff[sizeof buff - 1])
                    765:                                        *p++ = c;
                    766:                        *p = '\0';
                    767:                        yyInput--;
                    768:                        return LookupWord(buff);
                    769:                }
                    770:                if (c != '(')
                    771:                        return *yyInput++;
                    772:
                    773:                count = 0;
                    774:                do {
                    775:                        c = *yyInput++;
                    776:                        if (c == '\0')
                    777:                                return (c);
                    778:                        if (c == '(')
                    779:                                count++;
                    780:                        else if (c == ')')
                    781:                                count--;
                    782:                } while (count > 0);
                    783:        }
                    784: }
                    785:
                    786: /* Yield A - B, measured in seconds.  */
                    787: static long
1.3       jfb       788: difftm(struct tm *a, struct tm *b)
1.1       jfb       789: {
1.3       jfb       790:        int ay = a->tm_year + (YEAR_TMORIGIN - 1);
                    791:        int by = b->tm_year + (YEAR_TMORIGIN - 1);
1.1       jfb       792:        int days = (
                    793:                          /* difference in day of year */
                    794:                          a->tm_yday - b->tm_yday
                    795:                          /* + intervening leap days */
                    796:                          +  ((ay >> 2) - (by >> 2))
                    797:                          -  (ay/100 - by/100)
                    798:                          +  ((ay/100 >> 2) - (by/100 >> 2))
                    799:                          /* + difference in years * 365 */
                    800:                          +  (long)(ay-by) * 365
                    801:                          );
                    802:        return (60 * (60 * (24 * days + (a->tm_hour - b->tm_hour))
                    803:            + (a->tm_min - b->tm_min)) + (a->tm_sec - b->tm_sec));
                    804: }
                    805:
                    806: time_t
                    807: get_date(char *p, struct timeb *now)
                    808: {
                    809:        struct tm       *tm, gmt;
                    810:        struct timeb    ftz;
1.2       jfb       811:        time_t          Start, tod, nowtime;
1.1       jfb       812:
                    813:        yyInput = p;
                    814:        if (now == NULL) {
                    815:                struct tm *gmt_ptr;
                    816:
                    817:                now = &ftz;
                    818:                (void)time (&nowtime);
                    819:
                    820:                gmt_ptr = gmtime (&nowtime);
                    821:                if (gmt_ptr != NULL) {
                    822:                        /* Make a copy, in case localtime modifies *tm (I think
                    823:                         * that comment now applies to *gmt_ptr, but I am too
                    824:                         * lazy to dig into how gmtime and locatime allocate the
                    825:                         * structures they return pointers to).
                    826:                         */
                    827:                        gmt = *gmt_ptr;
                    828:                }
                    829:
                    830:                if (!(tm = localtime (&nowtime)))
                    831:                        return (-1);
                    832:
                    833:                if (gmt_ptr != NULL)
                    834:                        ftz.timezone = difftm (&gmt, tm) / 60;
                    835:                else
                    836:                        /* We are on a system like VMS, where the system clock is
                    837:                           in local time and the system has no concept of timezones.
                    838:                           Hopefully we can fake this out (for the case in which the
                    839:                           user specifies no timezone) by just saying the timezone
                    840:                           is zero.  */
                    841:                        ftz.timezone = 0;
                    842:
                    843:                if(tm->tm_isdst)
                    844:                        ftz.timezone += 60;
                    845:        }
                    846:        else {
                    847:                nowtime = now->time;
                    848:        }
                    849:
                    850:        tm = localtime(&nowtime);
                    851:        yyYear = tm->tm_year + 1900;
                    852:        yyMonth = tm->tm_mon + 1;
                    853:        yyDay = tm->tm_mday;
                    854:        yyTimezone = now->timezone;
                    855:        yyDSTmode = DSTmaybe;
                    856:        yyHour = 0;
                    857:        yyMinutes = 0;
                    858:        yySeconds = 0;
                    859:        yyMeridian = MER24;
                    860:        yyRelSeconds = 0;
                    861:        yyRelMonth = 0;
                    862:        yyHaveDate = 0;
                    863:        yyHaveDay = 0;
                    864:        yyHaveRel = 0;
                    865:        yyHaveTime = 0;
                    866:        yyHaveZone = 0;
                    867:
                    868:        if (yyparse() || yyHaveTime > 1 || yyHaveZone > 1 ||
                    869:            yyHaveDate > 1 || yyHaveDay > 1)
                    870:                return (-1);
                    871:
                    872:        if (yyHaveDate || yyHaveTime || yyHaveDay) {
                    873:                Start = Convert(yyMonth, yyDay, yyYear, yyHour, yyMinutes,
                    874:                    yySeconds, yyMeridian, yyDSTmode);
                    875:                if (Start < 0)
                    876:                        return (-1);
                    877:        } else {
                    878:                Start = nowtime;
                    879:                if (!yyHaveRel)
                    880:                        Start -= ((tm->tm_hour * 60L + tm->tm_min) * 60L) + tm->tm_sec;
                    881:        }
                    882:
                    883:        Start += yyRelSeconds;
                    884:        Start += RelativeMonth(Start, yyRelMonth);
                    885:
                    886:        if (yyHaveDay && !yyHaveDate) {
                    887:                tod = RelativeDate(Start, yyDayOrdinal, yyDayNumber);
                    888:                Start += tod;
                    889:        }
                    890:
                    891:        /* Have to do *something* with a legitimate -1 so it's distinguishable
                    892:         * from the error return value.  (Alternately could set errno on error.)
                    893:         */
                    894:        return (Start == -1) ? (0) : (Start);
                    895: }
                    896:
                    897: #if defined(TEST)
                    898: /* ARGSUSED */
                    899: int
                    900: main(int argc, char **argv)
                    901: {
                    902:        char    buff[128];
                    903:        time_t  d;
                    904:
                    905:        (void)printf("Enter date, or blank line to exit.\n\t> ");
                    906:        (void)fflush(stdout);
                    907:        while (fgets(buff, sizeof(buff), stdin) && buff[0]) {
                    908:                d = get_date(buff, (struct timeb *)NULL);
                    909:                if (d == -1)
                    910:                        (void)printf("Bad format - couldn't convert.\n");
                    911:                else
                    912:                        (void)printf("%s", ctime(&d));
                    913:                (void)printf("\t> ");
                    914:                (void)fflush(stdout);
                    915:        }
                    916:
                    917:        return (0);
                    918: }
                    919: #endif /* defined(TEST) */