4
0

getdate.y 28 KB

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