PageRenderTime 51ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/virtuoso-opensource-6.1.5/libsrc/util/getdate.y

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