PageRenderTime 77ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/libsrc/util/getdate.y

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