getdate.y 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115
  1. %{
  2. /* Parse a string into an internal time stamp.
  3. Copyright 1999, 2000 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__ < 8) || __STRICT_ANSI__
  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_BASE 1900
  70. #define HOUR(x) ((x) * 60)
  71. /* An integer value, and the number of digits in its textual
  72. representation. */
  73. typedef struct
  74. {
  75. int value;
  76. int digits;
  77. } textint;
  78. /* An entry in the lexical lookup table. */
  79. typedef struct
  80. {
  81. char const *name;
  82. int type;
  83. int value;
  84. } table;
  85. /* Meridian: am, pm, or 24-hour style. */
  86. enum { MERam, MERpm, MER24 };
  87. /* Information passed to and from the parser. */
  88. typedef struct
  89. {
  90. /* The input string remaining to be parsed. */
  91. const char *input;
  92. /* N, if this is the Nth Tuesday. */
  93. int day_ordinal;
  94. /* Day of week; Sunday is 0. */
  95. int day_number;
  96. /* tm_isdst flag for the local zone. */
  97. int local_isdst;
  98. /* Time zone, in minutes east of UTC. */
  99. int time_zone;
  100. /* Style used for time. */
  101. int meridian;
  102. /* Gregorian year, month, day, hour, minutes, and seconds. */
  103. textint year;
  104. int month;
  105. int day;
  106. int hour;
  107. int minutes;
  108. int seconds;
  109. /* Relative year, month, day, hour, minutes, and seconds. */
  110. int rel_year;
  111. int rel_month;
  112. int rel_day;
  113. int rel_hour;
  114. int rel_minutes;
  115. int rel_seconds;
  116. /* Counts of nonterminals of various flavors parsed so far. */
  117. int dates_seen;
  118. int days_seen;
  119. int local_zones_seen;
  120. int rels_seen;
  121. int times_seen;
  122. int zones_seen;
  123. /* Table of local time zone abbrevations, terminated by a null entry. */
  124. table local_time_zone_table[3];
  125. } parser_control;
  126. #define PC (* (parser_control *) parm)
  127. #define YYLEX_PARAM parm
  128. #define YYPARSE_PARAM parm
  129. static int yyerror ();
  130. static int yylex ();
  131. %}
  132. /* We want a reentrant parser. */
  133. %pure_parser
  134. /* This grammar has 13 shift/reduce conflicts. */
  135. %expect 13
  136. %union
  137. {
  138. int intval;
  139. textint textintval;
  140. }
  141. %token tAGO tDST
  142. %token <intval> tDAY tDAY_UNIT tDAYZONE tHOUR_UNIT tLOCAL_ZONE tMERIDIAN
  143. %token <intval> tMINUTE_UNIT tMONTH tMONTH_UNIT tSEC_UNIT tYEAR_UNIT tZONE
  144. %token <textintval> tSNUMBER tUNUMBER
  145. %type <intval> o_merid
  146. %%
  147. spec:
  148. /* empty */
  149. | spec item
  150. ;
  151. item:
  152. time
  153. { PC.times_seen++; }
  154. | local_zone
  155. { PC.local_zones_seen++; }
  156. | zone
  157. { PC.zones_seen++; }
  158. | date
  159. { PC.dates_seen++; }
  160. | day
  161. { PC.days_seen++; }
  162. | rel
  163. { PC.rels_seen++; }
  164. | number
  165. ;
  166. time:
  167. tUNUMBER tMERIDIAN
  168. {
  169. PC.hour = $1.value;
  170. PC.minutes = 0;
  171. PC.seconds = 0;
  172. PC.meridian = $2;
  173. }
  174. | tUNUMBER ':' tUNUMBER o_merid
  175. {
  176. PC.hour = $1.value;
  177. PC.minutes = $3.value;
  178. PC.seconds = 0;
  179. PC.meridian = $4;
  180. }
  181. | tUNUMBER ':' tUNUMBER tSNUMBER
  182. {
  183. PC.hour = $1.value;
  184. PC.minutes = $3.value;
  185. PC.meridian = MER24;
  186. PC.zones_seen++;
  187. PC.time_zone = $4.value % 100 + ($4.value / 100) * 60;
  188. }
  189. | tUNUMBER ':' tUNUMBER ':' tUNUMBER o_merid
  190. {
  191. PC.hour = $1.value;
  192. PC.minutes = $3.value;
  193. PC.seconds = $5.value;
  194. PC.meridian = $6;
  195. }
  196. | tUNUMBER ':' tUNUMBER ':' tUNUMBER tSNUMBER
  197. {
  198. PC.hour = $1.value;
  199. PC.minutes = $3.value;
  200. PC.seconds = $5.value;
  201. PC.meridian = MER24;
  202. PC.zones_seen++;
  203. PC.time_zone = $6.value % 100 + ($6.value / 100) * 60;
  204. }
  205. ;
  206. local_zone:
  207. tLOCAL_ZONE
  208. { PC.local_isdst = $1; }
  209. | tLOCAL_ZONE tDST
  210. { PC.local_isdst = $1 < 0 ? 1 : $1 + 1; }
  211. ;
  212. zone:
  213. tZONE
  214. { PC.time_zone = $1; }
  215. | tDAYZONE
  216. { PC.time_zone = $1 + 60; }
  217. | tZONE tDST
  218. { PC.time_zone = $1 + 60; }
  219. ;
  220. day:
  221. tDAY
  222. {
  223. PC.day_ordinal = 1;
  224. PC.day_number = $1;
  225. }
  226. | tDAY ','
  227. {
  228. PC.day_ordinal = 1;
  229. PC.day_number = $1;
  230. }
  231. | tUNUMBER tDAY
  232. {
  233. PC.day_ordinal = $1.value;
  234. PC.day_number = $2;
  235. }
  236. ;
  237. date:
  238. tUNUMBER '/' tUNUMBER
  239. {
  240. PC.month = $1.value;
  241. PC.day = $3.value;
  242. }
  243. | tUNUMBER '/' tUNUMBER '/' tUNUMBER
  244. {
  245. /* Interpret as YYYY/MM/DD if the first value has 4 or more digits,
  246. otherwise as MM/DD/YY.
  247. The goal in recognizing YYYY/MM/DD is solely to support legacy
  248. machine-generated dates like those in an RCS log listing. If
  249. you want portability, use the ISO 8601 format. */
  250. if (4 <= $1.digits)
  251. {
  252. PC.year = $1;
  253. PC.month = $3.value;
  254. PC.day = $5.value;
  255. }
  256. else
  257. {
  258. PC.month = $1.value;
  259. PC.day = $3.value;
  260. PC.year = $5;
  261. }
  262. }
  263. | tUNUMBER tSNUMBER tSNUMBER
  264. {
  265. /* ISO 8601 format. YYYY-MM-DD. */
  266. PC.year = $1;
  267. PC.month = -$2.value;
  268. PC.day = -$3.value;
  269. }
  270. | tUNUMBER tMONTH tSNUMBER
  271. {
  272. /* e.g. 17-JUN-1992. */
  273. PC.day = $1.value;
  274. PC.month = $2;
  275. PC.year.value = -$3.value;
  276. PC.year.digits = $3.digits;
  277. }
  278. | tMONTH tUNUMBER
  279. {
  280. PC.month = $1;
  281. PC.day = $2.value;
  282. }
  283. | tMONTH tUNUMBER ',' tUNUMBER
  284. {
  285. PC.month = $1;
  286. PC.day = $2.value;
  287. PC.year = $4;
  288. }
  289. | tUNUMBER tMONTH
  290. {
  291. PC.day = $1.value;
  292. PC.month = $2;
  293. }
  294. | tUNUMBER tMONTH tUNUMBER
  295. {
  296. PC.day = $1.value;
  297. PC.month = $2;
  298. PC.year = $3;
  299. }
  300. ;
  301. rel:
  302. relunit tAGO
  303. {
  304. PC.rel_seconds = -PC.rel_seconds;
  305. PC.rel_minutes = -PC.rel_minutes;
  306. PC.rel_hour = -PC.rel_hour;
  307. PC.rel_day = -PC.rel_day;
  308. PC.rel_month = -PC.rel_month;
  309. PC.rel_year = -PC.rel_year;
  310. }
  311. | relunit
  312. ;
  313. relunit:
  314. tUNUMBER tYEAR_UNIT
  315. { PC.rel_year += $1.value * $2; }
  316. | tSNUMBER tYEAR_UNIT
  317. { PC.rel_year += $1.value * $2; }
  318. | tYEAR_UNIT
  319. { PC.rel_year += $1; }
  320. | tUNUMBER tMONTH_UNIT
  321. { PC.rel_month += $1.value * $2; }
  322. | tSNUMBER tMONTH_UNIT
  323. { PC.rel_month += $1.value * $2; }
  324. | tMONTH_UNIT
  325. { PC.rel_month += $1; }
  326. | tUNUMBER tDAY_UNIT
  327. { PC.rel_day += $1.value * $2; }
  328. | tSNUMBER tDAY_UNIT
  329. { PC.rel_day += $1.value * $2; }
  330. | tDAY_UNIT
  331. { PC.rel_day += $1 }
  332. | tUNUMBER tHOUR_UNIT
  333. { PC.rel_hour += $1.value * $2; }
  334. | tSNUMBER tHOUR_UNIT
  335. { PC.rel_hour += $1.value * $2; }
  336. | tHOUR_UNIT
  337. { PC.rel_hour += $1 }
  338. | tUNUMBER tMINUTE_UNIT
  339. { PC.rel_minutes += $1.value * $2; }
  340. | tSNUMBER tMINUTE_UNIT
  341. { PC.rel_minutes += $1.value * $2; }
  342. | tMINUTE_UNIT
  343. { PC.rel_minutes += $1 }
  344. | tUNUMBER tSEC_UNIT
  345. { PC.rel_seconds += $1.value * $2; }
  346. | tSNUMBER tSEC_UNIT
  347. { PC.rel_seconds += $1.value * $2; }
  348. | tSEC_UNIT
  349. { PC.rel_seconds += $1; }
  350. ;
  351. number:
  352. tUNUMBER
  353. {
  354. if (PC.dates_seen
  355. && ! PC.rels_seen && (PC.times_seen || 2 < $1.digits))
  356. PC.year = $1;
  357. else
  358. {
  359. if (4 < $1.digits)
  360. {
  361. PC.dates_seen++;
  362. PC.day = $1.value % 100;
  363. PC.month = ($1.value / 100) % 100;
  364. PC.year.value = $1.value / 10000;
  365. PC.year.digits = $1.digits - 4;
  366. }
  367. else
  368. {
  369. PC.times_seen++;
  370. if ($1.digits <= 2)
  371. {
  372. PC.hour = $1.value;
  373. PC.minutes = 0;
  374. }
  375. else
  376. {
  377. PC.hour = $1.value / 100;
  378. PC.minutes = $1.value % 100;
  379. }
  380. PC.seconds = 0;
  381. PC.meridian = MER24;
  382. }
  383. }
  384. }
  385. ;
  386. o_merid:
  387. /* empty */
  388. { $$ = MER24; }
  389. | tMERIDIAN
  390. { $$ = $1; }
  391. ;
  392. %%
  393. /* Include this file down here because bison inserts code above which
  394. may define-away `const'. We want the prototype for get_date to have
  395. the same signature as the function definition. */
  396. #include "getdate.h"
  397. #ifndef gmtime
  398. struct tm *gmtime ();
  399. #endif
  400. #ifndef localtime
  401. struct tm *localtime ();
  402. #endif
  403. #ifndef mktime
  404. time_t mktime ();
  405. #endif
  406. static table const meridian_table[] =
  407. {
  408. { "AM", tMERIDIAN, MERam },
  409. { "A.M.", tMERIDIAN, MERam },
  410. { "PM", tMERIDIAN, MERpm },
  411. { "P.M.", tMERIDIAN, MERpm },
  412. { 0, 0, 0 }
  413. };
  414. static table const dst_table[] =
  415. {
  416. { "DST", tDST, 0 }
  417. };
  418. static table const month_and_day_table[] =
  419. {
  420. { "JANUARY", tMONTH, 1 },
  421. { "FEBRUARY", tMONTH, 2 },
  422. { "MARCH", tMONTH, 3 },
  423. { "APRIL", tMONTH, 4 },
  424. { "MAY", tMONTH, 5 },
  425. { "JUNE", tMONTH, 6 },
  426. { "JULY", tMONTH, 7 },
  427. { "AUGUST", tMONTH, 8 },
  428. { "SEPTEMBER",tMONTH, 9 },
  429. { "SEPT", tMONTH, 9 },
  430. { "OCTOBER", tMONTH, 10 },
  431. { "NOVEMBER", tMONTH, 11 },
  432. { "DECEMBER", tMONTH, 12 },
  433. { "SUNDAY", tDAY, 0 },
  434. { "MONDAY", tDAY, 1 },
  435. { "TUESDAY", tDAY, 2 },
  436. { "TUES", tDAY, 2 },
  437. { "WEDNESDAY",tDAY, 3 },
  438. { "WEDNES", tDAY, 3 },
  439. { "THURSDAY", tDAY, 4 },
  440. { "THUR", tDAY, 4 },
  441. { "THURS", tDAY, 4 },
  442. { "FRIDAY", tDAY, 5 },
  443. { "SATURDAY", tDAY, 6 },
  444. { 0, 0, 0 }
  445. };
  446. static table const time_units_table[] =
  447. {
  448. { "YEAR", tYEAR_UNIT, 1 },
  449. { "MONTH", tMONTH_UNIT, 1 },
  450. { "FORTNIGHT",tDAY_UNIT, 14 },
  451. { "WEEK", tDAY_UNIT, 7 },
  452. { "DAY", tDAY_UNIT, 1 },
  453. { "HOUR", tHOUR_UNIT, 1 },
  454. { "MINUTE", tMINUTE_UNIT, 1 },
  455. { "MIN", tMINUTE_UNIT, 1 },
  456. { "SECOND", tSEC_UNIT, 1 },
  457. { "SEC", tSEC_UNIT, 1 },
  458. { 0, 0, 0 }
  459. };
  460. /* Assorted relative-time words. */
  461. static table const relative_time_table[] =
  462. {
  463. { "TOMORROW", tMINUTE_UNIT, 24 * 60 },
  464. { "YESTERDAY",tMINUTE_UNIT, - (24 * 60) },
  465. { "TODAY", tMINUTE_UNIT, 0 },
  466. { "NOW", tMINUTE_UNIT, 0 },
  467. { "LAST", tUNUMBER, -1 },
  468. { "THIS", tUNUMBER, 0 },
  469. { "NEXT", tUNUMBER, 1 },
  470. { "FIRST", tUNUMBER, 1 },
  471. /*{ "SECOND", tUNUMBER, 2 }, */
  472. { "THIRD", tUNUMBER, 3 },
  473. { "FOURTH", tUNUMBER, 4 },
  474. { "FIFTH", tUNUMBER, 5 },
  475. { "SIXTH", tUNUMBER, 6 },
  476. { "SEVENTH", tUNUMBER, 7 },
  477. { "EIGHTH", tUNUMBER, 8 },
  478. { "NINTH", tUNUMBER, 9 },
  479. { "TENTH", tUNUMBER, 10 },
  480. { "ELEVENTH", tUNUMBER, 11 },
  481. { "TWELFTH", tUNUMBER, 12 },
  482. { "AGO", tAGO, 1 },
  483. { 0, 0, 0 }
  484. };
  485. /* The time zone table. This table is necessarily incomplete, as time
  486. zone abbreviations are ambiguous; e.g. Australians interpret "EST"
  487. as Eastern time in Australia, not as US Eastern Standard Time.
  488. You cannot rely on getdate to handle arbitrary time zone
  489. abbreviations; use numeric abbreviations like `-0500' instead. */
  490. static table const time_zone_table[] =
  491. {
  492. { "GMT", tZONE, HOUR ( 0) }, /* Greenwich Mean */
  493. { "UT", tZONE, HOUR ( 0) }, /* Universal (Coordinated) */
  494. { "UTC", tZONE, HOUR ( 0) },
  495. { "WET", tZONE, HOUR ( 0) }, /* Western European */
  496. { "WEST", tDAYZONE, HOUR ( 0) }, /* Western European Summer */
  497. { "BST", tDAYZONE, HOUR ( 0) }, /* British Summer */
  498. { "ART", tZONE, -HOUR ( 3) }, /* Argentina */
  499. { "BRT", tZONE, -HOUR ( 3) }, /* Brazil */
  500. { "BRST", tDAYZONE, -HOUR ( 3) }, /* Brazil Summer */
  501. { "NST", tZONE, -(HOUR ( 3) + 30) }, /* Newfoundland Standard */
  502. { "NDT", tDAYZONE,-(HOUR ( 3) + 30) }, /* Newfoundland Daylight */
  503. { "AST", tZONE, -HOUR ( 4) }, /* Atlantic Standard */
  504. { "ADT", tDAYZONE, -HOUR ( 4) }, /* Atlantic Daylight */
  505. { "CLT", tZONE, -HOUR ( 4) }, /* Chile */
  506. { "CLST", tDAYZONE, -HOUR ( 4) }, /* Chile Summer */
  507. { "EST", tZONE, -HOUR ( 5) }, /* Eastern Standard */
  508. { "EDT", tDAYZONE, -HOUR ( 5) }, /* Eastern Daylight */
  509. { "CST", tZONE, -HOUR ( 6) }, /* Central Standard */
  510. { "CDT", tDAYZONE, -HOUR ( 6) }, /* Central Daylight */
  511. { "MST", tZONE, -HOUR ( 7) }, /* Mountain Standard */
  512. { "MDT", tDAYZONE, -HOUR ( 7) }, /* Mountain Daylight */
  513. { "PST", tZONE, -HOUR ( 8) }, /* Pacific Standard */
  514. { "PDT", tDAYZONE, -HOUR ( 8) }, /* Pacific Daylight */
  515. { "AKST", tZONE, -HOUR ( 9) }, /* Alaska Standard */
  516. { "AKDT", tDAYZONE, -HOUR ( 9) }, /* Alaska Daylight */
  517. { "HST", tZONE, -HOUR (10) }, /* Hawaii Standard */
  518. { "HAST", tZONE, -HOUR (10) }, /* Hawaii-Aleutian Standard */
  519. { "HADT", tDAYZONE, -HOUR (10) }, /* Hawaii-Aleutian Daylight */
  520. { "SST", tZONE, -HOUR (12) }, /* Samoa Standard */
  521. { "WAT", tZONE, HOUR ( 1) }, /* West Africa */
  522. { "CET", tZONE, HOUR ( 1) }, /* Central European */
  523. { "CEST", tDAYZONE, HOUR ( 1) }, /* Central European Summer */
  524. { "MET", tZONE, HOUR ( 1) }, /* Middle European */
  525. { "MEZ", tZONE, HOUR ( 1) }, /* Middle European */
  526. { "MEST", tDAYZONE, HOUR ( 1) }, /* Middle European Summer */
  527. { "MESZ", tDAYZONE, HOUR ( 1) }, /* Middle European Summer */
  528. { "EET", tZONE, HOUR ( 2) }, /* Eastern European */
  529. { "EEST", tDAYZONE, HOUR ( 2) }, /* Eastern European Summer */
  530. { "CAT", tZONE, HOUR ( 2) }, /* Central Africa */
  531. { "SAST", tZONE, HOUR ( 2) }, /* South Africa Standard */
  532. { "EAT", tZONE, HOUR ( 3) }, /* East Africa */
  533. { "MSK", tZONE, HOUR ( 3) }, /* Moscow */
  534. { "MSD", tDAYZONE, HOUR ( 3) }, /* Moscow Daylight */
  535. { "IST", tZONE, (HOUR ( 5) + 30) }, /* India Standard */
  536. { "SGT", tZONE, HOUR ( 8) }, /* Singapore */
  537. { "KST", tZONE, HOUR ( 9) }, /* Korea Standard */
  538. { "JST", tZONE, HOUR ( 9) }, /* Japan Standard */
  539. { "GST", tZONE, HOUR (10) }, /* Guam Standard */
  540. { "NZST", tZONE, HOUR (12) }, /* New Zealand Standard */
  541. { "NZDT", tDAYZONE, HOUR (12) }, /* New Zealand Daylight */
  542. { 0, 0, 0 }
  543. };
  544. /* Military time zone table. */
  545. static table const military_table[] =
  546. {
  547. { "A", tZONE, -HOUR ( 1) },
  548. { "B", tZONE, -HOUR ( 2) },
  549. { "C", tZONE, -HOUR ( 3) },
  550. { "D", tZONE, -HOUR ( 4) },
  551. { "E", tZONE, -HOUR ( 5) },
  552. { "F", tZONE, -HOUR ( 6) },
  553. { "G", tZONE, -HOUR ( 7) },
  554. { "H", tZONE, -HOUR ( 8) },
  555. { "I", tZONE, -HOUR ( 9) },
  556. { "K", tZONE, -HOUR (10) },
  557. { "L", tZONE, -HOUR (11) },
  558. { "M", tZONE, -HOUR (12) },
  559. { "N", tZONE, HOUR ( 1) },
  560. { "O", tZONE, HOUR ( 2) },
  561. { "P", tZONE, HOUR ( 3) },
  562. { "Q", tZONE, HOUR ( 4) },
  563. { "R", tZONE, HOUR ( 5) },
  564. { "S", tZONE, HOUR ( 6) },
  565. { "T", tZONE, HOUR ( 7) },
  566. { "U", tZONE, HOUR ( 8) },
  567. { "V", tZONE, HOUR ( 9) },
  568. { "W", tZONE, HOUR (10) },
  569. { "X", tZONE, HOUR (11) },
  570. { "Y", tZONE, HOUR (12) },
  571. { "Z", tZONE, HOUR ( 0) },
  572. { 0, 0, 0 }
  573. };
  574. static int
  575. to_hour (int hours, int meridian)
  576. {
  577. switch (meridian)
  578. {
  579. case MER24:
  580. return 0 <= hours && hours < 24 ? hours : -1;
  581. case MERam:
  582. return 0 < hours && hours < 12 ? hours : hours == 12 ? 0 : -1;
  583. case MERpm:
  584. return 0 < hours && hours < 12 ? hours + 12 : hours == 12 ? 12 : -1;
  585. default:
  586. abort ();
  587. }
  588. /* NOTREACHED */
  589. }
  590. static int
  591. to_year (textint textyear)
  592. {
  593. int year = textyear.value;
  594. if (year < 0)
  595. year = -year;
  596. /* XPG4 suggests that years 00-68 map to 2000-2068, and
  597. years 69-99 map to 1969-1999. */
  598. if (textyear.digits == 2)
  599. year += year < 69 ? 2000 : 1900;
  600. return year;
  601. }
  602. static table const *
  603. lookup_zone (parser_control const *pc, char const *name)
  604. {
  605. table const *tp;
  606. /* Try local zone abbreviations first; they're more likely to be right. */
  607. for (tp = pc->local_time_zone_table; tp->name; tp++)
  608. if (strcmp (name, tp->name) == 0)
  609. return tp;
  610. for (tp = time_zone_table; tp->name; tp++)
  611. if (strcmp (name, tp->name) == 0)
  612. return tp;
  613. return 0;
  614. }
  615. #if ! HAVE_TM_GMTOFF
  616. /* Yield the difference between *A and *B,
  617. measured in seconds, ignoring leap seconds.
  618. The body of this function is taken directly from the GNU C Library;
  619. see src/strftime.c. */
  620. static int
  621. tm_diff (struct tm const *a, struct tm const *b)
  622. {
  623. /* Compute intervening leap days correctly even if year is negative.
  624. Take care to avoid int overflow in leap day calculations,
  625. but it's OK to assume that A and B are close to each other. */
  626. int a4 = (a->tm_year >> 2) + (TM_YEAR_BASE >> 2) - ! (a->tm_year & 3);
  627. int b4 = (b->tm_year >> 2) + (TM_YEAR_BASE >> 2) - ! (b->tm_year & 3);
  628. int a100 = a4 / 25 - (a4 % 25 < 0);
  629. int b100 = b4 / 25 - (b4 % 25 < 0);
  630. int a400 = a100 >> 2;
  631. int b400 = b100 >> 2;
  632. int intervening_leap_days = (a4 - b4) - (a100 - b100) + (a400 - b400);
  633. int years = a->tm_year - b->tm_year;
  634. int days = (365 * years + intervening_leap_days
  635. + (a->tm_yday - b->tm_yday));
  636. return (60 * (60 * (24 * days + (a->tm_hour - b->tm_hour))
  637. + (a->tm_min - b->tm_min))
  638. + (a->tm_sec - b->tm_sec));
  639. }
  640. #endif /* ! HAVE_TM_GMTOFF */
  641. static table const *
  642. lookup_word (parser_control const *pc, char *word)
  643. {
  644. char *p;
  645. char *q;
  646. size_t wordlen;
  647. table const *tp;
  648. int i;
  649. int abbrev;
  650. /* Make it uppercase. */
  651. for (p = word; *p; p++)
  652. if (ISLOWER ((unsigned char) *p))
  653. *p = toupper ((unsigned char) *p);
  654. for (tp = meridian_table; tp->name; tp++)
  655. if (strcmp (word, tp->name) == 0)
  656. return tp;
  657. /* See if we have an abbreviation for a month. */
  658. wordlen = strlen (word);
  659. abbrev = wordlen == 3 || (wordlen == 4 && word[3] == '.');
  660. for (tp = month_and_day_table; tp->name; tp++)
  661. if ((abbrev ? strncmp (word, tp->name, 3) : strcmp (word, tp->name)) == 0)
  662. return tp;
  663. if ((tp = lookup_zone (pc, word)))
  664. return tp;
  665. if (strcmp (word, dst_table[0].name) == 0)
  666. return dst_table;
  667. for (tp = time_units_table; tp->name; tp++)
  668. if (strcmp (word, tp->name) == 0)
  669. return tp;
  670. /* Strip off any plural and try the units table again. */
  671. if (word[wordlen - 1] == 'S')
  672. {
  673. word[wordlen - 1] = '\0';
  674. for (tp = time_units_table; tp->name; tp++)
  675. if (strcmp (word, tp->name) == 0)
  676. return tp;
  677. word[wordlen - 1] = 'S'; /* For "this" in relative_time_table. */
  678. }
  679. for (tp = relative_time_table; tp->name; tp++)
  680. if (strcmp (word, tp->name) == 0)
  681. return tp;
  682. /* Military time zones. */
  683. if (wordlen == 1)
  684. for (tp = military_table; tp->name; tp++)
  685. if (word[0] == tp->name[0])
  686. return tp;
  687. /* Drop out any periods and try the time zone table again. */
  688. for (i = 0, p = q = word; (*p = *q); q++)
  689. if (*q == '.')
  690. i = 1;
  691. else
  692. p++;
  693. if (i && (tp = lookup_zone (pc, word)))
  694. return tp;
  695. return 0;
  696. }
  697. static int
  698. yylex (YYSTYPE *lvalp, parser_control *pc)
  699. {
  700. unsigned char c;
  701. int count;
  702. for (;;)
  703. {
  704. while (c = *pc->input, ISSPACE (c))
  705. pc->input++;
  706. if (ISDIGIT (c) || c == '-' || c == '+')
  707. {
  708. char const *p;
  709. int sign;
  710. int value;
  711. if (c == '-' || c == '+')
  712. {
  713. sign = c == '-' ? -1 : 1;
  714. c = *++pc->input;
  715. if (! ISDIGIT (c))
  716. /* skip the '-' sign */
  717. continue;
  718. }
  719. else
  720. sign = 0;
  721. p = pc->input;
  722. value = 0;
  723. do
  724. {
  725. value = 10 * value + c - '0';
  726. c = *++p;
  727. }
  728. while (ISDIGIT (c));
  729. lvalp->textintval.value = sign < 0 ? -value : value;
  730. lvalp->textintval.digits = p - pc->input;
  731. pc->input = p;
  732. return sign ? tSNUMBER : tUNUMBER;
  733. }
  734. if (ISALPHA (c))
  735. {
  736. char buff[20];
  737. char *p = buff;
  738. table const *tp;
  739. do
  740. {
  741. if (p < buff + sizeof buff - 1)
  742. *p++ = c;
  743. c = *++pc->input;
  744. }
  745. while (ISALPHA (c) || c == '.');
  746. *p = '\0';
  747. tp = lookup_word (pc, buff);
  748. if (! tp)
  749. return '?';
  750. lvalp->intval = tp->value;
  751. return tp->type;
  752. }
  753. if (c != '(')
  754. return *pc->input++;
  755. count = 0;
  756. do
  757. {
  758. c = *pc->input++;
  759. if (c == '\0')
  760. return c;
  761. if (c == '(')
  762. count++;
  763. else if (c == ')')
  764. count--;
  765. }
  766. while (count > 0);
  767. }
  768. }
  769. /* Do nothing if the parser reports an error. */
  770. static int
  771. yyerror (char *s ATTRIBUTE_UNUSED)
  772. {
  773. return 0;
  774. }
  775. /* Parse a date/time string P. Return the corresponding time_t value,
  776. or (time_t) -1 if there is an error. P can be an incomplete or
  777. relative time specification; if so, use *NOW as the basis for the
  778. returned time. */
  779. time_t
  780. get_date (const char *p, const time_t *now)
  781. {
  782. time_t Start = now ? *now : time (0);
  783. struct tm *tmp = localtime (&Start);
  784. struct tm tm;
  785. struct tm tm0;
  786. parser_control pc;
  787. if (! tmp)
  788. return -1;
  789. pc.input = p;
  790. pc.year.value = tmp->tm_year + TM_YEAR_BASE;
  791. pc.year.digits = 4;
  792. pc.month = tmp->tm_mon + 1;
  793. pc.day = tmp->tm_mday;
  794. pc.hour = tmp->tm_hour;
  795. pc.minutes = tmp->tm_min;
  796. pc.seconds = tmp->tm_sec;
  797. tm.tm_isdst = tmp->tm_isdst;
  798. pc.meridian = MER24;
  799. pc.rel_seconds = 0;
  800. pc.rel_minutes = 0;
  801. pc.rel_hour = 0;
  802. pc.rel_day = 0;
  803. pc.rel_month = 0;
  804. pc.rel_year = 0;
  805. pc.dates_seen = 0;
  806. pc.days_seen = 0;
  807. pc.rels_seen = 0;
  808. pc.times_seen = 0;
  809. pc.local_zones_seen = 0;
  810. pc.zones_seen = 0;
  811. #if HAVE_TM_ZONE
  812. pc.local_time_zone_table[0].name = tmp->tm_zone;
  813. pc.local_time_zone_table[0].type = tLOCAL_ZONE;
  814. pc.local_time_zone_table[0].value = tmp->tm_isdst;
  815. pc.local_time_zone_table[1].name = 0;
  816. /* Probe the names used in the next three calendar quarters, looking
  817. for a tm_isdst different from the one we already have. */
  818. {
  819. int quarter;
  820. for (quarter = 1; quarter <= 3; quarter++)
  821. {
  822. time_t probe = Start + quarter * (90 * 24 * 60 * 60);
  823. struct tm *probe_tm = localtime (&probe);
  824. if (probe_tm && probe_tm->tm_zone
  825. && probe_tm->tm_isdst != pc.local_time_zone_table[0].value)
  826. {
  827. {
  828. pc.local_time_zone_table[1].name = probe_tm->tm_zone;
  829. pc.local_time_zone_table[1].type = tLOCAL_ZONE;
  830. pc.local_time_zone_table[1].value = probe_tm->tm_isdst;
  831. pc.local_time_zone_table[2].name = 0;
  832. }
  833. break;
  834. }
  835. }
  836. }
  837. #else
  838. #if HAVE_TZNAME
  839. {
  840. # ifndef tzname
  841. extern char *tzname[];
  842. # endif
  843. int i;
  844. for (i = 0; i < 2; i++)
  845. {
  846. pc.local_time_zone_table[i].name = tzname[i];
  847. pc.local_time_zone_table[i].type = tLOCAL_ZONE;
  848. pc.local_time_zone_table[i].value = i;
  849. }
  850. pc.local_time_zone_table[i].name = 0;
  851. }
  852. #else
  853. pc.local_time_zone_table[0].name = 0;
  854. #endif
  855. #endif
  856. if (pc.local_time_zone_table[0].name && pc.local_time_zone_table[1].name
  857. && ! strcmp (pc.local_time_zone_table[0].name,
  858. pc.local_time_zone_table[1].name))
  859. {
  860. /* This locale uses the same abbrevation for standard and
  861. daylight times. So if we see that abbreviation, we don't
  862. know whether it's daylight time. */
  863. pc.local_time_zone_table[0].value = -1;
  864. pc.local_time_zone_table[1].name = 0;
  865. }
  866. if (yyparse (&pc) != 0
  867. || 1 < pc.times_seen || 1 < pc.dates_seen || 1 < pc.days_seen
  868. || 1 < (pc.local_zones_seen + pc.zones_seen)
  869. || (pc.local_zones_seen && 1 < pc.local_isdst))
  870. return -1;
  871. tm.tm_year = to_year (pc.year) - TM_YEAR_BASE + pc.rel_year;
  872. tm.tm_mon = pc.month - 1 + pc.rel_month;
  873. tm.tm_mday = pc.day + pc.rel_day;
  874. if (pc.times_seen || (pc.rels_seen && ! pc.dates_seen && ! pc.days_seen))
  875. {
  876. tm.tm_hour = to_hour (pc.hour, pc.meridian);
  877. if (tm.tm_hour < 0)
  878. return -1;
  879. tm.tm_min = pc.minutes;
  880. tm.tm_sec = pc.seconds;
  881. }
  882. else
  883. {
  884. tm.tm_hour = tm.tm_min = tm.tm_sec = 0;
  885. }
  886. /* Let mktime deduce tm_isdst if we have an absolute time stamp,
  887. or if the relative time stamp mentions days, months, or years. */
  888. if (pc.dates_seen | pc.days_seen | pc.times_seen | pc.rel_day
  889. | pc.rel_month | pc.rel_year)
  890. tm.tm_isdst = -1;
  891. /* But if the input explicitly specifies local time with or without
  892. DST, give mktime that information. */
  893. if (pc.local_zones_seen)
  894. tm.tm_isdst = pc.local_isdst;
  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 (pc.zones_seen)
  908. {
  909. tm = tm0;
  910. if (tm.tm_year <= EPOCH_YEAR - TM_YEAR_BASE)
  911. {
  912. tm.tm_mday++;
  913. pc.time_zone += 24 * 60;
  914. }
  915. else
  916. {
  917. tm.tm_mday--;
  918. pc.time_zone -= 24 * 60;
  919. }
  920. Start = mktime (&tm);
  921. }
  922. if (Start == (time_t) -1)
  923. return Start;
  924. }
  925. if (pc.days_seen && ! pc.dates_seen)
  926. {
  927. tm.tm_mday += ((pc.day_number - tm.tm_wday + 7) % 7
  928. + 7 * (pc.day_ordinal - (0 < pc.day_ordinal)));
  929. tm.tm_isdst = -1;
  930. Start = mktime (&tm);
  931. if (Start == (time_t) -1)
  932. return Start;
  933. }
  934. if (pc.zones_seen)
  935. {
  936. int delta = pc.time_zone * 60;
  937. #ifdef HAVE_TM_GMTOFF
  938. delta -= tm.tm_gmtoff;
  939. #else
  940. struct tm *gmt = gmtime (&Start);
  941. if (! gmt)
  942. return -1;
  943. delta -= tm_diff (&tm, gmt);
  944. #endif
  945. if ((Start < Start - delta) != (delta < 0))
  946. return -1; /* time_t overflow */
  947. Start -= delta;
  948. }
  949. /* Add relative hours, minutes, and seconds. Ignore leap seconds;
  950. i.e. "+ 10 minutes" means 600 seconds, even if one of them is a
  951. leap second. Typically this is not what the user wants, but it's
  952. too hard to do it the other way, because the time zone indicator
  953. must be applied before relative times, and if mktime is applied
  954. again the time zone will be lost. */
  955. {
  956. time_t t0 = Start;
  957. long d1 = 60 * 60 * (long) pc.rel_hour;
  958. time_t t1 = t0 + d1;
  959. long d2 = 60 * (long) pc.rel_minutes;
  960. time_t t2 = t1 + d2;
  961. int d3 = pc.rel_seconds;
  962. time_t t3 = t2 + d3;
  963. if ((d1 / (60 * 60) ^ pc.rel_hour)
  964. | (d2 / 60 ^ pc.rel_minutes)
  965. | ((t0 + d1 < t0) ^ (d1 < 0))
  966. | ((t1 + d2 < t1) ^ (d2 < 0))
  967. | ((t2 + d3 < t2) ^ (d3 < 0)))
  968. return -1;
  969. Start = t3;
  970. }
  971. return Start;
  972. }
  973. #if TEST
  974. #include <stdio.h>
  975. int
  976. main (int ac, char **av)
  977. {
  978. char buff[BUFSIZ];
  979. time_t d;
  980. printf ("Enter date, or blank line to exit.\n\t> ");
  981. fflush (stdout);
  982. buff[BUFSIZ - 1] = 0;
  983. while (fgets (buff, BUFSIZ - 1, stdin) && buff[0])
  984. {
  985. d = get_date (buff, 0);
  986. if (d == (time_t) -1)
  987. printf ("Bad format - couldn't convert.\n");
  988. else
  989. printf ("%s", ctime (&d));
  990. printf ("\t> ");
  991. fflush (stdout);
  992. }
  993. return 0;
  994. }
  995. #endif /* defined TEST */