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.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
41 /* This file defines a single public function. */
42 time_t get_date(time_t now, char *);
44 /* Basic time units. */
47 #define HOUR (60L * MINUTE)
48 #define DAY (24L * HOUR)
50 /* Daylight-savings mode: on, off, or not yet known. */
51 enum DSTMODE { DSTon, DSToff, DSTmaybe };
52 /* Meridian: am or pm. */
54 /* Token types returned by nexttoken() */
55 enum { tAGO = 260, tDAY, tDAYZONE, tAMPM, tMONTH, tMONTH_UNIT, tSEC_UNIT,
56 tUNUMBER, tZONE, tDST };
57 struct token { int token; time_t value; };
63 struct token *tokenp; /* Pointer to next token. */
64 /* HaveXxxx counts how many of this kind of phrase we've seen;
65 * it's a fatal error to have more than one time, zone, day,
70 int HaveWeekDay; /* Day of week */
71 int HaveTime; /* Hour/minute/second */
72 int HaveZone; /* timezone and/or DST info */
73 int HaveRel; /* time offset; we can have more than one */
74 /* Absolute time values. */
75 time_t Timezone; /* Seconds offset from GMT */
84 /* Day of week accounting, e.g., "3rd Tuesday" */
85 time_t DayOrdinal; /* "3" in "3rd Tuesday" */
86 time_t DayNumber; /* "Tuesday" in "3rd Tuesday" */
87 /* Relative time values: hour/day/week offsets are measured in
88 * seconds, month/year are counted in months. */
94 * A series of functions that recognize certain common time phrases.
95 * Each function returns 1 if it managed to make sense of some of the
96 * tokens, zero otherwise.
100 * hour:minute or hour:minute:second with optional AM, PM, or numeric
104 timephrase(struct gdstate *gds)
106 if (gds->tokenp[0].token == tUNUMBER
107 && gds->tokenp[1].token == ':'
108 && gds->tokenp[2].token == tUNUMBER
109 && gds->tokenp[3].token == ':'
110 && gds->tokenp[4].token == tUNUMBER) {
111 /* "12:14:18" or "22:08:07" */
113 gds->Hour = gds->tokenp[0].value;
114 gds->Minutes = gds->tokenp[2].value;
115 gds->Seconds = gds->tokenp[4].value;
118 else if (gds->tokenp[0].token == tUNUMBER
119 && gds->tokenp[1].token == ':'
120 && gds->tokenp[2].token == tUNUMBER) {
121 /* "12:14" or "22:08" */
123 gds->Hour = gds->tokenp[0].value;
124 gds->Minutes = gds->tokenp[2].value;
128 else if (gds->tokenp[0].token == tUNUMBER
129 && gds->tokenp[1].token == tAMPM) {
130 /* "7" is a time if it's followed by "am" or "pm" */
132 gds->Hour = gds->tokenp[0].value;
133 gds->Minutes = gds->Seconds = 0;
134 /* We'll handle the AM/PM below. */
137 /* We can't handle this. */
141 if (gds->tokenp[0].token == tAMPM) {
142 /* "7:12pm", "12:20:13am" */
145 if (gds->tokenp[0].value == tPM)
149 if (gds->tokenp[0].token == '+'
150 && gds->tokenp[1].token == tUNUMBER) {
153 gds->DSTmode = DSToff;
154 gds->Timezone = - ((gds->tokenp[1].value / 100) * HOUR
155 + (gds->tokenp[1].value % 100) * MINUTE);
158 if (gds->tokenp[0].token == '-'
159 && gds->tokenp[1].token == tUNUMBER) {
160 /* "19:14:12-0530" */
162 gds->DSTmode = DSToff;
163 gds->Timezone = + ((gds->tokenp[1].value / 100) * HOUR
164 + (gds->tokenp[1].value % 100) * MINUTE);
171 * Timezone name, possibly including DST.
174 zonephrase(struct gdstate *gds)
176 if (gds->tokenp[0].token == tZONE
177 && gds->tokenp[1].token == tDST) {
179 gds->Timezone = gds->tokenp[0].value;
180 gds->DSTmode = DSTon;
185 if (gds->tokenp[0].token == tZONE) {
187 gds->Timezone = gds->tokenp[0].value;
188 gds->DSTmode = DSToff;
193 if (gds->tokenp[0].token == tDAYZONE) {
195 gds->Timezone = gds->tokenp[0].value;
196 gds->DSTmode = DSTon;
204 * Year/month/day in various combinations.
207 datephrase(struct gdstate *gds)
209 if (gds->tokenp[0].token == tUNUMBER
210 && gds->tokenp[1].token == '/'
211 && gds->tokenp[2].token == tUNUMBER
212 && gds->tokenp[3].token == '/'
213 && gds->tokenp[4].token == tUNUMBER) {
217 if (gds->tokenp[0].value >= 13) {
218 /* First number is big: 2004/01/29, 99/02/17 */
219 gds->Year = gds->tokenp[0].value;
220 gds->Month = gds->tokenp[2].value;
221 gds->Day = gds->tokenp[4].value;
222 } else if ((gds->tokenp[4].value >= 13) || (gds->tokenp[2].value >= 13)) {
223 /* Last number is big: 01/07/98 */
224 /* Middle number is big: 01/29/04 */
225 gds->Month = gds->tokenp[0].value;
226 gds->Day = gds->tokenp[2].value;
227 gds->Year = gds->tokenp[4].value;
229 /* No significant clues: 02/03/04 */
230 gds->Month = gds->tokenp[0].value;
231 gds->Day = gds->tokenp[2].value;
232 gds->Year = gds->tokenp[4].value;
238 if (gds->tokenp[0].token == tUNUMBER
239 && gds->tokenp[1].token == '/'
240 && gds->tokenp[2].token == tUNUMBER) {
244 gds->Month = gds->tokenp[0].value;
245 gds->Day = gds->tokenp[2].value;
250 if (gds->tokenp[0].token == tUNUMBER
251 && gds->tokenp[1].token == '-'
252 && gds->tokenp[2].token == tUNUMBER
253 && gds->tokenp[3].token == '-'
254 && gds->tokenp[4].token == tUNUMBER) {
255 /* ISO 8601 format. yyyy-mm-dd. */
259 gds->Year = gds->tokenp[0].value;
260 gds->Month = gds->tokenp[2].value;
261 gds->Day = gds->tokenp[4].value;
266 if (gds->tokenp[0].token == tUNUMBER
267 && gds->tokenp[1].token == '-'
268 && gds->tokenp[2].token == tMONTH
269 && gds->tokenp[3].token == '-'
270 && gds->tokenp[4].token == tUNUMBER) {
274 if (gds->tokenp[0].value > 31) {
275 /* e.g. 1992-Jun-17 */
276 gds->Year = gds->tokenp[0].value;
277 gds->Month = gds->tokenp[2].value;
278 gds->Day = gds->tokenp[4].value;
280 /* e.g. 17-JUN-1992. */
281 gds->Day = gds->tokenp[0].value;
282 gds->Month = gds->tokenp[2].value;
283 gds->Year = gds->tokenp[4].value;
289 if (gds->tokenp[0].token == tMONTH
290 && gds->tokenp[1].token == tUNUMBER
291 && gds->tokenp[2].token == ','
292 && gds->tokenp[3].token == tUNUMBER) {
293 /* "June 17, 2001" */
297 gds->Month = gds->tokenp[0].value;
298 gds->Day = gds->tokenp[1].value;
299 gds->Year = gds->tokenp[3].value;
304 if (gds->tokenp[0].token == tMONTH
305 && gds->tokenp[1].token == tUNUMBER) {
309 gds->Month = gds->tokenp[0].value;
310 gds->Day = gds->tokenp[1].value;
315 if (gds->tokenp[0].token == tUNUMBER
316 && gds->tokenp[1].token == tMONTH
317 && gds->tokenp[2].token == tUNUMBER) {
322 gds->Day = gds->tokenp[0].value;
323 gds->Month = gds->tokenp[1].value;
324 gds->Year = gds->tokenp[2].value;
329 if (gds->tokenp[0].token == tUNUMBER
330 && gds->tokenp[1].token == tMONTH) {
334 gds->Day = gds->tokenp[0].value;
335 gds->Month = gds->tokenp[1].value;
344 * Relative time phrase: "tomorrow", "yesterday", "+1 hour", etc.
347 relunitphrase(struct gdstate *gds)
349 if (gds->tokenp[0].token == '-'
350 && gds->tokenp[1].token == tUNUMBER
351 && gds->tokenp[2].token == tSEC_UNIT) {
354 gds->RelSeconds -= gds->tokenp[1].value * gds->tokenp[2].value;
358 if (gds->tokenp[0].token == '+'
359 && gds->tokenp[1].token == tUNUMBER
360 && gds->tokenp[2].token == tSEC_UNIT) {
363 gds->RelSeconds += gds->tokenp[1].value * gds->tokenp[2].value;
367 if (gds->tokenp[0].token == tUNUMBER
368 && gds->tokenp[1].token == tSEC_UNIT) {
371 gds->RelSeconds += gds->tokenp[1].value * gds->tokenp[2].value;
375 if (gds->tokenp[0].token == '-'
376 && gds->tokenp[1].token == tUNUMBER
377 && gds->tokenp[2].token == tMONTH_UNIT) {
380 gds->RelMonth -= gds->tokenp[1].value * gds->tokenp[2].value;
384 if (gds->tokenp[0].token == '+'
385 && gds->tokenp[1].token == tUNUMBER
386 && gds->tokenp[2].token == tMONTH_UNIT) {
389 gds->RelMonth += gds->tokenp[1].value * gds->tokenp[2].value;
393 if (gds->tokenp[0].token == tUNUMBER
394 && gds->tokenp[1].token == tMONTH_UNIT) {
397 gds->RelMonth += gds->tokenp[0].value * gds->tokenp[1].value;
401 if (gds->tokenp[0].token == tSEC_UNIT) {
402 /* "now", "tomorrow" */
404 gds->RelSeconds += gds->tokenp[0].value;
408 if (gds->tokenp[0].token == tMONTH_UNIT) {
411 gds->RelMonth += gds->tokenp[0].value;
419 * Day of the week specification.
422 dayphrase(struct gdstate *gds)
424 if (gds->tokenp[0].token == tDAY) {
425 /* "tues", "wednesday," */
428 gds->DayNumber = gds->tokenp[0].value;
430 if (gds->tokenp[0].token == ',')
434 if (gds->tokenp[0].token == tUNUMBER
435 && gds->tokenp[1].token == tDAY) {
436 /* "second tues" "3 wed" */
438 gds->DayOrdinal = gds->tokenp[0].value;
439 gds->DayNumber = gds->tokenp[1].value;
447 * Try to match a phrase using one of the above functions.
448 * This layer also deals with a couple of generic issues.
451 phrase(struct gdstate *gds)
461 if (relunitphrase(gds)) {
462 if (gds->tokenp[0].token == tAGO) {
463 gds->RelSeconds = -gds->RelSeconds;
464 gds->RelMonth = -gds->RelMonth;
470 /* Bare numbers sometimes have meaning. */
471 if (gds->tokenp[0].token == tUNUMBER) {
472 if (gds->HaveTime && !gds->HaveYear && !gds->HaveRel) {
474 gds->Year = gds->tokenp[0].value;
479 if(gds->tokenp[0].value > 10000) {
484 gds->Day= (gds->tokenp[0].value)%100;
485 gds->Month= (gds->tokenp[0].value/100)%100;
486 gds->Year = gds->tokenp[0].value/10000;
491 if (gds->tokenp[0].value < 24) {
493 gds->Hour = gds->tokenp[0].value;
500 if ((gds->tokenp[0].value / 100 < 24)
501 && (gds->tokenp[0].value % 100 < 60)) {
502 /* "513" is same as "5:13" */
503 gds->Hour = gds->tokenp[0].value / 100;
504 gds->Minutes = gds->tokenp[0].value % 100;
515 * A dictionary of time words.
517 static struct LEXICON {
522 } const TimeWords[] = {
524 { 0, "am", tAMPM, tAM },
525 { 0, "pm", tAMPM, tPM },
528 { 3, "january", tMONTH, 1 },
529 { 3, "february", tMONTH, 2 },
530 { 3, "march", tMONTH, 3 },
531 { 3, "april", tMONTH, 4 },
532 { 3, "may", tMONTH, 5 },
533 { 3, "june", tMONTH, 6 },
534 { 3, "july", tMONTH, 7 },
535 { 3, "august", tMONTH, 8 },
536 { 3, "september", tMONTH, 9 },
537 { 3, "october", tMONTH, 10 },
538 { 3, "november", tMONTH, 11 },
539 { 3, "december", tMONTH, 12 },
541 /* Days of the week. */
542 { 2, "sunday", tDAY, 0 },
543 { 3, "monday", tDAY, 1 },
544 { 2, "tuesday", tDAY, 2 },
545 { 3, "wednesday", tDAY, 3 },
546 { 2, "thursday", tDAY, 4 },
547 { 2, "friday", tDAY, 5 },
548 { 2, "saturday", tDAY, 6 },
550 /* Timezones: Offsets are in seconds. */
551 { 0, "gmt", tZONE, 0*HOUR }, /* Greenwich Mean */
552 { 0, "ut", tZONE, 0*HOUR }, /* Universal (Coordinated) */
553 { 0, "utc", tZONE, 0*HOUR },
554 { 0, "wet", tZONE, 0*HOUR }, /* Western European */
555 { 0, "bst", tDAYZONE, 0*HOUR }, /* British Summer */
556 { 0, "wat", tZONE, 1*HOUR }, /* West Africa */
557 { 0, "at", tZONE, 2*HOUR }, /* Azores */
558 /* { 0, "bst", tZONE, 3*HOUR }, */ /* Brazil Standard: Conflict */
559 /* { 0, "gst", tZONE, 3*HOUR }, */ /* Greenland Standard: Conflict*/
560 { 0, "nft", tZONE, 3*HOUR+30*MINUTE }, /* Newfoundland */
561 { 0, "nst", tZONE, 3*HOUR+30*MINUTE }, /* Newfoundland Standard */
562 { 0, "ndt", tDAYZONE, 3*HOUR+30*MINUTE }, /* Newfoundland Daylight */
563 { 0, "ast", tZONE, 4*HOUR }, /* Atlantic Standard */
564 { 0, "adt", tDAYZONE, 4*HOUR }, /* Atlantic Daylight */
565 { 0, "est", tZONE, 5*HOUR }, /* Eastern Standard */
566 { 0, "edt", tDAYZONE, 5*HOUR }, /* Eastern Daylight */
567 { 0, "cst", tZONE, 6*HOUR }, /* Central Standard */
568 { 0, "cdt", tDAYZONE, 6*HOUR }, /* Central Daylight */
569 { 0, "mst", tZONE, 7*HOUR }, /* Mountain Standard */
570 { 0, "mdt", tDAYZONE, 7*HOUR }, /* Mountain Daylight */
571 { 0, "pst", tZONE, 8*HOUR }, /* Pacific Standard */
572 { 0, "pdt", tDAYZONE, 8*HOUR }, /* Pacific Daylight */
573 { 0, "yst", tZONE, 9*HOUR }, /* Yukon Standard */
574 { 0, "ydt", tDAYZONE, 9*HOUR }, /* Yukon Daylight */
575 { 0, "hst", tZONE, 10*HOUR }, /* Hawaii Standard */
576 { 0, "hdt", tDAYZONE, 10*HOUR }, /* Hawaii Daylight */
577 { 0, "cat", tZONE, 10*HOUR }, /* Central Alaska */
578 { 0, "ahst", tZONE, 10*HOUR }, /* Alaska-Hawaii Standard */
579 { 0, "nt", tZONE, 11*HOUR }, /* Nome */
580 { 0, "idlw", tZONE, 12*HOUR }, /* Intl Date Line West */
581 { 0, "cet", tZONE, -1*HOUR }, /* Central European */
582 { 0, "met", tZONE, -1*HOUR }, /* Middle European */
583 { 0, "mewt", tZONE, -1*HOUR }, /* Middle European Winter */
584 { 0, "mest", tDAYZONE, -1*HOUR }, /* Middle European Summer */
585 { 0, "swt", tZONE, -1*HOUR }, /* Swedish Winter */
586 { 0, "sst", tDAYZONE, -1*HOUR }, /* Swedish Summer */
587 { 0, "fwt", tZONE, -1*HOUR }, /* French Winter */
588 { 0, "fst", tDAYZONE, -1*HOUR }, /* French Summer */
589 { 0, "eet", tZONE, -2*HOUR }, /* Eastern Eur, USSR Zone 1 */
590 { 0, "bt", tZONE, -3*HOUR }, /* Baghdad, USSR Zone 2 */
591 { 0, "it", tZONE, -3*HOUR-30*MINUTE },/* Iran */
592 { 0, "zp4", tZONE, -4*HOUR }, /* USSR Zone 3 */
593 { 0, "zp5", tZONE, -5*HOUR }, /* USSR Zone 4 */
594 { 0, "ist", tZONE, -5*HOUR-30*MINUTE },/* Indian Standard */
595 { 0, "zp6", tZONE, -6*HOUR }, /* USSR Zone 5 */
596 /* { 0, "nst", tZONE, -6.5*HOUR }, */ /* North Sumatra: Conflict */
597 /* { 0, "sst", tZONE, -7*HOUR }, */ /* So Sumatra, USSR 6: Conflict */
598 { 0, "wast", tZONE, -7*HOUR }, /* West Australian Standard */
599 { 0, "wadt", tDAYZONE, -7*HOUR }, /* West Australian Daylight */
600 { 0, "jt", tZONE, -7*HOUR-30*MINUTE },/* Java (3pm in Cronusland!)*/
601 { 0, "cct", tZONE, -8*HOUR }, /* China Coast, USSR Zone 7 */
602 { 0, "jst", tZONE, -9*HOUR }, /* Japan Std, USSR Zone 8 */
603 { 0, "cast", tZONE, -9*HOUR-30*MINUTE },/* Ctrl Australian Std */
604 { 0, "cadt", tDAYZONE, -9*HOUR-30*MINUTE },/* Ctrl Australian Daylt */
605 { 0, "east", tZONE, -10*HOUR }, /* Eastern Australian Std */
606 { 0, "eadt", tDAYZONE, -10*HOUR }, /* Eastern Australian Daylt */
607 { 0, "gst", tZONE, -10*HOUR }, /* Guam Std, USSR Zone 9 */
608 { 0, "nzt", tZONE, -12*HOUR }, /* New Zealand */
609 { 0, "nzst", tZONE, -12*HOUR }, /* New Zealand Standard */
610 { 0, "nzdt", tDAYZONE, -12*HOUR }, /* New Zealand Daylight */
611 { 0, "idle", tZONE, -12*HOUR }, /* Intl Date Line East */
613 { 0, "dst", tDST, 0 },
616 { 4, "years", tMONTH_UNIT, 12 },
617 { 5, "months", tMONTH_UNIT, 1 },
618 { 9, "fortnights", tSEC_UNIT, 14 * DAY },
619 { 4, "weeks", tSEC_UNIT, 7 * DAY },
620 { 3, "days", tSEC_UNIT, DAY },
621 { 4, "hours", tSEC_UNIT, HOUR },
622 { 3, "minutes", tSEC_UNIT, MINUTE },
623 { 3, "seconds", tSEC_UNIT, 1 },
625 /* Relative-time words. */
626 { 0, "tomorrow", tSEC_UNIT, DAY },
627 { 0, "yesterday", tSEC_UNIT, -DAY },
628 { 0, "today", tSEC_UNIT, 0 },
629 { 0, "now", tSEC_UNIT, 0 },
630 { 0, "last", tUNUMBER, -1 },
631 { 0, "this", tSEC_UNIT, 0 },
632 { 0, "next", tUNUMBER, 2 },
633 { 0, "first", tUNUMBER, 1 },
634 { 0, "1st", tUNUMBER, 1 },
635 /* { 0, "second", tUNUMBER, 2 }, */
636 { 0, "2nd", tUNUMBER, 2 },
637 { 0, "third", tUNUMBER, 3 },
638 { 0, "3rd", tUNUMBER, 3 },
639 { 0, "fourth", tUNUMBER, 4 },
640 { 0, "4th", tUNUMBER, 4 },
641 { 0, "fifth", tUNUMBER, 5 },
642 { 0, "5th", tUNUMBER, 5 },
643 { 0, "sixth", tUNUMBER, 6 },
644 { 0, "seventh", tUNUMBER, 7 },
645 { 0, "eighth", tUNUMBER, 8 },
646 { 0, "ninth", tUNUMBER, 9 },
647 { 0, "tenth", tUNUMBER, 10 },
648 { 0, "eleventh", tUNUMBER, 11 },
649 { 0, "twelfth", tUNUMBER, 12 },
650 { 0, "ago", tAGO, 1 },
652 /* Military timezones. */
653 { 0, "a", tZONE, 1*HOUR },
654 { 0, "b", tZONE, 2*HOUR },
655 { 0, "c", tZONE, 3*HOUR },
656 { 0, "d", tZONE, 4*HOUR },
657 { 0, "e", tZONE, 5*HOUR },
658 { 0, "f", tZONE, 6*HOUR },
659 { 0, "g", tZONE, 7*HOUR },
660 { 0, "h", tZONE, 8*HOUR },
661 { 0, "i", tZONE, 9*HOUR },
662 { 0, "k", tZONE, 10*HOUR },
663 { 0, "l", tZONE, 11*HOUR },
664 { 0, "m", tZONE, 12*HOUR },
665 { 0, "n", tZONE, -1*HOUR },
666 { 0, "o", tZONE, -2*HOUR },
667 { 0, "p", tZONE, -3*HOUR },
668 { 0, "q", tZONE, -4*HOUR },
669 { 0, "r", tZONE, -5*HOUR },
670 { 0, "s", tZONE, -6*HOUR },
671 { 0, "t", tZONE, -7*HOUR },
672 { 0, "u", tZONE, -8*HOUR },
673 { 0, "v", tZONE, -9*HOUR },
674 { 0, "w", tZONE, -10*HOUR },
675 { 0, "x", tZONE, -11*HOUR },
676 { 0, "y", tZONE, -12*HOUR },
677 { 0, "z", tZONE, 0*HOUR },
684 * Convert hour/minute/second to count of seconds.
687 ToSeconds(time_t Hours, time_t Minutes, time_t Seconds)
689 if (Minutes < 0 || Minutes > 59 || Seconds < 0 || Seconds > 59)
691 if (Hours < 0 || Hours > 23)
693 return Hours * HOUR + Minutes * MINUTE + Seconds;
699 * = A number from 0 to 99, which means a year from 1970 to 2069, or
700 * = The actual year (>=100).
703 Convert(time_t Month, time_t Day, time_t Year,
704 time_t Hours, time_t Minutes, time_t Seconds,
705 time_t Timezone, enum DSTMODE DSTmode)
707 static int DaysInMonth[12] = {
708 31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
718 DaysInMonth[1] = Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0)
720 /* Checking for 2038 bogusly assumes that time_t is 32 bits. But
721 I'm too lazy to try to check for time_t overflow in another way. */
722 if (Year < EPOCH || Year > 2038
723 || Month < 1 || Month > 12
724 /* Lint fluff: "conversion from long may lose accuracy" */
725 || Day < 1 || Day > DaysInMonth[(int)--Month])
729 for (i = 0; i < Month; i++)
730 Julian += DaysInMonth[i];
731 for (i = EPOCH; i < Year; i++)
732 Julian += 365 + (i % 4 == 0);
735 if ((tod = ToSeconds(Hours, Minutes, Seconds)) < 0)
739 || (DSTmode == DSTmaybe && localtime(&Julian)->tm_isdst))
746 DSTcorrect(time_t Start, time_t Future)
751 StartDay = (localtime(&Start)->tm_hour + 1) % 24;
752 FutureDay = (localtime(&Future)->tm_hour + 1) % 24;
753 return (Future - Start) + (StartDay - FutureDay) * HOUR;
758 RelativeDate(time_t Start, time_t zone, int dstmode,
759 time_t DayOrdinal, time_t DayNumber)
767 now += DAY * ((DayNumber - tm->tm_wday + 7) % 7);
768 now += 7 * DAY * (DayOrdinal <= 0 ? DayOrdinal : DayOrdinal - 1);
769 if (dstmode == DSTmaybe)
770 return DSTcorrect(Start, now);
776 RelativeMonth(time_t Start, time_t Timezone, time_t RelMonth)
784 tm = localtime(&Start);
785 Month = 12 * (tm->tm_year + 1900) + tm->tm_mon + RelMonth;
787 Month = Month % 12 + 1;
788 return DSTcorrect(Start,
789 Convert(Month, (time_t)tm->tm_mday, Year,
790 (time_t)tm->tm_hour, (time_t)tm->tm_min, (time_t)tm->tm_sec,
791 Timezone, DSTmaybe));
798 nexttoken(char **in, time_t *value)
804 while (isspace((unsigned char)**in))
807 /* Skip parenthesized comments. */
822 /* Try the next token in the word table first. */
823 /* This allows us to match "2nd", for example. */
826 const struct LEXICON *tp;
829 /* Force to lowercase and strip '.' characters. */
831 && (isalnum((unsigned char)*src) || *src == '.')
832 && i < sizeof(buff)-1) {
834 if (isupper((unsigned char)*src))
835 buff[i++] = tolower((unsigned char)*src);
844 * Find the first match. If the word can be
845 * abbreviated, make sure we match at least
846 * the minimum abbreviation.
848 for (tp = TimeWords; tp->name; tp++) {
849 size_t abbrev = tp->abbrev;
851 abbrev = strlen(tp->name);
852 if (strlen(buff) >= abbrev
853 && strncmp(tp->name, buff, strlen(buff))
855 /* Skip over token. */
857 /* Return the match. */
865 * Not in the word table, maybe it's a number. Note:
866 * Because '-' and '+' have other special meanings, I
867 * don't deal with signed numbers here.
869 if (isdigit((unsigned char)(c = **in))) {
870 for (*value = 0; isdigit((unsigned char)(c = *(*in)++)); )
871 *value = 10 * *value + c - '0';
880 #define TM_YEAR_ORIGIN 1900
882 /* Yield A - B, measured in seconds. */
884 difftm (struct tm *a, struct tm *b)
886 int ay = a->tm_year + (TM_YEAR_ORIGIN - 1);
887 int by = b->tm_year + (TM_YEAR_ORIGIN - 1);
889 /* difference in day of year */
890 a->tm_yday - b->tm_yday
891 /* + intervening leap days */
892 + ((ay >> 2) - (by >> 2))
894 + ((ay/100 >> 2) - (by/100 >> 2))
895 /* + difference in years * 365 */
896 + (long)(ay-by) * 365
898 return (days * DAY + (a->tm_hour - b->tm_hour) * HOUR
899 + (a->tm_min - b->tm_min) * MINUTE
900 + (a->tm_sec - b->tm_sec));
905 * The public function.
907 * TODO: tokens[] array should be dynamically sized.
910 get_date(time_t now, char *p)
912 struct token tokens[256];
914 struct token *lasttoken;
916 struct tm local, *tm;
917 struct tm gmt, *gmt_ptr;
922 /* Clear out the parsed token array. */
923 memset(tokens, 0, sizeof(tokens));
924 /* Initialize the parser state. */
925 memset(&_gds, 0, sizeof(_gds));
928 /* Look up the current time. */
929 memset(&local, 0, sizeof(local));
930 tm = localtime (&now);
935 /* Look up UTC if we can and use that to determine the current
936 * timezone offset. */
937 memset(&gmt, 0, sizeof(gmt));
938 gmt_ptr = gmtime (&now);
939 if (gmt_ptr != NULL) {
940 /* Copy, in case localtime and gmtime use the same buffer. */
944 tzone = difftm (&gmt, &local);
946 /* This system doesn't understand timezones; fake it. */
951 /* Tokenize the input string. */
953 while ((lasttoken->token = nexttoken(&p, &lasttoken->value)) != 0) {
955 if (lasttoken > tokens + 255)
958 gds->tokenp = tokens;
960 /* Match phrases until we run out of input tokens. */
961 while (gds->tokenp < lasttoken) {
966 /* Use current local timezone if none was specified. */
967 if (!gds->HaveZone) {
968 gds->Timezone = tzone;
969 gds->DSTmode = DSTmaybe;
972 /* If a timezone was specified, use that for generating the default
973 * time components instead of the local timezone. */
974 if (gds->HaveZone && gmt_ptr != NULL) {
975 now -= gds->Timezone;
976 gmt_ptr = gmtime (&now);
979 now += gds->Timezone;
983 gds->Year = local.tm_year + 1900;
985 gds->Month = local.tm_mon + 1;
987 gds->Day = local.tm_mday;
988 /* Note: No default for hour/min/sec; a specifier that just
989 * gives date always refers to 00:00 on that date. */
991 /* If we saw more than one time, timezone, weekday, year, month,
992 * or day, then give up. */
993 if (gds->HaveTime > 1 || gds->HaveZone > 1 || gds->HaveWeekDay > 1
994 || gds->HaveYear > 1 || gds->HaveMonth > 1 || gds->HaveDay > 1)
997 /* Compute an absolute time based on whatever absolute information
999 if (gds->HaveYear || gds->HaveMonth || gds->HaveDay
1000 || gds->HaveTime || gds->HaveWeekDay) {
1001 Start = Convert(gds->Month, gds->Day, gds->Year,
1002 gds->Hour, gds->Minutes, gds->Seconds,
1003 gds->Timezone, gds->DSTmode);
1009 Start -= local.tm_hour * HOUR + local.tm_min * MINUTE
1013 /* Add the relative offset. */
1014 Start += gds->RelSeconds;
1015 Start += RelativeMonth(Start, gds->Timezone, gds->RelMonth);
1017 /* Adjust for day-of-week offsets. */
1018 if (gds->HaveWeekDay
1019 && !(gds->HaveYear || gds->HaveMonth || gds->HaveDay)) {
1020 tod = RelativeDate(Start, gds->Timezone,
1021 gds->DSTmode, gds->DayOrdinal, gds->DayNumber);
1025 /* -1 is an error indicator, so return 0 instead of -1 if
1026 * that's the actual time. */
1027 return Start == -1 ? 0 : Start;
1035 main(int argc, char **argv)
1039 while (*++argv != NULL) {
1040 (void)printf("Input: %s\n", *argv);
1041 d = get_date(*argv);
1043 (void)printf("Bad format - couldn't convert.\n");
1045 (void)printf("Output: %s\n", ctime(&d));
1050 #endif /* defined(TEST) */