getdate.y 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048
  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 <[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. # 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_ORIGIN 1900
  70. #define HOUR(x) ((x) * 60)
  71. /* An entry in the lexical lookup table. */
  72. typedef struct
  73. {
  74. char const *name;
  75. int type;
  76. int value;
  77. } table;
  78. /* Meridian: am, pm, or 24-hour style. */
  79. enum { MERam, MERpm, MER24 };
  80. /* Information passed to and from the parser. */
  81. struct parser_control
  82. {
  83. /* The input string remaining to be parsed. */
  84. const char *input;
  85. /* N, if this is the Nth Tuesday. */
  86. int day_ordinal;
  87. /* Day of week; Sunday is 0. */
  88. int day_number;
  89. /* tm_isdst flag for the local zone. */
  90. int local_isdst;
  91. /* Time zone, in minutes east of UTC. */
  92. int time_zone;
  93. /* Style used for time. */
  94. int meridian;
  95. /* Gregorian year, month, day, hour, minutes, and seconds. */
  96. int year;
  97. int month;
  98. int day;
  99. int hour;
  100. int minutes;
  101. int seconds;
  102. /* Relative year, month, day, hour, minutes, and seconds. */
  103. int rel_year;
  104. int rel_month;
  105. int rel_day;
  106. int rel_hour;
  107. int rel_minutes;
  108. int rel_seconds;
  109. /* Counts of nonterminals of various flavors parsed so far. */
  110. int dates_seen;
  111. int days_seen;
  112. int local_zones_seen;
  113. int rels_seen;
  114. int times_seen;
  115. int zones_seen;
  116. /* Table of local time zone abbrevations, terminated by a null entry. */
  117. table local_time_zone_table[3];
  118. };
  119. #define 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. /* Yield A - B, measured in seconds. */
  600. static int
  601. difftm (struct tm *a, struct tm *b)
  602. {
  603. int ay = a->tm_year + (TM_YEAR_ORIGIN - 1);
  604. int by = b->tm_year + (TM_YEAR_ORIGIN - 1);
  605. int days = (
  606. /* difference in day of year */
  607. a->tm_yday - b->tm_yday
  608. /* + intervening leap days */
  609. + ((ay >> 2) - (by >> 2))
  610. - (ay / 100 - by / 100)
  611. + ((ay / 100 >> 2) - (by / 100 >> 2))
  612. /* + difference in years * 365 */
  613. + (int) (ay - by) * 365
  614. );
  615. return (60 * (60 * (24 * days + (a->tm_hour - b->tm_hour))
  616. + (a->tm_min - b->tm_min))
  617. + (a->tm_sec - b->tm_sec));
  618. }
  619. static table const *
  620. lookup_word (struct parser_control const *pc, char *word)
  621. {
  622. char *p;
  623. char *q;
  624. size_t wordlen;
  625. table const *tp;
  626. int i;
  627. int abbrev;
  628. /* Make it uppercase. */
  629. for (p = word; *p; p++)
  630. if (ISLOWER ((unsigned char) *p))
  631. *p = toupper ((unsigned char) *p);
  632. for (tp = meridian_table; tp->name; tp++)
  633. if (strcmp (word, tp->name) == 0)
  634. return tp;
  635. /* See if we have an abbreviation for a month. */
  636. wordlen = strlen (word);
  637. abbrev = wordlen == 3 || (wordlen == 4 && word[3] == '.');
  638. for (tp = month_and_day_table; tp->name; tp++)
  639. if ((abbrev ? strncmp (word, tp->name, 3) : strcmp (word, tp->name)) == 0)
  640. return tp;
  641. if ((tp = lookup_zone (pc, word)))
  642. return tp;
  643. if (strcmp (word, dst_table[0].name) == 0)
  644. return dst_table;
  645. for (tp = time_units_table; tp->name; tp++)
  646. if (strcmp (word, tp->name) == 0)
  647. return tp;
  648. /* Strip off any plural and try the units table again. */
  649. if (word[wordlen - 1] == 'S')
  650. {
  651. word[wordlen - 1] = '\0';
  652. for (tp = time_units_table; tp->name; tp++)
  653. if (strcmp (word, tp->name) == 0)
  654. return tp;
  655. word[wordlen - 1] = 'S'; /* For "this" in relative_time_table. */
  656. }
  657. for (tp = relative_time_table; tp->name; tp++)
  658. if (strcmp (word, tp->name) == 0)
  659. return tp;
  660. /* Military time zones. */
  661. if (wordlen == 1)
  662. for (tp = military_table; tp->name; tp++)
  663. if (word[0] == tp->name[0])
  664. return tp;
  665. /* Drop out any periods and try the time zone table again. */
  666. for (i = 0, p = q = word; (*p = *q); q++)
  667. if (*q == '.')
  668. i = 1;
  669. else
  670. p++;
  671. if (i && (tp = lookup_zone (pc, word)))
  672. return tp;
  673. return 0;
  674. }
  675. static int
  676. yylex (YYSTYPE *lvalp, struct parser_control *pc)
  677. {
  678. unsigned char c;
  679. int count;
  680. for (;;)
  681. {
  682. while (c = *pc->input, ISSPACE (c))
  683. pc->input++;
  684. if (ISDIGIT (c) || c == '-' || c == '+')
  685. {
  686. int sign;
  687. if (c == '-' || c == '+')
  688. {
  689. sign = c == '-' ? -1 : 1;
  690. if (! ISDIGIT (*++pc->input))
  691. /* skip the '-' sign */
  692. continue;
  693. }
  694. else
  695. sign = 0;
  696. for (*lvalp = 0; ISDIGIT (c = *pc->input++);)
  697. *lvalp = 10 * *lvalp + (c - '0');
  698. pc->input--;
  699. if (sign < 0)
  700. *lvalp = - *lvalp;
  701. return sign ? tSNUMBER : tUNUMBER;
  702. }
  703. if (ISALPHA (c))
  704. {
  705. char buff[20];
  706. char *p = buff;
  707. table const *tp;
  708. do
  709. {
  710. if (p < buff + sizeof buff - 1)
  711. *p++ = c;
  712. c = *++pc->input;
  713. }
  714. while (ISALPHA (c) || c == '.');
  715. *p = '\0';
  716. tp = lookup_word (pc, buff);
  717. if (! tp)
  718. return tID;
  719. *lvalp = tp->value;
  720. return tp->type;
  721. }
  722. if (c != '(')
  723. return *pc->input++;
  724. count = 0;
  725. do
  726. {
  727. c = *pc->input++;
  728. if (c == '\0')
  729. return c;
  730. if (c == '(')
  731. count++;
  732. else if (c == ')')
  733. count--;
  734. }
  735. while (count > 0);
  736. }
  737. }
  738. /* Do nothing if the parser reports an error. */
  739. static int
  740. yyerror (char *s ATTRIBUTE_UNUSED)
  741. {
  742. return 0;
  743. }
  744. /* ?? */
  745. time_t
  746. get_date (const char *p, const time_t *now)
  747. {
  748. time_t Start = now ? *now : time (0);
  749. struct tm *tmp = localtime (&Start);
  750. struct tm tm;
  751. struct tm tm0;
  752. struct parser_control pc;
  753. if (! tmp)
  754. return -1;
  755. pc.input = p;
  756. pc.year = tmp->tm_year + TM_YEAR_ORIGIN;
  757. pc.month = tmp->tm_mon + 1;
  758. pc.day = tmp->tm_mday;
  759. pc.hour = tmp->tm_hour;
  760. pc.minutes = tmp->tm_min;
  761. pc.seconds = tmp->tm_sec;
  762. tm.tm_isdst = tmp->tm_isdst;
  763. pc.meridian = MER24;
  764. pc.rel_seconds = 0;
  765. pc.rel_minutes = 0;
  766. pc.rel_hour = 0;
  767. pc.rel_day = 0;
  768. pc.rel_month = 0;
  769. pc.rel_year = 0;
  770. pc.dates_seen = 0;
  771. pc.days_seen = 0;
  772. pc.rels_seen = 0;
  773. pc.times_seen = 0;
  774. pc.local_zones_seen = 0;
  775. pc.zones_seen = 0;
  776. #if HAVE_TM_ZONE
  777. pc.local_time_zone_table[0].name = tmp->tm_zone;
  778. pc.local_time_zone_table[0].type = tLOCAL_ZONE;
  779. pc.local_time_zone_table[0].value = tmp->tm_isdst;
  780. pc.local_time_zone_table[1].name = 0;
  781. /* Probe the names used in the next three calendar quarters, looking
  782. for a tm_isdst different from the one we already have. */
  783. {
  784. int quarter;
  785. for (quarter = 1; quarter <= 3; quarter++)
  786. {
  787. time_t probe = Start + quarter * (90 * 24 * 60 * 60);
  788. struct tm *probe_tm = localtime (&probe);
  789. if (probe_tm && probe_tm->tm_zone
  790. && probe_tm->tm_isdst != pc.local_time_zone_table[0].value)
  791. {
  792. {
  793. pc.local_time_zone_table[1].name = probe_tm->tm_zone;
  794. pc.local_time_zone_table[1].type = tLOCAL_ZONE;
  795. pc.local_time_zone_table[1].value = probe_tm->tm_isdst;
  796. pc.local_time_zone_table[2].name = 0;
  797. }
  798. break;
  799. }
  800. }
  801. }
  802. #else
  803. #if HAVE_TZNAME
  804. {
  805. # ifndef tzname
  806. extern char *tzname[];
  807. # endif
  808. int i;
  809. for (i = 0; i < 2; i++)
  810. {
  811. pc.local_time_zone_table[i].name = tzname[i];
  812. pc.local_time_zone_table[i].type = tLOCAL_ZONE;
  813. pc.local_time_zone_table[i].value = i;
  814. }
  815. pc.local_time_zone_table[i].name = 0;
  816. }
  817. #else
  818. pc.local_time_zone_table[0].name = 0;
  819. #endif
  820. #endif
  821. if (pc.local_time_zone_table[0].name && pc.local_time_zone_table[1].name
  822. && ! strcmp (pc.local_time_zone_table[0].name,
  823. pc.local_time_zone_table[1].name))
  824. {
  825. /* This locale uses the same abbrevation for standard and
  826. daylight times. So if we see that abbreviation, we don't
  827. know whether it's daylight time. */
  828. pc.local_time_zone_table[0].value = -1;
  829. pc.local_time_zone_table[1].name = 0;
  830. }
  831. if (yyparse (&pc) != 0
  832. || 1 < pc.times_seen || 1 < pc.dates_seen || 1 < pc.days_seen
  833. || 1 < (pc.local_zones_seen + pc.zones_seen)
  834. || (pc.local_zones_seen && 1 < pc.local_isdst))
  835. return -1;
  836. tm.tm_year = to_year (pc.year) - TM_YEAR_ORIGIN + pc.rel_year;
  837. tm.tm_mon = pc.month - 1 + pc.rel_month;
  838. tm.tm_mday = pc.day + pc.rel_day;
  839. if (pc.times_seen || (pc.rels_seen && ! pc.dates_seen && ! pc.days_seen))
  840. {
  841. tm.tm_hour = to_hour (pc.hour, pc.meridian);
  842. if (tm.tm_hour < 0)
  843. return -1;
  844. tm.tm_min = pc.minutes;
  845. tm.tm_sec = pc.seconds;
  846. }
  847. else
  848. {
  849. tm.tm_hour = tm.tm_min = tm.tm_sec = 0;
  850. }
  851. tm.tm_hour += pc.rel_hour;
  852. tm.tm_min += pc.rel_minutes;
  853. tm.tm_sec += pc.rel_seconds;
  854. /* Let mktime deduce tm_isdst if we have an absolute time stamp,
  855. or if the relative time stamp mentions days, months, or years. */
  856. if (pc.dates_seen | pc.days_seen | pc.times_seen | pc.rel_day | pc.rel_month | pc.rel_year)
  857. tm.tm_isdst = -1;
  858. /* But if the input explicitly specifies local time with or without
  859. DST, give mktime that information. */
  860. if (pc.local_zones_seen)
  861. tm.tm_isdst = pc.local_isdst;
  862. tm0 = tm;
  863. Start = mktime (&tm);
  864. if (Start == (time_t) -1)
  865. {
  866. /* Guard against falsely reporting errors near the time_t boundaries
  867. when parsing times in other time zones. For example, if the min
  868. time_t value is 1970-01-01 00:00:00 UTC and we are 8 hours ahead
  869. of UTC, then the min localtime value is 1970-01-01 08:00:00; if
  870. we apply mktime to 1970-01-01 00:00:00 we will get an error, so
  871. we apply mktime to 1970-01-02 08:00:00 instead and adjust the time
  872. zone by 24 hours to compensate. This algorithm assumes that
  873. there is no DST transition within a day of the time_t boundaries. */
  874. if (pc.zones_seen)
  875. {
  876. tm = tm0;
  877. if (tm.tm_year <= EPOCH_YEAR - TM_YEAR_ORIGIN)
  878. {
  879. tm.tm_mday++;
  880. pc.time_zone += 24 * 60;
  881. }
  882. else
  883. {
  884. tm.tm_mday--;
  885. pc.time_zone -= 24 * 60;
  886. }
  887. Start = mktime (&tm);
  888. }
  889. if (Start == (time_t) -1)
  890. return Start;
  891. }
  892. if (pc.days_seen && ! pc.dates_seen)
  893. {
  894. tm.tm_mday += ((pc.day_number - tm.tm_wday + 7) % 7
  895. + 7 * (pc.day_ordinal - (0 < pc.day_ordinal)));
  896. Start = mktime (&tm);
  897. if (Start == (time_t) -1)
  898. return Start;
  899. }
  900. if (pc.zones_seen)
  901. {
  902. int delta;
  903. struct tm *gmt = gmtime (&Start);
  904. if (! gmt)
  905. return -1;
  906. delta = pc.time_zone * 60 + difftm (gmt, &tm);
  907. if ((Start - delta < Start) != (delta < 0))
  908. return -1; /* time_t overflow */
  909. Start -= delta;
  910. }
  911. return Start;
  912. }
  913. #if TEST
  914. #include <stdio.h>
  915. int
  916. main (int ac, char **av)
  917. {
  918. char buff[BUFSIZ];
  919. time_t d;
  920. printf ("Enter date, or blank line to exit.\n\t> ");
  921. fflush (stdout);
  922. buff[BUFSIZ - 1] = 0;
  923. while (fgets (buff, BUFSIZ - 1, stdin) && buff[0])
  924. {
  925. d = get_date (buff, 0);
  926. if (d == (time_t) -1)
  927. printf ("Bad format - couldn't convert.\n");
  928. else
  929. printf ("%s", ctime (&d));
  930. printf ("\t> ");
  931. fflush (stdout);
  932. }
  933. return 0;
  934. }
  935. #endif /* defined TEST */