PageRenderTime 109ms CodeModel.GetById 16ms RepoModel.GetById 4ms app.codeStats 0ms

/pl-6.0.2/packages/xpce/src/gnu/getdate.y

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