home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / g / gtak212.zip / 1.10 / getdate.y < prev    next >
Text File  |  1992-09-16  |  22KB  |  905 lines

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