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