1 /* 2 * This code is in the public domain and has no copyright. 3 * 4 * This is a plain C recursive-descent translation of an old 5 * public-domain YACC grammar that has been used for parsing dates in 6 * very many open-source projects. 7 * 8 * Since the original authors were generous enough to donate their 9 * work to the public domain, I feel compelled to match their 10 * generosity. 11 * 12 * Tim Kientzle, February 2009. 13 */ 14 15 /* 16 * Header comment from original getdate.y: 17 */ 18 19 /* 20 ** Originally written by Steven M. Bellovin <smb@research.att.com> while 21 ** at the University of North Carolina at Chapel Hill. Later tweaked by 22 ** a couple of people on Usenet. Completely overhauled by Rich $alz 23 ** <rsalz@bbn.com> and Jim Berets <jberets@bbn.com> in August, 1990; 24 ** 25 ** This grammar has 10 shift/reduce conflicts. 26 ** 27 ** This code is in the public domain and has no copyright. 28 */ 29 30 #include "archive_platform.h" 31 #ifdef __FreeBSD__ 32 #include <sys/cdefs.h> 33 __FBSDID("$FreeBSD$"); 34 #endif 35 36 #include <ctype.h> 37 #include <stdio.h> 38 #include <stdlib.h> 39 #include <string.h> 40 #include <time.h> 41 42 #define __LIBARCHIVE_BUILD 1 43 #include "archive_getdate.h" 44 45 /* Basic time units. */ 46 #define EPOCH 1970 47 #define MINUTE (60L) 48 #define HOUR (60L * MINUTE) 49 #define DAY (24L * HOUR) 50 51 /* Daylight-savings mode: on, off, or not yet known. */ 52 enum DSTMODE { DSTon, DSToff, DSTmaybe }; 53 /* Meridian: am or pm. */ 54 enum { tAM, tPM }; 55 /* Token types returned by nexttoken() */ 56 enum { tAGO = 260, tDAY, tDAYZONE, tAMPM, tMONTH, tMONTH_UNIT, tSEC_UNIT, 57 tUNUMBER, tZONE, tDST }; 58 struct token { int token; time_t value; }; 59 60 /* 61 * Parser state. 62 */ 63 struct gdstate { 64 struct token *tokenp; /* Pointer to next token. */ 65 /* HaveXxxx counts how many of this kind of phrase we've seen; 66 * it's a fatal error to have more than one time, zone, day, 67 * or date phrase. */ 68 int HaveYear; 69 int HaveMonth; 70 int HaveDay; 71 int HaveWeekDay; /* Day of week */ 72 int HaveTime; /* Hour/minute/second */ 73 int HaveZone; /* timezone and/or DST info */ 74 int HaveRel; /* time offset; we can have more than one */ 75 /* Absolute time values. */ 76 time_t Timezone; /* Seconds offset from GMT */ 77 time_t Day; 78 time_t Hour; 79 time_t Minutes; 80 time_t Month; 81 time_t Seconds; 82 time_t Year; 83 /* DST selection */ 84 enum DSTMODE DSTmode; 85 /* Day of week accounting, e.g., "3rd Tuesday" */ 86 time_t DayOrdinal; /* "3" in "3rd Tuesday" */ 87 time_t DayNumber; /* "Tuesday" in "3rd Tuesday" */ 88 /* Relative time values: hour/day/week offsets are measured in 89 * seconds, month/year are counted in months. */ 90 time_t RelMonth; 91 time_t RelSeconds; 92 }; 93 94 /* 95 * A series of functions that recognize certain common time phrases. 96 * Each function returns 1 if it managed to make sense of some of the 97 * tokens, zero otherwise. 98 */ 99 100 /* 101 * hour:minute or hour:minute:second with optional AM, PM, or numeric 102 * timezone offset 103 */ 104 static int 105 timephrase(struct gdstate *gds) 106 { 107 if (gds->tokenp[0].token == tUNUMBER 108 && gds->tokenp[1].token == ':' 109 && gds->tokenp[2].token == tUNUMBER 110 && gds->tokenp[3].token == ':' 111 && gds->tokenp[4].token == tUNUMBER) { 112 /* "12:14:18" or "22:08:07" */ 113 ++gds->HaveTime; 114 gds->Hour = gds->tokenp[0].value; 115 gds->Minutes = gds->tokenp[2].value; 116 gds->Seconds = gds->tokenp[4].value; 117 gds->tokenp += 5; 118 } 119 else if (gds->tokenp[0].token == tUNUMBER 120 && gds->tokenp[1].token == ':' 121 && gds->tokenp[2].token == tUNUMBER) { 122 /* "12:14" or "22:08" */ 123 ++gds->HaveTime; 124 gds->Hour = gds->tokenp[0].value; 125 gds->Minutes = gds->tokenp[2].value; 126 gds->Seconds = 0; 127 gds->tokenp += 3; 128 } 129 else if (gds->tokenp[0].token == tUNUMBER 130 && gds->tokenp[1].token == tAMPM) { 131 /* "7" is a time if it's followed by "am" or "pm" */ 132 ++gds->HaveTime; 133 gds->Hour = gds->tokenp[0].value; 134 gds->Minutes = gds->Seconds = 0; 135 /* We'll handle the AM/PM below. */ 136 gds->tokenp += 1; 137 } else { 138 /* We can't handle this. */ 139 return 0; 140 } 141 142 if (gds->tokenp[0].token == tAMPM) { 143 /* "7:12pm", "12:20:13am" */ 144 if (gds->Hour == 12) 145 gds->Hour = 0; 146 if (gds->tokenp[0].value == tPM) 147 gds->Hour += 12; 148 gds->tokenp += 1; 149 } 150 if (gds->tokenp[0].token == '+' 151 && gds->tokenp[1].token == tUNUMBER) { 152 /* "7:14+0700" */ 153 gds->HaveZone++; 154 gds->DSTmode = DSToff; 155 gds->Timezone = - ((gds->tokenp[1].value / 100) * HOUR 156 + (gds->tokenp[1].value % 100) * MINUTE); 157 gds->tokenp += 2; 158 } 159 if (gds->tokenp[0].token == '-' 160 && gds->tokenp[1].token == tUNUMBER) { 161 /* "19:14:12-0530" */ 162 gds->HaveZone++; 163 gds->DSTmode = DSToff; 164 gds->Timezone = + ((gds->tokenp[1].value / 100) * HOUR 165 + (gds->tokenp[1].value % 100) * MINUTE); 166 gds->tokenp += 2; 167 } 168 return 1; 169 } 170 171 /* 172 * Timezone name, possibly including DST. 173 */ 174 static int 175 zonephrase(struct gdstate *gds) 176 { 177 if (gds->tokenp[0].token == tZONE 178 && gds->tokenp[1].token == tDST) { 179 gds->HaveZone++; 180 gds->Timezone = gds->tokenp[0].value; 181 gds->DSTmode = DSTon; 182 gds->tokenp += 1; 183 return 1; 184 } 185 186 if (gds->tokenp[0].token == tZONE) { 187 gds->HaveZone++; 188 gds->Timezone = gds->tokenp[0].value; 189 gds->DSTmode = DSToff; 190 gds->tokenp += 1; 191 return 1; 192 } 193 194 if (gds->tokenp[0].token == tDAYZONE) { 195 gds->HaveZone++; 196 gds->Timezone = gds->tokenp[0].value; 197 gds->DSTmode = DSTon; 198 gds->tokenp += 1; 199 return 1; 200 } 201 return 0; 202 } 203 204 /* 205 * Year/month/day in various combinations. 206 */ 207 static int 208 datephrase(struct gdstate *gds) 209 { 210 if (gds->tokenp[0].token == tUNUMBER 211 && gds->tokenp[1].token == '/' 212 && gds->tokenp[2].token == tUNUMBER 213 && gds->tokenp[3].token == '/' 214 && gds->tokenp[4].token == tUNUMBER) { 215 gds->HaveYear++; 216 gds->HaveMonth++; 217 gds->HaveDay++; 218 if (gds->tokenp[0].value >= 13) { 219 /* First number is big: 2004/01/29, 99/02/17 */ 220 gds->Year = gds->tokenp[0].value; 221 gds->Month = gds->tokenp[2].value; 222 gds->Day = gds->tokenp[4].value; 223 } else if ((gds->tokenp[4].value >= 13) 224 || (gds->tokenp[2].value >= 13)) { 225 /* Last number is big: 01/07/98 */ 226 /* Middle number is big: 01/29/04 */ 227 gds->Month = gds->tokenp[0].value; 228 gds->Day = gds->tokenp[2].value; 229 gds->Year = gds->tokenp[4].value; 230 } else { 231 /* No significant clues: 02/03/04 */ 232 gds->Month = gds->tokenp[0].value; 233 gds->Day = gds->tokenp[2].value; 234 gds->Year = gds->tokenp[4].value; 235 } 236 gds->tokenp += 5; 237 return 1; 238 } 239 240 if (gds->tokenp[0].token == tUNUMBER 241 && gds->tokenp[1].token == '/' 242 && gds->tokenp[2].token == tUNUMBER) { 243 /* "1/15" */ 244 gds->HaveMonth++; 245 gds->HaveDay++; 246 gds->Month = gds->tokenp[0].value; 247 gds->Day = gds->tokenp[2].value; 248 gds->tokenp += 3; 249 return 1; 250 } 251 252 if (gds->tokenp[0].token == tUNUMBER 253 && gds->tokenp[1].token == '-' 254 && gds->tokenp[2].token == tUNUMBER 255 && gds->tokenp[3].token == '-' 256 && gds->tokenp[4].token == tUNUMBER) { 257 /* ISO 8601 format. yyyy-mm-dd. */ 258 gds->HaveYear++; 259 gds->HaveMonth++; 260 gds->HaveDay++; 261 gds->Year = gds->tokenp[0].value; 262 gds->Month = gds->tokenp[2].value; 263 gds->Day = gds->tokenp[4].value; 264 gds->tokenp += 5; 265 return 1; 266 } 267 268 if (gds->tokenp[0].token == tUNUMBER 269 && gds->tokenp[1].token == '-' 270 && gds->tokenp[2].token == tMONTH 271 && gds->tokenp[3].token == '-' 272 && gds->tokenp[4].token == tUNUMBER) { 273 gds->HaveYear++; 274 gds->HaveMonth++; 275 gds->HaveDay++; 276 if (gds->tokenp[0].value > 31) { 277 /* e.g. 1992-Jun-17 */ 278 gds->Year = gds->tokenp[0].value; 279 gds->Month = gds->tokenp[2].value; 280 gds->Day = gds->tokenp[4].value; 281 } else { 282 /* e.g. 17-JUN-1992. */ 283 gds->Day = gds->tokenp[0].value; 284 gds->Month = gds->tokenp[2].value; 285 gds->Year = gds->tokenp[4].value; 286 } 287 gds->tokenp += 5; 288 return 1; 289 } 290 291 if (gds->tokenp[0].token == tMONTH 292 && gds->tokenp[1].token == tUNUMBER 293 && gds->tokenp[2].token == ',' 294 && gds->tokenp[3].token == tUNUMBER) { 295 /* "June 17, 2001" */ 296 gds->HaveYear++; 297 gds->HaveMonth++; 298 gds->HaveDay++; 299 gds->Month = gds->tokenp[0].value; 300 gds->Day = gds->tokenp[1].value; 301 gds->Year = gds->tokenp[3].value; 302 gds->tokenp += 4; 303 return 1; 304 } 305 306 if (gds->tokenp[0].token == tMONTH 307 && gds->tokenp[1].token == tUNUMBER) { 308 /* "May 3" */ 309 gds->HaveMonth++; 310 gds->HaveDay++; 311 gds->Month = gds->tokenp[0].value; 312 gds->Day = gds->tokenp[1].value; 313 gds->tokenp += 2; 314 return 1; 315 } 316 317 if (gds->tokenp[0].token == tUNUMBER 318 && gds->tokenp[1].token == tMONTH 319 && gds->tokenp[2].token == tUNUMBER) { 320 /* "12 Sept 1997" */ 321 gds->HaveYear++; 322 gds->HaveMonth++; 323 gds->HaveDay++; 324 gds->Day = gds->tokenp[0].value; 325 gds->Month = gds->tokenp[1].value; 326 gds->Year = gds->tokenp[2].value; 327 gds->tokenp += 3; 328 return 1; 329 } 330 331 if (gds->tokenp[0].token == tUNUMBER 332 && gds->tokenp[1].token == tMONTH) { 333 /* "12 Sept" */ 334 gds->HaveMonth++; 335 gds->HaveDay++; 336 gds->Day = gds->tokenp[0].value; 337 gds->Month = gds->tokenp[1].value; 338 gds->tokenp += 2; 339 return 1; 340 } 341 342 return 0; 343 } 344 345 /* 346 * Relative time phrase: "tomorrow", "yesterday", "+1 hour", etc. 347 */ 348 static int 349 relunitphrase(struct gdstate *gds) 350 { 351 if (gds->tokenp[0].token == '-' 352 && gds->tokenp[1].token == tUNUMBER 353 && gds->tokenp[2].token == tSEC_UNIT) { 354 /* "-3 hours" */ 355 gds->HaveRel++; 356 gds->RelSeconds -= gds->tokenp[1].value * gds->tokenp[2].value; 357 gds->tokenp += 3; 358 return 1; 359 } 360 if (gds->tokenp[0].token == '+' 361 && gds->tokenp[1].token == tUNUMBER 362 && gds->tokenp[2].token == tSEC_UNIT) { 363 /* "+1 minute" */ 364 gds->HaveRel++; 365 gds->RelSeconds += gds->tokenp[1].value * gds->tokenp[2].value; 366 gds->tokenp += 3; 367 return 1; 368 } 369 if (gds->tokenp[0].token == tUNUMBER 370 && gds->tokenp[1].token == tSEC_UNIT) { 371 /* "1 day" */ 372 gds->HaveRel++; 373 gds->RelSeconds += gds->tokenp[0].value * gds->tokenp[1].value; 374 gds->tokenp += 2; 375 return 1; 376 } 377 if (gds->tokenp[0].token == '-' 378 && gds->tokenp[1].token == tUNUMBER 379 && gds->tokenp[2].token == tMONTH_UNIT) { 380 /* "-3 months" */ 381 gds->HaveRel++; 382 gds->RelMonth -= gds->tokenp[1].value * gds->tokenp[2].value; 383 gds->tokenp += 3; 384 return 1; 385 } 386 if (gds->tokenp[0].token == '+' 387 && gds->tokenp[1].token == tUNUMBER 388 && gds->tokenp[2].token == tMONTH_UNIT) { 389 /* "+5 years" */ 390 gds->HaveRel++; 391 gds->RelMonth += gds->tokenp[1].value * gds->tokenp[2].value; 392 gds->tokenp += 3; 393 return 1; 394 } 395 if (gds->tokenp[0].token == tUNUMBER 396 && gds->tokenp[1].token == tMONTH_UNIT) { 397 /* "2 years" */ 398 gds->HaveRel++; 399 gds->RelMonth += gds->tokenp[0].value * gds->tokenp[1].value; 400 gds->tokenp += 2; 401 return 1; 402 } 403 if (gds->tokenp[0].token == tSEC_UNIT) { 404 /* "now", "tomorrow" */ 405 gds->HaveRel++; 406 gds->RelSeconds += gds->tokenp[0].value; 407 gds->tokenp += 1; 408 return 1; 409 } 410 if (gds->tokenp[0].token == tMONTH_UNIT) { 411 /* "month" */ 412 gds->HaveRel++; 413 gds->RelMonth += gds->tokenp[0].value; 414 gds->tokenp += 1; 415 return 1; 416 } 417 return 0; 418 } 419 420 /* 421 * Day of the week specification. 422 */ 423 static int 424 dayphrase(struct gdstate *gds) 425 { 426 if (gds->tokenp[0].token == tDAY) { 427 /* "tues", "wednesday," */ 428 gds->HaveWeekDay++; 429 gds->DayOrdinal = 1; 430 gds->DayNumber = gds->tokenp[0].value; 431 gds->tokenp += 1; 432 if (gds->tokenp[0].token == ',') 433 gds->tokenp += 1; 434 return 1; 435 } 436 if (gds->tokenp[0].token == tUNUMBER 437 && gds->tokenp[1].token == tDAY) { 438 /* "second tues" "3 wed" */ 439 gds->HaveWeekDay++; 440 gds->DayOrdinal = gds->tokenp[0].value; 441 gds->DayNumber = gds->tokenp[1].value; 442 gds->tokenp += 2; 443 return 1; 444 } 445 return 0; 446 } 447 448 /* 449 * Try to match a phrase using one of the above functions. 450 * This layer also deals with a couple of generic issues. 451 */ 452 static int 453 phrase(struct gdstate *gds) 454 { 455 if (timephrase(gds)) 456 return 1; 457 if (zonephrase(gds)) 458 return 1; 459 if (datephrase(gds)) 460 return 1; 461 if (dayphrase(gds)) 462 return 1; 463 if (relunitphrase(gds)) { 464 if (gds->tokenp[0].token == tAGO) { 465 gds->RelSeconds = -gds->RelSeconds; 466 gds->RelMonth = -gds->RelMonth; 467 gds->tokenp += 1; 468 } 469 return 1; 470 } 471 472 /* Bare numbers sometimes have meaning. */ 473 if (gds->tokenp[0].token == tUNUMBER) { 474 if (gds->HaveTime && !gds->HaveYear && !gds->HaveRel) { 475 gds->HaveYear++; 476 gds->Year = gds->tokenp[0].value; 477 gds->tokenp += 1; 478 return 1; 479 } 480 481 if(gds->tokenp[0].value > 10000) { 482 /* "20040301" */ 483 gds->HaveYear++; 484 gds->HaveMonth++; 485 gds->HaveDay++; 486 gds->Day= (gds->tokenp[0].value)%100; 487 gds->Month= (gds->tokenp[0].value/100)%100; 488 gds->Year = gds->tokenp[0].value/10000; 489 gds->tokenp += 1; 490 return 1; 491 } 492 493 if (gds->tokenp[0].value < 24) { 494 gds->HaveTime++; 495 gds->Hour = gds->tokenp[0].value; 496 gds->Minutes = 0; 497 gds->Seconds = 0; 498 gds->tokenp += 1; 499 return 1; 500 } 501 502 if ((gds->tokenp[0].value / 100 < 24) 503 && (gds->tokenp[0].value % 100 < 60)) { 504 /* "513" is same as "5:13" */ 505 gds->Hour = gds->tokenp[0].value / 100; 506 gds->Minutes = gds->tokenp[0].value % 100; 507 gds->Seconds = 0; 508 gds->tokenp += 1; 509 return 1; 510 } 511 } 512 513 return 0; 514 } 515 516 /* 517 * A dictionary of time words. 518 */ 519 static struct LEXICON { 520 size_t abbrev; 521 const char *name; 522 int type; 523 time_t value; 524 } const TimeWords[] = { 525 /* am/pm */ 526 { 0, "am", tAMPM, tAM }, 527 { 0, "pm", tAMPM, tPM }, 528 529 /* Month names. */ 530 { 3, "january", tMONTH, 1 }, 531 { 3, "february", tMONTH, 2 }, 532 { 3, "march", tMONTH, 3 }, 533 { 3, "april", tMONTH, 4 }, 534 { 3, "may", tMONTH, 5 }, 535 { 3, "june", tMONTH, 6 }, 536 { 3, "july", tMONTH, 7 }, 537 { 3, "august", tMONTH, 8 }, 538 { 3, "september", tMONTH, 9 }, 539 { 3, "october", tMONTH, 10 }, 540 { 3, "november", tMONTH, 11 }, 541 { 3, "december", tMONTH, 12 }, 542 543 /* Days of the week. */ 544 { 2, "sunday", tDAY, 0 }, 545 { 3, "monday", tDAY, 1 }, 546 { 2, "tuesday", tDAY, 2 }, 547 { 3, "wednesday", tDAY, 3 }, 548 { 2, "thursday", tDAY, 4 }, 549 { 2, "friday", tDAY, 5 }, 550 { 2, "saturday", tDAY, 6 }, 551 552 /* Timezones: Offsets are in seconds. */ 553 { 0, "gmt", tZONE, 0*HOUR }, /* Greenwich Mean */ 554 { 0, "ut", tZONE, 0*HOUR }, /* Universal (Coordinated) */ 555 { 0, "utc", tZONE, 0*HOUR }, 556 { 0, "wet", tZONE, 0*HOUR }, /* Western European */ 557 { 0, "bst", tDAYZONE, 0*HOUR }, /* British Summer */ 558 { 0, "wat", tZONE, 1*HOUR }, /* West Africa */ 559 { 0, "at", tZONE, 2*HOUR }, /* Azores */ 560 /* { 0, "bst", tZONE, 3*HOUR }, */ /* Brazil Standard: Conflict */ 561 /* { 0, "gst", tZONE, 3*HOUR }, */ /* Greenland Standard: Conflict*/ 562 { 0, "nft", tZONE, 3*HOUR+30*MINUTE }, /* Newfoundland */ 563 { 0, "nst", tZONE, 3*HOUR+30*MINUTE }, /* Newfoundland Standard */ 564 { 0, "ndt", tDAYZONE, 3*HOUR+30*MINUTE }, /* Newfoundland Daylight */ 565 { 0, "ast", tZONE, 4*HOUR }, /* Atlantic Standard */ 566 { 0, "adt", tDAYZONE, 4*HOUR }, /* Atlantic Daylight */ 567 { 0, "est", tZONE, 5*HOUR }, /* Eastern Standard */ 568 { 0, "edt", tDAYZONE, 5*HOUR }, /* Eastern Daylight */ 569 { 0, "cst", tZONE, 6*HOUR }, /* Central Standard */ 570 { 0, "cdt", tDAYZONE, 6*HOUR }, /* Central Daylight */ 571 { 0, "mst", tZONE, 7*HOUR }, /* Mountain Standard */ 572 { 0, "mdt", tDAYZONE, 7*HOUR }, /* Mountain Daylight */ 573 { 0, "pst", tZONE, 8*HOUR }, /* Pacific Standard */ 574 { 0, "pdt", tDAYZONE, 8*HOUR }, /* Pacific Daylight */ 575 { 0, "yst", tZONE, 9*HOUR }, /* Yukon Standard */ 576 { 0, "ydt", tDAYZONE, 9*HOUR }, /* Yukon Daylight */ 577 { 0, "hst", tZONE, 10*HOUR }, /* Hawaii Standard */ 578 { 0, "hdt", tDAYZONE, 10*HOUR }, /* Hawaii Daylight */ 579 { 0, "cat", tZONE, 10*HOUR }, /* Central Alaska */ 580 { 0, "ahst", tZONE, 10*HOUR }, /* Alaska-Hawaii Standard */ 581 { 0, "nt", tZONE, 11*HOUR }, /* Nome */ 582 { 0, "idlw", tZONE, 12*HOUR }, /* Intl Date Line West */ 583 { 0, "cet", tZONE, -1*HOUR }, /* Central European */ 584 { 0, "met", tZONE, -1*HOUR }, /* Middle European */ 585 { 0, "mewt", tZONE, -1*HOUR }, /* Middle European Winter */ 586 { 0, "mest", tDAYZONE, -1*HOUR }, /* Middle European Summer */ 587 { 0, "swt", tZONE, -1*HOUR }, /* Swedish Winter */ 588 { 0, "sst", tDAYZONE, -1*HOUR }, /* Swedish Summer */ 589 { 0, "fwt", tZONE, -1*HOUR }, /* French Winter */ 590 { 0, "fst", tDAYZONE, -1*HOUR }, /* French Summer */ 591 { 0, "eet", tZONE, -2*HOUR }, /* Eastern Eur, USSR Zone 1 */ 592 { 0, "bt", tZONE, -3*HOUR }, /* Baghdad, USSR Zone 2 */ 593 { 0, "it", tZONE, -3*HOUR-30*MINUTE },/* Iran */ 594 { 0, "zp4", tZONE, -4*HOUR }, /* USSR Zone 3 */ 595 { 0, "zp5", tZONE, -5*HOUR }, /* USSR Zone 4 */ 596 { 0, "ist", tZONE, -5*HOUR-30*MINUTE },/* Indian Standard */ 597 { 0, "zp6", tZONE, -6*HOUR }, /* USSR Zone 5 */ 598 /* { 0, "nst", tZONE, -6.5*HOUR }, */ /* North Sumatra: Conflict */ 599 /* { 0, "sst", tZONE, -7*HOUR }, */ /* So Sumatra, USSR 6: Conflict */ 600 { 0, "wast", tZONE, -7*HOUR }, /* West Australian Standard */ 601 { 0, "wadt", tDAYZONE, -7*HOUR }, /* West Australian Daylight */ 602 { 0, "jt", tZONE, -7*HOUR-30*MINUTE },/* Java (3pm in Cronusland!)*/ 603 { 0, "cct", tZONE, -8*HOUR }, /* China Coast, USSR Zone 7 */ 604 { 0, "jst", tZONE, -9*HOUR }, /* Japan Std, USSR Zone 8 */ 605 { 0, "cast", tZONE, -9*HOUR-30*MINUTE },/* Ctrl Australian Std */ 606 { 0, "cadt", tDAYZONE, -9*HOUR-30*MINUTE },/* Ctrl Australian Daylt */ 607 { 0, "east", tZONE, -10*HOUR }, /* Eastern Australian Std */ 608 { 0, "eadt", tDAYZONE, -10*HOUR }, /* Eastern Australian Daylt */ 609 { 0, "gst", tZONE, -10*HOUR }, /* Guam Std, USSR Zone 9 */ 610 { 0, "nzt", tZONE, -12*HOUR }, /* New Zealand */ 611 { 0, "nzst", tZONE, -12*HOUR }, /* New Zealand Standard */ 612 { 0, "nzdt", tDAYZONE, -12*HOUR }, /* New Zealand Daylight */ 613 { 0, "idle", tZONE, -12*HOUR }, /* Intl Date Line East */ 614 615 { 0, "dst", tDST, 0 }, 616 617 /* Time units. */ 618 { 4, "years", tMONTH_UNIT, 12 }, 619 { 5, "months", tMONTH_UNIT, 1 }, 620 { 9, "fortnights", tSEC_UNIT, 14 * DAY }, 621 { 4, "weeks", tSEC_UNIT, 7 * DAY }, 622 { 3, "days", tSEC_UNIT, DAY }, 623 { 4, "hours", tSEC_UNIT, HOUR }, 624 { 3, "minutes", tSEC_UNIT, MINUTE }, 625 { 3, "seconds", tSEC_UNIT, 1 }, 626 627 /* Relative-time words. */ 628 { 0, "tomorrow", tSEC_UNIT, DAY }, 629 { 0, "yesterday", tSEC_UNIT, -DAY }, 630 { 0, "today", tSEC_UNIT, 0 }, 631 { 0, "now", tSEC_UNIT, 0 }, 632 { 0, "last", tUNUMBER, -1 }, 633 { 0, "this", tSEC_UNIT, 0 }, 634 { 0, "next", tUNUMBER, 2 }, 635 { 0, "first", tUNUMBER, 1 }, 636 { 0, "1st", tUNUMBER, 1 }, 637 /* { 0, "second", tUNUMBER, 2 }, */ 638 { 0, "2nd", tUNUMBER, 2 }, 639 { 0, "third", tUNUMBER, 3 }, 640 { 0, "3rd", tUNUMBER, 3 }, 641 { 0, "fourth", tUNUMBER, 4 }, 642 { 0, "4th", tUNUMBER, 4 }, 643 { 0, "fifth", tUNUMBER, 5 }, 644 { 0, "5th", tUNUMBER, 5 }, 645 { 0, "sixth", tUNUMBER, 6 }, 646 { 0, "seventh", tUNUMBER, 7 }, 647 { 0, "eighth", tUNUMBER, 8 }, 648 { 0, "ninth", tUNUMBER, 9 }, 649 { 0, "tenth", tUNUMBER, 10 }, 650 { 0, "eleventh", tUNUMBER, 11 }, 651 { 0, "twelfth", tUNUMBER, 12 }, 652 { 0, "ago", tAGO, 1 }, 653 654 /* Military timezones. */ 655 { 0, "a", tZONE, 1*HOUR }, 656 { 0, "b", tZONE, 2*HOUR }, 657 { 0, "c", tZONE, 3*HOUR }, 658 { 0, "d", tZONE, 4*HOUR }, 659 { 0, "e", tZONE, 5*HOUR }, 660 { 0, "f", tZONE, 6*HOUR }, 661 { 0, "g", tZONE, 7*HOUR }, 662 { 0, "h", tZONE, 8*HOUR }, 663 { 0, "i", tZONE, 9*HOUR }, 664 { 0, "k", tZONE, 10*HOUR }, 665 { 0, "l", tZONE, 11*HOUR }, 666 { 0, "m", tZONE, 12*HOUR }, 667 { 0, "n", tZONE, -1*HOUR }, 668 { 0, "o", tZONE, -2*HOUR }, 669 { 0, "p", tZONE, -3*HOUR }, 670 { 0, "q", tZONE, -4*HOUR }, 671 { 0, "r", tZONE, -5*HOUR }, 672 { 0, "s", tZONE, -6*HOUR }, 673 { 0, "t", tZONE, -7*HOUR }, 674 { 0, "u", tZONE, -8*HOUR }, 675 { 0, "v", tZONE, -9*HOUR }, 676 { 0, "w", tZONE, -10*HOUR }, 677 { 0, "x", tZONE, -11*HOUR }, 678 { 0, "y", tZONE, -12*HOUR }, 679 { 0, "z", tZONE, 0*HOUR }, 680 681 /* End of table. */ 682 { 0, NULL, 0, 0 } 683 }; 684 685 /* 686 * Year is either: 687 * = A number from 0 to 99, which means a year from 1970 to 2069, or 688 * = The actual year (>=100). 689 */ 690 static time_t 691 Convert(time_t Month, time_t Day, time_t Year, 692 time_t Hours, time_t Minutes, time_t Seconds, 693 time_t Timezone, enum DSTMODE DSTmode) 694 { 695 signed char DaysInMonth[12] = { 696 31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 697 }; 698 time_t Julian; 699 int i; 700 struct tm *ltime; 701 #if defined(HAVE_LOCALTIME_R) || defined(HAVE_LOCALTIME_S) 702 struct tm tmbuf; 703 #endif 704 705 if (Year < 69) 706 Year += 2000; 707 else if (Year < 100) 708 Year += 1900; 709 DaysInMonth[1] = Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0) 710 ? 29 : 28; 711 /* Checking for 2038 bogusly assumes that time_t is 32 bits. But 712 I'm too lazy to try to check for time_t overflow in another way. */ 713 if (Year < EPOCH || Year >= 2038 714 || Month < 1 || Month > 12 715 /* Lint fluff: "conversion from long may lose accuracy" */ 716 || Day < 1 || Day > DaysInMonth[(int)--Month] 717 || Hours < 0 || Hours > 23 718 || Minutes < 0 || Minutes > 59 719 || Seconds < 0 || Seconds > 59) 720 return -1; 721 722 Julian = Day - 1; 723 for (i = 0; i < Month; i++) 724 Julian += DaysInMonth[i]; 725 for (i = EPOCH; i < Year; i++) 726 Julian += 365 + (i % 4 == 0); 727 Julian *= DAY; 728 Julian += Timezone; 729 Julian += Hours * HOUR + Minutes * MINUTE + Seconds; 730 #if defined(HAVE_LOCALTIME_S) 731 ltime = localtime_s(&tmbuf, &Julian) ? NULL : &tmbuf; 732 #elif defined(HAVE_LOCALTIME_R) 733 ltime = localtime_r(&Julian, &tmbuf); 734 #else 735 ltime = localtime(&Julian); 736 #endif 737 if (DSTmode == DSTon 738 || (DSTmode == DSTmaybe && ltime->tm_isdst)) 739 Julian -= HOUR; 740 return Julian; 741 } 742 743 static time_t 744 DSTcorrect(time_t Start, time_t Future) 745 { 746 time_t StartDay; 747 time_t FutureDay; 748 struct tm *ltime; 749 #if defined(HAVE_LOCALTIME_R) || defined(HAVE_LOCALTIME_S) 750 struct tm tmbuf; 751 #endif 752 #if defined(HAVE_LOCALTIME_S) 753 ltime = localtime_s(&tmbuf, &Start) ? NULL : &tmbuf; 754 #elif defined(HAVE_LOCALTIME_R) 755 ltime = localtime_r(&Start, &tmbuf); 756 #else 757 ltime = localtime(&Start); 758 #endif 759 StartDay = (ltime->tm_hour + 1) % 24; 760 #if defined(HAVE_LOCALTIME_S) 761 ltime = localtime_s(&tmbuf, &Future) ? NULL : &tmbuf; 762 #elif defined(HAVE_LOCALTIME_R) 763 ltime = localtime_r(&Future, &tmbuf); 764 #else 765 ltime = localtime(&Future); 766 #endif 767 FutureDay = (ltime->tm_hour + 1) % 24; 768 return (Future - Start) + (StartDay - FutureDay) * HOUR; 769 } 770 771 772 static time_t 773 RelativeDate(time_t Start, time_t zone, int dstmode, 774 time_t DayOrdinal, time_t DayNumber) 775 { 776 struct tm *tm; 777 time_t t, now; 778 #if defined(HAVE_GMTIME_R) || defined(HAVE_GMTIME_S) 779 struct tm tmbuf; 780 #endif 781 782 t = Start - zone; 783 #if defined(HAVE_GMTIME_S) 784 tm = gmtime_s(&tmbuf, &t) ? NULL : &tmbuf; 785 #elif defined(HAVE_GMTIME_R) 786 tm = gmtime_r(&t, &tmbuf); 787 #else 788 tm = gmtime(&t); 789 #endif 790 now = Start; 791 now += DAY * ((DayNumber - tm->tm_wday + 7) % 7); 792 now += 7 * DAY * (DayOrdinal <= 0 ? DayOrdinal : DayOrdinal - 1); 793 if (dstmode == DSTmaybe) 794 return DSTcorrect(Start, now); 795 return now - Start; 796 } 797 798 799 static time_t 800 RelativeMonth(time_t Start, time_t Timezone, time_t RelMonth) 801 { 802 struct tm *tm; 803 time_t Month; 804 time_t Year; 805 #if defined(HAVE_LOCALTIME_R) || defined(HAVE_LOCALTIME_S) 806 struct tm tmbuf; 807 #endif 808 809 if (RelMonth == 0) 810 return 0; 811 #if defined(HAVE_LOCALTIME_S) 812 tm = localtime_s(&tmbuf, &Start) ? NULL : &tmbuf; 813 #elif defined(HAVE_LOCALTIME_R) 814 tm = localtime_r(&Start, &tmbuf); 815 #else 816 tm = localtime(&Start); 817 #endif 818 Month = 12 * (tm->tm_year + 1900) + tm->tm_mon + RelMonth; 819 Year = Month / 12; 820 Month = Month % 12 + 1; 821 return DSTcorrect(Start, 822 Convert(Month, (time_t)tm->tm_mday, Year, 823 (time_t)tm->tm_hour, (time_t)tm->tm_min, (time_t)tm->tm_sec, 824 Timezone, DSTmaybe)); 825 } 826 827 /* 828 * Tokenizer. 829 */ 830 static int 831 nexttoken(const char **in, time_t *value) 832 { 833 char c; 834 char buff[64]; 835 836 for ( ; ; ) { 837 while (isspace((unsigned char)**in)) 838 ++*in; 839 840 /* Skip parenthesized comments. */ 841 if (**in == '(') { 842 int Count = 0; 843 do { 844 c = *(*in)++; 845 if (c == '\0') 846 return c; 847 if (c == '(') 848 Count++; 849 else if (c == ')') 850 Count--; 851 } while (Count > 0); 852 continue; 853 } 854 855 /* Try the next token in the word table first. */ 856 /* This allows us to match "2nd", for example. */ 857 { 858 const char *src = *in; 859 const struct LEXICON *tp; 860 unsigned i = 0; 861 862 /* Force to lowercase and strip '.' characters. */ 863 while (*src != '\0' 864 && (isalnum((unsigned char)*src) || *src == '.') 865 && i < sizeof(buff)-1) { 866 if (*src != '.') { 867 if (isupper((unsigned char)*src)) 868 buff[i++] = tolower((unsigned char)*src); 869 else 870 buff[i++] = *src; 871 } 872 src++; 873 } 874 buff[i] = '\0'; 875 876 /* 877 * Find the first match. If the word can be 878 * abbreviated, make sure we match at least 879 * the minimum abbreviation. 880 */ 881 for (tp = TimeWords; tp->name; tp++) { 882 size_t abbrev = tp->abbrev; 883 if (abbrev == 0) 884 abbrev = strlen(tp->name); 885 if (strlen(buff) >= abbrev 886 && strncmp(tp->name, buff, strlen(buff)) 887 == 0) { 888 /* Skip over token. */ 889 *in = src; 890 /* Return the match. */ 891 *value = tp->value; 892 return tp->type; 893 } 894 } 895 } 896 897 /* 898 * Not in the word table, maybe it's a number. Note: 899 * Because '-' and '+' have other special meanings, I 900 * don't deal with signed numbers here. 901 */ 902 if (isdigit((unsigned char)(c = **in))) { 903 for (*value = 0; isdigit((unsigned char)(c = *(*in)++)); ) 904 *value = 10 * *value + c - '0'; 905 (*in)--; 906 return (tUNUMBER); 907 } 908 909 return *(*in)++; 910 } 911 } 912 913 #define TM_YEAR_ORIGIN 1900 914 915 /* Yield A - B, measured in seconds. */ 916 static long 917 difftm (struct tm *a, struct tm *b) 918 { 919 int ay = a->tm_year + (TM_YEAR_ORIGIN - 1); 920 int by = b->tm_year + (TM_YEAR_ORIGIN - 1); 921 int days = ( 922 /* difference in day of year */ 923 a->tm_yday - b->tm_yday 924 /* + intervening leap days */ 925 + ((ay >> 2) - (by >> 2)) 926 - (ay/100 - by/100) 927 + ((ay/100 >> 2) - (by/100 >> 2)) 928 /* + difference in years * 365 */ 929 + (long)(ay-by) * 365 930 ); 931 return (days * DAY + (a->tm_hour - b->tm_hour) * HOUR 932 + (a->tm_min - b->tm_min) * MINUTE 933 + (a->tm_sec - b->tm_sec)); 934 } 935 936 /* 937 * 938 * The public function. 939 * 940 * TODO: tokens[] array should be dynamically sized. 941 */ 942 time_t 943 __archive_get_date(time_t now, const char *p) 944 { 945 struct token tokens[256]; 946 struct gdstate _gds; 947 struct token *lasttoken; 948 struct gdstate *gds; 949 struct tm local, *tm; 950 struct tm gmt, *gmt_ptr; 951 time_t Start; 952 time_t tod; 953 long tzone; 954 955 /* Clear out the parsed token array. */ 956 memset(tokens, 0, sizeof(tokens)); 957 /* Initialize the parser state. */ 958 memset(&_gds, 0, sizeof(_gds)); 959 gds = &_gds; 960 961 /* Look up the current time. */ 962 #if defined(HAVE_LOCALTIME_S) 963 tm = localtime_s(&local, &now) ? NULL : &local; 964 #elif defined(HAVE_LOCALTIME_R) 965 tm = localtime_r(&now, &local); 966 #else 967 memset(&local, 0, sizeof(local)); 968 tm = localtime(&now); 969 #endif 970 if (tm == NULL) 971 return -1; 972 #if !defined(HAVE_LOCALTIME_R) && !defined(HAVE_LOCALTIME_S) 973 local = *tm; 974 #endif 975 976 /* Look up UTC if we can and use that to determine the current 977 * timezone offset. */ 978 #if defined(HAVE_GMTIME_S) 979 gmt_ptr = gmtime_s(&gmt, &now) ? NULL : &gmt; 980 #elif defined(HAVE_GMTIME_R) 981 gmt_ptr = gmtime_r(&now, &gmt); 982 #else 983 memset(&gmt, 0, sizeof(gmt)); 984 gmt_ptr = gmtime(&now); 985 if (gmt_ptr != NULL) { 986 /* Copy, in case localtime and gmtime use the same buffer. */ 987 gmt = *gmt_ptr; 988 } 989 #endif 990 if (gmt_ptr != NULL) 991 tzone = difftm (&gmt, &local); 992 else 993 /* This system doesn't understand timezones; fake it. */ 994 tzone = 0; 995 if(local.tm_isdst) 996 tzone += HOUR; 997 998 /* Tokenize the input string. */ 999 lasttoken = tokens; 1000 while ((lasttoken->token = nexttoken(&p, &lasttoken->value)) != 0) { 1001 ++lasttoken; 1002 if (lasttoken > tokens + 255) 1003 return -1; 1004 } 1005 gds->tokenp = tokens; 1006 1007 /* Match phrases until we run out of input tokens. */ 1008 while (gds->tokenp < lasttoken) { 1009 if (!phrase(gds)) 1010 return -1; 1011 } 1012 1013 /* Use current local timezone if none was specified. */ 1014 if (!gds->HaveZone) { 1015 gds->Timezone = tzone; 1016 gds->DSTmode = DSTmaybe; 1017 } 1018 1019 /* If a timezone was specified, use that for generating the default 1020 * time components instead of the local timezone. */ 1021 if (gds->HaveZone && gmt_ptr != NULL) { 1022 now -= gds->Timezone; 1023 #if defined(HAVE_GMTIME_S) 1024 gmt_ptr = gmtime_s(&gmt, &now) ? NULL : &gmt; 1025 #elif defined(HAVE_GMTIME_R) 1026 gmt_ptr = gmtime_r(&now, &gmt); 1027 #else 1028 gmt_ptr = gmtime(&now); 1029 #endif 1030 if (gmt_ptr != NULL) 1031 local = *gmt_ptr; 1032 now += gds->Timezone; 1033 } 1034 1035 if (!gds->HaveYear) 1036 gds->Year = local.tm_year + 1900; 1037 if (!gds->HaveMonth) 1038 gds->Month = local.tm_mon + 1; 1039 if (!gds->HaveDay) 1040 gds->Day = local.tm_mday; 1041 /* Note: No default for hour/min/sec; a specifier that just 1042 * gives date always refers to 00:00 on that date. */ 1043 1044 /* If we saw more than one time, timezone, weekday, year, month, 1045 * or day, then give up. */ 1046 if (gds->HaveTime > 1 || gds->HaveZone > 1 || gds->HaveWeekDay > 1 1047 || gds->HaveYear > 1 || gds->HaveMonth > 1 || gds->HaveDay > 1) 1048 return -1; 1049 1050 /* Compute an absolute time based on whatever absolute information 1051 * we collected. */ 1052 if (gds->HaveYear || gds->HaveMonth || gds->HaveDay 1053 || gds->HaveTime || gds->HaveWeekDay) { 1054 Start = Convert(gds->Month, gds->Day, gds->Year, 1055 gds->Hour, gds->Minutes, gds->Seconds, 1056 gds->Timezone, gds->DSTmode); 1057 if (Start < 0) 1058 return -1; 1059 } else { 1060 Start = now; 1061 if (!gds->HaveRel) 1062 Start -= local.tm_hour * HOUR + local.tm_min * MINUTE 1063 + local.tm_sec; 1064 } 1065 1066 /* Add the relative offset. */ 1067 Start += gds->RelSeconds; 1068 Start += RelativeMonth(Start, gds->Timezone, gds->RelMonth); 1069 1070 /* Adjust for day-of-week offsets. */ 1071 if (gds->HaveWeekDay 1072 && !(gds->HaveYear || gds->HaveMonth || gds->HaveDay)) { 1073 tod = RelativeDate(Start, gds->Timezone, 1074 gds->DSTmode, gds->DayOrdinal, gds->DayNumber); 1075 Start += tod; 1076 } 1077 1078 /* -1 is an error indicator, so return 0 instead of -1 if 1079 * that's the actual time. */ 1080 return Start == -1 ? 0 : Start; 1081 } 1082 1083 1084 #if defined(TEST) 1085 1086 /* ARGSUSED */ 1087 int 1088 main(int argc, char **argv) 1089 { 1090 time_t d; 1091 time_t now = time(NULL); 1092 1093 while (*++argv != NULL) { 1094 (void)printf("Input: %s\n", *argv); 1095 d = get_date(now, *argv); 1096 if (d == -1) 1097 (void)printf("Bad format - couldn't convert.\n"); 1098 else 1099 (void)printf("Output: %s\n", ctime(&d)); 1100 } 1101 exit(0); 1102 /* NOTREACHED */ 1103 } 1104 #endif /* defined(TEST) */ 1105