You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1019 lines
27 KiB

26 years ago
25 years ago
24 years ago
25 years ago
  1. %{
  2. /*
  3. ** Originally written by Steven M. Bellovin <smb@research.att.com> while
  4. ** at the University of North Carolina at Chapel Hill. Later tweaked by
  5. ** a couple of people on Usenet. Completely overhauled by Rich $alz
  6. ** <rsalz@bbn.com> and Jim Berets <jberets@bbn.com> in August, 1990.
  7. **
  8. ** This code is in the public domain and has no copyright.
  9. */
  10. /* $Id$ */
  11. #include "php.h"
  12. #ifdef PHP_WIN32
  13. #include <malloc.h>
  14. #endif
  15. #include <stdio.h>
  16. #include <sys/types.h>
  17. #include <time.h>
  18. #include <ctype.h>
  19. #ifdef HAVE_SYS_TIME_H
  20. # include <sys/time.h>
  21. #endif
  22. #ifdef PHP_WIN32
  23. # include "win32/time.h"
  24. #endif
  25. #include "php_parsedate.h"
  26. #if HAVE_STDLIB_H
  27. # include <stdlib.h> /* for `free'; used by Bison 1.27 */
  28. #endif
  29. #if defined(_HPUX_SOURCE)
  30. #include <alloca.h>
  31. #endif
  32. #if defined (STDC_HEADERS) || (!defined (isascii) && !defined (HAVE_ISASCII))
  33. # define IN_CTYPE_DOMAIN(c) 1
  34. #else
  35. # define IN_CTYPE_DOMAIN(c) isascii(c)
  36. #endif
  37. #define ISSPACE(c) (IN_CTYPE_DOMAIN (c) && isspace (c))
  38. #define ISALPHA(c) (IN_CTYPE_DOMAIN (c) && isalpha (c))
  39. #define ISUPPER(c) (IN_CTYPE_DOMAIN (c) && isupper (c))
  40. #define ISDIGIT_LOCALE(c) (IN_CTYPE_DOMAIN (c) && isdigit (c))
  41. /* ISDIGIT differs from ISDIGIT_LOCALE, as follows:
  42. - Its arg may be any int or unsigned int; it need not be an unsigned char.
  43. - It's guaranteed to evaluate its argument exactly once.
  44. - It's typically faster.
  45. Posix 1003.2-1992 section 2.5.2.1 page 50 lines 1556-1558 says that
  46. only '0' through '9' are digits. Prefer ISDIGIT to ISDIGIT_LOCALE unless
  47. it's important to use the locale's definition of `digit' even when the
  48. host does not conform to Posix. */
  49. #define ISDIGIT(c) ((unsigned) (c) - '0' <= 9)
  50. #if defined (STDC_HEADERS) || defined (USG)
  51. # include <string.h>
  52. #endif
  53. #if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 7)
  54. # define __attribute__(x)
  55. #endif
  56. #ifndef ATTRIBUTE_UNUSED
  57. # define ATTRIBUTE_UNUSED __attribute__ ((__unused__))
  58. #endif
  59. /* Some old versions of bison generate parsers that use bcopy.
  60. That loses on systems that don't provide the function, so we have
  61. to redefine it here. */
  62. #if !defined (HAVE_BCOPY) && defined (HAVE_MEMCPY) && !defined (bcopy)
  63. # define bcopy(from, to, len) memcpy ((to), (from), (len))
  64. #endif
  65. /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
  66. as well as gratuitiously global symbol names, so we can have multiple
  67. yacc generated parsers in the same program. Note that these are only
  68. the variables produced by yacc. If other parser generators (bison,
  69. byacc, etc) produce additional global names that conflict at link time,
  70. then those parser generators need to be fixed instead of adding those
  71. names to this list. */
  72. #define yyparse php_gd_parse
  73. #define yylex php_gd_lex
  74. static int yyerror ();
  75. #define EPOCH 1970
  76. #define HOUR(x) ((x) * 60)
  77. #define MAX_BUFF_LEN 128 /* size of buffer to read the date into */
  78. /*
  79. ** An entry in the lexical lookup table.
  80. */
  81. typedef struct _TABLE {
  82. const char *name;
  83. int type;
  84. int value;
  85. } TABLE;
  86. /*
  87. ** Meridian: am, pm, or 24-hour style.
  88. */
  89. typedef enum _MERIDIAN {
  90. MERam, MERpm, MER24
  91. } MERIDIAN;
  92. struct date_yy {
  93. const char *yyInput;
  94. int yyDayOrdinal;
  95. int yyDayNumber;
  96. int yyHaveDate;
  97. int yyHaveDay;
  98. int yyHaveRel;
  99. int yyHaveTime;
  100. int yyHaveZone;
  101. int yyTimezone;
  102. int yyDay;
  103. int yyHour;
  104. int yyMinutes;
  105. int yyMonth;
  106. int yySeconds;
  107. int yyYear;
  108. MERIDIAN yyMeridian;
  109. int yyRelDay;
  110. int yyRelHour;
  111. int yyRelMinutes;
  112. int yyRelMonth;
  113. int yyRelSeconds;
  114. int yyRelYear;
  115. };
  116. typedef union _date_ll {
  117. int Number;
  118. enum _MERIDIAN Meridian;
  119. } date_ll;
  120. #define YYPARSE_PARAM parm
  121. #define YYLEX_PARAM parm
  122. #define YYSTYPE date_ll
  123. #define YYLTYPE void
  124. %}
  125. /* This grammar has 14 shift/reduce conflicts. */
  126. %expect 14
  127. %pure_parser
  128. %token tAGO tDAY tDAY_UNIT tDAYZONE tDST tHOUR_UNIT tID
  129. %token tMERIDIAN tMINUTE_UNIT tMONTH tMONTH_UNIT
  130. %token tSEC_UNIT tSNUMBER tUNUMBER tYEAR_UNIT tZONE
  131. %type <Number> tDAY tDAY_UNIT tDAYZONE tHOUR_UNIT tMINUTE_UNIT
  132. %type <Number> tMONTH tMONTH_UNIT
  133. %type <Number> tSEC_UNIT tSNUMBER tUNUMBER tYEAR_UNIT tZONE
  134. %type <Meridian> tMERIDIAN o_merid
  135. %%
  136. spec : /* NULL */
  137. | spec item
  138. ;
  139. item : time {
  140. ((struct date_yy *)parm)->yyHaveTime++;
  141. }
  142. | zone {
  143. ((struct date_yy *)parm)->yyHaveZone++;
  144. }
  145. | date {
  146. ((struct date_yy *)parm)->yyHaveDate++;
  147. }
  148. | day {
  149. ((struct date_yy *)parm)->yyHaveDay++;
  150. }
  151. | rel {
  152. ((struct date_yy *)parm)->yyHaveRel++;
  153. }
  154. | number
  155. ;
  156. time : tUNUMBER tMERIDIAN {
  157. ((struct date_yy *)parm)->yyHour = $1;
  158. ((struct date_yy *)parm)->yyMinutes = 0;
  159. ((struct date_yy *)parm)->yySeconds = 0;
  160. ((struct date_yy *)parm)->yyMeridian = $2;
  161. }
  162. | tUNUMBER ':' tUNUMBER o_merid {
  163. ((struct date_yy *)parm)->yyHour = $1;
  164. ((struct date_yy *)parm)->yyMinutes = $3;
  165. ((struct date_yy *)parm)->yySeconds = 0;
  166. ((struct date_yy *)parm)->yyMeridian = $4;
  167. }
  168. | tUNUMBER ':' tUNUMBER tSNUMBER {
  169. ((struct date_yy *)parm)->yyHour = $1;
  170. ((struct date_yy *)parm)->yyMinutes = $3;
  171. ((struct date_yy *)parm)->yyMeridian = MER24;
  172. ((struct date_yy *)parm)->yyHaveZone++;
  173. ((struct date_yy *)parm)->yyTimezone = ($4 < 0
  174. ? -$4 % 100 + (-$4 / 100) * 60
  175. : - ($4 % 100 + ($4 / 100) * 60));
  176. }
  177. | tUNUMBER ':' tUNUMBER ':' tUNUMBER o_merid {
  178. ((struct date_yy *)parm)->yyHour = $1;
  179. ((struct date_yy *)parm)->yyMinutes = $3;
  180. ((struct date_yy *)parm)->yySeconds = $5;
  181. ((struct date_yy *)parm)->yyMeridian = $6;
  182. }
  183. | tUNUMBER ':' tUNUMBER ':' tUNUMBER tSNUMBER {
  184. /* ISO 8601 format. hh:mm:ss[+-][0-9]{2}([0-9]{2})?. */
  185. ((struct date_yy *)parm)->yyHour = $1;
  186. ((struct date_yy *)parm)->yyMinutes = $3;
  187. ((struct date_yy *)parm)->yySeconds = $5;
  188. ((struct date_yy *)parm)->yyMeridian = MER24;
  189. ((struct date_yy *)parm)->yyHaveZone++;
  190. if ($6 <= -100 || $6 >= 100) {
  191. ((struct date_yy *)parm)->yyTimezone =
  192. -$6 % 100 + (-$6 / 100) * 60;
  193. } else {
  194. ((struct date_yy *)parm)->yyTimezone = -$6 * 60;
  195. }
  196. }
  197. ;
  198. zone : tZONE {
  199. ((struct date_yy *)parm)->yyTimezone = $1;
  200. }
  201. | tDAYZONE {
  202. ((struct date_yy *)parm)->yyTimezone = $1 - 60;
  203. }
  204. |
  205. tZONE tDST {
  206. ((struct date_yy *)parm)->yyTimezone = $1 - 60;
  207. }
  208. ;
  209. day : tDAY {
  210. ((struct date_yy *)parm)->yyDayOrdinal = 1;
  211. ((struct date_yy *)parm)->yyDayNumber = $1;
  212. }
  213. | tDAY ',' {
  214. ((struct date_yy *)parm)->yyDayOrdinal = 1;
  215. ((struct date_yy *)parm)->yyDayNumber = $1;
  216. }
  217. | tUNUMBER tDAY {
  218. ((struct date_yy *)parm)->yyDayOrdinal = $1;
  219. ((struct date_yy *)parm)->yyDayNumber = $2;
  220. }
  221. ;
  222. date : tUNUMBER '/' tUNUMBER {
  223. ((struct date_yy *)parm)->yyMonth = $1;
  224. ((struct date_yy *)parm)->yyDay = $3;
  225. }
  226. | tUNUMBER '/' tUNUMBER '/' tUNUMBER {
  227. /* Interpret as YYYY/MM/DD if $1 >= 1000, otherwise as MM/DD/YY.
  228. The goal in recognizing YYYY/MM/DD is solely to support legacy
  229. machine-generated dates like those in an RCS log listing. If
  230. you want portability, use the ISO 8601 format. */
  231. if ($1 >= 1000)
  232. {
  233. ((struct date_yy *)parm)->yyYear = $1;
  234. ((struct date_yy *)parm)->yyMonth = $3;
  235. ((struct date_yy *)parm)->yyDay = $5;
  236. }
  237. else
  238. {
  239. ((struct date_yy *)parm)->yyMonth = $1;
  240. ((struct date_yy *)parm)->yyDay = $3;
  241. ((struct date_yy *)parm)->yyYear = $5;
  242. }
  243. }
  244. | tUNUMBER tSNUMBER tSNUMBER {
  245. /* ISO 8601 format. yyyy-mm-dd. */
  246. ((struct date_yy *)parm)->yyYear = $1;
  247. ((struct date_yy *)parm)->yyMonth = -$2;
  248. ((struct date_yy *)parm)->yyDay = -$3;
  249. }
  250. | tUNUMBER tMONTH tSNUMBER {
  251. /* e.g. 17-JUN-1992. */
  252. ((struct date_yy *)parm)->yyDay = $1;
  253. ((struct date_yy *)parm)->yyMonth = $2;
  254. ((struct date_yy *)parm)->yyYear = -$3;
  255. }
  256. | tMONTH tUNUMBER tUNUMBER {
  257. ((struct date_yy *)parm)->yyMonth = $1;
  258. ((struct date_yy *)parm)->yyDay = $2;
  259. ((struct date_yy *)parm)->yyYear = $3;
  260. }
  261. | tMONTH tUNUMBER {
  262. ((struct date_yy *)parm)->yyMonth = $1;
  263. ((struct date_yy *)parm)->yyDay = $2;
  264. }
  265. | tMONTH tUNUMBER ',' tUNUMBER {
  266. ((struct date_yy *)parm)->yyMonth = $1;
  267. ((struct date_yy *)parm)->yyDay = $2;
  268. ((struct date_yy *)parm)->yyYear = $4;
  269. }
  270. | tUNUMBER tMONTH {
  271. ((struct date_yy *)parm)->yyMonth = $2;
  272. ((struct date_yy *)parm)->yyDay = $1;
  273. }
  274. | tUNUMBER tMONTH tUNUMBER {
  275. ((struct date_yy *)parm)->yyMonth = $2;
  276. ((struct date_yy *)parm)->yyDay = $1;
  277. ((struct date_yy *)parm)->yyYear = $3;
  278. }
  279. ;
  280. rel : relunit tAGO {
  281. ((struct date_yy *)parm)->yyRelSeconds =
  282. -((struct date_yy *)parm)->yyRelSeconds;
  283. ((struct date_yy *)parm)->yyRelMinutes =
  284. -((struct date_yy *)parm)->yyRelMinutes;
  285. ((struct date_yy *)parm)->yyRelHour =
  286. -((struct date_yy *)parm)->yyRelHour;
  287. ((struct date_yy *)parm)->yyRelDay =
  288. -((struct date_yy *)parm)->yyRelDay;
  289. ((struct date_yy *)parm)->yyRelMonth =
  290. -((struct date_yy *)parm)->yyRelMonth;
  291. ((struct date_yy *)parm)->yyRelYear =
  292. -((struct date_yy *)parm)->yyRelYear;
  293. }
  294. | relunit
  295. ;
  296. relunit : tUNUMBER tYEAR_UNIT {
  297. ((struct date_yy *)parm)->yyRelYear += $1 * $2;
  298. }
  299. | tSNUMBER tYEAR_UNIT {
  300. ((struct date_yy *)parm)->yyRelYear += $1 * $2;
  301. }
  302. | tYEAR_UNIT {
  303. ((struct date_yy *)parm)->yyRelYear += $1;
  304. }
  305. | tUNUMBER tMONTH_UNIT {
  306. ((struct date_yy *)parm)->yyRelMonth += $1 * $2;
  307. }
  308. | tSNUMBER tMONTH_UNIT {
  309. ((struct date_yy *)parm)->yyRelMonth += $1 * $2;
  310. }
  311. | tMONTH_UNIT {
  312. ((struct date_yy *)parm)->yyRelMonth += $1;
  313. }
  314. | tUNUMBER tDAY_UNIT {
  315. ((struct date_yy *)parm)->yyRelDay += $1 * $2;
  316. }
  317. | tSNUMBER tDAY_UNIT {
  318. ((struct date_yy *)parm)->yyRelDay += $1 * $2;
  319. }
  320. | tDAY_UNIT {
  321. ((struct date_yy *)parm)->yyRelDay += $1;
  322. }
  323. | tUNUMBER tHOUR_UNIT {
  324. ((struct date_yy *)parm)->yyRelHour += $1 * $2;
  325. }
  326. | tSNUMBER tHOUR_UNIT {
  327. ((struct date_yy *)parm)->yyRelHour += $1 * $2;
  328. }
  329. | tHOUR_UNIT {
  330. ((struct date_yy *)parm)->yyRelHour += $1;
  331. }
  332. | tUNUMBER tMINUTE_UNIT {
  333. ((struct date_yy *)parm)->yyRelMinutes += $1 * $2;
  334. }
  335. | tSNUMBER tMINUTE_UNIT {
  336. ((struct date_yy *)parm)->yyRelMinutes += $1 * $2;
  337. }
  338. | tMINUTE_UNIT {
  339. ((struct date_yy *)parm)->yyRelMinutes += $1;
  340. }
  341. | tUNUMBER tSEC_UNIT {
  342. ((struct date_yy *)parm)->yyRelSeconds += $1 * $2;
  343. }
  344. | tSNUMBER tSEC_UNIT {
  345. ((struct date_yy *)parm)->yyRelSeconds += $1 * $2;
  346. }
  347. | tSEC_UNIT {
  348. ((struct date_yy *)parm)->yyRelSeconds += $1;
  349. }
  350. ;
  351. number : tUNUMBER
  352. {
  353. if (((struct date_yy *)parm)->yyHaveTime &&
  354. ((struct date_yy *)parm)->yyHaveDate &&
  355. !((struct date_yy *)parm)->yyHaveRel)
  356. ((struct date_yy *)parm)->yyYear = $1;
  357. else
  358. {
  359. if ($1>10000)
  360. {
  361. ((struct date_yy *)parm)->yyHaveDate++;
  362. ((struct date_yy *)parm)->yyDay= ($1)%100;
  363. ((struct date_yy *)parm)->yyMonth= ($1/100)%100;
  364. ((struct date_yy *)parm)->yyYear = $1/10000;
  365. }
  366. else
  367. {
  368. ((struct date_yy *)parm)->yyHaveTime++;
  369. if ($1 < 100)
  370. {
  371. ((struct date_yy *)parm)->yyHour = $1;
  372. ((struct date_yy *)parm)->yyMinutes = 0;
  373. }
  374. else
  375. {
  376. ((struct date_yy *)parm)->yyHour = $1 / 100;
  377. ((struct date_yy *)parm)->yyMinutes = $1 % 100;
  378. }
  379. ((struct date_yy *)parm)->yySeconds = 0;
  380. ((struct date_yy *)parm)->yyMeridian = MER24;
  381. }
  382. }
  383. }
  384. ;
  385. o_merid : /* NULL */
  386. {
  387. $$ = MER24;
  388. }
  389. | tMERIDIAN
  390. {
  391. $$ = $1;
  392. }
  393. ;
  394. %%
  395. time_t get_date (char *p, time_t *now);
  396. #ifndef PHP_WIN32
  397. extern struct tm *gmtime();
  398. extern struct tm *localtime();
  399. extern time_t mktime();
  400. #endif
  401. /* Month and day table. */
  402. static TABLE const MonthDayTable[] = {
  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. { NULL, 0, 0 }
  428. };
  429. /* Time units table. */
  430. static TABLE const UnitsTable[] = {
  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. { NULL, 0, 0 }
  442. };
  443. /* Assorted relative-time words. */
  444. static TABLE const OtherTable[] = {
  445. { "tomorrow", tDAY_UNIT, 1 },
  446. { "yesterday", tDAY_UNIT, -1 },
  447. { "today", tDAY_UNIT, 0 },
  448. { "now", tDAY_UNIT, 0 },
  449. { "last", tUNUMBER, -1 },
  450. { "this", tMINUTE_UNIT, 0 },
  451. { "next", tUNUMBER, 2 },
  452. { "first", tUNUMBER, 1 },
  453. /* { "second", tUNUMBER, 2 }, */
  454. { "third", tUNUMBER, 3 },
  455. { "fourth", tUNUMBER, 4 },
  456. { "fifth", tUNUMBER, 5 },
  457. { "sixth", tUNUMBER, 6 },
  458. { "seventh", tUNUMBER, 7 },
  459. { "eighth", tUNUMBER, 8 },
  460. { "ninth", tUNUMBER, 9 },
  461. { "tenth", tUNUMBER, 10 },
  462. { "eleventh", tUNUMBER, 11 },
  463. { "twelfth", tUNUMBER, 12 },
  464. { "ago", tAGO, 1 },
  465. { NULL, 0, 0 }
  466. };
  467. /* The timezone table. */
  468. static TABLE const TimezoneTable[] = {
  469. { "gmt", tZONE, HOUR ( 0) }, /* Greenwich Mean */
  470. { "ut", tZONE, HOUR ( 0) }, /* Universal (Coordinated) */
  471. { "utc", tZONE, HOUR ( 0) },
  472. { "wet", tZONE, HOUR ( 0) }, /* Western European */
  473. { "bst", tDAYZONE, HOUR ( 0) }, /* British Summer */
  474. { "wat", tZONE, HOUR ( 1) }, /* West Africa */
  475. { "at", tZONE, HOUR ( 2) }, /* Azores */
  476. #if 0
  477. /* For completeness. BST is also British Summer, and GST is
  478. * also Guam Standard. */
  479. { "bst", tZONE, HOUR ( 3) }, /* Brazil Standard */
  480. { "gst", tZONE, HOUR ( 3) }, /* Greenland Standard */
  481. #endif
  482. #if 0
  483. { "nft", tZONE, HOUR (3.5) }, /* Newfoundland */
  484. { "nst", tZONE, HOUR (3.5) }, /* Newfoundland Standard */
  485. { "ndt", tDAYZONE, HOUR (3.5) }, /* Newfoundland Daylight */
  486. #endif
  487. { "ast", tZONE, HOUR ( 4) }, /* Atlantic Standard */
  488. { "adt", tDAYZONE, HOUR ( 4) }, /* Atlantic Daylight */
  489. { "est", tZONE, HOUR ( 5) }, /* Eastern Standard */
  490. { "edt", tDAYZONE, HOUR ( 5) }, /* Eastern Daylight */
  491. { "cst", tZONE, HOUR ( 6) }, /* Central Standard */
  492. { "cdt", tDAYZONE, HOUR ( 6) }, /* Central Daylight */
  493. { "mst", tZONE, HOUR ( 7) }, /* Mountain Standard */
  494. { "mdt", tDAYZONE, HOUR ( 7) }, /* Mountain Daylight */
  495. { "pst", tZONE, HOUR ( 8) }, /* Pacific Standard */
  496. { "pdt", tDAYZONE, HOUR ( 8) }, /* Pacific Daylight */
  497. { "yst", tZONE, HOUR ( 9) }, /* Yukon Standard */
  498. { "ydt", tDAYZONE, HOUR ( 9) }, /* Yukon Daylight */
  499. { "hst", tZONE, HOUR (10) }, /* Hawaii Standard */
  500. { "hdt", tDAYZONE, HOUR (10) }, /* Hawaii Daylight */
  501. { "cat", tZONE, HOUR (10) }, /* Central Alaska */
  502. { "akst", tZONE, HOUR (10) }, /* Alaska Standard */
  503. { "akdt", tZONE, HOUR (10) }, /* Alaska Daylight */
  504. { "ahst", tZONE, HOUR (10) }, /* Alaska-Hawaii Standard */
  505. { "nt", tZONE, HOUR (11) }, /* Nome */
  506. { "idlw", tZONE, HOUR (12) }, /* International Date Line West */
  507. { "cet", tZONE, -HOUR (1) }, /* Central European */
  508. { "met", tZONE, -HOUR (1) }, /* Middle European */
  509. { "mewt", tZONE, -HOUR (1) }, /* Middle European Winter */
  510. { "mest", tDAYZONE, -HOUR (1) }, /* Middle European Summer */
  511. { "mesz", tDAYZONE, -HOUR (1) }, /* Middle European Summer */
  512. { "swt", tZONE, -HOUR (1) }, /* Swedish Winter */
  513. { "sst", tDAYZONE, -HOUR (1) }, /* Swedish Summer */
  514. { "fwt", tZONE, -HOUR (1) }, /* French Winter */
  515. { "fst", tDAYZONE, -HOUR (1) }, /* French Summer */
  516. { "eet", tZONE, -HOUR (2) }, /* Eastern Europe, USSR Zone 1 */
  517. { "bt", tZONE, -HOUR (3) }, /* Baghdad, USSR Zone 2 */
  518. #if 0
  519. { "it", tZONE, -HOUR (3.5) },/* Iran */
  520. #endif
  521. { "zp4", tZONE, -HOUR (4) }, /* USSR Zone 3 */
  522. { "zp5", tZONE, -HOUR (5) }, /* USSR Zone 4 */
  523. #if 0
  524. { "ist", tZONE, -HOUR (5.5) },/* Indian Standard */
  525. #endif
  526. { "zp6", tZONE, -HOUR (6) }, /* USSR Zone 5 */
  527. #if 0
  528. /* For completeness. NST is also Newfoundland Standard, and SST is
  529. * also Swedish Summer. */
  530. { "nst", tZONE, -HOUR (6.5) },/* North Sumatra */
  531. { "sst", tZONE, -HOUR (7) }, /* South Sumatra, USSR Zone 6 */
  532. #endif /* 0 */
  533. { "wast", tZONE, -HOUR (7) }, /* West Australian Standard */
  534. { "wadt", tDAYZONE, -HOUR (7) }, /* West Australian Daylight */
  535. #if 0
  536. { "jt", tZONE, -HOUR (7.5) },/* Java (3pm in Cronusland!) */
  537. #endif
  538. { "cct", tZONE, -HOUR (8) }, /* China Coast, USSR Zone 7 */
  539. { "jst", tZONE, -HOUR (9) }, /* Japan Standard, USSR Zone 8 */
  540. #if 0
  541. { "cast", tZONE, -HOUR (9.5) },/* Central Australian Standard */
  542. { "cadt", tDAYZONE, -HOUR (9.5) },/* Central Australian Daylight */
  543. #endif
  544. { "east", tZONE, -HOUR (10) }, /* Eastern Australian Standard */
  545. { "eadt", tDAYZONE, -HOUR (10) }, /* Eastern Australian Daylight */
  546. { "gst", tZONE, -HOUR (10) }, /* Guam Standard, USSR Zone 9 */
  547. { "nzt", tZONE, -HOUR (12) }, /* New Zealand */
  548. { "nzst", tZONE, -HOUR (12) }, /* New Zealand Standard */
  549. { "nzdt", tDAYZONE, -HOUR (12) }, /* New Zealand Daylight */
  550. { "idle", tZONE, -HOUR (12) }, /* International Date Line East */
  551. { NULL, 0, 0 }
  552. };
  553. /* Military timezone table. */
  554. static TABLE const MilitaryTable[] = {
  555. { "a", tZONE, HOUR ( 1) },
  556. { "b", tZONE, HOUR ( 2) },
  557. { "c", tZONE, HOUR ( 3) },
  558. { "d", tZONE, HOUR ( 4) },
  559. { "e", tZONE, HOUR ( 5) },
  560. { "f", tZONE, HOUR ( 6) },
  561. { "g", tZONE, HOUR ( 7) },
  562. { "h", tZONE, HOUR ( 8) },
  563. { "i", tZONE, HOUR ( 9) },
  564. { "k", tZONE, HOUR ( 10) },
  565. { "l", tZONE, HOUR ( 11) },
  566. { "m", tZONE, HOUR ( 12) },
  567. { "n", tZONE, HOUR (- 1) },
  568. { "o", tZONE, HOUR (- 2) },
  569. { "p", tZONE, HOUR (- 3) },
  570. { "q", tZONE, HOUR (- 4) },
  571. { "r", tZONE, HOUR (- 5) },
  572. { "s", tZONE, HOUR (- 6) },
  573. { "t", tZONE, HOUR (- 7) },
  574. { "u", tZONE, HOUR (- 8) },
  575. { "v", tZONE, HOUR (- 9) },
  576. { "w", tZONE, HOUR (-10) },
  577. { "x", tZONE, HOUR (-11) },
  578. { "y", tZONE, HOUR (-12) },
  579. { "z", tZONE, HOUR ( 0) },
  580. { NULL, 0, 0 }
  581. };
  582. /* ARGSUSED */
  583. static int
  584. yyerror (s)
  585. char *s ATTRIBUTE_UNUSED;
  586. {
  587. return 0;
  588. }
  589. static int
  590. ToHour (Hours, Meridian)
  591. int Hours;
  592. MERIDIAN Meridian;
  593. {
  594. switch (Meridian)
  595. {
  596. case MER24:
  597. if (Hours < 0 || Hours > 23)
  598. return -1;
  599. return Hours;
  600. case MERam:
  601. if (Hours < 1 || Hours > 12)
  602. return -1;
  603. if (Hours == 12)
  604. Hours = 0;
  605. return Hours;
  606. case MERpm:
  607. if (Hours < 1 || Hours > 12)
  608. return -1;
  609. if (Hours == 12)
  610. Hours = 0;
  611. return Hours + 12;
  612. default:
  613. abort ();
  614. }
  615. /* NOTREACHED */
  616. }
  617. static int
  618. ToYear (Year)
  619. int Year;
  620. {
  621. if (Year < 0)
  622. Year = -Year;
  623. /* XPG4 suggests that years 00-68 map to 2000-2068, and
  624. years 69-99 map to 1969-1999. */
  625. if (Year < 69)
  626. Year += 2000;
  627. else if (Year < 100)
  628. Year += 1900;
  629. return Year;
  630. }
  631. static int
  632. LookupWord (lvalp,buff)
  633. YYSTYPE *lvalp;
  634. char *buff;
  635. {
  636. register char *p;
  637. register char *q;
  638. register const TABLE *tp;
  639. int i;
  640. int abbrev;
  641. /* Make it lowercase. */
  642. for (p = buff; *p; p++)
  643. if (ISUPPER ((unsigned char) *p))
  644. *p = tolower (*p);
  645. if (strcmp (buff, "am") == 0 || strcmp (buff, "a.m.") == 0)
  646. {
  647. lvalp->Meridian = MERam;
  648. return tMERIDIAN;
  649. }
  650. if (strcmp (buff, "pm") == 0 || strcmp (buff, "p.m.") == 0)
  651. {
  652. lvalp->Meridian = MERpm;
  653. return tMERIDIAN;
  654. }
  655. /* See if we have an abbreviation for a month. */
  656. if (strlen (buff) == 3)
  657. abbrev = 1;
  658. else if (strlen (buff) == 4 && buff[3] == '.')
  659. {
  660. abbrev = 1;
  661. buff[3] = '\0';
  662. }
  663. else
  664. abbrev = 0;
  665. for (tp = MonthDayTable; tp->name; tp++)
  666. {
  667. if (abbrev)
  668. {
  669. if (strncmp (buff, tp->name, 3) == 0)
  670. {
  671. lvalp->Number = tp->value;
  672. return tp->type;
  673. }
  674. }
  675. else if (strcmp (buff, tp->name) == 0)
  676. {
  677. lvalp->Number = tp->value;
  678. return tp->type;
  679. }
  680. }
  681. for (tp = TimezoneTable; tp->name; tp++)
  682. if (strcmp (buff, tp->name) == 0)
  683. {
  684. lvalp->Number = tp->value;
  685. return tp->type;
  686. }
  687. if (strcmp (buff, "dst") == 0)
  688. return tDST;
  689. for (tp = UnitsTable; tp->name; tp++)
  690. if (strcmp (buff, tp->name) == 0)
  691. {
  692. lvalp->Number = tp->value;
  693. return tp->type;
  694. }
  695. /* Strip off any plural and try the units table again. */
  696. i = strlen (buff) - 1;
  697. if (buff[i] == 's')
  698. {
  699. buff[i] = '\0';
  700. for (tp = UnitsTable; tp->name; tp++)
  701. if (strcmp (buff, tp->name) == 0)
  702. {
  703. lvalp->Number = tp->value;
  704. return tp->type;
  705. }
  706. buff[i] = 's'; /* Put back for "this" in OtherTable. */
  707. }
  708. for (tp = OtherTable; tp->name; tp++)
  709. if (strcmp (buff, tp->name) == 0)
  710. {
  711. lvalp->Number = tp->value;
  712. return tp->type;
  713. }
  714. /* Military timezones. */
  715. if (buff[1] == '\0' && ISALPHA ((unsigned char) *buff))
  716. {
  717. for (tp = MilitaryTable; tp->name; tp++)
  718. if (strcmp (buff, tp->name) == 0)
  719. {
  720. lvalp->Number = tp->value;
  721. return tp->type;
  722. }
  723. }
  724. /* Drop out any periods and try the timezone table again. */
  725. for (i = 0, p = q = buff; *q; q++)
  726. if (*q != '.')
  727. *p++ = *q;
  728. else
  729. i++;
  730. *p = '\0';
  731. if (i)
  732. for (tp = TimezoneTable; tp->name; tp++)
  733. if (strcmp (buff, tp->name) == 0)
  734. {
  735. lvalp->Number = tp->value;
  736. return tp->type;
  737. }
  738. return tID;
  739. }
  740. yylex (YYSTYPE *lvalp, void *parm)
  741. {
  742. register unsigned char c;
  743. register char *p;
  744. char buff[20];
  745. int Count;
  746. int sign;
  747. struct date_yy * date = (struct date_yy *)parm;
  748. for (;;)
  749. {
  750. while (ISSPACE ((unsigned char) *date->yyInput))
  751. date->yyInput++;
  752. if (ISDIGIT (c = *date->yyInput) || c == '-' || c == '+')
  753. {
  754. if (c == '-' || c == '+')
  755. {
  756. sign = c == '-' ? -1 : 1;
  757. if (!ISDIGIT (*++date->yyInput))
  758. /* skip the '-' sign */
  759. continue;
  760. }
  761. else
  762. sign = 0;
  763. for (lvalp->Number = 0; ISDIGIT (c = *date->yyInput++);)
  764. lvalp->Number = 10 * lvalp->Number + c - '0';
  765. date->yyInput--;
  766. if (sign < 0)
  767. lvalp->Number = -lvalp->Number;
  768. /* Ignore ordinal suffixes on numbers */
  769. c = *date->yyInput;
  770. if (c == 's' || c == 'n' || c == 'r' || c == 't') {
  771. c = *++date->yyInput;
  772. if (c == 't' || c == 'd' || c == 'h') {
  773. date->yyInput++;
  774. } else {
  775. date->yyInput--;
  776. }
  777. }
  778. return sign ? tSNUMBER : tUNUMBER;
  779. }
  780. if (ISALPHA (c))
  781. {
  782. for (p = buff; (c = *date->yyInput++, ISALPHA (c)) || c == '.';)
  783. if (p < &buff[sizeof buff - 1])
  784. *p++ = c;
  785. *p = '\0';
  786. date->yyInput--;
  787. return LookupWord (lvalp, buff);
  788. }
  789. if (c != '(')
  790. return *date->yyInput++;
  791. Count = 0;
  792. do
  793. {
  794. c = *date->yyInput++;
  795. if (c == '\0')
  796. return c;
  797. if (c == '(')
  798. Count++;
  799. else if (c == ')')
  800. Count--;
  801. }
  802. while (Count > 0);
  803. }
  804. }
  805. #define TM_YEAR_ORIGIN 1900
  806. /* Yield A - B, measured in seconds. */
  807. static long
  808. difftm (struct tm *a, struct tm *b)
  809. {
  810. int ay = a->tm_year + (TM_YEAR_ORIGIN - 1);
  811. int by = b->tm_year + (TM_YEAR_ORIGIN - 1);
  812. long days = (
  813. /* difference in day of year */
  814. a->tm_yday - b->tm_yday
  815. /* + intervening leap days */
  816. + ((ay >> 2) - (by >> 2))
  817. - (ay / 100 - by / 100)
  818. + ((ay / 100 >> 2) - (by / 100 >> 2))
  819. /* + difference in years * 365 */
  820. + (long) (ay - by) * 365
  821. );
  822. return (60 * (60 * (24 * days + (a->tm_hour - b->tm_hour))
  823. + (a->tm_min - b->tm_min))
  824. + (a->tm_sec - b->tm_sec));
  825. }
  826. time_t php_parse_date(char *p, time_t *now)
  827. {
  828. struct tm tm, tm0, *tmp;
  829. time_t Start;
  830. struct date_yy date;
  831. date.yyInput = p;
  832. Start = now ? *now : time ((time_t *) NULL);
  833. tmp = localtime (&Start);
  834. if (!tmp)
  835. return -1;
  836. date.yyYear = tmp->tm_year + TM_YEAR_ORIGIN;
  837. date.yyMonth = tmp->tm_mon + 1;
  838. date.yyDay = tmp->tm_mday;
  839. date.yyHour = tmp->tm_hour;
  840. date.yyMinutes = tmp->tm_min;
  841. date.yySeconds = tmp->tm_sec;
  842. tm.tm_isdst = tmp->tm_isdst;
  843. date.yyMeridian = MER24;
  844. date.yyRelSeconds = 0;
  845. date.yyRelMinutes = 0;
  846. date.yyRelHour = 0;
  847. date.yyRelDay = 0;
  848. date.yyRelMonth = 0;
  849. date.yyRelYear = 0;
  850. date.yyHaveDate = 0;
  851. date.yyHaveDay = 0;
  852. date.yyHaveRel = 0;
  853. date.yyHaveTime = 0;
  854. date.yyHaveZone = 0;
  855. if (yyparse ((void *)&date)
  856. || date.yyHaveTime > 1 || date.yyHaveZone > 1
  857. || date.yyHaveDate > 1 || date.yyHaveDay > 1)
  858. return -1;
  859. tm.tm_year = ToYear (date.yyYear) - TM_YEAR_ORIGIN + date.yyRelYear;
  860. tm.tm_mon = date.yyMonth - 1 + date.yyRelMonth;
  861. tm.tm_mday = date.yyDay + date.yyRelDay;
  862. if (date.yyHaveTime || (date.yyHaveRel && !date.yyHaveDate && !date.yyHaveDay))
  863. {
  864. tm.tm_hour = ToHour (date.yyHour, date.yyMeridian);
  865. if (tm.tm_hour < 0)
  866. return -1;
  867. tm.tm_min = date.yyMinutes;
  868. tm.tm_sec = date.yySeconds;
  869. }
  870. else
  871. {
  872. tm.tm_hour = tm.tm_min = tm.tm_sec = 0;
  873. }
  874. tm.tm_hour += date.yyRelHour;
  875. tm.tm_min += date.yyRelMinutes;
  876. tm.tm_sec += date.yyRelSeconds;
  877. /* Let mktime deduce tm_isdst if we have an absolute timestamp,
  878. or if the relative timestamp mentions days, months, or years. */
  879. if (date.yyHaveDate | date.yyHaveDay | date.yyHaveTime | date.yyRelDay | date.yyRelMonth | date.yyRelYear)
  880. tm.tm_isdst = -1;
  881. tm0 = tm;
  882. Start = mktime (&tm);
  883. if (Start == (time_t) -1)
  884. {
  885. /* Guard against falsely reporting errors near the time_t boundaries
  886. when parsing times in other time zones. For example, if the min
  887. time_t value is 1970-01-01 00:00:00 UTC and we are 8 hours ahead
  888. of UTC, then the min localtime value is 1970-01-01 08:00:00; if
  889. we apply mktime to 1970-01-01 00:00:00 we will get an error, so
  890. we apply mktime to 1970-01-02 08:00:00 instead and adjust the time
  891. zone by 24 hours to compensate. This algorithm assumes that
  892. there is no DST transition within a day of the time_t boundaries. */
  893. if (date.yyHaveZone)
  894. {
  895. tm = tm0;
  896. if (tm.tm_year <= EPOCH - TM_YEAR_ORIGIN)
  897. {
  898. tm.tm_mday++;
  899. date.yyTimezone -= 24 * 60;
  900. }
  901. else
  902. {
  903. tm.tm_mday--;
  904. date.yyTimezone += 24 * 60;
  905. }
  906. Start = mktime (&tm);
  907. }
  908. if (Start == (time_t) -1)
  909. return Start;
  910. }
  911. if (date.yyHaveDay && !date.yyHaveDate)
  912. {
  913. tm.tm_mday += ((date.yyDayNumber - tm.tm_wday + 7) % 7
  914. + 7 * (date.yyDayOrdinal - (0 < date.yyDayOrdinal)));
  915. Start = mktime (&tm);
  916. if (Start == (time_t) -1)
  917. return Start;
  918. }
  919. if (date.yyHaveZone)
  920. {
  921. long delta;
  922. struct tm *gmt = gmtime (&Start);
  923. if (!gmt)
  924. return -1;
  925. delta = date.yyTimezone * 60L + difftm (&tm, gmt);
  926. if ((Start + delta < Start) != (delta < 0))
  927. return -1; /* time_t overflow */
  928. Start += delta;
  929. }
  930. return Start;
  931. }