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

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