PageRenderTime 53ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/shadow-4.1.5/libmisc/getdate.y

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