getdate.y 26 KB

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