2 * This code is in the public domain and has no copyright.
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.
8 * Since the original authors were generous enough to donate their
9 * work to the public domain, I feel compelled to match their
12 * Tim Kientzle, February 2009.
16 * Header comment from original getdate.y:
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;
25 ** This grammar has 10 shift/reduce conflicts.
27 ** This code is in the public domain and has no copyright.
30 #include "archive_platform.h"
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
42 #define __LIBARCHIVE_BUILD 1
43 #include "archive_getdate.h"
45 /* Basic time units. */
48 #define HOUR (60L * MINUTE)
49 #define DAY (24L * HOUR)
51 /* Daylight-savings mode: on, off, or not yet known. */
52 enum DSTMODE { DSTon, DSToff, DSTmaybe };
53 /* Meridian: am or pm. */
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; };
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,
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 */
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. */
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.
101 * hour:minute or hour:minute:second with optional AM, PM, or numeric
105 timephrase(struct gdstate *gds)
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" */
114 gds->Hour = gds->tokenp[0].value;
115 gds->Minutes = gds->tokenp[2].value;
116 gds->Seconds = gds->tokenp[4].value;
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" */
124 gds->Hour = gds->tokenp[0].value;
125 gds->Minutes = gds->tokenp[2].value;
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" */
133 gds->Hour = gds->tokenp[0].value;
134 gds->Minutes = gds->Seconds = 0;
135 /* We'll handle the AM/PM below. */
138 /* We can't handle this. */
142 if (gds->tokenp[0].token == tAMPM) {
143 /* "7:12pm", "12:20:13am" */
146 if (gds->tokenp[0].value == tPM)
150 if (gds->tokenp[0].token == '+'
151 && gds->tokenp[1].token == tUNUMBER) {
154 gds->DSTmode = DSToff;
155 gds->Timezone = - ((gds->tokenp[1].value / 100) * HOUR
156 + (gds->tokenp[1].value % 100) * MINUTE);
159 if (gds->tokenp[0].token == '-'
160 && gds->tokenp[1].token == tUNUMBER) {
161 /* "19:14:12-0530" */
163 gds->DSTmode = DSToff;
164 gds->Timezone = + ((gds->tokenp[1].value / 100) * HOUR
165 + (gds->tokenp[1].value % 100) * MINUTE);
172 * Timezone name, possibly including DST.
175 zonephrase(struct gdstate *gds)
177 if (gds->tokenp[0].token == tZONE
178 && gds->tokenp[1].token == tDST) {
180 gds->Timezone = gds->tokenp[0].value;
181 gds->DSTmode = DSTon;
186 if (gds->tokenp[0].token == tZONE) {
188 gds->Timezone = gds->tokenp[0].value;
189 gds->DSTmode = DSToff;
194 if (gds->tokenp[0].token == tDAYZONE) {
196 gds->Timezone = gds->tokenp[0].value;
197 gds->DSTmode = DSTon;
205 * Year/month/day in various combinations.
208 datephrase(struct gdstate *gds)
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) {
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;
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;
240 if (gds->tokenp[0].token == tUNUMBER
241 && gds->tokenp[1].token == '/'
242 && gds->tokenp[2].token == tUNUMBER) {
246 gds->Month = gds->tokenp[0].value;
247 gds->Day = gds->tokenp[2].value;
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. */
261 gds->Year = gds->tokenp[0].value;
262 gds->Month = gds->tokenp[2].value;
263 gds->Day = gds->tokenp[4].value;
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) {
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;
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;
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" */
299 gds->Month = gds->tokenp[0].value;
300 gds->Day = gds->tokenp[1].value;
301 gds->Year = gds->tokenp[3].value;
306 if (gds->tokenp[0].token == tMONTH
307 && gds->tokenp[1].token == tUNUMBER) {
311 gds->Month = gds->tokenp[0].value;
312 gds->Day = gds->tokenp[1].value;
317 if (gds->tokenp[0].token == tUNUMBER
318 && gds->tokenp[1].token == tMONTH
319 && gds->tokenp[2].token == tUNUMBER) {
324 gds->Day = gds->tokenp[0].value;
325 gds->Month = gds->tokenp[1].value;
326 gds->Year = gds->tokenp[2].value;
331 if (gds->tokenp[0].token == tUNUMBER
332 && gds->tokenp[1].token == tMONTH) {
336 gds->Day = gds->tokenp[0].value;
337 gds->Month = gds->tokenp[1].value;
346 * Relative time phrase: "tomorrow", "yesterday", "+1 hour", etc.
349 relunitphrase(struct gdstate *gds)
351 if (gds->tokenp[0].token == '-'
352 && gds->tokenp[1].token == tUNUMBER
353 && gds->tokenp[2].token == tSEC_UNIT) {
356 gds->RelSeconds -= gds->tokenp[1].value * gds->tokenp[2].value;
360 if (gds->tokenp[0].token == '+'
361 && gds->tokenp[1].token == tUNUMBER
362 && gds->tokenp[2].token == tSEC_UNIT) {
365 gds->RelSeconds += gds->tokenp[1].value * gds->tokenp[2].value;
369 if (gds->tokenp[0].token == tUNUMBER
370 && gds->tokenp[1].token == tSEC_UNIT) {
373 gds->RelSeconds += gds->tokenp[0].value * gds->tokenp[1].value;
377 if (gds->tokenp[0].token == '-'
378 && gds->tokenp[1].token == tUNUMBER
379 && gds->tokenp[2].token == tMONTH_UNIT) {
382 gds->RelMonth -= gds->tokenp[1].value * gds->tokenp[2].value;
386 if (gds->tokenp[0].token == '+'
387 && gds->tokenp[1].token == tUNUMBER
388 && gds->tokenp[2].token == tMONTH_UNIT) {
391 gds->RelMonth += gds->tokenp[1].value * gds->tokenp[2].value;
395 if (gds->tokenp[0].token == tUNUMBER
396 && gds->tokenp[1].token == tMONTH_UNIT) {
399 gds->RelMonth += gds->tokenp[0].value * gds->tokenp[1].value;
403 if (gds->tokenp[0].token == tSEC_UNIT) {
404 /* "now", "tomorrow" */
406 gds->RelSeconds += gds->tokenp[0].value;
410 if (gds->tokenp[0].token == tMONTH_UNIT) {
413 gds->RelMonth += gds->tokenp[0].value;
421 * Day of the week specification.
424 dayphrase(struct gdstate *gds)
426 if (gds->tokenp[0].token == tDAY) {
427 /* "tues", "wednesday," */
430 gds->DayNumber = gds->tokenp[0].value;
432 if (gds->tokenp[0].token == ',')
436 if (gds->tokenp[0].token == tUNUMBER
437 && gds->tokenp[1].token == tDAY) {
438 /* "second tues" "3 wed" */
440 gds->DayOrdinal = gds->tokenp[0].value;
441 gds->DayNumber = gds->tokenp[1].value;
449 * Try to match a phrase using one of the above functions.
450 * This layer also deals with a couple of generic issues.
453 phrase(struct gdstate *gds)
463 if (relunitphrase(gds)) {
464 if (gds->tokenp[0].token == tAGO) {
465 gds->RelSeconds = -gds->RelSeconds;
466 gds->RelMonth = -gds->RelMonth;
472 /* Bare numbers sometimes have meaning. */
473 if (gds->tokenp[0].token == tUNUMBER) {
474 if (gds->HaveTime && !gds->HaveYear && !gds->HaveRel) {
476 gds->Year = gds->tokenp[0].value;
481 if(gds->tokenp[0].value > 10000) {
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;
493 if (gds->tokenp[0].value < 24) {
495 gds->Hour = gds->tokenp[0].value;
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;
517 * A dictionary of time words.
519 static struct LEXICON {
524 } const TimeWords[] = {
526 { 0, "am", tAMPM, tAM },
527 { 0, "pm", tAMPM, tPM },
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 },
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 },
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 */
615 { 0, "dst", tDST, 0 },
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 },
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 },
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 },
687 * = A number from 0 to 99, which means a year from 1970 to 2069, or
688 * = The actual year (>=100).
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)
695 signed char DaysInMonth[12] = {
696 31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
701 #if defined(HAVE_LOCALTIME_R) || defined(HAVE__LOCALTIME64_S)
704 #if defined(HAVE__LOCALTIME64_S)
713 DaysInMonth[1] = Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0)
715 /* Checking for 2038 bogusly assumes that time_t is 32 bits. But
716 I'm too lazy to try to check for time_t overflow in another way. */
717 if (Year < EPOCH || Year >= 2038
718 || Month < 1 || Month > 12
719 /* Lint fluff: "conversion from long may lose accuracy" */
720 || Day < 1 || Day > DaysInMonth[(int)--Month]
721 || Hours < 0 || Hours > 23
722 || Minutes < 0 || Minutes > 59
723 || Seconds < 0 || Seconds > 59)
727 for (i = 0; i < Month; i++)
728 Julian += DaysInMonth[i];
729 for (i = EPOCH; i < Year; i++)
730 Julian += 365 + (i % 4 == 0);
733 Julian += Hours * HOUR + Minutes * MINUTE + Seconds;
734 #if defined(HAVE_LOCALTIME_R)
735 ltime = localtime_r(&Julian, &tmbuf);
736 #elif defined(HAVE__LOCALTIME64_S)
738 terr = _localtime64_s(&tmbuf, &tmptime);
744 ltime = localtime(&Julian);
747 || (DSTmode == DSTmaybe && ltime->tm_isdst))
753 DSTcorrect(time_t Start, time_t Future)
758 #if defined(HAVE_LOCALTIME_R) || defined(HAVE__LOCALTIME64_S)
761 #if defined(HAVE__LOCALTIME64_S)
766 #if defined(HAVE_LOCALTIME_R)
767 ltime = localtime_r(&Start, &tmbuf);
768 #elif defined(HAVE__LOCALTIME64_S)
770 terr = _localtime64_s(&tmbuf, &tmptime);
776 ltime = localtime(&Start);
778 StartDay = (ltime->tm_hour + 1) % 24;
779 #if defined(HAVE_LOCALTIME_R)
780 ltime = localtime_r(&Future, &tmbuf);
781 #elif defined(HAVE__LOCALTIME64_S)
783 terr = _localtime64_s(&tmbuf, &tmptime);
789 ltime = localtime(&Future);
791 FutureDay = (ltime->tm_hour + 1) % 24;
792 return (Future - Start) + (StartDay - FutureDay) * HOUR;
797 RelativeDate(time_t Start, time_t zone, int dstmode,
798 time_t DayOrdinal, time_t DayNumber)
802 #if defined(HAVE_GMTIME_R) || defined(HAVE__GMTIME64_S)
805 #if defined(HAVE__GMTIME64_S)
811 #if defined(HAVE_GMTIME_R)
812 tm = gmtime_r(&t, &tmbuf);
813 #elif defined(HAVE__GMTIME64_S)
815 terr = _gmtime64_s(&tmbuf, &tmptime);
824 now += DAY * ((DayNumber - tm->tm_wday + 7) % 7);
825 now += 7 * DAY * (DayOrdinal <= 0 ? DayOrdinal : DayOrdinal - 1);
826 if (dstmode == DSTmaybe)
827 return DSTcorrect(Start, now);
833 RelativeMonth(time_t Start, time_t Timezone, time_t RelMonth)
838 #if defined(HAVE_LOCALTIME_R) || defined(HAVE__LOCALTIME64_S)
841 #if defined(HAVE__LOCALTIME64_S)
848 #if defined(HAVE_LOCALTIME_R)
849 tm = localtime_r(&Start, &tmbuf);
850 #elif defined(HAVE__LOCALTIME64_S)
852 terr = _localtime64_s(&tmbuf, &tmptime);
858 tm = localtime(&Start);
860 Month = 12 * (tm->tm_year + 1900) + tm->tm_mon + RelMonth;
862 Month = Month % 12 + 1;
863 return DSTcorrect(Start,
864 Convert(Month, (time_t)tm->tm_mday, Year,
865 (time_t)tm->tm_hour, (time_t)tm->tm_min, (time_t)tm->tm_sec,
866 Timezone, DSTmaybe));
873 nexttoken(const char **in, time_t *value)
879 while (isspace((unsigned char)**in))
882 /* Skip parenthesized comments. */
897 /* Try the next token in the word table first. */
898 /* This allows us to match "2nd", for example. */
900 const char *src = *in;
901 const struct LEXICON *tp;
904 /* Force to lowercase and strip '.' characters. */
906 && (isalnum((unsigned char)*src) || *src == '.')
907 && i < sizeof(buff)-1) {
909 if (isupper((unsigned char)*src))
910 buff[i++] = tolower((unsigned char)*src);
919 * Find the first match. If the word can be
920 * abbreviated, make sure we match at least
921 * the minimum abbreviation.
923 for (tp = TimeWords; tp->name; tp++) {
924 size_t abbrev = tp->abbrev;
926 abbrev = strlen(tp->name);
927 if (strlen(buff) >= abbrev
928 && strncmp(tp->name, buff, strlen(buff))
930 /* Skip over token. */
932 /* Return the match. */
940 * Not in the word table, maybe it's a number. Note:
941 * Because '-' and '+' have other special meanings, I
942 * don't deal with signed numbers here.
944 if (isdigit((unsigned char)(c = **in))) {
945 for (*value = 0; isdigit((unsigned char)(c = *(*in)++)); )
946 *value = 10 * *value + c - '0';
955 #define TM_YEAR_ORIGIN 1900
957 /* Yield A - B, measured in seconds. */
959 difftm (struct tm *a, struct tm *b)
961 int ay = a->tm_year + (TM_YEAR_ORIGIN - 1);
962 int by = b->tm_year + (TM_YEAR_ORIGIN - 1);
964 /* difference in day of year */
965 a->tm_yday - b->tm_yday
966 /* + intervening leap days */
967 + ((ay >> 2) - (by >> 2))
969 + ((ay/100 >> 2) - (by/100 >> 2))
970 /* + difference in years * 365 */
971 + (long)(ay-by) * 365
973 return (days * DAY + (a->tm_hour - b->tm_hour) * HOUR
974 + (a->tm_min - b->tm_min) * MINUTE
975 + (a->tm_sec - b->tm_sec));
980 * The public function.
982 * TODO: tokens[] array should be dynamically sized.
985 __archive_get_date(time_t now, const char *p)
987 struct token tokens[256];
989 struct token *lasttoken;
991 struct tm local, *tm;
992 struct tm gmt, *gmt_ptr;
996 #if defined(HAVE__LOCALTIME64_S) || defined(HAVE__GMTIME64_S)
1001 /* Clear out the parsed token array. */
1002 memset(tokens, 0, sizeof(tokens));
1003 /* Initialize the parser state. */
1004 memset(&_gds, 0, sizeof(_gds));
1007 /* Look up the current time. */
1008 #if defined(HAVE_LOCALTIME_R)
1009 tm = localtime_r(&now, &local);
1010 #elif defined(HAVE__LOCALTIME64_S)
1012 terr = _localtime64_s(&local, &tmptime);
1018 memset(&local, 0, sizeof(local));
1019 tm = localtime(&now);
1023 #if !defined(HAVE_LOCALTIME_R) && !defined(HAVE__LOCALTIME64_S)
1027 /* Look up UTC if we can and use that to determine the current
1028 * timezone offset. */
1029 #if defined(HAVE_GMTIME_R)
1030 gmt_ptr = gmtime_r(&now, &gmt);
1031 #elif defined(HAVE__GMTIME64_S)
1033 terr = _gmtime64_s(&gmt, &tmptime);
1039 memset(&gmt, 0, sizeof(gmt));
1040 gmt_ptr = gmtime(&now);
1041 if (gmt_ptr != NULL) {
1042 /* Copy, in case localtime and gmtime use the same buffer. */
1046 if (gmt_ptr != NULL)
1047 tzone = difftm (&gmt, &local);
1049 /* This system doesn't understand timezones; fake it. */
1054 /* Tokenize the input string. */
1056 while ((lasttoken->token = nexttoken(&p, &lasttoken->value)) != 0) {
1058 if (lasttoken > tokens + 255)
1061 gds->tokenp = tokens;
1063 /* Match phrases until we run out of input tokens. */
1064 while (gds->tokenp < lasttoken) {
1069 /* Use current local timezone if none was specified. */
1070 if (!gds->HaveZone) {
1071 gds->Timezone = tzone;
1072 gds->DSTmode = DSTmaybe;
1075 /* If a timezone was specified, use that for generating the default
1076 * time components instead of the local timezone. */
1077 if (gds->HaveZone && gmt_ptr != NULL) {
1078 now -= gds->Timezone;
1079 #if defined(HAVE_GMTIME_R)
1080 gmt_ptr = gmtime_r(&now, &gmt);
1081 #elif defined(HAVE__GMTIME64_S)
1083 terr = _gmtime64_s(&gmt, &tmptime);
1089 gmt_ptr = gmtime(&now);
1091 if (gmt_ptr != NULL)
1093 now += gds->Timezone;
1097 gds->Year = local.tm_year + 1900;
1098 if (!gds->HaveMonth)
1099 gds->Month = local.tm_mon + 1;
1101 gds->Day = local.tm_mday;
1102 /* Note: No default for hour/min/sec; a specifier that just
1103 * gives date always refers to 00:00 on that date. */
1105 /* If we saw more than one time, timezone, weekday, year, month,
1106 * or day, then give up. */
1107 if (gds->HaveTime > 1 || gds->HaveZone > 1 || gds->HaveWeekDay > 1
1108 || gds->HaveYear > 1 || gds->HaveMonth > 1 || gds->HaveDay > 1)
1111 /* Compute an absolute time based on whatever absolute information
1113 if (gds->HaveYear || gds->HaveMonth || gds->HaveDay
1114 || gds->HaveTime || gds->HaveWeekDay) {
1115 Start = Convert(gds->Month, gds->Day, gds->Year,
1116 gds->Hour, gds->Minutes, gds->Seconds,
1117 gds->Timezone, gds->DSTmode);
1123 Start -= local.tm_hour * HOUR + local.tm_min * MINUTE
1127 /* Add the relative offset. */
1128 Start += gds->RelSeconds;
1129 Start += RelativeMonth(Start, gds->Timezone, gds->RelMonth);
1131 /* Adjust for day-of-week offsets. */
1132 if (gds->HaveWeekDay
1133 && !(gds->HaveYear || gds->HaveMonth || gds->HaveDay)) {
1134 tod = RelativeDate(Start, gds->Timezone,
1135 gds->DSTmode, gds->DayOrdinal, gds->DayNumber);
1139 /* -1 is an error indicator, so return 0 instead of -1 if
1140 * that's the actual time. */
1141 return Start == -1 ? 0 : Start;
1149 main(int argc, char **argv)
1152 time_t now = time(NULL);
1154 while (*++argv != NULL) {
1155 (void)printf("Input: %s\n", *argv);
1156 d = get_date(now, *argv);
1158 (void)printf("Bad format - couldn't convert.\n");
1160 (void)printf("Output: %s\n", ctime(&d));
1165 #endif /* defined(TEST) */