PageRenderTime 68ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/App/UnxUtilsSrc/unxutils/tar-1.12/lib/getdate.y

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