PageRenderTime 64ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/gtk-gnutella-0.97.1/src/lib/getdate.y

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