PageRenderTime 58ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/opensource.apple.com/source/curl/curl-5/curl/lib/getdate.y

#
Happy | 1108 lines | 1016 code | 92 blank | 0 comment | 0 complexity | a882c482c0186e4fc36c21ef2ef78c7e MD5 | raw file
Possible License(s): LGPL-2.0, MPL-2.0, GPL-2.0, ISC, LGPL-2.1, Apache-2.0, MPL-2.0-no-copyleft-exception, BSD-3-Clause, WTFPL, MIT, AGPL-1.0, AGPL-3.0
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  4. <head>
  5. <title>getdate.y</title>
  6. <style type="text/css">
  7. .enscript-comment { font-style: italic; color: rgb(178,34,34); }
  8. .enscript-function-name { font-weight: bold; color: rgb(0,0,255); }
  9. .enscript-variable-name { font-weight: bold; color: rgb(184,134,11); }
  10. .enscript-keyword { font-weight: bold; color: rgb(160,32,240); }
  11. .enscript-reference { font-weight: bold; color: rgb(95,158,160); }
  12. .enscript-string { font-weight: bold; color: rgb(188,143,143); }
  13. .enscript-builtin { font-weight: bold; color: rgb(218,112,214); }
  14. .enscript-type { font-weight: bold; color: rgb(34,139,34); }
  15. .enscript-highlight { text-decoration: underline; color: 0; }
  16. </style>
  17. </head>
  18. <body id="top">
  19. <h1 style="margin:8px;" id="f1">getdate.y&nbsp;&nbsp;&nbsp;<span style="font-weight: normal; font-size: 0.5em;">[<a href="?txt">plain text</a>]</span></h1>
  20. <hr/>
  21. <div></div>
  22. <pre>
  23. %{
  24. /*
  25. ** Originally written by Steven M. Bellovin &lt;<a href="mailto:smb@research.att.com">smb@research.att.com</a>&gt; while
  26. ** at the University of North Carolina at Chapel Hill. Later tweaked by
  27. ** a couple of people on Usenet. Completely overhauled by Rich $alz
  28. ** &lt;<a href="mailto:rsalz@bbn.com">rsalz@bbn.com</a>&gt; and Jim Berets &lt;<a href="mailto:jberets@bbn.com">jberets@bbn.com</a>&gt; in August, 1990.
  29. **
  30. ** This code is in the public domain and has no copyright.
  31. */
  32. #ifdef HAVE_CONFIG_H
  33. # include &quot;config.h&quot;
  34. # ifdef HAVE_ALLOCA_H
  35. # include &lt;alloca.h&gt;
  36. # endif
  37. # ifdef NEED_REENTRANT
  38. # define _REENTRANT /* Necessary to use in Solaris, since the silly guys at
  39. Sun made the localtime_r() prototype dependent on it
  40. (or _POSIX_C_SOURCE or _POSIX_PTHREAD_SEMANTICS) */
  41. # endif
  42. # ifdef HAVE_TIME_H
  43. # include &lt;time.h&gt;
  44. # endif
  45. #endif
  46. /* Since the code of getdate.y is not included in the Emacs executable
  47. itself, there is no need to #define static in this file. Even if
  48. the code were included in the Emacs executable, it probably
  49. wouldn't do any harm to #undef it here; this will only cause
  50. problems if we try to write to a static variable, which I don't
  51. think this code needs to do. */
  52. #ifdef emacs
  53. # undef static
  54. #endif
  55. #ifdef __APPLE__
  56. #include &lt;sys/types.h&gt;
  57. #include &lt;sys/malloc.h&gt;
  58. #else
  59. #endif
  60. #include &lt;string.h&gt;
  61. #include &lt;stdio.h&gt;
  62. #include &lt;ctype.h&gt;
  63. #if HAVE_STDLIB_H
  64. # include &lt;stdlib.h&gt; /* for `free'; used by Bison 1.27 */
  65. #else
  66. #ifdef HAVE_MALLOC_H
  67. #include &lt;malloc.h&gt;
  68. #endif
  69. #endif
  70. #if defined (STDC_HEADERS) || (!defined (isascii) &amp;&amp; !defined (HAVE_ISASCII))
  71. # define IN_CTYPE_DOMAIN(c) 1
  72. #else
  73. # define IN_CTYPE_DOMAIN(c) isascii(c)
  74. #endif
  75. #define ISSPACE(c) (IN_CTYPE_DOMAIN (c) &amp;&amp; isspace (c))
  76. #define ISALPHA(c) (IN_CTYPE_DOMAIN (c) &amp;&amp; isalpha (c))
  77. #define ISUPPER(c) (IN_CTYPE_DOMAIN (c) &amp;&amp; isupper (c))
  78. #define ISDIGIT_LOCALE(c) (IN_CTYPE_DOMAIN (c) &amp;&amp; isdigit (c))
  79. /* ISDIGIT differs from ISDIGIT_LOCALE, as follows:
  80. - Its arg may be any int or unsigned int; it need not be an unsigned char.
  81. - It's guaranteed to evaluate its argument exactly once.
  82. - It's typically faster.
  83. Posix 1003.2-1992 section 2.5.2.1 page 50 lines 1556-1558 says that
  84. only '0' through '9' are digits. Prefer ISDIGIT to ISDIGIT_LOCALE unless
  85. it's important to use the locale's definition of `digit' even when the
  86. host does not conform to Posix. */
  87. #define ISDIGIT(c) ((unsigned) (c) - '0' &lt;= 9)
  88. #if defined (STDC_HEADERS) || defined (USG)
  89. # include &lt;string.h&gt;
  90. #endif
  91. /* The last #include file should be: */
  92. #ifdef MALLOCDEBUG
  93. #include &quot;memdebug.h&quot;
  94. #endif
  95. #if __GNUC__ &lt; 2 || (__GNUC__ == 2 &amp;&amp; __GNUC_MINOR__ &lt; 7)
  96. # define __attribute__(x)
  97. #endif
  98. #ifndef ATTRIBUTE_UNUSED
  99. # define ATTRIBUTE_UNUSED __attribute__ ((__unused__))
  100. #endif
  101. /* Some old versions of bison generate parsers that use bcopy.
  102. That loses on systems that don't provide the function, so we have
  103. to redefine it here. */
  104. #if !defined (HAVE_BCOPY) &amp;&amp; defined (HAVE_MEMCPY) &amp;&amp; !defined (bcopy)
  105. # define bcopy(from, to, len) memcpy ((to), (from), (len))
  106. #endif
  107. /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
  108. as well as gratuitiously global symbol names, so we can have multiple
  109. yacc generated parsers in the same program. Note that these are only
  110. the variables produced by yacc. If other parser generators (bison,
  111. byacc, etc) produce additional global names that conflict at link time,
  112. then those parser generators need to be fixed instead of adding those
  113. names to this list. */
  114. #define yymaxdepth Curl_gd_maxdepth
  115. #define yyparse Curl_gd_parse
  116. #define yylex Curl_gd_lex
  117. #define yyerror Curl_gd_error
  118. #define yylval Curl_gd_lval
  119. #define yychar Curl_gd_char
  120. #define yydebug Curl_gd_debug
  121. #define yypact Curl_gd_pact
  122. #define yyr1 Curl_gd_r1
  123. #define yyr2 Curl_gd_r2
  124. #define yydef Curl_gd_def
  125. #define yychk Curl_gd_chk
  126. #define yypgo Curl_gd_pgo
  127. #define yyact Curl_gd_act
  128. #define yyexca Curl_gd_exca
  129. #define yyerrflag Curl_gd_errflag
  130. #define yynerrs Curl_gd_nerrs
  131. #define yyps Curl_gd_ps
  132. #define yypv Curl_gd_pv
  133. #define yys Curl_gd_s
  134. #define yy_yys Curl_gd_yys
  135. #define yystate Curl_gd_state
  136. #define yytmp Curl_gd_tmp
  137. #define yyv Curl_gd_v
  138. #define yy_yyv Curl_gd_yyv
  139. #define yyval Curl_gd_val
  140. #define yylloc Curl_gd_lloc
  141. #define yyreds Curl_gd_reds /* With YYDEBUG defined */
  142. #define yytoks Curl_gd_toks /* With YYDEBUG defined */
  143. #define yylhs Curl_gd_yylhs
  144. #define yylen Curl_gd_yylen
  145. #define yydefred Curl_gd_yydefred
  146. #define yydgoto Curl_gd_yydgoto
  147. #define yysindex Curl_gd_yysindex
  148. #define yyrindex Curl_gd_yyrindex
  149. #define yygindex Curl_gd_yygindex
  150. #define yytable Curl_gd_yytable
  151. #define yycheck Curl_gd_yycheck
  152. static int yylex ();
  153. static int yyerror ();
  154. #define EPOCH 1970
  155. #define HOUR(x) ((x) * 60)
  156. #define MAX_BUFF_LEN 128 /* size of buffer to read the date into */
  157. /*
  158. ** An entry in the lexical lookup table.
  159. */
  160. typedef struct _TABLE {
  161. const char *name;
  162. int type;
  163. int value;
  164. } TABLE;
  165. /*
  166. ** Meridian: am, pm, or 24-hour style.
  167. */
  168. typedef enum _MERIDIAN {
  169. MERam, MERpm, MER24
  170. } MERIDIAN;
  171. /*
  172. ** Global variables. We could get rid of most of these by using a good
  173. ** union as the yacc stack. (This routine was originally written before
  174. ** yacc had the %union construct.) Maybe someday; right now we only use
  175. ** the %union very rarely.
  176. */
  177. static const char *yyInput;
  178. static int yyDayOrdinal;
  179. static int yyDayNumber;
  180. static int yyHaveDate;
  181. static int yyHaveDay;
  182. static int yyHaveRel;
  183. static int yyHaveTime;
  184. static int yyHaveZone;
  185. static int yyTimezone;
  186. static int yyDay;
  187. static int yyHour;
  188. static int yyMinutes;
  189. static int yyMonth;
  190. static int yySeconds;
  191. static int yyYear;
  192. static MERIDIAN yyMeridian;
  193. static int yyRelDay;
  194. static int yyRelHour;
  195. static int yyRelMinutes;
  196. static int yyRelMonth;
  197. static int yyRelSeconds;
  198. static int yyRelYear;
  199. %}
  200. /* This grammar has 13 shift/reduce conflicts. */
  201. %expect 13
  202. %union {
  203. int Number;
  204. enum _MERIDIAN Meridian;
  205. }
  206. %token tAGO tDAY tDAY_UNIT tDAYZONE tDST tHOUR_UNIT tID
  207. %token tMERIDIAN tMINUTE_UNIT tMONTH tMONTH_UNIT
  208. %token tSEC_UNIT tSNUMBER tUNUMBER tYEAR_UNIT tZONE
  209. %type &lt;Number&gt; tDAY tDAY_UNIT tDAYZONE tHOUR_UNIT tMINUTE_UNIT
  210. %type &lt;Number&gt; tMONTH tMONTH_UNIT
  211. %type &lt;Number&gt; tSEC_UNIT tSNUMBER tUNUMBER tYEAR_UNIT tZONE
  212. %type &lt;Meridian&gt; tMERIDIAN o_merid
  213. %%
  214. spec : /* NULL */
  215. | spec item
  216. ;
  217. item : time {
  218. yyHaveTime++;
  219. }
  220. | zone {
  221. yyHaveZone++;
  222. }
  223. | date {
  224. yyHaveDate++;
  225. }
  226. | day {
  227. yyHaveDay++;
  228. }
  229. | rel {
  230. yyHaveRel++;
  231. }
  232. | number
  233. ;
  234. time : tUNUMBER tMERIDIAN {
  235. yyHour = $1;
  236. yyMinutes = 0;
  237. yySeconds = 0;
  238. yyMeridian = $2;
  239. }
  240. | tUNUMBER ':' tUNUMBER o_merid {
  241. yyHour = $1;
  242. yyMinutes = $3;
  243. yySeconds = 0;
  244. yyMeridian = $4;
  245. }
  246. | tUNUMBER ':' tUNUMBER tSNUMBER {
  247. yyHour = $1;
  248. yyMinutes = $3;
  249. yyMeridian = MER24;
  250. yyHaveZone++;
  251. yyTimezone = ($4 &lt; 0
  252. ? -$4 % 100 + (-$4 / 100) * 60
  253. : - ($4 % 100 + ($4 / 100) * 60));
  254. }
  255. | tUNUMBER ':' tUNUMBER ':' tUNUMBER o_merid {
  256. yyHour = $1;
  257. yyMinutes = $3;
  258. yySeconds = $5;
  259. yyMeridian = $6;
  260. }
  261. | tUNUMBER ':' tUNUMBER ':' tUNUMBER tSNUMBER {
  262. yyHour = $1;
  263. yyMinutes = $3;
  264. yySeconds = $5;
  265. yyMeridian = MER24;
  266. yyHaveZone++;
  267. yyTimezone = ($6 &lt; 0
  268. ? -$6 % 100 + (-$6 / 100) * 60
  269. : - ($6 % 100 + ($6 / 100) * 60));
  270. }
  271. ;
  272. zone : tZONE {
  273. yyTimezone = $1;
  274. }
  275. | tDAYZONE {
  276. yyTimezone = $1 - 60;
  277. }
  278. |
  279. tZONE tDST {
  280. yyTimezone = $1 - 60;
  281. }
  282. ;
  283. day : tDAY {
  284. yyDayOrdinal = 1;
  285. yyDayNumber = $1;
  286. }
  287. | tDAY ',' {
  288. yyDayOrdinal = 1;
  289. yyDayNumber = $1;
  290. }
  291. | tUNUMBER tDAY {
  292. yyDayOrdinal = $1;
  293. yyDayNumber = $2;
  294. }
  295. ;
  296. date : tUNUMBER '/' tUNUMBER {
  297. yyMonth = $1;
  298. yyDay = $3;
  299. }
  300. | tUNUMBER '/' tUNUMBER '/' tUNUMBER {
  301. /* Interpret as YYYY/MM/DD if $1 &gt;= 1000, otherwise as MM/DD/YY.
  302. The goal in recognizing YYYY/MM/DD is solely to support legacy
  303. machine-generated dates like those in an RCS log listing. If
  304. you want portability, use the ISO 8601 format. */
  305. if ($1 &gt;= 1000)
  306. {
  307. yyYear = $1;
  308. yyMonth = $3;
  309. yyDay = $5;
  310. }
  311. else
  312. {
  313. yyMonth = $1;
  314. yyDay = $3;
  315. yyYear = $5;
  316. }
  317. }
  318. | tUNUMBER tSNUMBER tSNUMBER {
  319. /* ISO 8601 format. yyyy-mm-dd. */
  320. yyYear = $1;
  321. yyMonth = -$2;
  322. yyDay = -$3;
  323. }
  324. | tUNUMBER tMONTH tSNUMBER {
  325. /* e.g. 17-JUN-1992. */
  326. yyDay = $1;
  327. yyMonth = $2;
  328. yyYear = -$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. rel : relunit tAGO {
  350. yyRelSeconds = -yyRelSeconds;
  351. yyRelMinutes = -yyRelMinutes;
  352. yyRelHour = -yyRelHour;
  353. yyRelDay = -yyRelDay;
  354. yyRelMonth = -yyRelMonth;
  355. yyRelYear = -yyRelYear;
  356. }
  357. | relunit
  358. ;
  359. relunit : tUNUMBER tYEAR_UNIT {
  360. yyRelYear += $1 * $2;
  361. }
  362. | tSNUMBER tYEAR_UNIT {
  363. yyRelYear += $1 * $2;
  364. }
  365. | tYEAR_UNIT {
  366. yyRelYear += $1;
  367. }
  368. | tUNUMBER tMONTH_UNIT {
  369. yyRelMonth += $1 * $2;
  370. }
  371. | tSNUMBER tMONTH_UNIT {
  372. yyRelMonth += $1 * $2;
  373. }
  374. | tMONTH_UNIT {
  375. yyRelMonth += $1;
  376. }
  377. | tUNUMBER tDAY_UNIT {
  378. yyRelDay += $1 * $2;
  379. }
  380. | tSNUMBER tDAY_UNIT {
  381. yyRelDay += $1 * $2;
  382. }
  383. | tDAY_UNIT {
  384. yyRelDay += $1;
  385. }
  386. | tUNUMBER tHOUR_UNIT {
  387. yyRelHour += $1 * $2;
  388. }
  389. | tSNUMBER tHOUR_UNIT {
  390. yyRelHour += $1 * $2;
  391. }
  392. | tHOUR_UNIT {
  393. yyRelHour += $1;
  394. }
  395. | tUNUMBER tMINUTE_UNIT {
  396. yyRelMinutes += $1 * $2;
  397. }
  398. | tSNUMBER tMINUTE_UNIT {
  399. yyRelMinutes += $1 * $2;
  400. }
  401. | tMINUTE_UNIT {
  402. yyRelMinutes += $1;
  403. }
  404. | tUNUMBER tSEC_UNIT {
  405. yyRelSeconds += $1 * $2;
  406. }
  407. | tSNUMBER tSEC_UNIT {
  408. yyRelSeconds += $1 * $2;
  409. }
  410. | tSEC_UNIT {
  411. yyRelSeconds += $1;
  412. }
  413. ;
  414. number : tUNUMBER
  415. {
  416. if (yyHaveTime &amp;&amp; yyHaveDate &amp;&amp; !yyHaveRel)
  417. yyYear = $1;
  418. else
  419. {
  420. if ($1&gt;10000)
  421. {
  422. yyHaveDate++;
  423. yyDay= ($1)%100;
  424. yyMonth= ($1/100)%100;
  425. yyYear = $1/10000;
  426. }
  427. else
  428. {
  429. yyHaveTime++;
  430. if ($1 &lt; 100)
  431. {
  432. yyHour = $1;
  433. yyMinutes = 0;
  434. }
  435. else
  436. {
  437. yyHour = $1 / 100;
  438. yyMinutes = $1 % 100;
  439. }
  440. yySeconds = 0;
  441. yyMeridian = MER24;
  442. }
  443. }
  444. }
  445. ;
  446. o_merid : /* NULL */
  447. {
  448. $$ = MER24;
  449. }
  450. | tMERIDIAN
  451. {
  452. $$ = $1;
  453. }
  454. ;
  455. %%
  456. /* Include this file down here because bison inserts code above which
  457. may define-away `const'. We want the prototype for get_date to have
  458. the same signature as the function definition does. */
  459. #include &quot;getdate.h&quot;
  460. extern struct tm *gmtime ();
  461. extern struct tm *localtime ();
  462. extern time_t mktime ();
  463. /* Month and day table. */
  464. static TABLE const MonthDayTable[] = {
  465. { &quot;january&quot;, tMONTH, 1 },
  466. { &quot;february&quot;, tMONTH, 2 },
  467. { &quot;march&quot;, tMONTH, 3 },
  468. { &quot;april&quot;, tMONTH, 4 },
  469. { &quot;may&quot;, tMONTH, 5 },
  470. { &quot;june&quot;, tMONTH, 6 },
  471. { &quot;july&quot;, tMONTH, 7 },
  472. { &quot;august&quot;, tMONTH, 8 },
  473. { &quot;september&quot;, tMONTH, 9 },
  474. { &quot;sept&quot;, tMONTH, 9 },
  475. { &quot;october&quot;, tMONTH, 10 },
  476. { &quot;november&quot;, tMONTH, 11 },
  477. { &quot;december&quot;, tMONTH, 12 },
  478. { &quot;sunday&quot;, tDAY, 0 },
  479. { &quot;monday&quot;, tDAY, 1 },
  480. { &quot;tuesday&quot;, tDAY, 2 },
  481. { &quot;tues&quot;, tDAY, 2 },
  482. { &quot;wednesday&quot;, tDAY, 3 },
  483. { &quot;wednes&quot;, tDAY, 3 },
  484. { &quot;thursday&quot;, tDAY, 4 },
  485. { &quot;thur&quot;, tDAY, 4 },
  486. { &quot;thurs&quot;, tDAY, 4 },
  487. { &quot;friday&quot;, tDAY, 5 },
  488. { &quot;saturday&quot;, tDAY, 6 },
  489. { NULL, 0, 0 }
  490. };
  491. /* Time units table. */
  492. static TABLE const UnitsTable[] = {
  493. { &quot;year&quot;, tYEAR_UNIT, 1 },
  494. { &quot;month&quot;, tMONTH_UNIT, 1 },
  495. { &quot;fortnight&quot;, tDAY_UNIT, 14 },
  496. { &quot;week&quot;, tDAY_UNIT, 7 },
  497. { &quot;day&quot;, tDAY_UNIT, 1 },
  498. { &quot;hour&quot;, tHOUR_UNIT, 1 },
  499. { &quot;minute&quot;, tMINUTE_UNIT, 1 },
  500. { &quot;min&quot;, tMINUTE_UNIT, 1 },
  501. { &quot;second&quot;, tSEC_UNIT, 1 },
  502. { &quot;sec&quot;, tSEC_UNIT, 1 },
  503. { NULL, 0, 0 }
  504. };
  505. /* Assorted relative-time words. */
  506. static TABLE const OtherTable[] = {
  507. { &quot;tomorrow&quot;, tMINUTE_UNIT, 1 * 24 * 60 },
  508. { &quot;yesterday&quot;, tMINUTE_UNIT, -1 * 24 * 60 },
  509. { &quot;today&quot;, tMINUTE_UNIT, 0 },
  510. { &quot;now&quot;, tMINUTE_UNIT, 0 },
  511. { &quot;last&quot;, tUNUMBER, -1 },
  512. { &quot;this&quot;, tMINUTE_UNIT, 0 },
  513. { &quot;next&quot;, tUNUMBER, 1 },
  514. { &quot;first&quot;, tUNUMBER, 1 },
  515. /* { &quot;second&quot;, tUNUMBER, 2 }, */
  516. { &quot;third&quot;, tUNUMBER, 3 },
  517. { &quot;fourth&quot;, tUNUMBER, 4 },
  518. { &quot;fifth&quot;, tUNUMBER, 5 },
  519. { &quot;sixth&quot;, tUNUMBER, 6 },
  520. { &quot;seventh&quot;, tUNUMBER, 7 },
  521. { &quot;eighth&quot;, tUNUMBER, 8 },
  522. { &quot;ninth&quot;, tUNUMBER, 9 },
  523. { &quot;tenth&quot;, tUNUMBER, 10 },
  524. { &quot;eleventh&quot;, tUNUMBER, 11 },
  525. { &quot;twelfth&quot;, tUNUMBER, 12 },
  526. { &quot;ago&quot;, tAGO, 1 },
  527. { NULL, 0, 0 }
  528. };
  529. /* The timezone table. */
  530. static TABLE const TimezoneTable[] = {
  531. { &quot;gmt&quot;, tZONE, HOUR ( 0) }, /* Greenwich Mean */
  532. { &quot;ut&quot;, tZONE, HOUR ( 0) }, /* Universal (Coordinated) */
  533. { &quot;utc&quot;, tZONE, HOUR ( 0) },
  534. { &quot;wet&quot;, tZONE, HOUR ( 0) }, /* Western European */
  535. { &quot;bst&quot;, tDAYZONE, HOUR ( 0) }, /* British Summer */
  536. { &quot;wat&quot;, tZONE, HOUR ( 1) }, /* West Africa */
  537. { &quot;at&quot;, tZONE, HOUR ( 2) }, /* Azores */
  538. #if 0
  539. /* For completeness. BST is also British Summer, and GST is
  540. * also Guam Standard. */
  541. { &quot;bst&quot;, tZONE, HOUR ( 3) }, /* Brazil Standard */
  542. { &quot;gst&quot;, tZONE, HOUR ( 3) }, /* Greenland Standard */
  543. #endif
  544. #if 0
  545. { &quot;nft&quot;, tZONE, HOUR (3.5) }, /* Newfoundland */
  546. { &quot;nst&quot;, tZONE, HOUR (3.5) }, /* Newfoundland Standard */
  547. { &quot;ndt&quot;, tDAYZONE, HOUR (3.5) }, /* Newfoundland Daylight */
  548. #endif
  549. { &quot;ast&quot;, tZONE, HOUR ( 4) }, /* Atlantic Standard */
  550. { &quot;adt&quot;, tDAYZONE, HOUR ( 4) }, /* Atlantic Daylight */
  551. { &quot;est&quot;, tZONE, HOUR ( 5) }, /* Eastern Standard */
  552. { &quot;edt&quot;, tDAYZONE, HOUR ( 5) }, /* Eastern Daylight */
  553. { &quot;cst&quot;, tZONE, HOUR ( 6) }, /* Central Standard */
  554. { &quot;cdt&quot;, tDAYZONE, HOUR ( 6) }, /* Central Daylight */
  555. { &quot;mst&quot;, tZONE, HOUR ( 7) }, /* Mountain Standard */
  556. { &quot;mdt&quot;, tDAYZONE, HOUR ( 7) }, /* Mountain Daylight */
  557. { &quot;pst&quot;, tZONE, HOUR ( 8) }, /* Pacific Standard */
  558. { &quot;pdt&quot;, tDAYZONE, HOUR ( 8) }, /* Pacific Daylight */
  559. { &quot;yst&quot;, tZONE, HOUR ( 9) }, /* Yukon Standard */
  560. { &quot;ydt&quot;, tDAYZONE, HOUR ( 9) }, /* Yukon Daylight */
  561. { &quot;hst&quot;, tZONE, HOUR (10) }, /* Hawaii Standard */
  562. { &quot;hdt&quot;, tDAYZONE, HOUR (10) }, /* Hawaii Daylight */
  563. { &quot;cat&quot;, tZONE, HOUR (10) }, /* Central Alaska */
  564. { &quot;ahst&quot;, tZONE, HOUR (10) }, /* Alaska-Hawaii Standard */
  565. { &quot;nt&quot;, tZONE, HOUR (11) }, /* Nome */
  566. { &quot;idlw&quot;, tZONE, HOUR (12) }, /* International Date Line West */
  567. { &quot;cet&quot;, tZONE, -HOUR (1) }, /* Central European */
  568. { &quot;met&quot;, tZONE, -HOUR (1) }, /* Middle European */
  569. { &quot;mewt&quot;, tZONE, -HOUR (1) }, /* Middle European Winter */
  570. { &quot;mest&quot;, tDAYZONE, -HOUR (1) }, /* Middle European Summer */
  571. { &quot;mesz&quot;, tDAYZONE, -HOUR (1) }, /* Middle European Summer */
  572. { &quot;swt&quot;, tZONE, -HOUR (1) }, /* Swedish Winter */
  573. { &quot;sst&quot;, tDAYZONE, -HOUR (1) }, /* Swedish Summer */
  574. { &quot;fwt&quot;, tZONE, -HOUR (1) }, /* French Winter */
  575. { &quot;fst&quot;, tDAYZONE, -HOUR (1) }, /* French Summer */
  576. { &quot;eet&quot;, tZONE, -HOUR (2) }, /* Eastern Europe, USSR Zone 1 */
  577. { &quot;bt&quot;, tZONE, -HOUR (3) }, /* Baghdad, USSR Zone 2 */
  578. #if 0
  579. { &quot;it&quot;, tZONE, -HOUR (3.5) },/* Iran */
  580. #endif
  581. { &quot;zp4&quot;, tZONE, -HOUR (4) }, /* USSR Zone 3 */
  582. { &quot;zp5&quot;, tZONE, -HOUR (5) }, /* USSR Zone 4 */
  583. #if 0
  584. { &quot;ist&quot;, tZONE, -HOUR (5.5) },/* Indian Standard */
  585. #endif
  586. { &quot;zp6&quot;, tZONE, -HOUR (6) }, /* USSR Zone 5 */
  587. #if 0
  588. /* For completeness. NST is also Newfoundland Standard, and SST is
  589. * also Swedish Summer. */
  590. { &quot;nst&quot;, tZONE, -HOUR (6.5) },/* North Sumatra */
  591. { &quot;sst&quot;, tZONE, -HOUR (7) }, /* South Sumatra, USSR Zone 6 */
  592. #endif /* 0 */
  593. { &quot;wast&quot;, tZONE, -HOUR (7) }, /* West Australian Standard */
  594. { &quot;wadt&quot;, tDAYZONE, -HOUR (7) }, /* West Australian Daylight */
  595. #if 0
  596. { &quot;jt&quot;, tZONE, -HOUR (7.5) },/* Java (3pm in Cronusland!) */
  597. #endif
  598. { &quot;cct&quot;, tZONE, -HOUR (8) }, /* China Coast, USSR Zone 7 */
  599. { &quot;jst&quot;, tZONE, -HOUR (9) }, /* Japan Standard, USSR Zone 8 */
  600. #if 0
  601. { &quot;cast&quot;, tZONE, -HOUR (9.5) },/* Central Australian Standard */
  602. { &quot;cadt&quot;, tDAYZONE, -HOUR (9.5) },/* Central Australian Daylight */
  603. #endif
  604. { &quot;east&quot;, tZONE, -HOUR (10) }, /* Eastern Australian Standard */
  605. { &quot;eadt&quot;, tDAYZONE, -HOUR (10) }, /* Eastern Australian Daylight */
  606. { &quot;gst&quot;, tZONE, -HOUR (10) }, /* Guam Standard, USSR Zone 9 */
  607. { &quot;nzt&quot;, tZONE, -HOUR (12) }, /* New Zealand */
  608. { &quot;nzst&quot;, tZONE, -HOUR (12) }, /* New Zealand Standard */
  609. { &quot;nzdt&quot;, tDAYZONE, -HOUR (12) }, /* New Zealand Daylight */
  610. { &quot;idle&quot;, tZONE, -HOUR (12) }, /* International Date Line East */
  611. { NULL, 0, 0 }
  612. };
  613. /* Military timezone table. */
  614. static TABLE const MilitaryTable[] = {
  615. { &quot;a&quot;, tZONE, HOUR ( 1) },
  616. { &quot;b&quot;, tZONE, HOUR ( 2) },
  617. { &quot;c&quot;, tZONE, HOUR ( 3) },
  618. { &quot;d&quot;, tZONE, HOUR ( 4) },
  619. { &quot;e&quot;, tZONE, HOUR ( 5) },
  620. { &quot;f&quot;, tZONE, HOUR ( 6) },
  621. { &quot;g&quot;, tZONE, HOUR ( 7) },
  622. { &quot;h&quot;, tZONE, HOUR ( 8) },
  623. { &quot;i&quot;, tZONE, HOUR ( 9) },
  624. { &quot;k&quot;, tZONE, HOUR ( 10) },
  625. { &quot;l&quot;, tZONE, HOUR ( 11) },
  626. { &quot;m&quot;, tZONE, HOUR ( 12) },
  627. { &quot;n&quot;, tZONE, HOUR (- 1) },
  628. { &quot;o&quot;, tZONE, HOUR (- 2) },
  629. { &quot;p&quot;, tZONE, HOUR (- 3) },
  630. { &quot;q&quot;, tZONE, HOUR (- 4) },
  631. { &quot;r&quot;, tZONE, HOUR (- 5) },
  632. { &quot;s&quot;, tZONE, HOUR (- 6) },
  633. { &quot;t&quot;, tZONE, HOUR (- 7) },
  634. { &quot;u&quot;, tZONE, HOUR (- 8) },
  635. { &quot;v&quot;, tZONE, HOUR (- 9) },
  636. { &quot;w&quot;, tZONE, HOUR (-10) },
  637. { &quot;x&quot;, tZONE, HOUR (-11) },
  638. { &quot;y&quot;, tZONE, HOUR (-12) },
  639. { &quot;z&quot;, tZONE, HOUR ( 0) },
  640. { NULL, 0, 0 }
  641. };
  642. /* ARGSUSED */
  643. static int
  644. yyerror (s)
  645. char *s ATTRIBUTE_UNUSED;
  646. {
  647. return 0;
  648. }
  649. static int
  650. ToHour (Hours, Meridian)
  651. int Hours;
  652. MERIDIAN Meridian;
  653. {
  654. switch (Meridian)
  655. {
  656. case MER24:
  657. if (Hours &lt; 0 || Hours &gt; 23)
  658. return -1;
  659. return Hours;
  660. case MERam:
  661. if (Hours &lt; 1 || Hours &gt; 12)
  662. return -1;
  663. if (Hours == 12)
  664. Hours = 0;
  665. return Hours;
  666. case MERpm:
  667. if (Hours &lt; 1 || Hours &gt; 12)
  668. return -1;
  669. if (Hours == 12)
  670. Hours = 0;
  671. return Hours + 12;
  672. default:
  673. abort ();
  674. }
  675. /* NOTREACHED */
  676. }
  677. static int
  678. ToYear (Year)
  679. int Year;
  680. {
  681. if (Year &lt; 0)
  682. Year = -Year;
  683. /* XPG4 suggests that years 00-68 map to 2000-2068, and
  684. years 69-99 map to 1969-1999. */
  685. if (Year &lt; 69)
  686. Year += 2000;
  687. else if (Year &lt; 100)
  688. Year += 1900;
  689. return Year;
  690. }
  691. static int
  692. LookupWord (buff)
  693. char *buff;
  694. {
  695. register char *p;
  696. register char *q;
  697. register const TABLE *tp;
  698. int i;
  699. int abbrev;
  700. /* Make it lowercase. */
  701. for (p = buff; *p; p++)
  702. if (ISUPPER ((unsigned char) *p))
  703. *p = tolower (*p);
  704. if (strcmp (buff, &quot;am&quot;) == 0 || strcmp (buff, &quot;a.m.&quot;) == 0)
  705. {
  706. yylval.Meridian = MERam;
  707. return tMERIDIAN;
  708. }
  709. if (strcmp (buff, &quot;pm&quot;) == 0 || strcmp (buff, &quot;p.m.&quot;) == 0)
  710. {
  711. yylval.Meridian = MERpm;
  712. return tMERIDIAN;
  713. }
  714. /* See if we have an abbreviation for a month. */
  715. if (strlen (buff) == 3)
  716. abbrev = 1;
  717. else if (strlen (buff) == 4 &amp;&amp; buff[3] == '.')
  718. {
  719. abbrev = 1;
  720. buff[3] = '\0';
  721. }
  722. else
  723. abbrev = 0;
  724. for (tp = MonthDayTable; tp-&gt;name; tp++)
  725. {
  726. if (abbrev)
  727. {
  728. if (strncmp (buff, tp-&gt;name, 3) == 0)
  729. {
  730. yylval.Number = tp-&gt;value;
  731. return tp-&gt;type;
  732. }
  733. }
  734. else if (strcmp (buff, tp-&gt;name) == 0)
  735. {
  736. yylval.Number = tp-&gt;value;
  737. return tp-&gt;type;
  738. }
  739. }
  740. for (tp = TimezoneTable; tp-&gt;name; tp++)
  741. if (strcmp (buff, tp-&gt;name) == 0)
  742. {
  743. yylval.Number = tp-&gt;value;
  744. return tp-&gt;type;
  745. }
  746. if (strcmp (buff, &quot;dst&quot;) == 0)
  747. return tDST;
  748. for (tp = UnitsTable; tp-&gt;name; tp++)
  749. if (strcmp (buff, tp-&gt;name) == 0)
  750. {
  751. yylval.Number = tp-&gt;value;
  752. return tp-&gt;type;
  753. }
  754. /* Strip off any plural and try the units table again. */
  755. i = strlen (buff) - 1;
  756. if (buff[i] == 's')
  757. {
  758. buff[i] = '\0';
  759. for (tp = UnitsTable; tp-&gt;name; tp++)
  760. if (strcmp (buff, tp-&gt;name) == 0)
  761. {
  762. yylval.Number = tp-&gt;value;
  763. return tp-&gt;type;
  764. }
  765. buff[i] = 's'; /* Put back for &quot;this&quot; in OtherTable. */
  766. }
  767. for (tp = OtherTable; tp-&gt;name; tp++)
  768. if (strcmp (buff, tp-&gt;name) == 0)
  769. {
  770. yylval.Number = tp-&gt;value;
  771. return tp-&gt;type;
  772. }
  773. /* Military timezones. */
  774. if (buff[1] == '\0' &amp;&amp; ISALPHA ((unsigned char) *buff))
  775. {
  776. for (tp = MilitaryTable; tp-&gt;name; tp++)
  777. if (strcmp (buff, tp-&gt;name) == 0)
  778. {
  779. yylval.Number = tp-&gt;value;
  780. return tp-&gt;type;
  781. }
  782. }
  783. /* Drop out any periods and try the timezone table again. */
  784. for (i = 0, p = q = buff; *q; q++)
  785. if (*q != '.')
  786. *p++ = *q;
  787. else
  788. i++;
  789. *p = '\0';
  790. if (i)
  791. for (tp = TimezoneTable; tp-&gt;name; tp++)
  792. if (strcmp (buff, tp-&gt;name) == 0)
  793. {
  794. yylval.Number = tp-&gt;value;
  795. return tp-&gt;type;
  796. }
  797. return tID;
  798. }
  799. static int
  800. yylex ()
  801. {
  802. register unsigned char c;
  803. register char *p;
  804. char buff[20];
  805. int Count;
  806. int sign;
  807. for (;;)
  808. {
  809. while (ISSPACE ((unsigned char) *yyInput))
  810. yyInput++;
  811. if (ISDIGIT (c = *yyInput) || c == '-' || c == '+')
  812. {
  813. if (c == '-' || c == '+')
  814. {
  815. sign = c == '-' ? -1 : 1;
  816. if (!ISDIGIT (*++yyInput))
  817. /* skip the '-' sign */
  818. continue;
  819. }
  820. else
  821. sign = 0;
  822. for (yylval.Number = 0; ISDIGIT (c = *yyInput++);)
  823. yylval.Number = 10 * yylval.Number + c - '0';
  824. yyInput--;
  825. if (sign &lt; 0)
  826. yylval.Number = -yylval.Number;
  827. return sign ? tSNUMBER : tUNUMBER;
  828. }
  829. if (ISALPHA (c))
  830. {
  831. for (p = buff; (c = *yyInput++, ISALPHA (c)) || c == '.';)
  832. if (p &lt; &amp;buff[sizeof buff - 1])
  833. *p++ = c;
  834. *p = '\0';
  835. yyInput--;
  836. return LookupWord (buff);
  837. }
  838. if (c != '(')
  839. return *yyInput++;
  840. Count = 0;
  841. do
  842. {
  843. c = *yyInput++;
  844. if (c == '\0')
  845. return c;
  846. if (c == '(')
  847. Count++;
  848. else if (c == ')')
  849. Count--;
  850. }
  851. while (Count &gt; 0);
  852. }
  853. }
  854. #define TM_YEAR_ORIGIN 1900
  855. /* Yield A - B, measured in seconds. */
  856. static long
  857. difftm (struct tm *a, struct tm *b)
  858. {
  859. int ay = a-&gt;tm_year + (TM_YEAR_ORIGIN - 1);
  860. int by = b-&gt;tm_year + (TM_YEAR_ORIGIN - 1);
  861. long days = (
  862. /* difference in day of year */
  863. a-&gt;tm_yday - b-&gt;tm_yday
  864. /* + intervening leap days */
  865. + ((ay &gt;&gt; 2) - (by &gt;&gt; 2))
  866. - (ay / 100 - by / 100)
  867. + ((ay / 100 &gt;&gt; 2) - (by / 100 &gt;&gt; 2))
  868. /* + difference in years * 365 */
  869. + (long) (ay - by) * 365
  870. );
  871. return (60 * (60 * (24 * days + (a-&gt;tm_hour - b-&gt;tm_hour))
  872. + (a-&gt;tm_min - b-&gt;tm_min))
  873. + (a-&gt;tm_sec - b-&gt;tm_sec));
  874. }
  875. time_t
  876. curl_getdate (const char *p, const time_t *now)
  877. {
  878. struct tm tm, tm0, *tmp;
  879. time_t Start;
  880. #ifdef HAVE_LOCALTIME_R
  881. struct tm keeptime;
  882. #endif
  883. yyInput = p;
  884. Start = now ? *now : time ((time_t *) NULL);
  885. #ifdef HAVE_LOCALTIME_R
  886. tmp = (struct tm *)localtime_r(&amp;Start, &amp;keeptime);
  887. #else
  888. tmp = localtime (&amp;Start);
  889. #endif
  890. if (!tmp)
  891. return -1;
  892. yyYear = tmp-&gt;tm_year + TM_YEAR_ORIGIN;
  893. yyMonth = tmp-&gt;tm_mon + 1;
  894. yyDay = tmp-&gt;tm_mday;
  895. yyHour = tmp-&gt;tm_hour;
  896. yyMinutes = tmp-&gt;tm_min;
  897. yySeconds = tmp-&gt;tm_sec;
  898. tm.tm_isdst = tmp-&gt;tm_isdst;
  899. yyMeridian = MER24;
  900. yyRelSeconds = 0;
  901. yyRelMinutes = 0;
  902. yyRelHour = 0;
  903. yyRelDay = 0;
  904. yyRelMonth = 0;
  905. yyRelYear = 0;
  906. yyHaveDate = 0;
  907. yyHaveDay = 0;
  908. yyHaveRel = 0;
  909. yyHaveTime = 0;
  910. yyHaveZone = 0;
  911. if (yyparse ()
  912. || yyHaveTime &gt; 1 || yyHaveZone &gt; 1 || yyHaveDate &gt; 1 || yyHaveDay &gt; 1)
  913. return -1;
  914. tm.tm_year = ToYear (yyYear) - TM_YEAR_ORIGIN + yyRelYear;
  915. tm.tm_mon = yyMonth - 1 + yyRelMonth;
  916. tm.tm_mday = yyDay + yyRelDay;
  917. if (yyHaveTime || (yyHaveRel &amp;&amp; !yyHaveDate &amp;&amp; !yyHaveDay))
  918. {
  919. tm.tm_hour = ToHour (yyHour, yyMeridian);
  920. if (tm.tm_hour &lt; 0)
  921. return -1;
  922. tm.tm_min = yyMinutes;
  923. tm.tm_sec = yySeconds;
  924. }
  925. else
  926. {
  927. tm.tm_hour = tm.tm_min = tm.tm_sec = 0;
  928. }
  929. tm.tm_hour += yyRelHour;
  930. tm.tm_min += yyRelMinutes;
  931. tm.tm_sec += yyRelSeconds;
  932. /* Let mktime deduce tm_isdst if we have an absolute timestamp,
  933. or if the relative timestamp mentions days, months, or years. */
  934. if (yyHaveDate | yyHaveDay | yyHaveTime | yyRelDay | yyRelMonth | yyRelYear)
  935. tm.tm_isdst = -1;
  936. tm0 = tm;
  937. Start = mktime (&amp;tm);
  938. if (Start == (time_t) -1)
  939. {
  940. /* Guard against falsely reporting errors near the time_t boundaries
  941. when parsing times in other time zones. For example, if the min
  942. time_t value is 1970-01-01 00:00:00 UTC and we are 8 hours ahead
  943. of UTC, then the min localtime value is 1970-01-01 08:00:00; if
  944. we apply mktime to 1970-01-01 00:00:00 we will get an error, so
  945. we apply mktime to 1970-01-02 08:00:00 instead and adjust the time
  946. zone by 24 hours to compensate. This algorithm assumes that
  947. there is no DST transition within a day of the time_t boundaries. */
  948. if (yyHaveZone)
  949. {
  950. tm = tm0;
  951. if (tm.tm_year &lt;= EPOCH - TM_YEAR_ORIGIN)
  952. {
  953. tm.tm_mday++;
  954. yyTimezone -= 24 * 60;
  955. }
  956. else
  957. {
  958. tm.tm_mday--;
  959. yyTimezone += 24 * 60;
  960. }
  961. Start = mktime (&amp;tm);
  962. }
  963. if (Start == (time_t) -1)
  964. return Start;
  965. }
  966. if (yyHaveDay &amp;&amp; !yyHaveDate)
  967. {
  968. tm.tm_mday += ((yyDayNumber - tm.tm_wday + 7) % 7
  969. + 7 * (yyDayOrdinal - (0 &lt; yyDayOrdinal)));
  970. Start = mktime (&amp;tm);
  971. if (Start == (time_t) -1)
  972. return Start;
  973. }
  974. if (yyHaveZone)
  975. {
  976. long delta;
  977. struct tm *gmt = gmtime (&amp;Start);
  978. if (!gmt)
  979. return -1;
  980. delta = yyTimezone * 60L + difftm (&amp;tm, gmt);
  981. if ((Start + delta &lt; Start) != (delta &lt; 0))
  982. return -1; /* time_t overflow */
  983. Start += delta;
  984. }
  985. return Start;
  986. }
  987. #if defined (TEST)
  988. /* ARGSUSED */
  989. int
  990. main (ac, av)
  991. int ac;
  992. char *av[];
  993. {
  994. char buff[MAX_BUFF_LEN + 1];
  995. time_t d;
  996. (void) printf (&quot;Enter date, or blank line to exit.\n\t&gt; &quot;);
  997. (void) fflush (stdout);
  998. buff[MAX_BUFF_LEN] = 0;
  999. while (fgets (buff, MAX_BUFF_LEN, stdin) &amp;&amp; buff[0])
  1000. {
  1001. d = curl_getdate (buff, (time_t *) NULL);
  1002. if (d == -1)
  1003. (void) printf (&quot;Bad format - couldn't convert.\n&quot;);
  1004. else
  1005. (void) printf (&quot;%s&quot;, ctime (&amp;d));
  1006. (void) printf (&quot;\t&gt; &quot;);
  1007. (void) fflush (stdout);
  1008. }
  1009. exit (0);
  1010. /* NOTREACHED */
  1011. }
  1012. #endif /* defined (TEST) */
  1013. </pre>
  1014. <hr />
  1015. </body></html>