getdate.y 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051
  1. %{
  2. /* Parse a string into an internal time stamp.
  3. Copyright 1999 Free Software Foundation, Inc.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software Foundation,
  14. Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  15. /* Originally written by Steven M. Bellovin <smb@research.att.com> while
  16. at the University of North Carolina at Chapel Hill. Later tweaked by
  17. a couple of people on Usenet. Completely overhauled by Rich $alz
  18. <rsalz@bbn.com> and Jim Berets <jberets@bbn.com> in August, 1990.
  19. Modified by Paul Eggert <eggert@twinsun.com> in August 1999 to do
  20. the right thing about local DST. Unlike previous versions, this
  21. version is reentrant. */
  22. #ifdef HAVE_CONFIG_H
  23. # include <config.h>
  24. # ifdef HAVE_ALLOCA_H
  25. # include <alloca.h>
  26. # endif
  27. #endif
  28. /* Since the code of getdate.y is not included in the Emacs executable
  29. itself, there is no need to #define static in this file. Even if
  30. the code were included in the Emacs executable, it probably
  31. wouldn't do any harm to #undef it here; this will only cause
  32. problems if we try to write to a static variable, which I don't
  33. think this code needs to do. */
  34. #ifdef emacs
  35. # undef static
  36. #endif
  37. #include <ctype.h>
  38. #if HAVE_STDLIB_H
  39. # include <stdlib.h> /* for `free'; used by Bison 1.27 */
  40. #endif
  41. #if STDC_HEADERS || (! defined isascii && ! HAVE_ISASCII)
  42. # define IN_CTYPE_DOMAIN(c) 1
  43. #else
  44. # define IN_CTYPE_DOMAIN(c) isascii (c)
  45. #endif
  46. #define ISSPACE(c) (IN_CTYPE_DOMAIN (c) && isspace (c))
  47. #define ISALPHA(c) (IN_CTYPE_DOMAIN (c) && isalpha (c))
  48. #define ISLOWER(c) (IN_CTYPE_DOMAIN (c) && islower (c))
  49. #define ISDIGIT_LOCALE(c) (IN_CTYPE_DOMAIN (c) && isdigit (c))
  50. /* ISDIGIT differs from ISDIGIT_LOCALE, as follows:
  51. - Its arg may be any int or unsigned int; it need not be an unsigned char.
  52. - It's guaranteed to evaluate its argument exactly once.
  53. - It's typically faster.
  54. Posix 1003.2-1992 section 2.5.2.1 page 50 lines 1556-1558 says that
  55. only '0' through '9' are digits. Prefer ISDIGIT to ISDIGIT_LOCALE unless
  56. it's important to use the locale's definition of `digit' even when the
  57. host does not conform to Posix. */
  58. #define ISDIGIT(c) ((unsigned) (c) - '0' <= 9)
  59. #if STDC_HEADERS || HAVE_STRING_H
  60. # include <string.h>
  61. #endif
  62. #if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 7)
  63. # define __attribute__(x)
  64. #endif
  65. #ifndef ATTRIBUTE_UNUSED
  66. # define ATTRIBUTE_UNUSED __attribute__ ((__unused__))
  67. #endif
  68. #define EPOCH_YEAR 1970
  69. #define TM_YEAR_ORIGIN 1900
  70. #define HOUR(x) ((x) * 60)
  71. /* An entry in the lexical lookup table. */
  72. typedef struct
  73. {
  74. char const *name;
  75. int type;
  76. int value;
  77. } table;
  78. /* Meridian: am, pm, or 24-hour style. */
  79. enum { MERam, MERpm, MER24 };
  80. /* Information passed to and from the parser. */
  81. struct parser_control
  82. {
  83. /* The input string remaining to be parsed. */
  84. const char *input;
  85. /* N, if this is the Nth Tuesday. */
  86. int day_ordinal;
  87. /* Day of week; Sunday is 0. */
  88. int day_number;
  89. /* tm_isdst flag for the local zone. */
  90. int local_isdst;
  91. /* Time zone, in minutes east of UTC. */
  92. int time_zone;
  93. /* Style used for time. */
  94. int meridian;
  95. /* Gregorian year, month, day, hour, minutes, and seconds. */
  96. int year;
  97. int month;
  98. int day;
  99. int hour;
  100. int minutes;
  101. int seconds;
  102. /* Relative year, month, day, hour, minutes, and seconds. */
  103. int rel_year;
  104. int rel_month;
  105. int rel_day;
  106. int rel_hour;
  107. int rel_minutes;
  108. int rel_seconds;
  109. /* Counts of nonterminals of various flavors parsed so far. */
  110. int dates_seen;
  111. int days_seen;
  112. int local_zones_seen;
  113. int rels_seen;
  114. int times_seen;
  115. int zones_seen;
  116. /* Table of local time zone abbrevations, terminated by a null entry. */
  117. table local_time_zone_table[3];
  118. };
  119. #define YYLEX_PARAM parm
  120. #define YYPARSE_PARAM parm
  121. #define YYSTYPE int
  122. static int yyerror ();
  123. static int yylex ();
  124. %}
  125. /* We want a reentrant parser. */
  126. %pure_parser
  127. /* This grammar has 13 shift/reduce conflicts. */
  128. %expect 13
  129. %token tAGO tDAY tDAY_UNIT tDAYZONE tDST tHOUR_UNIT tID
  130. %token tLOCAL_ZONE tMERIDIAN tMINUTE_UNIT tMONTH tMONTH_UNIT
  131. %token tSEC_UNIT tSNUMBER tUNUMBER tYEAR_UNIT tZONE
  132. %%
  133. spec:
  134. /* empty */
  135. | spec item
  136. ;
  137. item:
  138. time
  139. { ((struct parser_control *) parm)->times_seen++; }
  140. | local_zone
  141. { ((struct parser_control *) parm)->local_zones_seen++; }
  142. | zone
  143. { ((struct parser_control *) parm)->zones_seen++; }
  144. | date
  145. { ((struct parser_control *) parm)->dates_seen++; }
  146. | day
  147. { ((struct parser_control *) parm)->days_seen++; }
  148. | rel
  149. { ((struct parser_control *) parm)->rels_seen++; }
  150. | number
  151. ;
  152. time:
  153. tUNUMBER tMERIDIAN
  154. {
  155. ((struct parser_control *) parm)->hour = $1;
  156. ((struct parser_control *) parm)->minutes = 0;
  157. ((struct parser_control *) parm)->seconds = 0;
  158. ((struct parser_control *) parm)->meridian = $2;
  159. }
  160. | tUNUMBER ':' tUNUMBER o_merid
  161. {
  162. ((struct parser_control *) parm)->hour = $1;
  163. ((struct parser_control *) parm)->minutes = $3;
  164. ((struct parser_control *) parm)->seconds = 0;
  165. ((struct parser_control *) parm)->meridian = $4;
  166. }
  167. | tUNUMBER ':' tUNUMBER tSNUMBER
  168. {
  169. ((struct parser_control *) parm)->hour = $1;
  170. ((struct parser_control *) parm)->minutes = $3;
  171. ((struct parser_control *) parm)->meridian = MER24;
  172. ((struct parser_control *) parm)->zones_seen++;
  173. ((struct parser_control *) parm)->time_zone =
  174. $4 % 100 + ($4 / 100) * 60;
  175. }
  176. | tUNUMBER ':' tUNUMBER ':' tUNUMBER o_merid
  177. {
  178. ((struct parser_control *) parm)->hour = $1;
  179. ((struct parser_control *) parm)->minutes = $3;
  180. ((struct parser_control *) parm)->seconds = $5;
  181. ((struct parser_control *) parm)->meridian = $6;
  182. }
  183. | tUNUMBER ':' tUNUMBER ':' tUNUMBER tSNUMBER
  184. {
  185. ((struct parser_control *) parm)->hour = $1;
  186. ((struct parser_control *) parm)->minutes = $3;
  187. ((struct parser_control *) parm)->seconds = $5;
  188. ((struct parser_control *) parm)->meridian = MER24;
  189. ((struct parser_control *) parm)->zones_seen++;
  190. ((struct parser_control *) parm)->time_zone =
  191. $6 % 100 + ($6 / 100) * 60;
  192. }
  193. ;
  194. local_zone:
  195. tLOCAL_ZONE
  196. { ((struct parser_control *) parm)->local_isdst = $1; }
  197. | tLOCAL_ZONE tDST
  198. { ((struct parser_control *) parm)->local_isdst = $1 < 0 ? 1 : $1 + 1; }
  199. ;
  200. zone:
  201. tZONE
  202. { ((struct parser_control *) parm)->time_zone = $1; }
  203. | tDAYZONE
  204. { ((struct parser_control *) parm)->time_zone = $1 + 60; }
  205. | tZONE tDST
  206. { ((struct parser_control *) parm)->time_zone = $1 + 60; }
  207. ;
  208. day:
  209. tDAY
  210. {
  211. ((struct parser_control *) parm)->day_ordinal = 1;
  212. ((struct parser_control *) parm)->day_number = $1;
  213. }
  214. | tDAY ','
  215. {
  216. ((struct parser_control *) parm)->day_ordinal = 1;
  217. ((struct parser_control *) parm)->day_number = $1;
  218. }
  219. | tUNUMBER tDAY
  220. {
  221. ((struct parser_control *) parm)->day_ordinal = $1;
  222. ((struct parser_control *) parm)->day_number = $2;
  223. }
  224. ;
  225. date:
  226. tUNUMBER '/' tUNUMBER
  227. {
  228. ((struct parser_control *) parm)->month = $1;
  229. ((struct parser_control *) parm)->day = $3;
  230. }
  231. | tUNUMBER '/' tUNUMBER '/' tUNUMBER
  232. {
  233. /* Interpret as YYYY/MM/DD if 1000 <= $1, otherwise as MM/DD/YY.
  234. The goal in recognizing YYYY/MM/DD is solely to support legacy
  235. machine-generated dates like those in an RCS log listing. If
  236. you want portability, use the ISO 8601 format. */
  237. if (1000 <= $1)
  238. {
  239. ((struct parser_control *) parm)->year = $1;
  240. ((struct parser_control *) parm)->month = $3;
  241. ((struct parser_control *) parm)->day = $5;
  242. }
  243. else
  244. {
  245. ((struct parser_control *) parm)->month = $1;
  246. ((struct parser_control *) parm)->day = $3;
  247. ((struct parser_control *) parm)->year = $5;
  248. }
  249. }
  250. | tUNUMBER tSNUMBER tSNUMBER
  251. {
  252. /* ISO 8601 format. YYYY-MM-DD. */
  253. ((struct parser_control *) parm)->year = $1;
  254. ((struct parser_control *) parm)->month = -$2;
  255. ((struct parser_control *) parm)->day = -$3;
  256. }
  257. | tUNUMBER tMONTH tSNUMBER
  258. {
  259. /* e.g. 17-JUN-1992. */
  260. ((struct parser_control *) parm)->day = $1;
  261. ((struct parser_control *) parm)->month = $2;
  262. ((struct parser_control *) parm)->year = -$3;
  263. }
  264. | tMONTH tUNUMBER
  265. {
  266. ((struct parser_control *) parm)->month = $1;
  267. ((struct parser_control *) parm)->day = $2;
  268. }
  269. | tMONTH tUNUMBER ',' tUNUMBER
  270. {
  271. ((struct parser_control *) parm)->month = $1;
  272. ((struct parser_control *) parm)->day = $2;
  273. ((struct parser_control *) parm)->year = $4;
  274. }
  275. | tUNUMBER tMONTH
  276. {
  277. ((struct parser_control *) parm)->month = $2;
  278. ((struct parser_control *) parm)->day = $1;
  279. }
  280. | tUNUMBER tMONTH tUNUMBER
  281. {
  282. ((struct parser_control *) parm)->month = $2;
  283. ((struct parser_control *) parm)->day = $1;
  284. ((struct parser_control *) parm)->year = $3;
  285. }
  286. ;
  287. rel:
  288. relunit tAGO
  289. {
  290. ((struct parser_control *) parm)->rel_seconds = -((struct parser_control *) parm)->rel_seconds;
  291. ((struct parser_control *) parm)->rel_minutes = -((struct parser_control *) parm)->rel_minutes;
  292. ((struct parser_control *) parm)->rel_hour = -((struct parser_control *) parm)->rel_hour;
  293. ((struct parser_control *) parm)->rel_day = -((struct parser_control *) parm)->rel_day;
  294. ((struct parser_control *) parm)->rel_month = -((struct parser_control *) parm)->rel_month;
  295. ((struct parser_control *) parm)->rel_year = -((struct parser_control *) parm)->rel_year;
  296. }
  297. | relunit
  298. ;
  299. relunit:
  300. tUNUMBER tYEAR_UNIT
  301. { ((struct parser_control *) parm)->rel_year += $1 * $2; }
  302. | tSNUMBER tYEAR_UNIT
  303. { ((struct parser_control *) parm)->rel_year += $1 * $2; }
  304. | tYEAR_UNIT
  305. { ((struct parser_control *) parm)->rel_year += $1; }
  306. | tUNUMBER tMONTH_UNIT
  307. { ((struct parser_control *) parm)->rel_month += $1 * $2; }
  308. | tSNUMBER tMONTH_UNIT
  309. { ((struct parser_control *) parm)->rel_month += $1 * $2; }
  310. | tMONTH_UNIT
  311. { ((struct parser_control *) parm)->rel_month += $1; }
  312. | tUNUMBER tDAY_UNIT
  313. { ((struct parser_control *) parm)->rel_day += $1 * $2; }
  314. | tSNUMBER tDAY_UNIT
  315. { ((struct parser_control *) parm)->rel_day += $1 * $2; }
  316. | tDAY_UNIT
  317. { ((struct parser_control *) parm)->rel_day += $1; }
  318. | tUNUMBER tHOUR_UNIT
  319. { ((struct parser_control *) parm)->rel_hour += $1 * $2; }
  320. | tSNUMBER tHOUR_UNIT
  321. { ((struct parser_control *) parm)->rel_hour += $1 * $2; }
  322. | tHOUR_UNIT
  323. { ((struct parser_control *) parm)->rel_hour += $1; }
  324. | tUNUMBER tMINUTE_UNIT
  325. { ((struct parser_control *) parm)->rel_minutes += $1 * $2; }
  326. | tSNUMBER tMINUTE_UNIT
  327. { ((struct parser_control *) parm)->rel_minutes += $1 * $2; }
  328. | tMINUTE_UNIT
  329. { ((struct parser_control *) parm)->rel_minutes += $1; }
  330. | tUNUMBER tSEC_UNIT
  331. { ((struct parser_control *) parm)->rel_seconds += $1 * $2; }
  332. | tSNUMBER tSEC_UNIT
  333. { ((struct parser_control *) parm)->rel_seconds += $1 * $2; }
  334. | tSEC_UNIT
  335. { ((struct parser_control *) parm)->rel_seconds += $1; }
  336. ;
  337. number:
  338. tUNUMBER
  339. {
  340. if (((struct parser_control *) parm)->times_seen
  341. && ((struct parser_control *) parm)->dates_seen
  342. && ! ((struct parser_control *) parm)->rels_seen)
  343. ((struct parser_control *) parm)->year = $1;
  344. else
  345. {
  346. if (10000 < $1)
  347. {
  348. ((struct parser_control *) parm)->dates_seen++;
  349. ((struct parser_control *) parm)->day = $1 % 100;
  350. ((struct parser_control *) parm)->month = ($1 / 100) % 100;
  351. ((struct parser_control *) parm)->year = $1 / 10000;
  352. }
  353. else
  354. {
  355. ((struct parser_control *) parm)->times_seen++;
  356. if ($1 < 100)
  357. {
  358. ((struct parser_control *) parm)->hour = $1;
  359. ((struct parser_control *) parm)->minutes = 0;
  360. }
  361. else
  362. {
  363. ((struct parser_control *) parm)->hour = $1 / 100;
  364. ((struct parser_control *) parm)->minutes = $1 % 100;
  365. }
  366. ((struct parser_control *) parm)->seconds = 0;
  367. ((struct parser_control *) parm)->meridian = MER24;
  368. }
  369. }
  370. }
  371. ;
  372. o_merid:
  373. /* empty */
  374. { $$ = MER24; }
  375. | tMERIDIAN
  376. { $$ = $1; }
  377. ;
  378. %%
  379. /* Include this file down here because bison inserts code above which
  380. may define-away `const'. We want the prototype for get_date to have
  381. the same signature as the function definition. */
  382. #include "getdate.h"
  383. #ifndef gmtime
  384. struct tm *gmtime ();
  385. #endif
  386. #ifndef localtime
  387. struct tm *localtime ();
  388. #endif
  389. #ifndef mktime
  390. time_t mktime ();
  391. #endif
  392. static table const meridian_table[] =
  393. {
  394. { "AM", tMERIDIAN, MERam },
  395. { "A.M.", tMERIDIAN, MERam },
  396. { "PM", tMERIDIAN, MERpm },
  397. { "P.M.", tMERIDIAN, MERpm },
  398. { 0, 0, 0 }
  399. };
  400. static table const dst_table[] =
  401. {
  402. { "DST", tDST, 0 }
  403. };
  404. static table const month_and_day_table[] =
  405. {
  406. { "JANUARY", tMONTH, 1 },
  407. { "FEBRUARY", tMONTH, 2 },
  408. { "MARCH", tMONTH, 3 },
  409. { "APRIL", tMONTH, 4 },
  410. { "MAY", tMONTH, 5 },
  411. { "JUNE", tMONTH, 6 },
  412. { "JULY", tMONTH, 7 },
  413. { "AUGUST", tMONTH, 8 },
  414. { "SEPTEMBER",tMONTH, 9 },
  415. { "SEPT", tMONTH, 9 },
  416. { "OCTOBER", tMONTH, 10 },
  417. { "NOVEMBER", tMONTH, 11 },
  418. { "DECEMBER", tMONTH, 12 },
  419. { "SUNDAY", tDAY, 0 },
  420. { "MONDAY", tDAY, 1 },
  421. { "TUESDAY", tDAY, 2 },
  422. { "TUES", tDAY, 2 },
  423. { "WEDNESDAY",tDAY, 3 },
  424. { "WEDNES", tDAY, 3 },
  425. { "THURSDAY", tDAY, 4 },
  426. { "THUR", tDAY, 4 },
  427. { "THURS", tDAY, 4 },
  428. { "FRIDAY", tDAY, 5 },
  429. { "SATURDAY", tDAY, 6 },
  430. { 0, 0, 0 }
  431. };
  432. static table const time_units_table[] =
  433. {
  434. { "YEAR", tYEAR_UNIT, 1 },
  435. { "MONTH", tMONTH_UNIT, 1 },
  436. { "FORTNIGHT",tDAY_UNIT, 14 },
  437. { "WEEK", tDAY_UNIT, 7 },
  438. { "DAY", tDAY_UNIT, 1 },
  439. { "HOUR", tHOUR_UNIT, 1 },
  440. { "MINUTE", tMINUTE_UNIT, 1 },
  441. { "MIN", tMINUTE_UNIT, 1 },
  442. { "SECOND", tSEC_UNIT, 1 },
  443. { "SEC", tSEC_UNIT, 1 },
  444. { 0, 0, 0 }
  445. };
  446. /* Assorted relative-time words. */
  447. static table const relative_time_table[] =
  448. {
  449. { "TOMORROW", tMINUTE_UNIT, 24 * 60 },
  450. { "YESTERDAY",tMINUTE_UNIT, - (24 * 60) },
  451. { "TODAY", tMINUTE_UNIT, 0 },
  452. { "NOW", tMINUTE_UNIT, 0 },
  453. { "LAST", tUNUMBER, -1 },
  454. { "THIS", tMINUTE_UNIT, 0 },
  455. { "NEXT", tUNUMBER, 1 },
  456. { "FIRST", tUNUMBER, 1 },
  457. /*{ "SECOND", tUNUMBER, 2 }, */
  458. { "THIRD", tUNUMBER, 3 },
  459. { "FOURTH", tUNUMBER, 4 },
  460. { "FIFTH", tUNUMBER, 5 },
  461. { "SIXTH", tUNUMBER, 6 },
  462. { "SEVENTH", tUNUMBER, 7 },
  463. { "EIGHTH", tUNUMBER, 8 },
  464. { "NINTH", tUNUMBER, 9 },
  465. { "TENTH", tUNUMBER, 10 },
  466. { "ELEVENTH", tUNUMBER, 11 },
  467. { "TWELFTH", tUNUMBER, 12 },
  468. { "AGO", tAGO, 1 },
  469. { 0, 0, 0 }
  470. };
  471. /* The time zone table. This table is necessarily incomplete, as time
  472. zone abbreviations are ambiguous; e.g. Australians interpret "EST"
  473. as Eastern time in Australia, not as US Eastern Standard Time.
  474. You cannot rely on getdate to handle arbitrary time zone
  475. abbreviations; use numeric abbreviations like `-0500' instead. */
  476. static table const time_zone_table[] =
  477. {
  478. { "GMT", tZONE, HOUR ( 0) }, /* Greenwich Mean */
  479. { "UT", tZONE, HOUR ( 0) }, /* Universal (Coordinated) */
  480. { "UTC", tZONE, HOUR ( 0) },
  481. { "WET", tZONE, HOUR ( 0) }, /* Western European */
  482. { "WEST", tDAYZONE, HOUR ( 0) }, /* Western European Summer */
  483. { "BST", tDAYZONE, HOUR ( 0) }, /* British Summer */
  484. { "ART", tZONE, -HOUR ( 3) }, /* Argentina */
  485. { "BRT", tZONE, -HOUR ( 3) }, /* Brazil */
  486. { "BRST", tDAYZONE, -HOUR ( 3) }, /* Brazil Summer */
  487. { "NST", tZONE, -(HOUR ( 3) + 30) }, /* Newfoundland Standard */
  488. { "NDT", tDAYZONE,-(HOUR ( 3) + 30) }, /* Newfoundland Daylight */
  489. { "AST", tZONE, -HOUR ( 4) }, /* Atlantic Standard */
  490. { "ADT", tDAYZONE, -HOUR ( 4) }, /* Atlantic Daylight */
  491. { "CLT", tZONE, -HOUR ( 4) }, /* Chile */
  492. { "CLST", tDAYZONE, -HOUR ( 4) }, /* Chile Summer */
  493. { "EST", tZONE, -HOUR ( 5) }, /* Eastern Standard */
  494. { "EDT", tDAYZONE, -HOUR ( 5) }, /* Eastern Daylight */
  495. { "CST", tZONE, -HOUR ( 6) }, /* Central Standard */
  496. { "CDT", tDAYZONE, -HOUR ( 6) }, /* Central Daylight */
  497. { "MST", tZONE, -HOUR ( 7) }, /* Mountain Standard */
  498. { "MDT", tDAYZONE, -HOUR ( 7) }, /* Mountain Daylight */
  499. { "PST", tZONE, -HOUR ( 8) }, /* Pacific Standard */
  500. { "PDT", tDAYZONE, -HOUR ( 8) }, /* Pacific Daylight */
  501. { "AKST", tZONE, -HOUR ( 9) }, /* Alaska Standard */
  502. { "AKDT", tDAYZONE, -HOUR ( 9) }, /* Alaska Daylight */
  503. { "HST", tZONE, -HOUR (10) }, /* Hawaii Standard */
  504. { "HAST", tZONE, -HOUR (10) }, /* Hawaii-Aleutian Standard */
  505. { "HADT", tDAYZONE, -HOUR (10) }, /* Hawaii-Aleutian Daylight */
  506. { "SST", tZONE, -HOUR (12) }, /* Samoa Standard */
  507. { "WAT", tZONE, HOUR ( 1) }, /* West Africa */
  508. { "CET", tZONE, HOUR ( 1) }, /* Central European */
  509. { "CEST", tDAYZONE, HOUR ( 1) }, /* Central European Summer */
  510. { "MET", tZONE, HOUR ( 1) }, /* Middle European */
  511. { "MEZ", tZONE, HOUR ( 1) }, /* Middle European */
  512. { "MEST", tDAYZONE, HOUR ( 1) }, /* Middle European Summer */
  513. { "MESZ", tDAYZONE, HOUR ( 1) }, /* Middle European Summer */
  514. { "EET", tZONE, HOUR ( 2) }, /* Eastern European */
  515. { "EEST", tDAYZONE, HOUR ( 2) }, /* Eastern European Summer */
  516. { "CAT", tZONE, HOUR ( 2) }, /* Central Africa */
  517. { "SAST", tZONE, HOUR ( 2) }, /* South Africa Standard */
  518. { "EAT", tZONE, HOUR ( 3) }, /* East Africa */
  519. { "MSK", tZONE, HOUR ( 3) }, /* Moscow */
  520. { "MSD", tDAYZONE, HOUR ( 3) }, /* Moscow Daylight */
  521. { "IST", tZONE, (HOUR ( 5) + 30) }, /* India Standard */
  522. { "SGT", tZONE, HOUR ( 8) }, /* Singapore */
  523. { "KST", tZONE, HOUR ( 9) }, /* Korea Standard */
  524. { "JST", tZONE, HOUR ( 9) }, /* Japan Standard */
  525. { "GST", tZONE, HOUR (10) }, /* Guam Standard */
  526. { "NZST", tZONE, HOUR (12) }, /* New Zealand Standard */
  527. { "NZDT", tDAYZONE, HOUR (12) }, /* New Zealand Daylight */
  528. { 0, 0, 0 }
  529. };
  530. /* Military time zone table. */
  531. static table const military_table[] =
  532. {
  533. { "A", tZONE, -HOUR ( 1) },
  534. { "B", tZONE, -HOUR ( 2) },
  535. { "C", tZONE, -HOUR ( 3) },
  536. { "D", tZONE, -HOUR ( 4) },
  537. { "E", tZONE, -HOUR ( 5) },
  538. { "F", tZONE, -HOUR ( 6) },
  539. { "G", tZONE, -HOUR ( 7) },
  540. { "H", tZONE, -HOUR ( 8) },
  541. { "I", tZONE, -HOUR ( 9) },
  542. { "K", tZONE, -HOUR (10) },
  543. { "L", tZONE, -HOUR (11) },
  544. { "M", tZONE, -HOUR (12) },
  545. { "N", tZONE, HOUR ( 1) },
  546. { "O", tZONE, HOUR ( 2) },
  547. { "P", tZONE, HOUR ( 3) },
  548. { "Q", tZONE, HOUR ( 4) },
  549. { "R", tZONE, HOUR ( 5) },
  550. { "S", tZONE, HOUR ( 6) },
  551. { "T", tZONE, HOUR ( 7) },
  552. { "U", tZONE, HOUR ( 8) },
  553. { "V", tZONE, HOUR ( 9) },
  554. { "W", tZONE, HOUR (10) },
  555. { "X", tZONE, HOUR (11) },
  556. { "Y", tZONE, HOUR (12) },
  557. { "Z", tZONE, HOUR ( 0) },
  558. { 0, 0, 0 }
  559. };
  560. static int
  561. to_hour (int hours, int meridian)
  562. {
  563. switch (meridian)
  564. {
  565. case MER24:
  566. return 0 <= hours && hours < 24 ? hours : -1;
  567. case MERam:
  568. return 0 < hours && hours < 12 ? hours : hours == 12 ? 0 : -1;
  569. case MERpm:
  570. return 0 < hours && hours < 12 ? hours + 12 : hours == 12 ? 12 : -1;
  571. default:
  572. abort ();
  573. }
  574. /* NOTREACHED */
  575. }
  576. static int
  577. to_year (int year)
  578. {
  579. if (year < 0)
  580. year = -year;
  581. /* XPG4 suggests that years 00-68 map to 2000-2068, and
  582. years 69-99 map to 1969-1999. */
  583. if (year < 69)
  584. year += 2000;
  585. else if (year < 100)
  586. year += 1900;
  587. return year;
  588. }
  589. static table const *
  590. lookup_zone (struct parser_control const *pc, char const *name)
  591. {
  592. table const *tp;
  593. /* Try local zone abbreviations first; they're more likely to be right. */
  594. for (tp = pc->local_time_zone_table; tp->name; tp++)
  595. if (strcmp (name, tp->name) == 0)
  596. return tp;
  597. for (tp = time_zone_table; tp->name; tp++)
  598. if (strcmp (name, tp->name) == 0)
  599. return tp;
  600. return 0;
  601. }
  602. /* Yield A - B, measured in seconds. */
  603. static int
  604. difftm (struct tm *a, struct tm *b)
  605. {
  606. int ay = a->tm_year + (TM_YEAR_ORIGIN - 1);
  607. int by = b->tm_year + (TM_YEAR_ORIGIN - 1);
  608. int days = (
  609. /* difference in day of year */
  610. a->tm_yday - b->tm_yday
  611. /* + intervening leap days */
  612. + ((ay >> 2) - (by >> 2))
  613. - (ay / 100 - by / 100)
  614. + ((ay / 100 >> 2) - (by / 100 >> 2))
  615. /* + difference in years * 365 */
  616. + (int) (ay - by) * 365
  617. );
  618. return (60 * (60 * (24 * days + (a->tm_hour - b->tm_hour))
  619. + (a->tm_min - b->tm_min))
  620. + (a->tm_sec - b->tm_sec));
  621. }
  622. static table const *
  623. lookup_word (struct parser_control const *pc, char *word)
  624. {
  625. char *p;
  626. char *q;
  627. size_t wordlen;
  628. table const *tp;
  629. int i;
  630. int abbrev;
  631. /* Make it uppercase. */
  632. for (p = word; *p; p++)
  633. if (ISLOWER ((unsigned char) *p))
  634. *p = toupper ((unsigned char) *p);
  635. for (tp = meridian_table; tp->name; tp++)
  636. if (strcmp (word, tp->name) == 0)
  637. return tp;
  638. /* See if we have an abbreviation for a month. */
  639. wordlen = strlen (word);
  640. abbrev = wordlen == 3 || (wordlen == 4 && word[3] == '.');
  641. for (tp = month_and_day_table; tp->name; tp++)
  642. if ((abbrev ? strncmp (word, tp->name, 3) : strcmp (word, tp->name)) == 0)
  643. return tp;
  644. if ((tp = lookup_zone (pc, word)))
  645. return tp;
  646. if (strcmp (word, dst_table[0].name) == 0)
  647. return dst_table;
  648. for (tp = time_units_table; tp->name; tp++)
  649. if (strcmp (word, tp->name) == 0)
  650. return tp;
  651. /* Strip off any plural and try the units table again. */
  652. if (word[wordlen - 1] == 'S')
  653. {
  654. word[wordlen - 1] = '\0';
  655. for (tp = time_units_table; tp->name; tp++)
  656. if (strcmp (word, tp->name) == 0)
  657. return tp;
  658. word[wordlen - 1] = 'S'; /* For "this" in relative_time_table. */
  659. }
  660. for (tp = relative_time_table; tp->name; tp++)
  661. if (strcmp (word, tp->name) == 0)
  662. return tp;
  663. /* Military time zones. */
  664. if (wordlen == 1)
  665. for (tp = military_table; tp->name; tp++)
  666. if (word[0] == tp->name[0])
  667. return tp;
  668. /* Drop out any periods and try the time zone table again. */
  669. for (i = 0, p = q = word; (*p = *q); q++)
  670. if (*q == '.')
  671. i = 1;
  672. else
  673. p++;
  674. if (i && (tp = lookup_zone (pc, word)))
  675. return tp;
  676. return 0;
  677. }
  678. static int
  679. yylex (YYSTYPE *lvalp, struct parser_control *pc)
  680. {
  681. unsigned char c;
  682. int count;
  683. for (;;)
  684. {
  685. while (c = *pc->input, ISSPACE (c))
  686. pc->input++;
  687. if (ISDIGIT (c) || c == '-' || c == '+')
  688. {
  689. int sign;
  690. if (c == '-' || c == '+')
  691. {
  692. sign = c == '-' ? -1 : 1;
  693. if (! ISDIGIT (*++pc->input))
  694. /* skip the '-' sign */
  695. continue;
  696. }
  697. else
  698. sign = 0;
  699. for (*lvalp = 0; ISDIGIT (c = *pc->input++);)
  700. *lvalp = 10 * *lvalp + (c - '0');
  701. pc->input--;
  702. if (sign < 0)
  703. *lvalp = - *lvalp;
  704. return sign ? tSNUMBER : tUNUMBER;
  705. }
  706. if (ISALPHA (c))
  707. {
  708. char buff[20];
  709. char *p = buff;
  710. table const *tp;
  711. do
  712. {
  713. if (p < buff + sizeof buff - 1)
  714. *p++ = c;
  715. c = *++pc->input;
  716. }
  717. while (ISALPHA (c) || c == '.');
  718. *p = '\0';
  719. tp = lookup_word (pc, buff);
  720. if (! tp)
  721. return tID;
  722. *lvalp = tp->value;
  723. return tp->type;
  724. }
  725. if (c != '(')
  726. return *pc->input++;
  727. count = 0;
  728. do
  729. {
  730. c = *pc->input++;
  731. if (c == '\0')
  732. return c;
  733. if (c == '(')
  734. count++;
  735. else if (c == ')')
  736. count--;
  737. }
  738. while (count > 0);
  739. }
  740. }
  741. /* Do nothing if the parser reports an error. */
  742. static int
  743. yyerror (char *s ATTRIBUTE_UNUSED)
  744. {
  745. return 0;
  746. }
  747. /* ?? */
  748. time_t
  749. get_date (const char *p, const time_t *now)
  750. {
  751. time_t Start = now ? *now : time (0);
  752. struct tm *tmp = localtime (&Start);
  753. struct tm tm;
  754. struct tm tm0;
  755. struct parser_control pc;
  756. if (! tmp)
  757. return -1;
  758. pc.input = p;
  759. pc.year = tmp->tm_year + TM_YEAR_ORIGIN;
  760. pc.month = tmp->tm_mon + 1;
  761. pc.day = tmp->tm_mday;
  762. pc.hour = tmp->tm_hour;
  763. pc.minutes = tmp->tm_min;
  764. pc.seconds = tmp->tm_sec;
  765. tm.tm_isdst = tmp->tm_isdst;
  766. pc.meridian = MER24;
  767. pc.rel_seconds = 0;
  768. pc.rel_minutes = 0;
  769. pc.rel_hour = 0;
  770. pc.rel_day = 0;
  771. pc.rel_month = 0;
  772. pc.rel_year = 0;
  773. pc.dates_seen = 0;
  774. pc.days_seen = 0;
  775. pc.rels_seen = 0;
  776. pc.times_seen = 0;
  777. pc.local_zones_seen = 0;
  778. pc.zones_seen = 0;
  779. #if HAVE_TM_ZONE
  780. pc.local_time_zone_table[0].name = tmp->tm_zone;
  781. pc.local_time_zone_table[0].type = tLOCAL_ZONE;
  782. pc.local_time_zone_table[0].value = tmp->tm_isdst;
  783. pc.local_time_zone_table[1].name = 0;
  784. /* Probe the names used in the next three calendar quarters, looking
  785. for a tm_isdst different from the one we already have. */
  786. {
  787. int probe;
  788. for (probe = 1; probe <= 3; probe++)
  789. {
  790. time_t probe = Start + probe * (90 * 24 * 60 * 60);
  791. struct tm *tm = localtime (&probe);
  792. if (tm && tm->tm_zone
  793. && tm->tm_isdst != pc.local_time_zone_table[0].value)
  794. {
  795. {
  796. pc.local_time_zone_table[1].name = tm->tm_zone;
  797. pc.local_time_zone_table[1].type = tLOCAL_ZONE;
  798. pc.local_time_zone_table[1].value = tm->tm_isdst;
  799. pc.local_time_zone_table[2].name = 0;
  800. }
  801. break;
  802. }
  803. }
  804. }
  805. #else
  806. #if HAVE_TZNAME
  807. {
  808. # ifndef tzname
  809. extern char *tzname[];
  810. # endif
  811. int i;
  812. for (i = 0; i < 2; i++)
  813. {
  814. pc.local_time_zone_table[i].name = tzname[i];
  815. pc.local_time_zone_table[i].type = tLOCAL_ZONE;
  816. pc.local_time_zone_table[i].value = i;
  817. }
  818. pc.local_time_zone_table[i].name = 0;
  819. }
  820. #else
  821. pc.local_time_zone_table[0].name = 0;
  822. #endif
  823. #endif
  824. if (pc.local_time_zone_table[0].name && pc.local_time_zone_table[1].name
  825. && ! strcmp (pc.local_time_zone_table[0].name,
  826. pc.local_time_zone_table[1].name))
  827. {
  828. /* This locale uses the same abbrevation for standard and
  829. daylight times. So if we see that abbreviation, we don't
  830. know whether it's daylight time. */
  831. pc.local_time_zone_table[0].value = -1;
  832. pc.local_time_zone_table[1].name = 0;
  833. }
  834. if (yyparse (&pc) != 0
  835. || 1 < pc.times_seen || 1 < pc.dates_seen || 1 < pc.days_seen
  836. || 1 < (pc.local_zones_seen + pc.zones_seen)
  837. || (pc.local_zones_seen && 1 < pc.local_isdst))
  838. return -1;
  839. tm.tm_year = to_year (pc.year) - TM_YEAR_ORIGIN + pc.rel_year;
  840. tm.tm_mon = pc.month - 1 + pc.rel_month;
  841. tm.tm_mday = pc.day + pc.rel_day;
  842. if (pc.times_seen || (pc.rels_seen && ! pc.dates_seen && ! pc.days_seen))
  843. {
  844. tm.tm_hour = to_hour (pc.hour, pc.meridian);
  845. if (tm.tm_hour < 0)
  846. return -1;
  847. tm.tm_min = pc.minutes;
  848. tm.tm_sec = pc.seconds;
  849. }
  850. else
  851. {
  852. tm.tm_hour = tm.tm_min = tm.tm_sec = 0;
  853. }
  854. tm.tm_hour += pc.rel_hour;
  855. tm.tm_min += pc.rel_minutes;
  856. tm.tm_sec += pc.rel_seconds;
  857. /* Let mktime deduce tm_isdst if we have an absolute time stamp,
  858. or if the relative time stamp mentions days, months, or years. */
  859. if (pc.dates_seen | pc.days_seen | pc.times_seen | pc.rel_day | pc.rel_month | pc.rel_year)
  860. tm.tm_isdst = -1;
  861. /* But if the input explicitly specifies local time with or without
  862. DST, give mktime that information. */
  863. if (pc.local_zones_seen)
  864. tm.tm_isdst = pc.local_isdst;
  865. tm0 = tm;
  866. Start = mktime (&tm);
  867. if (Start == (time_t) -1)
  868. {
  869. /* Guard against falsely reporting errors near the time_t boundaries
  870. when parsing times in other time zones. For example, if the min
  871. time_t value is 1970-01-01 00:00:00 UTC and we are 8 hours ahead
  872. of UTC, then the min localtime value is 1970-01-01 08:00:00; if
  873. we apply mktime to 1970-01-01 00:00:00 we will get an error, so
  874. we apply mktime to 1970-01-02 08:00:00 instead and adjust the time
  875. zone by 24 hours to compensate. This algorithm assumes that
  876. there is no DST transition within a day of the time_t boundaries. */
  877. if (pc.zones_seen)
  878. {
  879. tm = tm0;
  880. if (tm.tm_year <= EPOCH_YEAR - TM_YEAR_ORIGIN)
  881. {
  882. tm.tm_mday++;
  883. pc.time_zone += 24 * 60;
  884. }
  885. else
  886. {
  887. tm.tm_mday--;
  888. pc.time_zone -= 24 * 60;
  889. }
  890. Start = mktime (&tm);
  891. }
  892. if (Start == (time_t) -1)
  893. return Start;
  894. }
  895. if (pc.days_seen && ! pc.dates_seen)
  896. {
  897. tm.tm_mday += ((pc.day_number - tm.tm_wday + 7) % 7
  898. + 7 * (pc.day_ordinal - (0 < pc.day_ordinal)));
  899. Start = mktime (&tm);
  900. if (Start == (time_t) -1)
  901. return Start;
  902. }
  903. if (pc.zones_seen)
  904. {
  905. int delta;
  906. struct tm *gmt = gmtime (&Start);
  907. if (! gmt)
  908. return -1;
  909. delta = pc.time_zone * 60 + difftm (gmt, &tm);
  910. if ((Start - delta < Start) != (delta < 0))
  911. return -1; /* time_t overflow */
  912. Start -= delta;
  913. }
  914. return Start;
  915. }
  916. #if TEST
  917. #include <stdio.h>
  918. int
  919. main (int ac, char **av)
  920. {
  921. char buff[BUFSIZ];
  922. time_t d;
  923. printf ("Enter date, or blank line to exit.\n\t> ");
  924. fflush (stdout);
  925. buff[BUFSIZ - 1] = 0;
  926. while (fgets (buff, BUFSIZ - 1, stdin) && buff[0])
  927. {
  928. d = get_date (buff, 0);
  929. if (d == (time_t) -1)
  930. printf ("Bad format - couldn't convert.\n");
  931. else
  932. printf ("%s", ctime (&d));
  933. printf ("\t> ");
  934. fflush (stdout);
  935. }
  936. return 0;
  937. }
  938. #endif /* defined TEST */