PageRenderTime 52ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/chrony-1.27-pre1/getdate.y

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