home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / cvs-1.8.7-src.tgz / tar.out / fsf / cvs / lib / getdate.y < prev    next >
Text File  |  1996-09-28  |  25KB  |  1,011 lines

  1. %{
  2. /*
  3. **  Originally written by Steven M. Bellovin <smb@research.att.com> while
  4. **  at the University of North Carolina at Chapel Hill.  Later tweaked by
  5. **  a couple of people on Usenet.  Completely overhauled by Rich $alz
  6. **  <rsalz@bbn.com> and Jim Berets <jberets@bbn.com> in August, 1990;
  7. **  send any email to Rich.
  8. **
  9. **  This grammar has 10 shift/reduce conflicts.
  10. **
  11. **  This code is in the public domain and has no copyright.
  12. */
  13. /* SUPPRESS 287 on yaccpar_sccsid *//* Unused static variable */
  14. /* SUPPRESS 288 on yyerrlab *//* Label unused */
  15.  
  16. #ifdef HAVE_CONFIG_H
  17. #if defined (emacs) || defined (CONFIG_BROKETS)
  18. #include <config.h>
  19. #else
  20. #include "config.h"
  21. #endif
  22. #endif
  23.  
  24. /* Since the code of getdate.y is not included in the Emacs executable
  25.    itself, there is no need to #define static in this file.  Even if
  26.    the code were included in the Emacs executable, it probably
  27.    wouldn't do any harm to #undef it here; this will only cause
  28.    problems if we try to write to a static variable, which I don't
  29.    think this code needs to do.  */
  30. #ifdef emacs
  31. #undef static
  32. #endif
  33.  
  34. #include <stdio.h>
  35. #include <ctype.h>
  36.  
  37. /* The code at the top of get_date which figures out the offset of the
  38.    current time zone checks various CPP symbols to see if special
  39.    tricks are need, but defaults to using the gettimeofday system call.
  40.    Include <sys/time.h> if that will be used.  */
  41.  
  42. #if    defined(vms)
  43.  
  44. #include <types.h>
  45. #include <time.h>
  46.  
  47. #else
  48.  
  49. #include <sys/types.h>
  50.  
  51. #ifdef TIME_WITH_SYS_TIME
  52. #include <sys/time.h>
  53. #include <time.h>
  54. #else
  55. #ifdef HAVE_SYS_TIME_H
  56. #include <sys/time.h>
  57. #else
  58. #include <time.h>
  59. #endif
  60. #endif
  61.  
  62. #ifdef timezone
  63. #undef timezone /* needed for sgi */
  64. #endif
  65.  
  66. #if defined(HAVE_SYS_TIMEB_H)
  67. #include <sys/timeb.h>
  68. #else
  69. /*
  70. ** We use the obsolete `struct timeb' as part of our interface!
  71. ** Since the system doesn't have it, we define it here;
  72. ** our callers must do likewise.
  73. */
  74. struct timeb {
  75.     time_t        time;        /* Seconds since the epoch    */
  76.     unsigned short    millitm;    /* Field not used        */
  77.     short        timezone;    /* Minutes west of GMT        */
  78.     short        dstflag;    /* Field not used        */
  79. };
  80. #endif /* defined(HAVE_SYS_TIMEB_H) */
  81.  
  82. #endif    /* defined(vms) */
  83.  
  84. #if defined (STDC_HEADERS) || defined (USG)
  85. #include <string.h>
  86. #endif
  87.  
  88. /* Some old versions of bison generate parsers that use bcopy.
  89.    That loses on systems that don't provide the function, so we have
  90.    to redefine it here.  */
  91. #if !defined (HAVE_BCOPY) && defined (HAVE_MEMCPY) && !defined (bcopy)
  92. #define bcopy(from, to, len) memcpy ((to), (from), (len))
  93. #endif
  94.  
  95. #if defined (STDC_HEADERS)
  96. #include <stdlib.h>
  97. #endif
  98.  
  99. /* NOTES on rebuilding getdate.c (particularly for inclusion in CVS
  100.    releases):
  101.  
  102.    We don't want to mess with all the portability hassles of alloca.
  103.    In particular, most (all?) versions of bison will use alloca in
  104.    their parser.  If bison works on your system (e.g. it should work
  105.    with gcc), then go ahead and use it, but the more general solution
  106.    is to use byacc instead of bison, which should generate a portable
  107.    parser.  I played with adding "#define alloca dont_use_alloca", to
  108.    give an error if the parser generator uses alloca (and thus detect
  109.    unportable getdate.c's), but that seems to cause as many problems
  110.    as it solves.  */
  111.  
  112. extern struct tm    *gmtime();
  113. extern struct tm    *localtime();
  114.  
  115. #define yyparse getdate_yyparse
  116. #define yylex getdate_yylex
  117. #define yyerror getdate_yyerror
  118.  
  119. #if    !defined(lint) && !defined(SABER)
  120. static char RCS[] = "$CVSid: @(#)getdate.y 1.11 94/09/21 $";
  121. #endif    /* !defined(lint) && !defined(SABER) */
  122.  
  123. static int yylex ();
  124. static int yyerror ();
  125.  
  126. #define EPOCH        1970
  127. #define HOUR(x)        ((time_t)(x) * 60)
  128. #define SECSPERDAY    (24L * 60L * 60L)
  129.  
  130.  
  131. /*
  132. **  An entry in the lexical lookup table.
  133. */
  134. typedef struct _TABLE {
  135.     char    *name;
  136.     int        type;
  137.     time_t    value;
  138. } TABLE;
  139.  
  140.  
  141. /*
  142. **  Daylight-savings mode:  on, off, or not yet known.
  143. */
  144. typedef enum _DSTMODE {
  145.     DSTon, DSToff, DSTmaybe
  146. } DSTMODE;
  147.  
  148. /*
  149. **  Meridian:  am, pm, or 24-hour style.
  150. */
  151. typedef enum _MERIDIAN {
  152.     MERam, MERpm, MER24
  153. } MERIDIAN;
  154.  
  155.  
  156. /*
  157. **  Global variables.  We could get rid of most of these by using a good
  158. **  union as the yacc stack.  (This routine was originally written before
  159. **  yacc had the %union construct.)  Maybe someday; right now we only use
  160. **  the %union very rarely.
  161. */
  162. static char    *yyInput;
  163. static DSTMODE    yyDSTmode;
  164. static time_t    yyDayOrdinal;
  165. static time_t    yyDayNumber;
  166. static int    yyHaveDate;
  167. static int    yyHaveDay;
  168. static int    yyHaveRel;
  169. static int    yyHaveTime;
  170. static int    yyHaveZone;
  171. static time_t    yyTimezone;
  172. static time_t    yyDay;
  173. static time_t    yyHour;
  174. static time_t    yyMinutes;
  175. static time_t    yyMonth;
  176. static time_t    yySeconds;
  177. static time_t    yyYear;
  178. static MERIDIAN    yyMeridian;
  179. static time_t    yyRelMonth;
  180. static time_t    yyRelSeconds;
  181.  
  182. %}
  183.  
  184. %union {
  185.     time_t        Number;
  186.     enum _MERIDIAN    Meridian;
  187. }
  188.  
  189. %token    tAGO tDAY tDAYZONE tID tMERIDIAN tMINUTE_UNIT tMONTH tMONTH_UNIT
  190. %token    tSEC_UNIT tSNUMBER tUNUMBER tZONE tDST
  191.  
  192. %type    <Number>    tDAY tDAYZONE tMINUTE_UNIT tMONTH tMONTH_UNIT
  193. %type    <Number>    tSEC_UNIT tSNUMBER tUNUMBER tZONE
  194. %type    <Meridian>    tMERIDIAN o_merid
  195.  
  196. %%
  197.  
  198. spec    : /* NULL */
  199.     | spec item
  200.     ;
  201.  
  202. item    : time {
  203.         yyHaveTime++;
  204.     }
  205.     | zone {
  206.         yyHaveZone++;
  207.     }
  208.     | date {
  209.         yyHaveDate++;
  210.     }
  211.     | day {
  212.         yyHaveDay++;
  213.     }
  214.     | rel {
  215.         yyHaveRel++;
  216.     }
  217.     | number
  218.     ;
  219.  
  220. time    : tUNUMBER tMERIDIAN {
  221.         yyHour = $1;
  222.         yyMinutes = 0;
  223.         yySeconds = 0;
  224.         yyMeridian = $2;
  225.     }
  226.     | tUNUMBER ':' tUNUMBER o_merid {
  227.         yyHour = $1;
  228.         yyMinutes = $3;
  229.         yySeconds = 0;
  230.         yyMeridian = $4;
  231.     }
  232.     | tUNUMBER ':' tUNUMBER tSNUMBER {
  233.         yyHour = $1;
  234.         yyMinutes = $3;
  235.         yyMeridian = MER24;
  236.         yyDSTmode = DSToff;
  237.         yyTimezone = - ($4 % 100 + ($4 / 100) * 60);
  238.     }
  239.     | tUNUMBER ':' tUNUMBER ':' tUNUMBER o_merid {
  240.         yyHour = $1;
  241.         yyMinutes = $3;
  242.         yySeconds = $5;
  243.         yyMeridian = $6;
  244.     }
  245.     | tUNUMBER ':' tUNUMBER ':' tUNUMBER tSNUMBER {
  246.         yyHour = $1;
  247.         yyMinutes = $3;
  248.         yySeconds = $5;
  249.         yyMeridian = MER24;
  250.         yyDSTmode = DSToff;
  251.         yyTimezone = - ($6 % 100 + ($6 / 100) * 60);
  252.     }
  253.     ;
  254.  
  255. zone    : tZONE {
  256.         yyTimezone = $1;
  257.         yyDSTmode = DSToff;
  258.     }
  259.     | tDAYZONE {
  260.         yyTimezone = $1;
  261.         yyDSTmode = DSTon;
  262.     }
  263.     |
  264.       tZONE tDST {
  265.         yyTimezone = $1;
  266.         yyDSTmode = DSTon;
  267.     }
  268.     ;
  269.  
  270. day    : tDAY {
  271.         yyDayOrdinal = 1;
  272.         yyDayNumber = $1;
  273.     }
  274.     | tDAY ',' {
  275.         yyDayOrdinal = 1;
  276.         yyDayNumber = $1;
  277.     }
  278.     | tUNUMBER tDAY {
  279.         yyDayOrdinal = $1;
  280.         yyDayNumber = $2;
  281.     }
  282.     ;
  283.  
  284. date    : tUNUMBER '/' tUNUMBER {
  285.         yyMonth = $1;
  286.         yyDay = $3;
  287.     }
  288.     | tUNUMBER '/' tUNUMBER '/' tUNUMBER {
  289.         yyMonth = $1;
  290.         yyDay = $3;
  291.         yyYear = $5;
  292.     }
  293.     | tUNUMBER tSNUMBER tSNUMBER {
  294.         /* ISO 8601 format.  yyyy-mm-dd.  */
  295.         yyYear = $1;
  296.         yyMonth = -$2;
  297.         yyDay = -$3;
  298.     }
  299.     | tUNUMBER tMONTH tSNUMBER {
  300.         /* e.g. 17-JUN-1992.  */
  301.         yyDay = $1;
  302.         yyMonth = $2;
  303.         yyYear = -$3;
  304.     }
  305.     | tMONTH tUNUMBER {
  306.         yyMonth = $1;
  307.         yyDay = $2;
  308.     }
  309.     | tMONTH tUNUMBER ',' tUNUMBER {
  310.         yyMonth = $1;
  311.         yyDay = $2;
  312.         yyYear = $4;
  313.     }
  314.     | tUNUMBER tMONTH {
  315.         yyMonth = $2;
  316.         yyDay = $1;
  317.     }
  318.     | tUNUMBER tMONTH tUNUMBER {
  319.         yyMonth = $2;
  320.         yyDay = $1;
  321.         yyYear = $3;
  322.     }
  323.     ;
  324.  
  325. rel    : relunit tAGO {
  326.         yyRelSeconds = -yyRelSeconds;
  327.         yyRelMonth = -yyRelMonth;
  328.     }
  329.     | relunit
  330.     ;
  331.  
  332. relunit    : tUNUMBER tMINUTE_UNIT {
  333.         yyRelSeconds += $1 * $2 * 60L;
  334.     }
  335.     | tSNUMBER tMINUTE_UNIT {
  336.         yyRelSeconds += $1 * $2 * 60L;
  337.     }
  338.     | tMINUTE_UNIT {
  339.         yyRelSeconds += $1 * 60L;
  340.     }
  341.     | tSNUMBER tSEC_UNIT {
  342.         yyRelSeconds += $1;
  343.     }
  344.     | tUNUMBER tSEC_UNIT {
  345.         yyRelSeconds += $1;
  346.     }
  347.     | tSEC_UNIT {
  348.         yyRelSeconds++;
  349.     }
  350.     | tSNUMBER tMONTH_UNIT {
  351.         yyRelMonth += $1 * $2;
  352.     }
  353.     | tUNUMBER tMONTH_UNIT {
  354.         yyRelMonth += $1 * $2;
  355.     }
  356.     | tMONTH_UNIT {
  357.         yyRelMonth += $1;
  358.     }
  359.     ;
  360.  
  361. number    : tUNUMBER {
  362.         if (yyHaveTime && yyHaveDate && !yyHaveRel)
  363.         yyYear = $1;
  364.         else {
  365.         if($1>10000) {
  366.             yyHaveDate++;
  367.             yyDay= ($1)%100;
  368.             yyMonth= ($1/100)%100;
  369.             yyYear = $1/10000;
  370.         }
  371.         else {
  372.             yyHaveTime++;
  373.             if ($1 < 100) {
  374.             yyHour = $1;
  375.             yyMinutes = 0;
  376.             }
  377.             else {
  378.                 yyHour = $1 / 100;
  379.                 yyMinutes = $1 % 100;
  380.             }
  381.             yySeconds = 0;
  382.             yyMeridian = MER24;
  383.             }
  384.         }
  385.     }
  386.     ;
  387.  
  388. o_merid    : /* NULL */ {
  389.         $$ = MER24;
  390.     }
  391.     | tMERIDIAN {
  392.         $$ = $1;
  393.     }
  394.     ;
  395.  
  396. %%
  397.  
  398. /* Month and day table. */
  399. static TABLE const MonthDayTable[] = {
  400.     { "january",    tMONTH,  1 },
  401.     { "february",    tMONTH,  2 },
  402.     { "march",        tMONTH,  3 },
  403.     { "april",        tMONTH,  4 },
  404.     { "may",        tMONTH,  5 },
  405.     { "june",        tMONTH,  6 },
  406.     { "july",        tMONTH,  7 },
  407.     { "august",        tMONTH,  8 },
  408.     { "september",    tMONTH,  9 },
  409.     { "sept",        tMONTH,  9 },
  410.     { "october",    tMONTH, 10 },
  411.     { "november",    tMONTH, 11 },
  412.     { "december",    tMONTH, 12 },
  413.     { "sunday",        tDAY, 0 },
  414.     { "monday",        tDAY, 1 },
  415.     { "tuesday",    tDAY, 2 },
  416.     { "tues",        tDAY, 2 },
  417.     { "wednesday",    tDAY, 3 },
  418.     { "wednes",        tDAY, 3 },
  419.     { "thursday",    tDAY, 4 },
  420.     { "thur",        tDAY, 4 },
  421.     { "thurs",        tDAY, 4 },
  422.     { "friday",        tDAY, 5 },
  423.     { "saturday",    tDAY, 6 },
  424.     { NULL }
  425. };
  426.  
  427. /* Time units table. */
  428. static TABLE const UnitsTable[] = {
  429.     { "year",        tMONTH_UNIT,    12 },
  430.     { "month",        tMONTH_UNIT,    1 },
  431.     { "fortnight",    tMINUTE_UNIT,    14 * 24 * 60 },
  432.     { "week",        tMINUTE_UNIT,    7 * 24 * 60 },
  433.     { "day",        tMINUTE_UNIT,    1 * 24 * 60 },
  434.     { "hour",        tMINUTE_UNIT,    60 },
  435.     { "minute",        tMINUTE_UNIT,    1 },
  436.     { "min",        tMINUTE_UNIT,    1 },
  437.     { "second",        tSEC_UNIT,    1 },
  438.     { "sec",        tSEC_UNIT,    1 },
  439.     { NULL }
  440. };
  441.  
  442. /* Assorted relative-time words. */
  443. static TABLE const OtherTable[] = {
  444.     { "tomorrow",    tMINUTE_UNIT,    1 * 24 * 60 },
  445.     { "yesterday",    tMINUTE_UNIT,    -1 * 24 * 60 },
  446.     { "today",        tMINUTE_UNIT,    0 },
  447.     { "now",        tMINUTE_UNIT,    0 },
  448.     { "last",        tUNUMBER,    -1 },
  449.     { "this",        tMINUTE_UNIT,    0 },
  450.     { "next",        tUNUMBER,    2 },
  451.     { "first",        tUNUMBER,    1 },
  452. /*  { "second",        tUNUMBER,    2 }, */
  453.     { "third",        tUNUMBER,    3 },
  454.     { "fourth",        tUNUMBER,    4 },
  455.     { "fifth",        tUNUMBER,    5 },
  456.     { "sixth",        tUNUMBER,    6 },
  457.     { "seventh",    tUNUMBER,    7 },
  458.     { "eighth",        tUNUMBER,    8 },
  459.     { "ninth",        tUNUMBER,    9 },
  460.     { "tenth",        tUNUMBER,    10 },
  461.     { "eleventh",    tUNUMBER,    11 },
  462.     { "twelfth",    tUNUMBER,    12 },
  463.     { "ago",        tAGO,    1 },
  464.     { NULL }
  465. };
  466.  
  467. /* The timezone table. */
  468. /* Some of these are commented out because a time_t can't store a float. */
  469. static TABLE const TimezoneTable[] = {
  470.     { "gmt",    tZONE,     HOUR( 0) },    /* Greenwich Mean */
  471.     { "ut",    tZONE,     HOUR( 0) },    /* Universal (Coordinated) */
  472.     { "utc",    tZONE,     HOUR( 0) },
  473.     { "wet",    tZONE,     HOUR( 0) },    /* Western European */
  474.     { "bst",    tDAYZONE,  HOUR( 0) },    /* British Summer */
  475.     { "wat",    tZONE,     HOUR( 1) },    /* West Africa */
  476.     { "at",    tZONE,     HOUR( 2) },    /* Azores */
  477. #if    0
  478.     /* For completeness.  BST is also British Summer, and GST is
  479.      * also Guam Standard. */
  480.     { "bst",    tZONE,     HOUR( 3) },    /* Brazil Standard */
  481.     { "gst",    tZONE,     HOUR( 3) },    /* Greenland Standard */
  482. #endif
  483. #if 0
  484.     { "nft",    tZONE,     HOUR(3.5) },    /* Newfoundland */
  485.     { "nst",    tZONE,     HOUR(3.5) },    /* Newfoundland Standard */
  486.     { "ndt",    tDAYZONE,  HOUR(3.5) },    /* Newfoundland Daylight */
  487. #endif
  488.     { "ast",    tZONE,     HOUR( 4) },    /* Atlantic Standard */
  489.     { "adt",    tDAYZONE,  HOUR( 4) },    /* Atlantic Daylight */
  490.     { "est",    tZONE,     HOUR( 5) },    /* Eastern Standard */
  491.     { "edt",    tDAYZONE,  HOUR( 5) },    /* Eastern Daylight */
  492.     { "cst",    tZONE,     HOUR( 6) },    /* Central Standard */
  493.     { "cdt",    tDAYZONE,  HOUR( 6) },    /* Central Daylight */
  494.     { "mst",    tZONE,     HOUR( 7) },    /* Mountain Standard */
  495.     { "mdt",    tDAYZONE,  HOUR( 7) },    /* Mountain Daylight */
  496.     { "pst",    tZONE,     HOUR( 8) },    /* Pacific Standard */
  497.     { "pdt",    tDAYZONE,  HOUR( 8) },    /* Pacific Daylight */
  498.     { "yst",    tZONE,     HOUR( 9) },    /* Yukon Standard */
  499.     { "ydt",    tDAYZONE,  HOUR( 9) },    /* Yukon Daylight */
  500.     { "hst",    tZONE,     HOUR(10) },    /* Hawaii Standard */
  501.     { "hdt",    tDAYZONE,  HOUR(10) },    /* Hawaii Daylight */
  502.     { "cat",    tZONE,     HOUR(10) },    /* Central Alaska */
  503.     { "ahst",    tZONE,     HOUR(10) },    /* Alaska-Hawaii Standard */
  504.     { "nt",    tZONE,     HOUR(11) },    /* Nome */
  505.     { "idlw",    tZONE,     HOUR(12) },    /* International Date Line West */
  506.     { "cet",    tZONE,     -HOUR(1) },    /* Central European */
  507.     { "met",    tZONE,     -HOUR(1) },    /* Middle European */
  508.     { "mewt",    tZONE,     -HOUR(1) },    /* Middle European Winter */
  509.     { "mest",    tDAYZONE,  -HOUR(1) },    /* Middle European Summer */
  510.     { "swt",    tZONE,     -HOUR(1) },    /* Swedish Winter */
  511.     { "sst",    tDAYZONE,  -HOUR(1) },    /* Swedish Summer */
  512.     { "fwt",    tZONE,     -HOUR(1) },    /* French Winter */
  513.     { "fst",    tDAYZONE,  -HOUR(1) },    /* French Summer */
  514.     { "eet",    tZONE,     -HOUR(2) },    /* Eastern Europe, USSR Zone 1 */
  515.     { "bt",    tZONE,     -HOUR(3) },    /* Baghdad, USSR Zone 2 */
  516. #if 0
  517.     { "it",    tZONE,     -HOUR(3.5) },/* Iran */
  518. #endif
  519.     { "zp4",    tZONE,     -HOUR(4) },    /* USSR Zone 3 */
  520.     { "zp5",    tZONE,     -HOUR(5) },    /* USSR Zone 4 */
  521. #if 0
  522.     { "ist",    tZONE,     -HOUR(5.5) },/* Indian Standard */
  523. #endif
  524.     { "zp6",    tZONE,     -HOUR(6) },    /* USSR Zone 5 */
  525. #if    0
  526.     /* For completeness.  NST is also Newfoundland Stanard, and SST is
  527.      * also Swedish Summer. */
  528.     { "nst",    tZONE,     -HOUR(6.5) },/* North Sumatra */
  529.     { "sst",    tZONE,     -HOUR(7) },    /* South Sumatra, USSR Zone 6 */
  530. #endif    /* 0 */
  531.     { "wast",    tZONE,     -HOUR(7) },    /* West Australian Standard */
  532.     { "wadt",    tDAYZONE,  -HOUR(7) },    /* West Australian Daylight */
  533. #if 0
  534.     { "jt",    tZONE,     -HOUR(7.5) },/* Java (3pm in Cronusland!) */
  535. #endif
  536.     { "cct",    tZONE,     -HOUR(8) },    /* China Coast, USSR Zone 7 */
  537.     { "jst",    tZONE,     -HOUR(9) },    /* Japan Standard, USSR Zone 8 */
  538. #if 0
  539.     { "cast",    tZONE,     -HOUR(9.5) },/* Central Australian Standard */
  540.     { "cadt",    tDAYZONE,  -HOUR(9.5) },/* Central Australian Daylight */
  541. #endif
  542.     { "east",    tZONE,     -HOUR(10) },    /* Eastern Australian Standard */
  543.     { "eadt",    tDAYZONE,  -HOUR(10) },    /* Eastern Australian Daylight */
  544.     { "gst",    tZONE,     -HOUR(10) },    /* Guam Standard, USSR Zone 9 */
  545.     { "nzt",    tZONE,     -HOUR(12) },    /* New Zealand */
  546.     { "nzst",    tZONE,     -HOUR(12) },    /* New Zealand Standard */
  547.     { "nzdt",    tDAYZONE,  -HOUR(12) },    /* New Zealand Daylight */
  548.     { "idle",    tZONE,     -HOUR(12) },    /* International Date Line East */
  549.     {  NULL  }
  550. };
  551.  
  552. /* Military timezone table. */
  553. static TABLE const MilitaryTable[] = {
  554.     { "a",    tZONE,    HOUR(  1) },
  555.     { "b",    tZONE,    HOUR(  2) },
  556.     { "c",    tZONE,    HOUR(  3) },
  557.     { "d",    tZONE,    HOUR(  4) },
  558.     { "e",    tZONE,    HOUR(  5) },
  559.     { "f",    tZONE,    HOUR(  6) },
  560.     { "g",    tZONE,    HOUR(  7) },
  561.     { "h",    tZONE,    HOUR(  8) },
  562.     { "i",    tZONE,    HOUR(  9) },
  563.     { "k",    tZONE,    HOUR( 10) },
  564.     { "l",    tZONE,    HOUR( 11) },
  565.     { "m",    tZONE,    HOUR( 12) },
  566.     { "n",    tZONE,    HOUR(- 1) },
  567.     { "o",    tZONE,    HOUR(- 2) },
  568.     { "p",    tZONE,    HOUR(- 3) },
  569.     { "q",    tZONE,    HOUR(- 4) },
  570.     { "r",    tZONE,    HOUR(- 5) },
  571.     { "s",    tZONE,    HOUR(- 6) },
  572.     { "t",    tZONE,    HOUR(- 7) },
  573.     { "u",    tZONE,    HOUR(- 8) },
  574.     { "v",    tZONE,    HOUR(- 9) },
  575.     { "w",    tZONE,    HOUR(-10) },
  576.     { "x",    tZONE,    HOUR(-11) },
  577.     { "y",    tZONE,    HOUR(-12) },
  578.     { "z",    tZONE,    HOUR(  0) },
  579.     { NULL }
  580. };
  581.  
  582.  
  583.  
  584.  
  585. /* ARGSUSED */
  586. static int
  587. yyerror(s)
  588.     char    *s;
  589. {
  590.   return 0;
  591. }
  592.  
  593.  
  594. static time_t
  595. ToSeconds(Hours, Minutes, Seconds, Meridian)
  596.     time_t    Hours;
  597.     time_t    Minutes;
  598.     time_t    Seconds;
  599.     MERIDIAN    Meridian;
  600. {
  601.     if (Minutes < 0 || Minutes > 59 || Seconds < 0 || Seconds > 59)
  602.     return -1;
  603.     switch (Meridian) {
  604.     case MER24:
  605.     if (Hours < 0 || Hours > 23)
  606.         return -1;
  607.     return (Hours * 60L + Minutes) * 60L + Seconds;
  608.     case MERam:
  609.     if (Hours < 1 || Hours > 12)
  610.         return -1;
  611.     return (Hours * 60L + Minutes) * 60L + Seconds;
  612.     case MERpm:
  613.     if (Hours < 1 || Hours > 12)
  614.         return -1;
  615.     return ((Hours + 12) * 60L + Minutes) * 60L + Seconds;
  616.     default:
  617.     abort ();
  618.     }
  619.     /* NOTREACHED */
  620. }
  621.  
  622.  
  623. static time_t
  624. Convert(Month, Day, Year, Hours, Minutes, Seconds, Meridian, DSTmode)
  625.     time_t    Month;
  626.     time_t    Day;
  627.     time_t    Year;
  628.     time_t    Hours;
  629.     time_t    Minutes;
  630.     time_t    Seconds;
  631.     MERIDIAN    Meridian;
  632.     DSTMODE    DSTmode;
  633. {
  634.     static int DaysInMonth[12] = {
  635.     31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
  636.     };
  637.     time_t    tod;
  638.     time_t    Julian;
  639.     int        i;
  640.  
  641.     if (Year < 0)
  642.     Year = -Year;
  643.     if (Year < 100)
  644.     Year += 1900;
  645.     DaysInMonth[1] = Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0)
  646.             ? 29 : 28;
  647.     if (Year < EPOCH || Year > 1999
  648.      || Month < 1 || Month > 12
  649.      /* Lint fluff:  "conversion from long may lose accuracy" */
  650.      || Day < 1 || Day > DaysInMonth[(int)--Month])
  651.     return -1;
  652.  
  653.     for (Julian = Day - 1, i = 0; i < Month; i++)
  654.     Julian += DaysInMonth[i];
  655.     for (i = EPOCH; i < Year; i++)
  656.     Julian += 365 + (i % 4 == 0);
  657.     Julian *= SECSPERDAY;
  658.     Julian += yyTimezone * 60L;
  659.     if ((tod = ToSeconds(Hours, Minutes, Seconds, Meridian)) < 0)
  660.     return -1;
  661.     Julian += tod;
  662.     if (DSTmode == DSTon
  663.      || (DSTmode == DSTmaybe && localtime(&Julian)->tm_isdst))
  664.     Julian -= 60 * 60;
  665.     return Julian;
  666. }
  667.  
  668.  
  669. static time_t
  670. DSTcorrect(Start, Future)
  671.     time_t    Start;
  672.     time_t    Future;
  673. {
  674.     time_t    StartDay;
  675.     time_t    FutureDay;
  676.  
  677.     StartDay = (localtime(&Start)->tm_hour + 1) % 24;
  678.     FutureDay = (localtime(&Future)->tm_hour + 1) % 24;
  679.     return (Future - Start) + (StartDay - FutureDay) * 60L * 60L;
  680. }
  681.  
  682.  
  683. static time_t
  684. RelativeDate(Start, DayOrdinal, DayNumber)
  685.     time_t    Start;
  686.     time_t    DayOrdinal;
  687.     time_t    DayNumber;
  688. {
  689.     struct tm    *tm;
  690.     time_t    now;
  691.  
  692.     now = Start;
  693.     tm = localtime(&now);
  694.     now += SECSPERDAY * ((DayNumber - tm->tm_wday + 7) % 7);
  695.     now += 7 * SECSPERDAY * (DayOrdinal <= 0 ? DayOrdinal : DayOrdinal - 1);
  696.     return DSTcorrect(Start, now);
  697. }
  698.  
  699.  
  700. static time_t
  701. RelativeMonth(Start, RelMonth)
  702.     time_t    Start;
  703.     time_t    RelMonth;
  704. {
  705.     struct tm    *tm;
  706.     time_t    Month;
  707.     time_t    Year;
  708.  
  709.     if (RelMonth == 0)
  710.     return 0;
  711.     tm = localtime(&Start);
  712.     Month = 12 * tm->tm_year + tm->tm_mon + RelMonth;
  713.     Year = Month / 12;
  714.     Month = Month % 12 + 1;
  715.     return DSTcorrect(Start,
  716.         Convert(Month, (time_t)tm->tm_mday, Year,
  717.         (time_t)tm->tm_hour, (time_t)tm->tm_min, (time_t)tm->tm_sec,
  718.         MER24, DSTmaybe));
  719. }
  720.  
  721.  
  722. static int
  723. LookupWord(buff)
  724.     char        *buff;
  725. {
  726.     register char    *p;
  727.     register char    *q;
  728.     register const TABLE    *tp;
  729.     int            i;
  730.     int            abbrev;
  731.  
  732.     /* Make it lowercase. */
  733.     for (p = buff; *p; p++)
  734.     if (isupper(*p))
  735.         *p = tolower(*p);
  736.  
  737.     if (strcmp(buff, "am") == 0 || strcmp(buff, "a.m.") == 0) {
  738.     yylval.Meridian = MERam;
  739.     return tMERIDIAN;
  740.     }
  741.     if (strcmp(buff, "pm") == 0 || strcmp(buff, "p.m.") == 0) {
  742.     yylval.Meridian = MERpm;
  743.     return tMERIDIAN;
  744.     }
  745.  
  746.     /* See if we have an abbreviation for a month. */
  747.     if (strlen(buff) == 3)
  748.     abbrev = 1;
  749.     else if (strlen(buff) == 4 && buff[3] == '.') {
  750.     abbrev = 1;
  751.     buff[3] = '\0';
  752.     }
  753.     else
  754.     abbrev = 0;
  755.  
  756.     for (tp = MonthDayTable; tp->name; tp++) {
  757.     if (abbrev) {
  758.         if (strncmp(buff, tp->name, 3) == 0) {
  759.         yylval.Number = tp->value;
  760.         return tp->type;
  761.         }
  762.     }
  763.     else if (strcmp(buff, tp->name) == 0) {
  764.         yylval.Number = tp->value;
  765.         return tp->type;
  766.     }
  767.     }
  768.  
  769.     for (tp = TimezoneTable; tp->name; tp++)
  770.     if (strcmp(buff, tp->name) == 0) {
  771.         yylval.Number = tp->value;
  772.         return tp->type;
  773.     }
  774.  
  775.     if (strcmp(buff, "dst") == 0) 
  776.     return tDST;
  777.  
  778.     for (tp = UnitsTable; tp->name; tp++)
  779.     if (strcmp(buff, tp->name) == 0) {
  780.         yylval.Number = tp->value;
  781.         return tp->type;
  782.     }
  783.  
  784.     /* Strip off any plural and try the units table again. */
  785.     i = strlen(buff) - 1;
  786.     if (buff[i] == 's') {
  787.     buff[i] = '\0';
  788.     for (tp = UnitsTable; tp->name; tp++)
  789.         if (strcmp(buff, tp->name) == 0) {
  790.         yylval.Number = tp->value;
  791.         return tp->type;
  792.         }
  793.     buff[i] = 's';        /* Put back for "this" in OtherTable. */
  794.     }
  795.  
  796.     for (tp = OtherTable; tp->name; tp++)
  797.     if (strcmp(buff, tp->name) == 0) {
  798.         yylval.Number = tp->value;
  799.         return tp->type;
  800.     }
  801.  
  802.     /* Military timezones. */
  803.     if (buff[1] == '\0' && isalpha(*buff)) {
  804.     for (tp = MilitaryTable; tp->name; tp++)
  805.         if (strcmp(buff, tp->name) == 0) {
  806.         yylval.Number = tp->value;
  807.         return tp->type;
  808.         }
  809.     }
  810.  
  811.     /* Drop out any periods and try the timezone table again. */
  812.     for (i = 0, p = q = buff; *q; q++)
  813.     if (*q != '.')
  814.         *p++ = *q;
  815.     else
  816.         i++;
  817.     *p = '\0';
  818.     if (i)
  819.     for (tp = TimezoneTable; tp->name; tp++)
  820.         if (strcmp(buff, tp->name) == 0) {
  821.         yylval.Number = tp->value;
  822.         return tp->type;
  823.         }
  824.  
  825.     return tID;
  826. }
  827.  
  828.  
  829. static int
  830. yylex()
  831. {
  832.     register char    c;
  833.     register char    *p;
  834.     char        buff[20];
  835.     int            Count;
  836.     int            sign;
  837.  
  838.     for ( ; ; ) {
  839.     while (isspace(*yyInput))
  840.         yyInput++;
  841.  
  842.     if (isdigit(c = *yyInput) || c == '-' || c == '+') {
  843.         if (c == '-' || c == '+') {
  844.         sign = c == '-' ? -1 : 1;
  845.         if (!isdigit(*++yyInput))
  846.             /* skip the '-' sign */
  847.             continue;
  848.         }
  849.         else
  850.         sign = 0;
  851.         for (yylval.Number = 0; isdigit(c = *yyInput++); )
  852.         yylval.Number = 10 * yylval.Number + c - '0';
  853.         yyInput--;
  854.         if (sign < 0)
  855.         yylval.Number = -yylval.Number;
  856.         return sign ? tSNUMBER : tUNUMBER;
  857.     }
  858.     if (isalpha(c)) {
  859.         for (p = buff; isalpha(c = *yyInput++) || c == '.'; )
  860.         if (p < &buff[sizeof buff - 1])
  861.             *p++ = c;
  862.         *p = '\0';
  863.         yyInput--;
  864.         return LookupWord(buff);
  865.     }
  866.     if (c != '(')
  867.         return *yyInput++;
  868.     Count = 0;
  869.     do {
  870.         c = *yyInput++;
  871.         if (c == '\0')
  872.         return c;
  873.         if (c == '(')
  874.         Count++;
  875.         else if (c == ')')
  876.         Count--;
  877.     } while (Count > 0);
  878.     }
  879. }
  880.  
  881. #define TM_YEAR_ORIGIN 1900
  882.  
  883. /* Yield A - B, measured in seconds.  */
  884. static long
  885. difftm (a, b)
  886.      struct tm *a, *b;
  887. {
  888.   int ay = a->tm_year + (TM_YEAR_ORIGIN - 1);
  889.   int by = b->tm_year + (TM_YEAR_ORIGIN - 1);
  890.   int days = (
  891.           /* difference in day of year */
  892.           a->tm_yday - b->tm_yday
  893.           /* + intervening leap days */
  894.           +  ((ay >> 2) - (by >> 2))
  895.           -  (ay/100 - by/100)
  896.           +  ((ay/100 >> 2) - (by/100 >> 2))
  897.           /* + difference in years * 365 */
  898.           +  (long)(ay-by) * 365
  899.           );
  900.   return (60*(60*(24*days + (a->tm_hour - b->tm_hour))
  901.           + (a->tm_min - b->tm_min))
  902.       + (a->tm_sec - b->tm_sec));
  903. }
  904.  
  905. time_t
  906. get_date(p, now)
  907.     char        *p;
  908.     struct timeb    *now;
  909. {
  910.     struct tm        *tm, gmt;
  911.     struct timeb    ftz;
  912.     time_t        Start;
  913.     time_t        tod;
  914.     time_t nowtime;
  915.  
  916.     yyInput = p;
  917.     if (now == NULL) {
  918.         now = &ftz;
  919.     (void)time (&nowtime);
  920.  
  921.     if (! (tm = gmtime (&nowtime)))
  922.         return -1;
  923.     gmt = *tm;    /* Make a copy, in case localtime modifies *tm.  */
  924.  
  925.     if (! (tm = localtime (&nowtime)))
  926.         return -1;
  927.     
  928.     ftz.timezone = difftm (&gmt, tm) / 60;
  929.     if(tm->tm_isdst)
  930.         ftz.timezone += 60;
  931.     }
  932.     else
  933.     {
  934.     nowtime = now->time;
  935.     }
  936.  
  937.     tm = localtime(&nowtime);
  938.     yyYear = tm->tm_year;
  939.     yyMonth = tm->tm_mon + 1;
  940.     yyDay = tm->tm_mday;
  941.     yyTimezone = now->timezone;
  942.     yyDSTmode = DSTmaybe;
  943.     yyHour = 0;
  944.     yyMinutes = 0;
  945.     yySeconds = 0;
  946.     yyMeridian = MER24;
  947.     yyRelSeconds = 0;
  948.     yyRelMonth = 0;
  949.     yyHaveDate = 0;
  950.     yyHaveDay = 0;
  951.     yyHaveRel = 0;
  952.     yyHaveTime = 0;
  953.     yyHaveZone = 0;
  954.  
  955.     if (yyparse()
  956.      || yyHaveTime > 1 || yyHaveZone > 1 || yyHaveDate > 1 || yyHaveDay > 1)
  957.     return -1;
  958.  
  959.     if (yyHaveDate || yyHaveTime || yyHaveDay) {
  960.     Start = Convert(yyMonth, yyDay, yyYear, yyHour, yyMinutes, yySeconds,
  961.             yyMeridian, yyDSTmode);
  962.     if (Start < 0)
  963.         return -1;
  964.     }
  965.     else {
  966.     Start = nowtime;
  967.     if (!yyHaveRel)
  968.         Start -= ((tm->tm_hour * 60L + tm->tm_min) * 60L) + tm->tm_sec;
  969.     }
  970.  
  971.     Start += yyRelSeconds;
  972.     Start += RelativeMonth(Start, yyRelMonth);
  973.  
  974.     if (yyHaveDay && !yyHaveDate) {
  975.     tod = RelativeDate(Start, yyDayOrdinal, yyDayNumber);
  976.     Start += tod;
  977.     }
  978.  
  979.     /* Have to do *something* with a legitimate -1 so it's distinguishable
  980.      * from the error return value.  (Alternately could set errno on error.) */
  981.     return Start == -1 ? 0 : Start;
  982. }
  983.  
  984.  
  985. #if    defined(TEST)
  986.  
  987. /* ARGSUSED */
  988. int
  989. main(ac, av)
  990.     int        ac;
  991.     char    *av[];
  992. {
  993.     char    buff[128];
  994.     time_t    d;
  995.  
  996.     (void)printf("Enter date, or blank line to exit.\n\t> ");
  997.     (void)fflush(stdout);
  998.     while (gets(buff) && buff[0]) {
  999.     d = get_date(buff, (struct timeb *)NULL);
  1000.     if (d == -1)
  1001.         (void)printf("Bad format - couldn't convert.\n");
  1002.     else
  1003.         (void)printf("%s", ctime(&d));
  1004.     (void)printf("\t> ");
  1005.     (void)fflush(stdout);
  1006.     }
  1007.     exit(0);
  1008.     /* NOTREACHED */
  1009. }
  1010. #endif    /* defined(TEST) */
  1011.