getdate.y 28 KB

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