2 /* Parse a string into an internal time stamp.
3 Copyright 1999 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19 /* Originally written by Steven M. Bellovin <smb@research.att.com> while
20 at the University of North Carolina at Chapel Hill. Later tweaked by
21 a couple of people on Usenet. Completely overhauled by Rich $alz
22 <rsalz@bbn.com> and Jim Berets <jberets@bbn.com> in August, 1990.
24 Modified by Paul Eggert <eggert@twinsun.com> in August 1999 to do
25 the right thing about local DST. Unlike previous versions, this
26 version is reentrant. */
35 /* Since the code of getdate.y is not included in the Emacs executable
36 itself, there is no need to #define static in this file. Even if
37 the code were included in the Emacs executable, it probably
38 wouldn't do any harm to #undef it here; this will only cause
39 problems if we try to write to a static variable, which I don't
40 think this code needs to do. */
48 # include <stdlib.h> /* for `free'; used by Bison 1.27 */
51 #if STDC_HEADERS || (! defined isascii && ! HAVE_ISASCII)
52 # define IN_CTYPE_DOMAIN(c) 1
54 # define IN_CTYPE_DOMAIN(c) isascii (c)
57 #define ISSPACE(c) (IN_CTYPE_DOMAIN (c) && isspace (c))
58 #define ISALPHA(c) (IN_CTYPE_DOMAIN (c) && isalpha (c))
59 #define ISLOWER(c) (IN_CTYPE_DOMAIN (c) && islower (c))
60 #define ISDIGIT_LOCALE(c) (IN_CTYPE_DOMAIN (c) && isdigit (c))
62 /* ISDIGIT differs from ISDIGIT_LOCALE, as follows:
63 - Its arg may be any int or unsigned int; it need not be an unsigned char.
64 - It's guaranteed to evaluate its argument exactly once.
65 - It's typically faster.
66 Posix 1003.2-1992 section 2.5.2.1 page 50 lines 1556-1558 says that
67 only '0' through '9' are digits. Prefer ISDIGIT to ISDIGIT_LOCALE unless
68 it's important to use the locale's definition of `digit' even when the
69 host does not conform to Posix. */
70 #define ISDIGIT(c) ((unsigned) (c) - '0' <= 9)
72 #if STDC_HEADERS || HAVE_STRING_H
76 #if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8) || __STRICT_ANSI__
77 # define __attribute__(x)
80 #ifndef ATTRIBUTE_UNUSED
81 # define ATTRIBUTE_UNUSED __attribute__ ((__unused__))
84 #define EPOCH_YEAR 1970
85 #define TM_YEAR_ORIGIN 1900
87 #define HOUR(x) ((x) * 60)
89 /* An entry in the lexical lookup table. */
97 /* Meridian: am, pm, or 24-hour style. */
98 enum { MERam, MERpm, MER24 };
100 /* Information passed to and from the parser. */
101 struct parser_control
103 /* The input string remaining to be parsed. */
106 /* N, if this is the Nth Tuesday. */
109 /* Day of week; Sunday is 0. */
112 /* tm_isdst flag for the local zone. */
115 /* Time zone, in minutes east of UTC. */
118 /* Style used for time. */
121 /* Gregorian year, month, day, hour, minutes, and seconds. */
129 /* Relative year, month, day, hour, minutes, and seconds. */
137 /* Counts of nonterminals of various flavors parsed so far. */
140 int local_zones_seen;
145 /* Table of local time zone abbrevations, terminated by a null entry. */
146 table local_time_zone_table[3];
149 #define PC (* (struct parser_control *) parm)
150 #define YYLEX_PARAM parm
151 #define YYPARSE_PARAM parm
154 static int yyerror ();
159 /* We want a reentrant parser. */
162 /* This grammar has 13 shift/reduce conflicts. */
165 %token tAGO tDAY tDAY_UNIT tDAYZONE tDST tHOUR_UNIT tID
166 %token tLOCAL_ZONE tMERIDIAN tMINUTE_UNIT tMONTH tMONTH_UNIT
167 %token tSEC_UNIT tSNUMBER tUNUMBER tYEAR_UNIT tZONE
180 { PC.local_zones_seen++; }
200 | tUNUMBER ':' tUNUMBER o_merid
207 | tUNUMBER ':' tUNUMBER tSNUMBER
213 PC.time_zone = $4 % 100 + ($4 / 100) * 60;
215 | tUNUMBER ':' tUNUMBER ':' tUNUMBER o_merid
222 | tUNUMBER ':' tUNUMBER ':' tUNUMBER tSNUMBER
229 PC.time_zone = $6 % 100 + ($6 / 100) * 60;
235 { PC.local_isdst = $1; }
237 { PC.local_isdst = $1 < 0 ? 1 : $1 + 1; }
242 { PC.time_zone = $1; }
244 { PC.time_zone = $1 + 60; }
246 { PC.time_zone = $1 + 60; }
268 tUNUMBER '/' tUNUMBER
273 | tUNUMBER '/' tUNUMBER '/' tUNUMBER
275 /* Interpret as YYYY/MM/DD if 1000 <= $1, otherwise as MM/DD/YY.
276 The goal in recognizing YYYY/MM/DD is solely to support legacy
277 machine-generated dates like those in an RCS log listing. If
278 you want portability, use the ISO 8601 format. */
292 | tUNUMBER tSNUMBER tSNUMBER
294 /* ISO 8601 format. YYYY-MM-DD. */
299 | tUNUMBER tMONTH tSNUMBER
301 /* e.g. 17-JUN-1992. */
311 | tMONTH tUNUMBER ',' tUNUMBER
322 | tUNUMBER tMONTH tUNUMBER
333 PC.rel_seconds = -PC.rel_seconds;
334 PC.rel_minutes = -PC.rel_minutes;
335 PC.rel_hour = -PC.rel_hour;
336 PC.rel_day = -PC.rel_day;
337 PC.rel_month = -PC.rel_month;
338 PC.rel_year = -PC.rel_year;
345 { PC.rel_year += $1 * $2; }
346 | tSNUMBER tYEAR_UNIT
347 { PC.rel_year += $1 * $2; }
349 { PC.rel_year += $1; }
350 | tUNUMBER tMONTH_UNIT
351 { PC.rel_month += $1 * $2; }
352 | tSNUMBER tMONTH_UNIT
353 { PC.rel_month += $1 * $2; }
355 { PC.rel_month += $1; }
357 { PC.rel_day += $1 * $2; }
359 { PC.rel_day += $1 * $2; }
361 { PC.rel_day += $1; }
362 | tUNUMBER tHOUR_UNIT
363 { PC.rel_hour += $1 * $2; }
364 | tSNUMBER tHOUR_UNIT
365 { PC.rel_hour += $1 * $2; }
367 { PC.rel_hour += $1; }
368 | tUNUMBER tMINUTE_UNIT
369 { PC.rel_minutes += $1 * $2; }
370 | tSNUMBER tMINUTE_UNIT
371 { PC.rel_minutes += $1 * $2; }
373 { PC.rel_minutes += $1; }
375 { PC.rel_seconds += $1 * $2; }
377 { PC.rel_seconds += $1 * $2; }
379 { PC.rel_seconds += $1; }
385 if (PC.dates_seen && ! PC.rels_seen && (PC.times_seen || 100 <= $1))
393 PC.month = ($1 / 100) % 100;
394 PC.year = $1 / 10000;
407 PC.minutes = $1 % 100;
425 /* Include this file down here because bison inserts code above which
426 may define-away `const'. We want the prototype for get_date to have
427 the same signature as the function definition. */
431 struct tm *gmtime ();
434 struct tm *localtime ();
440 static table const meridian_table[] =
442 { "AM", tMERIDIAN, MERam },
443 { "A.M.", tMERIDIAN, MERam },
444 { "PM", tMERIDIAN, MERpm },
445 { "P.M.", tMERIDIAN, MERpm },
449 static table const dst_table[] =
454 static table const month_and_day_table[] =
456 { "JANUARY", tMONTH, 1 },
457 { "FEBRUARY", tMONTH, 2 },
458 { "MARCH", tMONTH, 3 },
459 { "APRIL", tMONTH, 4 },
460 { "MAY", tMONTH, 5 },
461 { "JUNE", tMONTH, 6 },
462 { "JULY", tMONTH, 7 },
463 { "AUGUST", tMONTH, 8 },
464 { "SEPTEMBER",tMONTH, 9 },
465 { "SEPT", tMONTH, 9 },
466 { "OCTOBER", tMONTH, 10 },
467 { "NOVEMBER", tMONTH, 11 },
468 { "DECEMBER", tMONTH, 12 },
469 { "SUNDAY", tDAY, 0 },
470 { "MONDAY", tDAY, 1 },
471 { "TUESDAY", tDAY, 2 },
473 { "WEDNESDAY",tDAY, 3 },
474 { "WEDNES", tDAY, 3 },
475 { "THURSDAY", tDAY, 4 },
477 { "THURS", tDAY, 4 },
478 { "FRIDAY", tDAY, 5 },
479 { "SATURDAY", tDAY, 6 },
483 static table const time_units_table[] =
485 { "YEAR", tYEAR_UNIT, 1 },
486 { "MONTH", tMONTH_UNIT, 1 },
487 { "FORTNIGHT",tDAY_UNIT, 14 },
488 { "WEEK", tDAY_UNIT, 7 },
489 { "DAY", tDAY_UNIT, 1 },
490 { "HOUR", tHOUR_UNIT, 1 },
491 { "MINUTE", tMINUTE_UNIT, 1 },
492 { "MIN", tMINUTE_UNIT, 1 },
493 { "SECOND", tSEC_UNIT, 1 },
494 { "SEC", tSEC_UNIT, 1 },
498 /* Assorted relative-time words. */
499 static table const relative_time_table[] =
501 { "TOMORROW", tMINUTE_UNIT, 24 * 60 },
502 { "YESTERDAY",tMINUTE_UNIT, - (24 * 60) },
503 { "TODAY", tMINUTE_UNIT, 0 },
504 { "NOW", tMINUTE_UNIT, 0 },
505 { "LAST", tUNUMBER, -1 },
506 { "THIS", tUNUMBER, 0 },
507 { "NEXT", tUNUMBER, 1 },
508 { "FIRST", tUNUMBER, 1 },
509 /*{ "SECOND", tUNUMBER, 2 }, */
510 { "THIRD", tUNUMBER, 3 },
511 { "FOURTH", tUNUMBER, 4 },
512 { "FIFTH", tUNUMBER, 5 },
513 { "SIXTH", tUNUMBER, 6 },
514 { "SEVENTH", tUNUMBER, 7 },
515 { "EIGHTH", tUNUMBER, 8 },
516 { "NINTH", tUNUMBER, 9 },
517 { "TENTH", tUNUMBER, 10 },
518 { "ELEVENTH", tUNUMBER, 11 },
519 { "TWELFTH", tUNUMBER, 12 },
524 /* The time zone table. This table is necessarily incomplete, as time
525 zone abbreviations are ambiguous; e.g. Australians interpret "EST"
526 as Eastern time in Australia, not as US Eastern Standard Time.
527 You cannot rely on getdate to handle arbitrary time zone
528 abbreviations; use numeric abbreviations like `-0500' instead. */
529 static table const time_zone_table[] =
531 { "GMT", tZONE, HOUR ( 0) }, /* Greenwich Mean */
532 { "UT", tZONE, HOUR ( 0) }, /* Universal (Coordinated) */
533 { "UTC", tZONE, HOUR ( 0) },
534 { "WET", tZONE, HOUR ( 0) }, /* Western European */
535 { "WEST", tDAYZONE, HOUR ( 0) }, /* Western European Summer */
536 { "BST", tDAYZONE, HOUR ( 0) }, /* British Summer */
537 { "ART", tZONE, -HOUR ( 3) }, /* Argentina */
538 { "BRT", tZONE, -HOUR ( 3) }, /* Brazil */
539 { "BRST", tDAYZONE, -HOUR ( 3) }, /* Brazil Summer */
540 { "NST", tZONE, -(HOUR ( 3) + 30) }, /* Newfoundland Standard */
541 { "NDT", tDAYZONE,-(HOUR ( 3) + 30) }, /* Newfoundland Daylight */
542 { "AST", tZONE, -HOUR ( 4) }, /* Atlantic Standard */
543 { "ADT", tDAYZONE, -HOUR ( 4) }, /* Atlantic Daylight */
544 { "CLT", tZONE, -HOUR ( 4) }, /* Chile */
545 { "CLST", tDAYZONE, -HOUR ( 4) }, /* Chile Summer */
546 { "EST", tZONE, -HOUR ( 5) }, /* Eastern Standard */
547 { "EDT", tDAYZONE, -HOUR ( 5) }, /* Eastern Daylight */
548 { "CST", tZONE, -HOUR ( 6) }, /* Central Standard */
549 { "CDT", tDAYZONE, -HOUR ( 6) }, /* Central Daylight */
550 { "MST", tZONE, -HOUR ( 7) }, /* Mountain Standard */
551 { "MDT", tDAYZONE, -HOUR ( 7) }, /* Mountain Daylight */
552 { "PST", tZONE, -HOUR ( 8) }, /* Pacific Standard */
553 { "PDT", tDAYZONE, -HOUR ( 8) }, /* Pacific Daylight */
554 { "AKST", tZONE, -HOUR ( 9) }, /* Alaska Standard */
555 { "AKDT", tDAYZONE, -HOUR ( 9) }, /* Alaska Daylight */
556 { "HST", tZONE, -HOUR (10) }, /* Hawaii Standard */
557 { "HAST", tZONE, -HOUR (10) }, /* Hawaii-Aleutian Standard */
558 { "HADT", tDAYZONE, -HOUR (10) }, /* Hawaii-Aleutian Daylight */
559 { "SST", tZONE, -HOUR (12) }, /* Samoa Standard */
560 { "WAT", tZONE, HOUR ( 1) }, /* West Africa */
561 { "CET", tZONE, HOUR ( 1) }, /* Central European */
562 { "CEST", tDAYZONE, HOUR ( 1) }, /* Central European Summer */
563 { "MET", tZONE, HOUR ( 1) }, /* Middle European */
564 { "MEZ", tZONE, HOUR ( 1) }, /* Middle European */
565 { "MEST", tDAYZONE, HOUR ( 1) }, /* Middle European Summer */
566 { "MESZ", tDAYZONE, HOUR ( 1) }, /* Middle European Summer */
567 { "EET", tZONE, HOUR ( 2) }, /* Eastern European */
568 { "EEST", tDAYZONE, HOUR ( 2) }, /* Eastern European Summer */
569 { "CAT", tZONE, HOUR ( 2) }, /* Central Africa */
570 { "SAST", tZONE, HOUR ( 2) }, /* South Africa Standard */
571 { "EAT", tZONE, HOUR ( 3) }, /* East Africa */
572 { "MSK", tZONE, HOUR ( 3) }, /* Moscow */
573 { "MSD", tDAYZONE, HOUR ( 3) }, /* Moscow Daylight */
574 { "IST", tZONE, (HOUR ( 5) + 30) }, /* India Standard */
575 { "SGT", tZONE, HOUR ( 8) }, /* Singapore */
576 { "KST", tZONE, HOUR ( 9) }, /* Korea Standard */
577 { "JST", tZONE, HOUR ( 9) }, /* Japan Standard */
578 { "GST", tZONE, HOUR (10) }, /* Guam Standard */
579 { "NZST", tZONE, HOUR (12) }, /* New Zealand Standard */
580 { "NZDT", tDAYZONE, HOUR (12) }, /* New Zealand Daylight */
584 /* Military time zone table. */
585 static table const military_table[] =
587 { "A", tZONE, -HOUR ( 1) },
588 { "B", tZONE, -HOUR ( 2) },
589 { "C", tZONE, -HOUR ( 3) },
590 { "D", tZONE, -HOUR ( 4) },
591 { "E", tZONE, -HOUR ( 5) },
592 { "F", tZONE, -HOUR ( 6) },
593 { "G", tZONE, -HOUR ( 7) },
594 { "H", tZONE, -HOUR ( 8) },
595 { "I", tZONE, -HOUR ( 9) },
596 { "K", tZONE, -HOUR (10) },
597 { "L", tZONE, -HOUR (11) },
598 { "M", tZONE, -HOUR (12) },
599 { "N", tZONE, HOUR ( 1) },
600 { "O", tZONE, HOUR ( 2) },
601 { "P", tZONE, HOUR ( 3) },
602 { "Q", tZONE, HOUR ( 4) },
603 { "R", tZONE, HOUR ( 5) },
604 { "S", tZONE, HOUR ( 6) },
605 { "T", tZONE, HOUR ( 7) },
606 { "U", tZONE, HOUR ( 8) },
607 { "V", tZONE, HOUR ( 9) },
608 { "W", tZONE, HOUR (10) },
609 { "X", tZONE, HOUR (11) },
610 { "Y", tZONE, HOUR (12) },
611 { "Z", tZONE, HOUR ( 0) },
618 to_hour (int hours, int meridian)
623 return 0 <= hours && hours < 24 ? hours : -1;
625 return 0 < hours && hours < 12 ? hours : hours == 12 ? 0 : -1;
627 return 0 < hours && hours < 12 ? hours + 12 : hours == 12 ? 12 : -1;
640 /* XPG4 suggests that years 00-68 map to 2000-2068, and
641 years 69-99 map to 1969-1999. */
651 lookup_zone (struct parser_control const *pc, char const *name)
655 /* Try local zone abbreviations first; they're more likely to be right. */
656 for (tp = pc->local_time_zone_table; tp->name; tp++)
657 if (strcmp (name, tp->name) == 0)
660 for (tp = time_zone_table; tp->name; tp++)
661 if (strcmp (name, tp->name) == 0)
667 /* Yield A - B, measured in seconds. */
669 difftm (struct tm *a, struct tm *b)
671 int ay = a->tm_year + (TM_YEAR_ORIGIN - 1);
672 int by = b->tm_year + (TM_YEAR_ORIGIN - 1);
674 /* difference in day of year */
675 a->tm_yday - b->tm_yday
676 /* + intervening leap days */
677 + ((ay >> 2) - (by >> 2))
678 - (ay / 100 - by / 100)
679 + ((ay / 100 >> 2) - (by / 100 >> 2))
680 /* + difference in years * 365 */
681 + (int) (ay - by) * 365
683 return (60 * (60 * (24 * days + (a->tm_hour - b->tm_hour))
684 + (a->tm_min - b->tm_min))
685 + (a->tm_sec - b->tm_sec));
689 lookup_word (struct parser_control const *pc, char *word)
698 /* Make it uppercase. */
699 for (p = word; *p; p++)
700 if (ISLOWER ((unsigned char) *p))
701 *p = toupper ((unsigned char) *p);
703 for (tp = meridian_table; tp->name; tp++)
704 if (strcmp (word, tp->name) == 0)
707 /* See if we have an abbreviation for a month. */
708 wordlen = strlen (word);
709 abbrev = wordlen == 3 || (wordlen == 4 && word[3] == '.');
711 for (tp = month_and_day_table; tp->name; tp++)
712 if ((abbrev ? strncmp (word, tp->name, 3) : strcmp (word, tp->name)) == 0)
715 if ((tp = lookup_zone (pc, word)))
718 if (strcmp (word, dst_table[0].name) == 0)
721 for (tp = time_units_table; tp->name; tp++)
722 if (strcmp (word, tp->name) == 0)
725 /* Strip off any plural and try the units table again. */
726 if (word[wordlen - 1] == 'S')
728 word[wordlen - 1] = '\0';
729 for (tp = time_units_table; tp->name; tp++)
730 if (strcmp (word, tp->name) == 0)
732 word[wordlen - 1] = 'S'; /* For "this" in relative_time_table. */
735 for (tp = relative_time_table; tp->name; tp++)
736 if (strcmp (word, tp->name) == 0)
739 /* Military time zones. */
741 for (tp = military_table; tp->name; tp++)
742 if (word[0] == tp->name[0])
745 /* Drop out any periods and try the time zone table again. */
746 for (i = 0, p = q = word; (*p = *q); q++)
751 if (i && (tp = lookup_zone (pc, word)))
758 yylex (YYSTYPE *lvalp, struct parser_control *pc)
765 while (c = *pc->input, ISSPACE (c))
768 if (ISDIGIT (c) || c == '-' || c == '+')
771 if (c == '-' || c == '+')
773 sign = c == '-' ? -1 : 1;
774 if (! ISDIGIT (*++pc->input))
775 /* skip the '-' sign */
780 for (*lvalp = 0; ISDIGIT (c = *pc->input++);)
781 *lvalp = 10 * *lvalp + (c - '0');
785 return sign ? tSNUMBER : tUNUMBER;
796 if (p < buff + sizeof buff - 1)
800 while (ISALPHA (c) || c == '.');
803 tp = lookup_word (pc, buff);
827 /* Do nothing if the parser reports an error. */
829 yyerror (char *s ATTRIBUTE_UNUSED)
836 get_date (const char *p, const time_t *now)
838 time_t Start = now ? *now : time (0);
839 struct tm *tmp = localtime (&Start);
842 struct parser_control pc;
848 pc.year = tmp->tm_year + TM_YEAR_ORIGIN;
849 pc.month = tmp->tm_mon + 1;
850 pc.day = tmp->tm_mday;
851 pc.hour = tmp->tm_hour;
852 pc.minutes = tmp->tm_min;
853 pc.seconds = tmp->tm_sec;
854 tm.tm_isdst = tmp->tm_isdst;
867 pc.local_zones_seen = 0;
871 pc.local_time_zone_table[0].name = tmp->tm_zone;
872 pc.local_time_zone_table[0].type = tLOCAL_ZONE;
873 pc.local_time_zone_table[0].value = tmp->tm_isdst;
874 pc.local_time_zone_table[1].name = 0;
876 /* Probe the names used in the next three calendar quarters, looking
877 for a tm_isdst different from the one we already have. */
880 for (quarter = 1; quarter <= 3; quarter++)
882 time_t probe = Start + quarter * (90 * 24 * 60 * 60);
883 struct tm *probe_tm = localtime (&probe);
884 if (probe_tm && probe_tm->tm_zone
885 && probe_tm->tm_isdst != pc.local_time_zone_table[0].value)
888 pc.local_time_zone_table[1].name = probe_tm->tm_zone;
889 pc.local_time_zone_table[1].type = tLOCAL_ZONE;
890 pc.local_time_zone_table[1].value = probe_tm->tm_isdst;
891 pc.local_time_zone_table[2].name = 0;
901 extern char *tzname[];
904 for (i = 0; i < 2; i++)
906 pc.local_time_zone_table[i].name = tzname[i];
907 pc.local_time_zone_table[i].type = tLOCAL_ZONE;
908 pc.local_time_zone_table[i].value = i;
910 pc.local_time_zone_table[i].name = 0;
913 pc.local_time_zone_table[0].name = 0;
917 if (pc.local_time_zone_table[0].name && pc.local_time_zone_table[1].name
918 && ! strcmp (pc.local_time_zone_table[0].name,
919 pc.local_time_zone_table[1].name))
921 /* This locale uses the same abbrevation for standard and
922 daylight times. So if we see that abbreviation, we don't
923 know whether it's daylight time. */
924 pc.local_time_zone_table[0].value = -1;
925 pc.local_time_zone_table[1].name = 0;
928 if (yyparse (&pc) != 0
929 || 1 < pc.times_seen || 1 < pc.dates_seen || 1 < pc.days_seen
930 || 1 < (pc.local_zones_seen + pc.zones_seen)
931 || (pc.local_zones_seen && 1 < pc.local_isdst))
934 tm.tm_year = to_year (pc.year) - TM_YEAR_ORIGIN + pc.rel_year;
935 tm.tm_mon = pc.month - 1 + pc.rel_month;
936 tm.tm_mday = pc.day + pc.rel_day;
937 if (pc.times_seen || (pc.rels_seen && ! pc.dates_seen && ! pc.days_seen))
939 tm.tm_hour = to_hour (pc.hour, pc.meridian);
942 tm.tm_min = pc.minutes;
943 tm.tm_sec = pc.seconds;
947 tm.tm_hour = tm.tm_min = tm.tm_sec = 0;
949 tm.tm_hour += pc.rel_hour;
950 tm.tm_min += pc.rel_minutes;
951 tm.tm_sec += pc.rel_seconds;
953 /* Let mktime deduce tm_isdst if we have an absolute time stamp,
954 or if the relative time stamp mentions days, months, or years. */
955 if (pc.dates_seen | pc.days_seen | pc.times_seen | pc.rel_day | pc.rel_month | pc.rel_year)
958 /* But if the input explicitly specifies local time with or without
959 DST, give mktime that information. */
960 if (pc.local_zones_seen)
961 tm.tm_isdst = pc.local_isdst;
965 Start = mktime (&tm);
967 if (Start == (time_t) -1)
970 /* Guard against falsely reporting errors near the time_t boundaries
971 when parsing times in other time zones. For example, if the min
972 time_t value is 1970-01-01 00:00:00 UTC and we are 8 hours ahead
973 of UTC, then the min localtime value is 1970-01-01 08:00:00; if
974 we apply mktime to 1970-01-01 00:00:00 we will get an error, so
975 we apply mktime to 1970-01-02 08:00:00 instead and adjust the time
976 zone by 24 hours to compensate. This algorithm assumes that
977 there is no DST transition within a day of the time_t boundaries. */
981 if (tm.tm_year <= EPOCH_YEAR - TM_YEAR_ORIGIN)
984 pc.time_zone += 24 * 60;
989 pc.time_zone -= 24 * 60;
991 Start = mktime (&tm);
994 if (Start == (time_t) -1)
998 if (pc.days_seen && ! pc.dates_seen)
1000 tm.tm_mday += ((pc.day_number - tm.tm_wday + 7) % 7
1001 + 7 * (pc.day_ordinal - (0 < pc.day_ordinal)));
1002 Start = mktime (&tm);
1003 if (Start == (time_t) -1)
1010 struct tm *gmt = gmtime (&Start);
1013 delta = pc.time_zone * 60 + difftm (gmt, &tm);
1014 if ((Start - delta < Start) != (delta < 0))
1015 return -1; /* time_t overflow */
1027 main (int ac, char **av)
1032 printf ("Enter date, or blank line to exit.\n\t> ");
1035 buff[BUFSIZ - 1] = 0;
1036 while (fgets (buff, BUFSIZ - 1, stdin) && buff[0])
1038 d = get_date (buff, 0);
1039 if (d == (time_t) -1)
1040 printf ("Bad format - couldn't convert.\n");
1042 printf ("%s", ctime (&d));
1048 #endif /* defined TEST */