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