PageRenderTime 35ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/ifm-5.1/vars/src/getdate.y

#
Happy | 1030 lines | 947 code | 83 blank | 0 comment | 0 complexity | f0f4e75b2c47143214517fa5aba37cd5 MD5 | raw file
Possible License(s): GPL-2.0, 0BSD, LGPL-2.1
  1. %{
  2. /*
  3. * This file is part of the Vars library, copyright (C) Glenn
  4. * Hutchings 1996-2003.
  5. *
  6. * The Vars library comes with ABSOLUTELY NO WARRANTY. This is free
  7. * software, and you are welcome to redistribute it under certain
  8. * conditions; see the file COPYING for details.
  9. */
  10. /*
  11. ** Originally written by Steven M. Bellovin <smb@research.att.com> while
  12. ** at the University of North Carolina at Chapel Hill. Later tweaked by
  13. ** a couple of people on Usenet. Completely overhauled by Rich $alz
  14. ** <rsalz@bbn.com> and Jim Berets <jberets@bbn.com> in August, 1990.
  15. **
  16. ** This code is in the public domain and has no copyright.
  17. */
  18. #include "getdate.h"
  19. /* Since the code of getdate.y is not included in the Emacs executable
  20. itself, there is no need to #define static in this file. Even if
  21. the code were included in the Emacs executable, it probably
  22. wouldn't do any harm to #undef it here; this will only cause
  23. problems if we try to write to a static variable, which I don't
  24. think this code needs to do. */
  25. #ifdef emacs
  26. # undef static
  27. #endif
  28. #include <stdio.h>
  29. #include <ctype.h>
  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. #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. /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
  58. as well as gratuitiously global symbol names, so we can have multiple
  59. yacc generated parsers in the same program. Note that these are only
  60. the variables produced by yacc. If other parser generators (bison,
  61. byacc, etc) produce additional global names that conflict at link time,
  62. then those parser generators need to be fixed instead of adding those
  63. names to this list. */
  64. #define yymaxdepth gd_maxdepth
  65. #define yyparse gd_parse
  66. #define yylex gd_lex
  67. #define yyerror gd_error
  68. #define yylval gd_lval
  69. #define yychar gd_char
  70. #define yydebug gd_debug
  71. #define yypact gd_pact
  72. #define yyr1 gd_r1
  73. #define yyr2 gd_r2
  74. #define yydef gd_def
  75. #define yychk gd_chk
  76. #define yypgo gd_pgo
  77. #define yyact gd_act
  78. #define yyexca gd_exca
  79. #define yyerrflag gd_errflag
  80. #define yynerrs gd_nerrs
  81. #define yyps gd_ps
  82. #define yypv gd_pv
  83. #define yys gd_s
  84. #define yy_yys gd_yys
  85. #define yystate gd_state
  86. #define yytmp gd_tmp
  87. #define yyv gd_v
  88. #define yy_yyv gd_yyv
  89. #define yyval gd_val
  90. #define yylloc gd_lloc
  91. #define yyreds gd_reds /* With YYDEBUG defined */
  92. #define yytoks gd_toks /* With YYDEBUG defined */
  93. #define yylhs gd_yylhs
  94. #define yylen gd_yylen
  95. #define yydefred gd_yydefred
  96. #define yydgoto gd_yydgoto
  97. #define yysindex gd_yysindex
  98. #define yyrindex gd_yyrindex
  99. #define yygindex gd_yygindex
  100. #define yytable gd_yytable
  101. #define yycheck gd_yycheck
  102. static int yylex ();
  103. static int yyerror ();
  104. #define EPOCH 1970
  105. #define HOUR(x) ((x) * 60)
  106. #define MAX_BUFF_LEN 128 /* size of buffer to read the date into */
  107. /*
  108. ** An entry in the lexical lookup table.
  109. */
  110. typedef struct _TABLE {
  111. const char *name;
  112. int type;
  113. int value;
  114. } TABLE;
  115. /*
  116. ** Meridian: am, pm, or 24-hour style.
  117. */
  118. typedef enum _MERIDIAN {
  119. MERam, MERpm, MER24
  120. } MERIDIAN;
  121. /*
  122. ** Global variables. We could get rid of most of these by using a good
  123. ** union as the yacc stack. (This routine was originally written before
  124. ** yacc had the %union construct.) Maybe someday; right now we only use
  125. ** the %union very rarely.
  126. */
  127. static const char *yyInput;
  128. static int yyDayOrdinal;
  129. static int yyDayNumber;
  130. static int yyHaveDate;
  131. static int yyHaveDay;
  132. static int yyHaveRel;
  133. static int yyHaveTime;
  134. static int yyHaveZone;
  135. static int yyTimezone;
  136. static int yyDay;
  137. static int yyHour;
  138. static int yyMinutes;
  139. static int yyMonth;
  140. static int yySeconds;
  141. static int yyYear;
  142. static MERIDIAN yyMeridian;
  143. static int yyRelDay;
  144. static int yyRelHour;
  145. static int yyRelMinutes;
  146. static int yyRelMonth;
  147. static int yyRelSeconds;
  148. static int yyRelYear;
  149. %}
  150. /* This grammar has 13 shift/reduce conflicts. */
  151. %expect 13
  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 += $1;
  317. }
  318. | tUNUMBER tMONTH_UNIT {
  319. yyRelMonth += $1 * $2;
  320. }
  321. | tSNUMBER tMONTH_UNIT {
  322. yyRelMonth += $1 * $2;
  323. }
  324. | tMONTH_UNIT {
  325. yyRelMonth += $1;
  326. }
  327. | tUNUMBER tDAY_UNIT {
  328. yyRelDay += $1 * $2;
  329. }
  330. | tSNUMBER tDAY_UNIT {
  331. yyRelDay += $1 * $2;
  332. }
  333. | tDAY_UNIT {
  334. yyRelDay += $1;
  335. }
  336. | tUNUMBER tHOUR_UNIT {
  337. yyRelHour += $1 * $2;
  338. }
  339. | tSNUMBER tHOUR_UNIT {
  340. yyRelHour += $1 * $2;
  341. }
  342. | tHOUR_UNIT {
  343. yyRelHour += $1;
  344. }
  345. | tUNUMBER tMINUTE_UNIT {
  346. yyRelMinutes += $1 * $2;
  347. }
  348. | tSNUMBER tMINUTE_UNIT {
  349. yyRelMinutes += $1 * $2;
  350. }
  351. | tMINUTE_UNIT {
  352. yyRelMinutes += $1;
  353. }
  354. | tUNUMBER tSEC_UNIT {
  355. yyRelSeconds += $1 * $2;
  356. }
  357. | tSNUMBER tSEC_UNIT {
  358. yyRelSeconds += $1 * $2;
  359. }
  360. | tSEC_UNIT {
  361. yyRelSeconds += $1;
  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. /* Include this file down here because bison inserts code above which
  407. may define-away `const'. We want the prototype for get_date to have
  408. the same signature as the function definition does. */
  409. #include "getdate.h"
  410. extern struct tm *gmtime ();
  411. extern struct tm *localtime ();
  412. extern time_t mktime ();
  413. /* Month and day table. */
  414. static TABLE const MonthDayTable[] = {
  415. { "january", tMONTH, 1 },
  416. { "february", tMONTH, 2 },
  417. { "march", tMONTH, 3 },
  418. { "april", tMONTH, 4 },
  419. { "may", tMONTH, 5 },
  420. { "june", tMONTH, 6 },
  421. { "july", tMONTH, 7 },
  422. { "august", tMONTH, 8 },
  423. { "september", tMONTH, 9 },
  424. { "sept", tMONTH, 9 },
  425. { "october", tMONTH, 10 },
  426. { "november", tMONTH, 11 },
  427. { "december", tMONTH, 12 },
  428. { "sunday", tDAY, 0 },
  429. { "monday", tDAY, 1 },
  430. { "tuesday", tDAY, 2 },
  431. { "tues", tDAY, 2 },
  432. { "wednesday", tDAY, 3 },
  433. { "wednes", tDAY, 3 },
  434. { "thursday", tDAY, 4 },
  435. { "thur", tDAY, 4 },
  436. { "thurs", tDAY, 4 },
  437. { "friday", tDAY, 5 },
  438. { "saturday", tDAY, 6 },
  439. { NULL }
  440. };
  441. /* Time units table. */
  442. static TABLE const UnitsTable[] = {
  443. { "year", tYEAR_UNIT, 1 },
  444. { "month", tMONTH_UNIT, 1 },
  445. { "fortnight", tDAY_UNIT, 14 },
  446. { "week", tDAY_UNIT, 7 },
  447. { "day", tDAY_UNIT, 1 },
  448. { "hour", tHOUR_UNIT, 1 },
  449. { "minute", tMINUTE_UNIT, 1 },
  450. { "min", tMINUTE_UNIT, 1 },
  451. { "second", tSEC_UNIT, 1 },
  452. { "sec", tSEC_UNIT, 1 },
  453. { NULL }
  454. };
  455. /* Assorted relative-time words. */
  456. static TABLE const OtherTable[] = {
  457. { "tomorrow", tMINUTE_UNIT, 1 * 24 * 60 },
  458. { "yesterday", tMINUTE_UNIT, -1 * 24 * 60 },
  459. { "today", tMINUTE_UNIT, 0 },
  460. { "now", tMINUTE_UNIT, 0 },
  461. { "last", tUNUMBER, -1 },
  462. { "this", tMINUTE_UNIT, 0 },
  463. { "next", tUNUMBER, 1 },
  464. { "first", tUNUMBER, 1 },
  465. /* { "second", tUNUMBER, 2 }, */
  466. { "third", tUNUMBER, 3 },
  467. { "fourth", tUNUMBER, 4 },
  468. { "fifth", tUNUMBER, 5 },
  469. { "sixth", tUNUMBER, 6 },
  470. { "seventh", tUNUMBER, 7 },
  471. { "eighth", tUNUMBER, 8 },
  472. { "ninth", tUNUMBER, 9 },
  473. { "tenth", tUNUMBER, 10 },
  474. { "eleventh", tUNUMBER, 11 },
  475. { "twelfth", tUNUMBER, 12 },
  476. { "ago", tAGO, 1 },
  477. { NULL }
  478. };
  479. /* The timezone table. */
  480. static TABLE const TimezoneTable[] = {
  481. { "gmt", tZONE, HOUR ( 0) }, /* Greenwich Mean */
  482. { "ut", tZONE, HOUR ( 0) }, /* Universal (Coordinated) */
  483. { "utc", tZONE, HOUR ( 0) },
  484. { "wet", tZONE, HOUR ( 0) }, /* Western European */
  485. { "bst", tDAYZONE, HOUR ( 0) }, /* British Summer */
  486. { "wat", tZONE, HOUR ( 1) }, /* West Africa */
  487. { "at", tZONE, HOUR ( 2) }, /* Azores */
  488. #if 0
  489. /* For completeness. BST is also British Summer, and GST is
  490. * also Guam Standard. */
  491. { "bst", tZONE, HOUR ( 3) }, /* Brazil Standard */
  492. { "gst", tZONE, HOUR ( 3) }, /* Greenland Standard */
  493. #endif
  494. #if 0
  495. { "nft", tZONE, HOUR (3.5) }, /* Newfoundland */
  496. { "nst", tZONE, HOUR (3.5) }, /* Newfoundland Standard */
  497. { "ndt", tDAYZONE, HOUR (3.5) }, /* Newfoundland Daylight */
  498. #endif
  499. { "ast", tZONE, HOUR ( 4) }, /* Atlantic Standard */
  500. { "adt", tDAYZONE, HOUR ( 4) }, /* Atlantic Daylight */
  501. { "est", tZONE, HOUR ( 5) }, /* Eastern Standard */
  502. { "edt", tDAYZONE, HOUR ( 5) }, /* Eastern Daylight */
  503. { "cst", tZONE, HOUR ( 6) }, /* Central Standard */
  504. { "cdt", tDAYZONE, HOUR ( 6) }, /* Central Daylight */
  505. { "mst", tZONE, HOUR ( 7) }, /* Mountain Standard */
  506. { "mdt", tDAYZONE, HOUR ( 7) }, /* Mountain Daylight */
  507. { "pst", tZONE, HOUR ( 8) }, /* Pacific Standard */
  508. { "pdt", tDAYZONE, HOUR ( 8) }, /* Pacific Daylight */
  509. { "yst", tZONE, HOUR ( 9) }, /* Yukon Standard */
  510. { "ydt", tDAYZONE, HOUR ( 9) }, /* Yukon Daylight */
  511. { "hst", tZONE, HOUR (10) }, /* Hawaii Standard */
  512. { "hdt", tDAYZONE, HOUR (10) }, /* Hawaii Daylight */
  513. { "cat", tZONE, HOUR (10) }, /* Central Alaska */
  514. { "ahst", tZONE, HOUR (10) }, /* Alaska-Hawaii Standard */
  515. { "nt", tZONE, HOUR (11) }, /* Nome */
  516. { "idlw", tZONE, HOUR (12) }, /* International Date Line West */
  517. { "cet", tZONE, -HOUR (1) }, /* Central European */
  518. { "met", tZONE, -HOUR (1) }, /* Middle European */
  519. { "mewt", tZONE, -HOUR (1) }, /* Middle European Winter */
  520. { "mest", tDAYZONE, -HOUR (1) }, /* Middle European Summer */
  521. { "mesz", tDAYZONE, -HOUR (1) }, /* Middle European Summer */
  522. { "swt", tZONE, -HOUR (1) }, /* Swedish Winter */
  523. { "sst", tDAYZONE, -HOUR (1) }, /* Swedish Summer */
  524. { "fwt", tZONE, -HOUR (1) }, /* French Winter */
  525. { "fst", tDAYZONE, -HOUR (1) }, /* French Summer */
  526. { "eet", tZONE, -HOUR (2) }, /* Eastern Europe, USSR Zone 1 */
  527. { "bt", tZONE, -HOUR (3) }, /* Baghdad, USSR Zone 2 */
  528. #if 0
  529. { "it", tZONE, -HOUR (3.5) },/* Iran */
  530. #endif
  531. { "zp4", tZONE, -HOUR (4) }, /* USSR Zone 3 */
  532. { "zp5", tZONE, -HOUR (5) }, /* USSR Zone 4 */
  533. #if 0
  534. { "ist", tZONE, -HOUR (5.5) },/* Indian Standard */
  535. #endif
  536. { "zp6", tZONE, -HOUR (6) }, /* USSR Zone 5 */
  537. #if 0
  538. /* For completeness. NST is also Newfoundland Standard, and SST is
  539. * also Swedish Summer. */
  540. { "nst", tZONE, -HOUR (6.5) },/* North Sumatra */
  541. { "sst", tZONE, -HOUR (7) }, /* South Sumatra, USSR Zone 6 */
  542. #endif /* 0 */
  543. { "wast", tZONE, -HOUR (7) }, /* West Australian Standard */
  544. { "wadt", tDAYZONE, -HOUR (7) }, /* West Australian Daylight */
  545. #if 0
  546. { "jt", tZONE, -HOUR (7.5) },/* Java (3pm in Cronusland!) */
  547. #endif
  548. { "cct", tZONE, -HOUR (8) }, /* China Coast, USSR Zone 7 */
  549. { "jst", tZONE, -HOUR (9) }, /* Japan Standard, USSR Zone 8 */
  550. #if 0
  551. { "cast", tZONE, -HOUR (9.5) },/* Central Australian Standard */
  552. { "cadt", tDAYZONE, -HOUR (9.5) },/* Central Australian Daylight */
  553. #endif
  554. { "east", tZONE, -HOUR (10) }, /* Eastern Australian Standard */
  555. { "eadt", tDAYZONE, -HOUR (10) }, /* Eastern Australian Daylight */
  556. { "gst", tZONE, -HOUR (10) }, /* Guam Standard, USSR Zone 9 */
  557. { "nzt", tZONE, -HOUR (12) }, /* New Zealand */
  558. { "nzst", tZONE, -HOUR (12) }, /* New Zealand Standard */
  559. { "nzdt", tDAYZONE, -HOUR (12) }, /* New Zealand Daylight */
  560. { "idle", tZONE, -HOUR (12) }, /* International Date Line East */
  561. { NULL }
  562. };
  563. /* Military timezone table. */
  564. static TABLE const MilitaryTable[] = {
  565. { "a", tZONE, HOUR ( 1) },
  566. { "b", tZONE, HOUR ( 2) },
  567. { "c", tZONE, HOUR ( 3) },
  568. { "d", tZONE, HOUR ( 4) },
  569. { "e", tZONE, HOUR ( 5) },
  570. { "f", tZONE, HOUR ( 6) },
  571. { "g", tZONE, HOUR ( 7) },
  572. { "h", tZONE, HOUR ( 8) },
  573. { "i", tZONE, HOUR ( 9) },
  574. { "k", tZONE, HOUR ( 10) },
  575. { "l", tZONE, HOUR ( 11) },
  576. { "m", tZONE, HOUR ( 12) },
  577. { "n", tZONE, HOUR (- 1) },
  578. { "o", tZONE, HOUR (- 2) },
  579. { "p", tZONE, HOUR (- 3) },
  580. { "q", tZONE, HOUR (- 4) },
  581. { "r", tZONE, HOUR (- 5) },
  582. { "s", tZONE, HOUR (- 6) },
  583. { "t", tZONE, HOUR (- 7) },
  584. { "u", tZONE, HOUR (- 8) },
  585. { "v", tZONE, HOUR (- 9) },
  586. { "w", tZONE, HOUR (-10) },
  587. { "x", tZONE, HOUR (-11) },
  588. { "y", tZONE, HOUR (-12) },
  589. { "z", tZONE, HOUR ( 0) },
  590. { NULL }
  591. };
  592. /* ARGSUSED */
  593. static int
  594. yyerror (s)
  595. char *s;
  596. {
  597. return 0;
  598. }
  599. static int
  600. ToHour (Hours, Meridian)
  601. int Hours;
  602. MERIDIAN Meridian;
  603. {
  604. switch (Meridian)
  605. {
  606. case MER24:
  607. if (Hours < 0 || Hours > 23)
  608. return -1;
  609. return Hours;
  610. case MERam:
  611. if (Hours < 1 || Hours > 12)
  612. return -1;
  613. if (Hours == 12)
  614. Hours = 0;
  615. return Hours;
  616. case MERpm:
  617. if (Hours < 1 || Hours > 12)
  618. return -1;
  619. if (Hours == 12)
  620. Hours = 0;
  621. return Hours + 12;
  622. default:
  623. abort ();
  624. }
  625. /* NOTREACHED */
  626. }
  627. static int
  628. ToYear (Year)
  629. int Year;
  630. {
  631. if (Year < 0)
  632. Year = -Year;
  633. /* XPG4 suggests that years 00-68 map to 2000-2068, and
  634. years 69-99 map to 1969-1999. */
  635. if (Year < 69)
  636. Year += 2000;
  637. else if (Year < 100)
  638. Year += 1900;
  639. return Year;
  640. }
  641. static int
  642. LookupWord (buff)
  643. char *buff;
  644. {
  645. register char *p;
  646. register char *q;
  647. register const TABLE *tp;
  648. int i;
  649. int abbrev;
  650. /* Make it lowercase. */
  651. for (p = buff; *p; p++)
  652. if (ISUPPER (*p))
  653. *p = tolower (*p);
  654. if (strcmp (buff, "am") == 0 || strcmp (buff, "a.m.") == 0)
  655. {
  656. yylval.Meridian = MERam;
  657. return tMERIDIAN;
  658. }
  659. if (strcmp (buff, "pm") == 0 || strcmp (buff, "p.m.") == 0)
  660. {
  661. yylval.Meridian = MERpm;
  662. return tMERIDIAN;
  663. }
  664. /* See if we have an abbreviation for a month. */
  665. if (strlen (buff) == 3)
  666. abbrev = 1;
  667. else if (strlen (buff) == 4 && buff[3] == '.')
  668. {
  669. abbrev = 1;
  670. buff[3] = '\0';
  671. }
  672. else
  673. abbrev = 0;
  674. for (tp = MonthDayTable; tp->name; tp++)
  675. {
  676. if (abbrev)
  677. {
  678. if (strncmp (buff, tp->name, 3) == 0)
  679. {
  680. yylval.Number = tp->value;
  681. return tp->type;
  682. }
  683. }
  684. else if (strcmp (buff, tp->name) == 0)
  685. {
  686. yylval.Number = tp->value;
  687. return tp->type;
  688. }
  689. }
  690. for (tp = TimezoneTable; tp->name; tp++)
  691. if (strcmp (buff, tp->name) == 0)
  692. {
  693. yylval.Number = tp->value;
  694. return tp->type;
  695. }
  696. if (strcmp (buff, "dst") == 0)
  697. return tDST;
  698. for (tp = UnitsTable; tp->name; tp++)
  699. if (strcmp (buff, tp->name) == 0)
  700. {
  701. yylval.Number = tp->value;
  702. return tp->type;
  703. }
  704. /* Strip off any plural and try the units table again. */
  705. i = strlen (buff) - 1;
  706. if (buff[i] == 's')
  707. {
  708. buff[i] = '\0';
  709. for (tp = UnitsTable; tp->name; tp++)
  710. if (strcmp (buff, tp->name) == 0)
  711. {
  712. yylval.Number = tp->value;
  713. return tp->type;
  714. }
  715. buff[i] = 's'; /* Put back for "this" in OtherTable. */
  716. }
  717. for (tp = OtherTable; tp->name; tp++)
  718. if (strcmp (buff, tp->name) == 0)
  719. {
  720. yylval.Number = tp->value;
  721. return tp->type;
  722. }
  723. /* Military timezones. */
  724. if (buff[1] == '\0' && ISALPHA (*buff))
  725. {
  726. for (tp = MilitaryTable; tp->name; tp++)
  727. if (strcmp (buff, tp->name) == 0)
  728. {
  729. yylval.Number = tp->value;
  730. return tp->type;
  731. }
  732. }
  733. /* Drop out any periods and try the timezone table again. */
  734. for (i = 0, p = q = buff; *q; q++)
  735. if (*q != '.')
  736. *p++ = *q;
  737. else
  738. i++;
  739. *p = '\0';
  740. if (i)
  741. for (tp = TimezoneTable; tp->name; tp++)
  742. if (strcmp (buff, tp->name) == 0)
  743. {
  744. yylval.Number = tp->value;
  745. return tp->type;
  746. }
  747. return tID;
  748. }
  749. static int
  750. yylex ()
  751. {
  752. register char c;
  753. register char *p;
  754. char buff[20];
  755. int Count;
  756. int sign;
  757. for (;;)
  758. {
  759. while (ISSPACE (*yyInput))
  760. yyInput++;
  761. if (ISDIGIT (c = *yyInput) || c == '-' || c == '+')
  762. {
  763. if (c == '-' || c == '+')
  764. {
  765. sign = c == '-' ? -1 : 1;
  766. if (!ISDIGIT (*++yyInput))
  767. /* skip the '-' sign */
  768. continue;
  769. }
  770. else
  771. sign = 0;
  772. for (yylval.Number = 0; ISDIGIT (c = *yyInput++);)
  773. yylval.Number = 10 * yylval.Number + c - '0';
  774. yyInput--;
  775. if (sign < 0)
  776. yylval.Number = -yylval.Number;
  777. return sign ? tSNUMBER : tUNUMBER;
  778. }
  779. if (ISALPHA (c))
  780. {
  781. for (p = buff; (c = *yyInput++, ISALPHA (c)) || c == '.';)
  782. if (p < &buff[sizeof buff - 1])
  783. *p++ = c;
  784. *p = '\0';
  785. yyInput--;
  786. return LookupWord (buff);
  787. }
  788. if (c != '(')
  789. return *yyInput++;
  790. Count = 0;
  791. do
  792. {
  793. c = *yyInput++;
  794. if (c == '\0')
  795. return c;
  796. if (c == '(')
  797. Count++;
  798. else if (c == ')')
  799. Count--;
  800. }
  801. while (Count > 0);
  802. }
  803. }
  804. #define TM_YEAR_ORIGIN 1900
  805. /* Yield A - B, measured in seconds. */
  806. static long
  807. difftm (a, b)
  808. struct tm *a, *b;
  809. {
  810. int ay = a->tm_year + (TM_YEAR_ORIGIN - 1);
  811. int by = b->tm_year + (TM_YEAR_ORIGIN - 1);
  812. long days = (
  813. /* difference in day of year */
  814. a->tm_yday - b->tm_yday
  815. /* + intervening leap days */
  816. + ((ay >> 2) - (by >> 2))
  817. - (ay / 100 - by / 100)
  818. + ((ay / 100 >> 2) - (by / 100 >> 2))
  819. /* + difference in years * 365 */
  820. + (long) (ay - by) * 365
  821. );
  822. return (60 * (60 * (24 * days + (a->tm_hour - b->tm_hour))
  823. + (a->tm_min - b->tm_min))
  824. + (a->tm_sec - b->tm_sec));
  825. }
  826. time_t
  827. get_date (const char *p, const time_t *now)
  828. {
  829. struct tm tm, tm0, *tmp;
  830. time_t Start;
  831. yyInput = p;
  832. Start = now ? *now : time ((time_t *) NULL);
  833. tmp = localtime (&Start);
  834. yyYear = tmp->tm_year + TM_YEAR_ORIGIN;
  835. yyMonth = tmp->tm_mon + 1;
  836. yyDay = tmp->tm_mday;
  837. yyHour = tmp->tm_hour;
  838. yyMinutes = tmp->tm_min;
  839. yySeconds = tmp->tm_sec;
  840. yyMeridian = MER24;
  841. yyRelSeconds = 0;
  842. yyRelMinutes = 0;
  843. yyRelHour = 0;
  844. yyRelDay = 0;
  845. yyRelMonth = 0;
  846. yyRelYear = 0;
  847. yyHaveDate = 0;
  848. yyHaveDay = 0;
  849. yyHaveRel = 0;
  850. yyHaveTime = 0;
  851. yyHaveZone = 0;
  852. if (yyparse ()
  853. || yyHaveTime > 1 || yyHaveZone > 1 || yyHaveDate > 1 || yyHaveDay > 1)
  854. return -1;
  855. tm.tm_year = ToYear (yyYear) - TM_YEAR_ORIGIN + yyRelYear;
  856. tm.tm_mon = yyMonth - 1 + yyRelMonth;
  857. tm.tm_mday = yyDay + yyRelDay;
  858. if (yyHaveTime || (yyHaveRel && !yyHaveDate && !yyHaveDay))
  859. {
  860. tm.tm_hour = ToHour (yyHour, yyMeridian);
  861. if (tm.tm_hour < 0)
  862. return -1;
  863. tm.tm_min = yyMinutes;
  864. tm.tm_sec = yySeconds;
  865. }
  866. else
  867. {
  868. tm.tm_hour = tm.tm_min = tm.tm_sec = 0;
  869. }
  870. tm.tm_hour += yyRelHour;
  871. tm.tm_min += yyRelMinutes;
  872. tm.tm_sec += yyRelSeconds;
  873. tm.tm_isdst = -1;
  874. tm0 = tm;
  875. Start = mktime (&tm);
  876. if (Start == (time_t) -1)
  877. {
  878. /* Guard against falsely reporting errors near the time_t boundaries
  879. when parsing times in other time zones. For example, if the min
  880. time_t value is 1970-01-01 00:00:00 UTC and we are 8 hours ahead
  881. of UTC, then the min localtime value is 1970-01-01 08:00:00; if
  882. we apply mktime to 1970-01-01 00:00:00 we will get an error, so
  883. we apply mktime to 1970-01-02 08:00:00 instead and adjust the time
  884. zone by 24 hours to compensate. This algorithm assumes that
  885. there is no DST transition within a day of the time_t boundaries. */
  886. if (yyHaveZone)
  887. {
  888. tm = tm0;
  889. if (tm.tm_year <= EPOCH - TM_YEAR_ORIGIN)
  890. {
  891. tm.tm_mday++;
  892. yyTimezone -= 24 * 60;
  893. }
  894. else
  895. {
  896. tm.tm_mday--;
  897. yyTimezone += 24 * 60;
  898. }
  899. Start = mktime (&tm);
  900. }
  901. if (Start == (time_t) -1)
  902. return Start;
  903. }
  904. if (yyHaveDay && !yyHaveDate)
  905. {
  906. tm.tm_mday += ((yyDayNumber - tm.tm_wday + 7) % 7
  907. + 7 * (yyDayOrdinal - (0 < yyDayOrdinal)));
  908. Start = mktime (&tm);
  909. if (Start == (time_t) -1)
  910. return Start;
  911. }
  912. if (yyHaveZone)
  913. {
  914. long delta = yyTimezone * 60L + difftm (&tm, gmtime (&Start));
  915. if ((Start + delta < Start) != (delta < 0))
  916. return -1; /* time_t overflow */
  917. Start += delta;
  918. }
  919. return Start;
  920. }
  921. #if defined (TEST)
  922. /* ARGSUSED */
  923. int
  924. main (ac, av)
  925. int ac;
  926. char *av[];
  927. {
  928. char buff[MAX_BUFF_LEN + 1];
  929. time_t d;
  930. (void) printf ("Enter date, or blank line to exit.\n\t> ");
  931. (void) fflush (stdout);
  932. buff[MAX_BUFF_LEN] = 0;
  933. while (fgets (buff, MAX_BUFF_LEN, stdin) && buff[0])
  934. {
  935. d = get_date (buff, (time_t *) NULL);
  936. if (d == -1)
  937. (void) printf ("Bad format - couldn't convert.\n");
  938. else
  939. (void) printf ("%s", ctime (&d));
  940. (void) printf ("\t> ");
  941. (void) fflush (stdout);
  942. }
  943. exit (0);
  944. /* NOTREACHED */
  945. }
  946. #endif /* defined (TEST) */