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