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

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