PageRenderTime 60ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/js/src/jsdate.c

https://bitbucket.org/mkato/mozilla-1.9.0-win64
C | 2390 lines | 1698 code | 351 blank | 341 comment | 431 complexity | d38a9e73d46b1b88eb4f67fe8e21644f MD5 | raw file
Possible License(s): LGPL-3.0, MIT, BSD-3-Clause, MPL-2.0-no-copyleft-exception, GPL-2.0, LGPL-2.1

Large files files are truncated, but you can click here to view the full file

  1. /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2. * vim: set ts=8 sw=4 et tw=78:
  3. *
  4. * ***** BEGIN LICENSE BLOCK *****
  5. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  6. *
  7. * The contents of this file are subject to the Mozilla Public License Version
  8. * 1.1 (the "License"); you may not use this file except in compliance with
  9. * the License. You may obtain a copy of the License at
  10. * http://www.mozilla.org/MPL/
  11. *
  12. * Software distributed under the License is distributed on an "AS IS" basis,
  13. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  14. * for the specific language governing rights and limitations under the
  15. * License.
  16. *
  17. * The Original Code is Mozilla Communicator client code, released
  18. * March 31, 1998.
  19. *
  20. * The Initial Developer of the Original Code is
  21. * Netscape Communications Corporation.
  22. * Portions created by the Initial Developer are Copyright (C) 1998
  23. * the Initial Developer. All Rights Reserved.
  24. *
  25. * Contributor(s):
  26. *
  27. * Alternatively, the contents of this file may be used under the terms of
  28. * either of the GNU General Public License Version 2 or later (the "GPL"),
  29. * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  30. * in which case the provisions of the GPL or the LGPL are applicable instead
  31. * of those above. If you wish to allow use of your version of this file only
  32. * under the terms of either the GPL or the LGPL, and not to allow others to
  33. * use your version of this file under the terms of the MPL, indicate your
  34. * decision by deleting the provisions above and replace them with the notice
  35. * and other provisions required by the GPL or the LGPL. If you do not delete
  36. * the provisions above, a recipient may use your version of this file under
  37. * the terms of any one of the MPL, the GPL or the LGPL.
  38. *
  39. * ***** END LICENSE BLOCK ***** */
  40. /*
  41. * JS date methods.
  42. */
  43. /*
  44. * "For example, OS/360 devotes 26 bytes of the permanently
  45. * resident date-turnover routine to the proper handling of
  46. * December 31 on leap years (when it is Day 366). That
  47. * might have been left to the operator."
  48. *
  49. * Frederick Brooks, 'The Second-System Effect'.
  50. */
  51. #include "jsstddef.h"
  52. #include <ctype.h>
  53. #include <locale.h>
  54. #include <math.h>
  55. #include <stdlib.h>
  56. #include <string.h>
  57. #include "jstypes.h"
  58. #include "jsprf.h"
  59. #include "prmjtime.h"
  60. #include "jsutil.h" /* Added by JSIFY */
  61. #include "jsapi.h"
  62. #include "jsconfig.h"
  63. #include "jscntxt.h"
  64. #include "jsdate.h"
  65. #include "jsinterp.h"
  66. #include "jsnum.h"
  67. #include "jsobj.h"
  68. #include "jsstr.h"
  69. /*
  70. * The JS 'Date' object is patterned after the Java 'Date' object.
  71. * Here is an script:
  72. *
  73. * today = new Date();
  74. *
  75. * print(today.toLocaleString());
  76. *
  77. * weekDay = today.getDay();
  78. *
  79. *
  80. * These Java (and ECMA-262) methods are supported:
  81. *
  82. * UTC
  83. * getDate (getUTCDate)
  84. * getDay (getUTCDay)
  85. * getHours (getUTCHours)
  86. * getMinutes (getUTCMinutes)
  87. * getMonth (getUTCMonth)
  88. * getSeconds (getUTCSeconds)
  89. * getMilliseconds (getUTCMilliseconds)
  90. * getTime
  91. * getTimezoneOffset
  92. * getYear
  93. * getFullYear (getUTCFullYear)
  94. * parse
  95. * setDate (setUTCDate)
  96. * setHours (setUTCHours)
  97. * setMinutes (setUTCMinutes)
  98. * setMonth (setUTCMonth)
  99. * setSeconds (setUTCSeconds)
  100. * setMilliseconds (setUTCMilliseconds)
  101. * setTime
  102. * setYear (setFullYear, setUTCFullYear)
  103. * toGMTString (toUTCString)
  104. * toLocaleString
  105. * toString
  106. *
  107. *
  108. * These Java methods are not supported
  109. *
  110. * setDay
  111. * before
  112. * after
  113. * equals
  114. * hashCode
  115. */
  116. /*
  117. * 11/97 - jsdate.c has been rewritten to conform to the ECMA-262 language
  118. * definition and reduce dependence on NSPR. NSPR is used to get the current
  119. * time in milliseconds, the time zone offset, and the daylight savings time
  120. * offset for a given time. NSPR is also used for Date.toLocaleString(), for
  121. * locale-specific formatting, and to get a string representing the timezone.
  122. * (Which turns out to be platform-dependent.)
  123. *
  124. * To do:
  125. * (I did some performance tests by timing how long it took to run what
  126. * I had of the js ECMA conformance tests.)
  127. *
  128. * - look at saving results across multiple calls to supporting
  129. * functions; the toString functions compute some of the same values
  130. * multiple times. Although - I took a quick stab at this, and I lost
  131. * rather than gained. (Fractionally.) Hard to tell what compilers/processors
  132. * are doing these days.
  133. *
  134. * - look at tweaking function return types to return double instead
  135. * of int; this seems to make things run slightly faster sometimes.
  136. * (though it could be architecture-dependent.) It'd be good to see
  137. * how this does on win32. (Tried it on irix.) Types could use a
  138. * general going-over.
  139. */
  140. /*
  141. * Supporting functions - ECMA 15.9.1.*
  142. */
  143. #define HalfTimeDomain 8.64e15
  144. #define HoursPerDay 24.0
  145. #define MinutesPerDay (HoursPerDay * MinutesPerHour)
  146. #define MinutesPerHour 60.0
  147. #define SecondsPerDay (MinutesPerDay * SecondsPerMinute)
  148. #define SecondsPerHour (MinutesPerHour * SecondsPerMinute)
  149. #define SecondsPerMinute 60.0
  150. #if defined(XP_WIN) || defined(XP_OS2)
  151. /* Work around msvc double optimization bug by making these runtime values; if
  152. * they're available at compile time, msvc optimizes division by them by
  153. * computing the reciprocal and multiplying instead of dividing - this loses
  154. * when the reciprocal isn't representable in a double.
  155. */
  156. static jsdouble msPerSecond = 1000.0;
  157. static jsdouble msPerDay = SecondsPerDay * 1000.0;
  158. static jsdouble msPerHour = SecondsPerHour * 1000.0;
  159. static jsdouble msPerMinute = SecondsPerMinute * 1000.0;
  160. #else
  161. #define msPerDay (SecondsPerDay * msPerSecond)
  162. #define msPerHour (SecondsPerHour * msPerSecond)
  163. #define msPerMinute (SecondsPerMinute * msPerSecond)
  164. #define msPerSecond 1000.0
  165. #endif
  166. #define Day(t) floor((t) / msPerDay)
  167. static jsdouble
  168. TimeWithinDay(jsdouble t)
  169. {
  170. jsdouble result;
  171. result = fmod(t, msPerDay);
  172. if (result < 0)
  173. result += msPerDay;
  174. return result;
  175. }
  176. #define DaysInYear(y) ((y) % 4 == 0 && ((y) % 100 || ((y) % 400 == 0)) \
  177. ? 366 : 365)
  178. /* math here has to be f.p, because we need
  179. * floor((1968 - 1969) / 4) == -1
  180. */
  181. #define DayFromYear(y) (365 * ((y)-1970) + floor(((y)-1969)/4.0) \
  182. - floor(((y)-1901)/100.0) + floor(((y)-1601)/400.0))
  183. #define TimeFromYear(y) (DayFromYear(y) * msPerDay)
  184. static jsint
  185. YearFromTime(jsdouble t)
  186. {
  187. jsint y = (jsint) floor(t /(msPerDay*365.2425)) + 1970;
  188. jsdouble t2 = (jsdouble) TimeFromYear(y);
  189. if (t2 > t) {
  190. y--;
  191. } else {
  192. if (t2 + msPerDay * DaysInYear(y) <= t)
  193. y++;
  194. }
  195. return y;
  196. }
  197. #define InLeapYear(t) (JSBool) (DaysInYear(YearFromTime(t)) == 366)
  198. #define DayWithinYear(t, year) ((intN) (Day(t) - DayFromYear(year)))
  199. /*
  200. * The following array contains the day of year for the first day of
  201. * each month, where index 0 is January, and day 0 is January 1.
  202. */
  203. static jsdouble firstDayOfMonth[2][12] = {
  204. {0.0, 31.0, 59.0, 90.0, 120.0, 151.0, 181.0, 212.0, 243.0, 273.0, 304.0, 334.0},
  205. {0.0, 31.0, 60.0, 91.0, 121.0, 152.0, 182.0, 213.0, 244.0, 274.0, 305.0, 335.0}
  206. };
  207. #define DayFromMonth(m, leap) firstDayOfMonth[leap][(intN)m];
  208. static intN
  209. MonthFromTime(jsdouble t)
  210. {
  211. intN d, step;
  212. jsint year = YearFromTime(t);
  213. d = DayWithinYear(t, year);
  214. if (d < (step = 31))
  215. return 0;
  216. step += (InLeapYear(t) ? 29 : 28);
  217. if (d < step)
  218. return 1;
  219. if (d < (step += 31))
  220. return 2;
  221. if (d < (step += 30))
  222. return 3;
  223. if (d < (step += 31))
  224. return 4;
  225. if (d < (step += 30))
  226. return 5;
  227. if (d < (step += 31))
  228. return 6;
  229. if (d < (step += 31))
  230. return 7;
  231. if (d < (step += 30))
  232. return 8;
  233. if (d < (step += 31))
  234. return 9;
  235. if (d < (step += 30))
  236. return 10;
  237. return 11;
  238. }
  239. static intN
  240. DateFromTime(jsdouble t)
  241. {
  242. intN d, step, next;
  243. jsint year = YearFromTime(t);
  244. d = DayWithinYear(t, year);
  245. if (d <= (next = 30))
  246. return d + 1;
  247. step = next;
  248. next += (InLeapYear(t) ? 29 : 28);
  249. if (d <= next)
  250. return d - step;
  251. step = next;
  252. if (d <= (next += 31))
  253. return d - step;
  254. step = next;
  255. if (d <= (next += 30))
  256. return d - step;
  257. step = next;
  258. if (d <= (next += 31))
  259. return d - step;
  260. step = next;
  261. if (d <= (next += 30))
  262. return d - step;
  263. step = next;
  264. if (d <= (next += 31))
  265. return d - step;
  266. step = next;
  267. if (d <= (next += 31))
  268. return d - step;
  269. step = next;
  270. if (d <= (next += 30))
  271. return d - step;
  272. step = next;
  273. if (d <= (next += 31))
  274. return d - step;
  275. step = next;
  276. if (d <= (next += 30))
  277. return d - step;
  278. step = next;
  279. return d - step;
  280. }
  281. static intN
  282. WeekDay(jsdouble t)
  283. {
  284. jsint result;
  285. result = (jsint) Day(t) + 4;
  286. result = result % 7;
  287. if (result < 0)
  288. result += 7;
  289. return (intN) result;
  290. }
  291. #define MakeTime(hour, min, sec, ms) \
  292. ((((hour) * MinutesPerHour + (min)) * SecondsPerMinute + (sec)) * msPerSecond + (ms))
  293. static jsdouble
  294. MakeDay(jsdouble year, jsdouble month, jsdouble date)
  295. {
  296. JSBool leap;
  297. jsdouble yearday;
  298. jsdouble monthday;
  299. year += floor(month / 12);
  300. month = fmod(month, 12.0);
  301. if (month < 0)
  302. month += 12;
  303. leap = (DaysInYear((jsint) year) == 366);
  304. yearday = floor(TimeFromYear(year) / msPerDay);
  305. monthday = DayFromMonth(month, leap);
  306. return yearday + monthday + date - 1;
  307. }
  308. #define MakeDate(day, time) ((day) * msPerDay + (time))
  309. /*
  310. * Years and leap years on which Jan 1 is a Sunday, Monday, etc.
  311. *
  312. * yearStartingWith[0][i] is an example non-leap year where
  313. * Jan 1 appears on Sunday (i == 0), Monday (i == 1), etc.
  314. *
  315. * yearStartingWith[1][i] is an example leap year where
  316. * Jan 1 appears on Sunday (i == 0), Monday (i == 1), etc.
  317. */
  318. static jsint yearStartingWith[2][7] = {
  319. {1978, 1973, 1974, 1975, 1981, 1971, 1977},
  320. {1984, 1996, 1980, 1992, 1976, 1988, 1972}
  321. };
  322. /*
  323. * Find a year for which any given date will fall on the same weekday.
  324. *
  325. * This function should be used with caution when used other than
  326. * for determining DST; it hasn't been proven not to produce an
  327. * incorrect year for times near year boundaries.
  328. */
  329. static jsint
  330. EquivalentYearForDST(jsint year)
  331. {
  332. jsint day;
  333. JSBool isLeapYear;
  334. day = (jsint) DayFromYear(year) + 4;
  335. day = day % 7;
  336. if (day < 0)
  337. day += 7;
  338. isLeapYear = (DaysInYear(year) == 366);
  339. return yearStartingWith[isLeapYear][day];
  340. }
  341. /* LocalTZA gets set by js_InitDateClass() */
  342. static jsdouble LocalTZA;
  343. static jsdouble
  344. DaylightSavingTA(jsdouble t)
  345. {
  346. volatile int64 PR_t;
  347. int64 ms2us;
  348. int64 offset;
  349. jsdouble result;
  350. /* abort if NaN */
  351. if (JSDOUBLE_IS_NaN(t))
  352. return t;
  353. /*
  354. * If earlier than 1970 or after 2038, potentially beyond the ken of
  355. * many OSes, map it to an equivalent year before asking.
  356. */
  357. if (t < 0.0 || t > 2145916800000.0) {
  358. jsint year;
  359. jsdouble day;
  360. year = EquivalentYearForDST(YearFromTime(t));
  361. day = MakeDay(year, MonthFromTime(t), DateFromTime(t));
  362. t = MakeDate(day, TimeWithinDay(t));
  363. }
  364. /* put our t in an LL, and map it to usec for prtime */
  365. JSLL_D2L(PR_t, t);
  366. JSLL_I2L(ms2us, PRMJ_USEC_PER_MSEC);
  367. JSLL_MUL(PR_t, PR_t, ms2us);
  368. offset = PRMJ_DSTOffset(PR_t);
  369. JSLL_DIV(offset, offset, ms2us);
  370. JSLL_L2D(result, offset);
  371. return result;
  372. }
  373. #define AdjustTime(t) fmod(LocalTZA + DaylightSavingTA(t), msPerDay)
  374. #define LocalTime(t) ((t) + AdjustTime(t))
  375. static jsdouble
  376. UTC(jsdouble t)
  377. {
  378. return t - AdjustTime(t - LocalTZA);
  379. }
  380. static intN
  381. HourFromTime(jsdouble t)
  382. {
  383. intN result = (intN) fmod(floor(t/msPerHour), HoursPerDay);
  384. if (result < 0)
  385. result += (intN)HoursPerDay;
  386. return result;
  387. }
  388. static intN
  389. MinFromTime(jsdouble t)
  390. {
  391. intN result = (intN) fmod(floor(t / msPerMinute), MinutesPerHour);
  392. if (result < 0)
  393. result += (intN)MinutesPerHour;
  394. return result;
  395. }
  396. static intN
  397. SecFromTime(jsdouble t)
  398. {
  399. intN result = (intN) fmod(floor(t / msPerSecond), SecondsPerMinute);
  400. if (result < 0)
  401. result += (intN)SecondsPerMinute;
  402. return result;
  403. }
  404. static intN
  405. msFromTime(jsdouble t)
  406. {
  407. intN result = (intN) fmod(t, msPerSecond);
  408. if (result < 0)
  409. result += (intN)msPerSecond;
  410. return result;
  411. }
  412. #define TIMECLIP(d) ((JSDOUBLE_IS_FINITE(d) \
  413. && !((d < 0 ? -d : d) > HalfTimeDomain)) \
  414. ? js_DoubleToInteger(d + (+0.)) : *cx->runtime->jsNaN)
  415. /**
  416. * end of ECMA 'support' functions
  417. */
  418. /*
  419. * Other Support routines and definitions
  420. */
  421. /*
  422. * We use the first reseved slot to store UTC time, and the second for caching
  423. * the local time. The initial value of the cache entry is NaN.
  424. */
  425. #define JSSLOT_UTC_TIME JSSLOT_PRIVATE
  426. #define JSSLOT_LOCAL_TIME (JSSLOT_PRIVATE + 1)
  427. #define DATE_RESERVED_SLOTS 2
  428. JSClass js_DateClass = {
  429. js_Date_str,
  430. JSCLASS_HAS_RESERVED_SLOTS(DATE_RESERVED_SLOTS) |
  431. JSCLASS_HAS_CACHED_PROTO(JSProto_Date),
  432. JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub,
  433. JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub,
  434. JSCLASS_NO_OPTIONAL_MEMBERS
  435. };
  436. /* for use by date_parse */
  437. static const char* wtb[] = {
  438. "am", "pm",
  439. "monday", "tuesday", "wednesday", "thursday", "friday",
  440. "saturday", "sunday",
  441. "january", "february", "march", "april", "may", "june",
  442. "july", "august", "september", "october", "november", "december",
  443. "gmt", "ut", "utc",
  444. "est", "edt",
  445. "cst", "cdt",
  446. "mst", "mdt",
  447. "pst", "pdt"
  448. /* time zone table needs to be expanded */
  449. };
  450. static int ttb[] = {
  451. -1, -2, 0, 0, 0, 0, 0, 0, 0, /* AM/PM */
  452. 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
  453. 10000 + 0, 10000 + 0, 10000 + 0, /* GMT/UT/UTC */
  454. 10000 + 5 * 60, 10000 + 4 * 60, /* EST/EDT */
  455. 10000 + 6 * 60, 10000 + 5 * 60, /* CST/CDT */
  456. 10000 + 7 * 60, 10000 + 6 * 60, /* MST/MDT */
  457. 10000 + 8 * 60, 10000 + 7 * 60 /* PST/PDT */
  458. };
  459. /* helper for date_parse */
  460. static JSBool
  461. date_regionMatches(const char* s1, int s1off, const jschar* s2, int s2off,
  462. int count, int ignoreCase)
  463. {
  464. JSBool result = JS_FALSE;
  465. /* return true if matches, otherwise, false */
  466. while (count > 0 && s1[s1off] && s2[s2off]) {
  467. if (ignoreCase) {
  468. if (JS_TOLOWER((jschar)s1[s1off]) != JS_TOLOWER(s2[s2off])) {
  469. break;
  470. }
  471. } else {
  472. if ((jschar)s1[s1off] != s2[s2off]) {
  473. break;
  474. }
  475. }
  476. s1off++;
  477. s2off++;
  478. count--;
  479. }
  480. if (count == 0) {
  481. result = JS_TRUE;
  482. }
  483. return result;
  484. }
  485. /* find UTC time from given date... no 1900 correction! */
  486. static jsdouble
  487. date_msecFromDate(jsdouble year, jsdouble mon, jsdouble mday, jsdouble hour,
  488. jsdouble min, jsdouble sec, jsdouble msec)
  489. {
  490. jsdouble day;
  491. jsdouble msec_time;
  492. jsdouble result;
  493. day = MakeDay(year, mon, mday);
  494. msec_time = MakeTime(hour, min, sec, msec);
  495. result = MakeDate(day, msec_time);
  496. return result;
  497. }
  498. /* compute the time in msec (unclipped) from the given args */
  499. #define MAXARGS 7
  500. static JSBool
  501. date_msecFromArgs(JSContext *cx, uintN argc, jsval *argv, jsdouble *rval)
  502. {
  503. uintN loop;
  504. jsdouble array[MAXARGS];
  505. jsdouble d;
  506. jsdouble msec_time;
  507. for (loop = 0; loop < MAXARGS; loop++) {
  508. if (loop < argc) {
  509. d = js_ValueToNumber(cx, &argv[loop]);
  510. if (JSVAL_IS_NULL(argv[loop]))
  511. return JS_FALSE;
  512. /* return NaN if any arg is not finite */
  513. if (!JSDOUBLE_IS_FINITE(d)) {
  514. *rval = *cx->runtime->jsNaN;
  515. return JS_TRUE;
  516. }
  517. array[loop] = js_DoubleToInteger(d);
  518. } else {
  519. if (loop == 2) {
  520. array[loop] = 1; /* Default the date argument to 1. */
  521. } else {
  522. array[loop] = 0;
  523. }
  524. }
  525. }
  526. /* adjust 2-digit years into the 20th century */
  527. if (array[0] >= 0 && array[0] <= 99)
  528. array[0] += 1900;
  529. msec_time = date_msecFromDate(array[0], array[1], array[2],
  530. array[3], array[4], array[5], array[6]);
  531. *rval = msec_time;
  532. return JS_TRUE;
  533. }
  534. /*
  535. * See ECMA 15.9.4.[3-10];
  536. */
  537. static JSBool
  538. date_UTC(JSContext *cx, uintN argc, jsval *vp)
  539. {
  540. jsdouble msec_time;
  541. if (!date_msecFromArgs(cx, argc, vp + 2, &msec_time))
  542. return JS_FALSE;
  543. msec_time = TIMECLIP(msec_time);
  544. return js_NewNumberInRootedValue(cx, msec_time, vp);
  545. }
  546. static JSBool
  547. date_parseString(JSString *str, jsdouble *result)
  548. {
  549. jsdouble msec;
  550. const jschar *s;
  551. size_t limit;
  552. size_t i = 0;
  553. int year = -1;
  554. int mon = -1;
  555. int mday = -1;
  556. int hour = -1;
  557. int min = -1;
  558. int sec = -1;
  559. int c = -1;
  560. int n = -1;
  561. int tzoffset = -1;
  562. int prevc = 0;
  563. JSBool seenplusminus = JS_FALSE;
  564. int temp;
  565. JSBool seenmonthname = JS_FALSE;
  566. JSSTRING_CHARS_AND_LENGTH(str, s, limit);
  567. if (limit == 0)
  568. goto syntax;
  569. while (i < limit) {
  570. c = s[i];
  571. i++;
  572. if (c <= ' ' || c == ',' || c == '-') {
  573. if (c == '-' && '0' <= s[i] && s[i] <= '9') {
  574. prevc = c;
  575. }
  576. continue;
  577. }
  578. if (c == '(') { /* comments) */
  579. int depth = 1;
  580. while (i < limit) {
  581. c = s[i];
  582. i++;
  583. if (c == '(') depth++;
  584. else if (c == ')')
  585. if (--depth <= 0)
  586. break;
  587. }
  588. continue;
  589. }
  590. if ('0' <= c && c <= '9') {
  591. n = c - '0';
  592. while (i < limit && '0' <= (c = s[i]) && c <= '9') {
  593. n = n * 10 + c - '0';
  594. i++;
  595. }
  596. /* allow TZA before the year, so
  597. * 'Wed Nov 05 21:49:11 GMT-0800 1997'
  598. * works */
  599. /* uses of seenplusminus allow : in TZA, so Java
  600. * no-timezone style of GMT+4:30 works
  601. */
  602. if ((prevc == '+' || prevc == '-')/* && year>=0 */) {
  603. /* make ':' case below change tzoffset */
  604. seenplusminus = JS_TRUE;
  605. /* offset */
  606. if (n < 24)
  607. n = n * 60; /* EG. "GMT-3" */
  608. else
  609. n = n % 100 + n / 100 * 60; /* eg "GMT-0430" */
  610. if (prevc == '+') /* plus means east of GMT */
  611. n = -n;
  612. if (tzoffset != 0 && tzoffset != -1)
  613. goto syntax;
  614. tzoffset = n;
  615. } else if (prevc == '/' && mon >= 0 && mday >= 0 && year < 0) {
  616. if (c <= ' ' || c == ',' || c == '/' || i >= limit)
  617. year = n;
  618. else
  619. goto syntax;
  620. } else if (c == ':') {
  621. if (hour < 0)
  622. hour = /*byte*/ n;
  623. else if (min < 0)
  624. min = /*byte*/ n;
  625. else
  626. goto syntax;
  627. } else if (c == '/') {
  628. /* until it is determined that mon is the actual
  629. month, keep it as 1-based rather than 0-based */
  630. if (mon < 0)
  631. mon = /*byte*/ n;
  632. else if (mday < 0)
  633. mday = /*byte*/ n;
  634. else
  635. goto syntax;
  636. } else if (i < limit && c != ',' && c > ' ' && c != '-' && c != '(') {
  637. goto syntax;
  638. } else if (seenplusminus && n < 60) { /* handle GMT-3:30 */
  639. if (tzoffset < 0)
  640. tzoffset -= n;
  641. else
  642. tzoffset += n;
  643. } else if (hour >= 0 && min < 0) {
  644. min = /*byte*/ n;
  645. } else if (prevc == ':' && min >= 0 && sec < 0) {
  646. sec = /*byte*/ n;
  647. } else if (mon < 0) {
  648. mon = /*byte*/n;
  649. } else if (mon >= 0 && mday < 0) {
  650. mday = /*byte*/ n;
  651. } else if (mon >= 0 && mday >= 0 && year < 0) {
  652. year = n;
  653. } else {
  654. goto syntax;
  655. }
  656. prevc = 0;
  657. } else if (c == '/' || c == ':' || c == '+' || c == '-') {
  658. prevc = c;
  659. } else {
  660. size_t st = i - 1;
  661. int k;
  662. while (i < limit) {
  663. c = s[i];
  664. if (!(('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z')))
  665. break;
  666. i++;
  667. }
  668. if (i <= st + 1)
  669. goto syntax;
  670. for (k = JS_ARRAY_LENGTH(wtb); --k >= 0;)
  671. if (date_regionMatches(wtb[k], 0, s, st, i-st, 1)) {
  672. int action = ttb[k];
  673. if (action != 0) {
  674. if (action < 0) {
  675. /*
  676. * AM/PM. Count 12:30 AM as 00:30, 12:30 PM as
  677. * 12:30, instead of blindly adding 12 if PM.
  678. */
  679. JS_ASSERT(action == -1 || action == -2);
  680. if (hour > 12 || hour < 0) {
  681. goto syntax;
  682. } else {
  683. if (action == -1 && hour == 12) { /* am */
  684. hour = 0;
  685. } else if (action == -2 && hour != 12) { /* pm */
  686. hour += 12;
  687. }
  688. }
  689. } else if (action <= 13) { /* month! */
  690. /* Adjust mon to be 1-based until the final values
  691. for mon, mday and year are adjusted below */
  692. if (seenmonthname) {
  693. goto syntax;
  694. }
  695. seenmonthname = JS_TRUE;
  696. temp = /*byte*/ (action - 2) + 1;
  697. if (mon < 0) {
  698. mon = temp;
  699. } else if (mday < 0) {
  700. mday = mon;
  701. mon = temp;
  702. } else if (year < 0) {
  703. year = mon;
  704. mon = temp;
  705. } else {
  706. goto syntax;
  707. }
  708. } else {
  709. tzoffset = action - 10000;
  710. }
  711. }
  712. break;
  713. }
  714. if (k < 0)
  715. goto syntax;
  716. prevc = 0;
  717. }
  718. }
  719. if (year < 0 || mon < 0 || mday < 0)
  720. goto syntax;
  721. /*
  722. Case 1. The input string contains an English month name.
  723. The form of the string can be month f l, or f month l, or
  724. f l month which each evaluate to the same date.
  725. If f and l are both greater than or equal to 70, or
  726. both less than 70, the date is invalid.
  727. The year is taken to be the greater of the values f, l.
  728. If the year is greater than or equal to 70 and less than 100,
  729. it is considered to be the number of years after 1900.
  730. Case 2. The input string is of the form "f/m/l" where f, m and l are
  731. integers, e.g. 7/16/45.
  732. Adjust the mon, mday and year values to achieve 100% MSIE
  733. compatibility.
  734. a. If 0 <= f < 70, f/m/l is interpreted as month/day/year.
  735. i. If year < 100, it is the number of years after 1900
  736. ii. If year >= 100, it is the number of years after 0.
  737. b. If 70 <= f < 100
  738. i. If m < 70, f/m/l is interpreted as
  739. year/month/day where year is the number of years after
  740. 1900.
  741. ii. If m >= 70, the date is invalid.
  742. c. If f >= 100
  743. i. If m < 70, f/m/l is interpreted as
  744. year/month/day where year is the number of years after 0.
  745. ii. If m >= 70, the date is invalid.
  746. */
  747. if (seenmonthname) {
  748. if ((mday >= 70 && year >= 70) || (mday < 70 && year < 70)) {
  749. goto syntax;
  750. }
  751. if (mday > year) {
  752. temp = year;
  753. year = mday;
  754. mday = temp;
  755. }
  756. if (year >= 70 && year < 100) {
  757. year += 1900;
  758. }
  759. } else if (mon < 70) { /* (a) month/day/year */
  760. if (year < 100) {
  761. year += 1900;
  762. }
  763. } else if (mon < 100) { /* (b) year/month/day */
  764. if (mday < 70) {
  765. temp = year;
  766. year = mon + 1900;
  767. mon = mday;
  768. mday = temp;
  769. } else {
  770. goto syntax;
  771. }
  772. } else { /* (c) year/month/day */
  773. if (mday < 70) {
  774. temp = year;
  775. year = mon;
  776. mon = mday;
  777. mday = temp;
  778. } else {
  779. goto syntax;
  780. }
  781. }
  782. mon -= 1; /* convert month to 0-based */
  783. if (sec < 0)
  784. sec = 0;
  785. if (min < 0)
  786. min = 0;
  787. if (hour < 0)
  788. hour = 0;
  789. if (tzoffset == -1) { /* no time zone specified, have to use local */
  790. jsdouble msec_time;
  791. msec_time = date_msecFromDate(year, mon, mday, hour, min, sec, 0);
  792. *result = UTC(msec_time);
  793. return JS_TRUE;
  794. }
  795. msec = date_msecFromDate(year, mon, mday, hour, min, sec, 0);
  796. msec += tzoffset * msPerMinute;
  797. *result = msec;
  798. return JS_TRUE;
  799. syntax:
  800. /* syntax error */
  801. *result = 0;
  802. return JS_FALSE;
  803. }
  804. static JSBool
  805. date_parse(JSContext *cx, uintN argc, jsval *vp)
  806. {
  807. JSString *str;
  808. jsdouble result;
  809. str = js_ValueToString(cx, vp[2]);
  810. if (!str)
  811. return JS_FALSE;
  812. if (!date_parseString(str, &result)) {
  813. *vp = DOUBLE_TO_JSVAL(cx->runtime->jsNaN);
  814. return JS_TRUE;
  815. }
  816. result = TIMECLIP(result);
  817. return js_NewNumberInRootedValue(cx, result, vp);
  818. }
  819. static JSBool
  820. date_now(JSContext *cx, uintN argc, jsval *vp)
  821. {
  822. int64 us, ms, us2ms;
  823. jsdouble msec_time;
  824. us = PRMJ_Now();
  825. JSLL_UI2L(us2ms, PRMJ_USEC_PER_MSEC);
  826. JSLL_DIV(ms, us, us2ms);
  827. JSLL_L2D(msec_time, ms);
  828. return js_NewDoubleInRootedValue(cx, msec_time, vp);
  829. }
  830. /*
  831. * Get UTC time from the date object. Returns false if the object is not
  832. * Date type.
  833. */
  834. static JSBool
  835. GetUTCTime(JSContext *cx, JSObject *obj, jsval *vp, jsdouble *dp)
  836. {
  837. if (!JS_InstanceOf(cx, obj, &js_DateClass, vp ? vp + 2 : NULL))
  838. return JS_FALSE;
  839. *dp = *JSVAL_TO_DOUBLE(obj->fslots[JSSLOT_UTC_TIME]);
  840. return JS_TRUE;
  841. }
  842. /*
  843. * Set UTC time slot with a pointer pointing to a jsdouble. This function is
  844. * used only for setting UTC time to some predefined values, such as NaN.
  845. *
  846. * It also invalidates cached local time.
  847. */
  848. static JSBool
  849. SetUTCTimePtr(JSContext *cx, JSObject *obj, jsval *vp, jsdouble *dp)
  850. {
  851. if (vp && !JS_InstanceOf(cx, obj, &js_DateClass, vp + 2))
  852. return JS_FALSE;
  853. JS_ASSERT_IF(!vp, STOBJ_GET_CLASS(obj) == &js_DateClass);
  854. /* Invalidate local time cache. */
  855. obj->fslots[JSSLOT_LOCAL_TIME] = DOUBLE_TO_JSVAL(cx->runtime->jsNaN);
  856. obj->fslots[JSSLOT_UTC_TIME] = DOUBLE_TO_JSVAL(dp);
  857. return JS_TRUE;
  858. }
  859. /*
  860. * Set UTC time to a given time.
  861. */
  862. static JSBool
  863. SetUTCTime(JSContext *cx, JSObject *obj, jsval *vp, jsdouble t)
  864. {
  865. jsdouble *dp = js_NewWeaklyRootedDouble(cx, t);
  866. if (!dp)
  867. return JS_FALSE;
  868. return SetUTCTimePtr(cx, obj, vp, dp);
  869. }
  870. /*
  871. * Get the local time, cache it if necessary. If UTC time is not finite
  872. * (e.g., NaN), the local time slot is set to the UTC time without conversion.
  873. */
  874. static JSBool
  875. GetAndCacheLocalTime(JSContext *cx, JSObject *obj, jsval *vp, jsdouble *dp)
  876. {
  877. jsval v;
  878. jsdouble result;
  879. jsdouble *cached;
  880. if (!obj || !JS_InstanceOf(cx, obj, &js_DateClass, vp ? vp + 2 : NULL))
  881. return JS_FALSE;
  882. v = obj->fslots[JSSLOT_LOCAL_TIME];
  883. result = *JSVAL_TO_DOUBLE(v);
  884. if (JSDOUBLE_IS_NaN(result)) {
  885. if (!GetUTCTime(cx, obj, vp, &result))
  886. return JS_FALSE;
  887. /* if result is NaN, it couldn't be finite. */
  888. if (JSDOUBLE_IS_FINITE(result))
  889. result = LocalTime(result);
  890. cached = js_NewWeaklyRootedDouble(cx, result);
  891. if (!cached)
  892. return JS_FALSE;
  893. obj->fslots[JSSLOT_LOCAL_TIME] = DOUBLE_TO_JSVAL(cached);
  894. }
  895. *dp = result;
  896. return JS_TRUE;
  897. }
  898. /*
  899. * See ECMA 15.9.5.4 thru 15.9.5.23
  900. */
  901. static JSBool
  902. date_getTime(JSContext *cx, uintN argc, jsval *vp)
  903. {
  904. jsdouble result;
  905. return GetUTCTime(cx, JS_THIS_OBJECT(cx, vp), vp, &result) &&
  906. js_NewNumberInRootedValue(cx, result, vp);
  907. }
  908. static JSBool
  909. GetYear(JSContext *cx, JSBool fullyear, jsval *vp)
  910. {
  911. jsdouble result;
  912. if (!GetAndCacheLocalTime(cx, JS_THIS_OBJECT(cx, vp), vp, &result))
  913. return JS_FALSE;
  914. if (JSDOUBLE_IS_FINITE(result)) {
  915. result = YearFromTime(result);
  916. /* Follow ECMA-262 to the letter, contrary to IE JScript. */
  917. if (!fullyear)
  918. result -= 1900;
  919. }
  920. return js_NewNumberInRootedValue(cx, result, vp);
  921. }
  922. static JSBool
  923. date_getYear(JSContext *cx, uintN argc, jsval *vp)
  924. {
  925. return GetYear(cx, JS_FALSE, vp);
  926. }
  927. static JSBool
  928. date_getFullYear(JSContext *cx, uintN argc, jsval *vp)
  929. {
  930. return GetYear(cx, JS_TRUE, vp);
  931. }
  932. static JSBool
  933. date_getUTCFullYear(JSContext *cx, uintN argc, jsval *vp)
  934. {
  935. jsdouble result;
  936. if (!GetUTCTime(cx, JS_THIS_OBJECT(cx, vp), vp, &result))
  937. return JS_FALSE;
  938. if (JSDOUBLE_IS_FINITE(result))
  939. result = YearFromTime(result);
  940. return js_NewNumberInRootedValue(cx, result, vp);
  941. }
  942. static JSBool
  943. date_getMonth(JSContext *cx, uintN argc, jsval *vp)
  944. {
  945. jsdouble result;
  946. if (!GetAndCacheLocalTime(cx, JS_THIS_OBJECT(cx, vp), vp, &result))
  947. return JS_FALSE;
  948. if (JSDOUBLE_IS_FINITE(result))
  949. result = MonthFromTime(result);
  950. return js_NewNumberInRootedValue(cx, result, vp);
  951. }
  952. static JSBool
  953. date_getUTCMonth(JSContext *cx, uintN argc, jsval *vp)
  954. {
  955. jsdouble result;
  956. if (!GetUTCTime(cx, JS_THIS_OBJECT(cx, vp), vp, &result))
  957. return JS_FALSE;
  958. if (JSDOUBLE_IS_FINITE(result))
  959. result = MonthFromTime(result);
  960. return js_NewNumberInRootedValue(cx, result, vp);
  961. }
  962. static JSBool
  963. date_getDate(JSContext *cx, uintN argc, jsval *vp)
  964. {
  965. jsdouble result;
  966. if (!GetAndCacheLocalTime(cx, JS_THIS_OBJECT(cx, vp), vp, &result))
  967. return JS_FALSE;
  968. if (JSDOUBLE_IS_FINITE(result))
  969. result = DateFromTime(result);
  970. return js_NewNumberInRootedValue(cx, result, vp);
  971. }
  972. static JSBool
  973. date_getUTCDate(JSContext *cx, uintN argc, jsval *vp)
  974. {
  975. jsdouble result;
  976. if (!GetUTCTime(cx, JS_THIS_OBJECT(cx, vp), vp, &result))
  977. return JS_FALSE;
  978. if (JSDOUBLE_IS_FINITE(result))
  979. result = DateFromTime(result);
  980. return js_NewNumberInRootedValue(cx, result, vp);
  981. }
  982. static JSBool
  983. date_getDay(JSContext *cx, uintN argc, jsval *vp)
  984. {
  985. jsdouble result;
  986. if (!GetAndCacheLocalTime(cx, JS_THIS_OBJECT(cx, vp), vp, &result))
  987. return JS_FALSE;
  988. if (JSDOUBLE_IS_FINITE(result))
  989. result = WeekDay(result);
  990. return js_NewNumberInRootedValue(cx, result, vp);
  991. }
  992. static JSBool
  993. date_getUTCDay(JSContext *cx, uintN argc, jsval *vp)
  994. {
  995. jsdouble result;
  996. if (!GetUTCTime(cx, JS_THIS_OBJECT(cx, vp), vp, &result))
  997. return JS_FALSE;
  998. if (JSDOUBLE_IS_FINITE(result))
  999. result = WeekDay(result);
  1000. return js_NewNumberInRootedValue(cx, result, vp);
  1001. }
  1002. static JSBool
  1003. date_getHours(JSContext *cx, uintN argc, jsval *vp)
  1004. {
  1005. jsdouble result;
  1006. if (!GetAndCacheLocalTime(cx, JS_THIS_OBJECT(cx, vp), vp, &result))
  1007. return JS_FALSE;
  1008. if (JSDOUBLE_IS_FINITE(result))
  1009. result = HourFromTime(result);
  1010. return js_NewNumberInRootedValue(cx, result, vp);
  1011. }
  1012. static JSBool
  1013. date_getUTCHours(JSContext *cx, uintN argc, jsval *vp)
  1014. {
  1015. jsdouble result;
  1016. if (!GetUTCTime(cx, JS_THIS_OBJECT(cx, vp), vp, &result))
  1017. return JS_FALSE;
  1018. if (JSDOUBLE_IS_FINITE(result))
  1019. result = HourFromTime(result);
  1020. return js_NewNumberInRootedValue(cx, result, vp);
  1021. }
  1022. static JSBool
  1023. date_getMinutes(JSContext *cx, uintN argc, jsval *vp)
  1024. {
  1025. jsdouble result;
  1026. if (!GetAndCacheLocalTime(cx, JS_THIS_OBJECT(cx, vp), vp, &result))
  1027. return JS_FALSE;
  1028. if (JSDOUBLE_IS_FINITE(result))
  1029. result = MinFromTime(result);
  1030. return js_NewNumberInRootedValue(cx, result, vp);
  1031. }
  1032. static JSBool
  1033. date_getUTCMinutes(JSContext *cx, uintN argc, jsval *vp)
  1034. {
  1035. jsdouble result;
  1036. if (!GetUTCTime(cx, JS_THIS_OBJECT(cx, vp), vp, &result))
  1037. return JS_FALSE;
  1038. if (JSDOUBLE_IS_FINITE(result))
  1039. result = MinFromTime(result);
  1040. return js_NewNumberInRootedValue(cx, result, vp);
  1041. }
  1042. /* Date.getSeconds is mapped to getUTCSeconds */
  1043. static JSBool
  1044. date_getUTCSeconds(JSContext *cx, uintN argc, jsval *vp)
  1045. {
  1046. jsdouble result;
  1047. if (!GetUTCTime(cx, JS_THIS_OBJECT(cx, vp), vp, &result))
  1048. return JS_FALSE;
  1049. if (JSDOUBLE_IS_FINITE(result))
  1050. result = SecFromTime(result);
  1051. return js_NewNumberInRootedValue(cx, result, vp);
  1052. }
  1053. /* Date.getMilliseconds is mapped to getUTCMilliseconds */
  1054. static JSBool
  1055. date_getUTCMilliseconds(JSContext *cx, uintN argc, jsval *vp)
  1056. {
  1057. jsdouble result;
  1058. if (!GetUTCTime(cx, JS_THIS_OBJECT(cx, vp), vp, &result))
  1059. return JS_FALSE;
  1060. if (JSDOUBLE_IS_FINITE(result))
  1061. result = msFromTime(result);
  1062. return js_NewNumberInRootedValue(cx, result, vp);
  1063. }
  1064. static JSBool
  1065. date_getTimezoneOffset(JSContext *cx, uintN argc, jsval *vp)
  1066. {
  1067. JSObject *obj;
  1068. jsdouble utctime, localtime, result;
  1069. obj = JS_THIS_OBJECT(cx, vp);
  1070. if (!GetUTCTime(cx, obj, vp, &utctime))
  1071. return JS_FALSE;
  1072. if (!GetAndCacheLocalTime(cx, obj, NULL, &localtime))
  1073. return JS_FALSE;
  1074. /*
  1075. * Return the time zone offset in minutes for the current locale that is
  1076. * appropriate for this time. This value would be a constant except for
  1077. * daylight savings time.
  1078. */
  1079. result = (utctime - localtime) / msPerMinute;
  1080. return js_NewNumberInRootedValue(cx, result, vp);
  1081. }
  1082. static JSBool
  1083. date_setTime(JSContext *cx, uintN argc, jsval *vp)
  1084. {
  1085. jsdouble result;
  1086. result = js_ValueToNumber(cx, &vp[2]);
  1087. if (JSVAL_IS_NULL(vp[2]))
  1088. return JS_FALSE;
  1089. result = TIMECLIP(result);
  1090. if (!SetUTCTime(cx, JS_THIS_OBJECT(cx, vp), vp, result))
  1091. return JS_FALSE;
  1092. return js_NewNumberInRootedValue(cx, result, vp);
  1093. }
  1094. static JSBool
  1095. date_makeTime(JSContext *cx, uintN maxargs, JSBool local, uintN argc, jsval *vp)
  1096. {
  1097. JSObject *obj;
  1098. jsval *argv;
  1099. uintN i;
  1100. jsdouble args[4], *argp, *stop;
  1101. jsdouble hour, min, sec, msec;
  1102. jsdouble lorutime; /* Local or UTC version of *date */
  1103. jsdouble msec_time;
  1104. jsdouble result;
  1105. obj = JS_THIS_OBJECT(cx, vp);
  1106. if (!GetUTCTime(cx, obj, vp, &result))
  1107. return JS_FALSE;
  1108. /* just return NaN if the date is already NaN */
  1109. if (!JSDOUBLE_IS_FINITE(result))
  1110. return js_NewNumberInRootedValue(cx, result, vp);
  1111. /*
  1112. * Satisfy the ECMA rule that if a function is called with
  1113. * fewer arguments than the specified formal arguments, the
  1114. * remaining arguments are set to undefined. Seems like all
  1115. * the Date.setWhatever functions in ECMA are only varargs
  1116. * beyond the first argument; this should be set to undefined
  1117. * if it's not given. This means that "d = new Date();
  1118. * d.setMilliseconds()" returns NaN. Blech.
  1119. */
  1120. if (argc == 0)
  1121. argc = 1; /* should be safe, because length of all setters is 1 */
  1122. else if (argc > maxargs)
  1123. argc = maxargs; /* clamp argc */
  1124. JS_ASSERT(1 <= argc && argc <= 4);
  1125. argv = vp + 2;
  1126. for (i = 0; i < argc; i++) {
  1127. args[i] = js_ValueToNumber(cx, &argv[i]);
  1128. if (JSVAL_IS_NULL(argv[i]))
  1129. return JS_FALSE;
  1130. if (!JSDOUBLE_IS_FINITE(args[i])) {
  1131. if (!SetUTCTimePtr(cx, obj, NULL, cx->runtime->jsNaN))
  1132. return JS_FALSE;
  1133. *vp = DOUBLE_TO_JSVAL(cx->runtime->jsNaN);
  1134. return JS_TRUE;
  1135. }
  1136. args[i] = js_DoubleToInteger(args[i]);
  1137. }
  1138. if (local)
  1139. lorutime = LocalTime(result);
  1140. else
  1141. lorutime = result;
  1142. argp = args;
  1143. stop = argp + argc;
  1144. if (maxargs >= 4 && argp < stop)
  1145. hour = *argp++;
  1146. else
  1147. hour = HourFromTime(lorutime);
  1148. if (maxargs >= 3 && argp < stop)
  1149. min = *argp++;
  1150. else
  1151. min = MinFromTime(lorutime);
  1152. if (maxargs >= 2 && argp < stop)
  1153. sec = *argp++;
  1154. else
  1155. sec = SecFromTime(lorutime);
  1156. if (maxargs >= 1 && argp < stop)
  1157. msec = *argp;
  1158. else
  1159. msec = msFromTime(lorutime);
  1160. msec_time = MakeTime(hour, min, sec, msec);
  1161. result = MakeDate(Day(lorutime), msec_time);
  1162. /* fprintf(stderr, "%f\n", result); */
  1163. if (local)
  1164. result = UTC(result);
  1165. /* fprintf(stderr, "%f\n", result); */
  1166. result = TIMECLIP(result);
  1167. if (!SetUTCTime(cx, obj, NULL, result))
  1168. return JS_FALSE;
  1169. return js_NewNumberInRootedValue(cx, result, vp);
  1170. }
  1171. static JSBool
  1172. date_setMilliseconds(JSContext *cx, uintN argc, jsval *vp)
  1173. {
  1174. return date_makeTime(cx, 1, JS_TRUE, argc, vp);
  1175. }
  1176. static JSBool
  1177. date_setUTCMilliseconds(JSContext *cx, uintN argc, jsval *vp)
  1178. {
  1179. return date_makeTime(cx, 1, JS_FALSE, argc, vp);
  1180. }
  1181. static JSBool
  1182. date_setSeconds(JSContext *cx, uintN argc, jsval *vp)
  1183. {
  1184. return date_makeTime(cx, 2, JS_TRUE, argc, vp);
  1185. }
  1186. static JSBool
  1187. date_setUTCSeconds(JSContext *cx, uintN argc, jsval *vp)
  1188. {
  1189. return date_makeTime(cx, 2, JS_FALSE, argc, vp);
  1190. }
  1191. static JSBool
  1192. date_setMinutes(JSContext *cx, uintN argc, jsval *vp)
  1193. {
  1194. return date_makeTime(cx, 3, JS_TRUE, argc, vp);
  1195. }
  1196. static JSBool
  1197. date_setUTCMinutes(JSContext *cx, uintN argc, jsval *vp)
  1198. {
  1199. return date_makeTime(cx, 3, JS_FALSE, argc, vp);
  1200. }
  1201. static JSBool
  1202. date_setHours(JSContext *cx, uintN argc, jsval *vp)
  1203. {
  1204. return date_makeTime(cx, 4, JS_TRUE, argc, vp);
  1205. }
  1206. static JSBool
  1207. date_setUTCHours(JSContext *cx, uintN argc, jsval *vp)
  1208. {
  1209. return date_makeTime(cx, 4, JS_FALSE, argc, vp);
  1210. }
  1211. static JSBool
  1212. date_makeDate(JSContext *cx, uintN maxargs, JSBool local, uintN argc, jsval *vp)
  1213. {
  1214. JSObject *obj;
  1215. jsval *argv;
  1216. uintN i;
  1217. jsdouble lorutime; /* local or UTC version of *date */
  1218. jsdouble args[3], *argp, *stop;
  1219. jsdouble year, month, day;
  1220. jsdouble result;
  1221. obj = JS_THIS_OBJECT(cx, vp);
  1222. if (!GetUTCTime(cx, obj, vp, &result))
  1223. return JS_FALSE;
  1224. /* see complaint about ECMA in date_MakeTime */
  1225. if (argc == 0)
  1226. argc = 1; /* should be safe, because length of all setters is 1 */
  1227. else if (argc > maxargs)
  1228. argc = maxargs; /* clamp argc */
  1229. JS_ASSERT(1 <= argc && argc <= 3);
  1230. argv = vp + 2;
  1231. for (i = 0; i < argc; i++) {
  1232. args[i] = js_ValueToNumber(cx, &argv[i]);
  1233. if (JSVAL_IS_NULL(argv[i]))
  1234. return JS_FALSE;
  1235. if (!JSDOUBLE_IS_FINITE(args[i])) {
  1236. if (!SetUTCTimePtr(cx, obj, NULL, cx->runtime->jsNaN))
  1237. return JS_FALSE;
  1238. *vp = DOUBLE_TO_JSVAL(cx->runtime->jsNaN);
  1239. return JS_TRUE;
  1240. }
  1241. args[i] = js_DoubleToInteger(args[i]);
  1242. }
  1243. /* return NaN if date is NaN and we're not setting the year,
  1244. * If we are, use 0 as the time. */
  1245. if (!(JSDOUBLE_IS_FINITE(result))) {
  1246. if (maxargs < 3)
  1247. return js_NewNumberInRootedValue(cx, result, vp);
  1248. lorutime = +0.;
  1249. } else {
  1250. lorutime = local ? LocalTime(result) : result;
  1251. }
  1252. argp = args;
  1253. stop = argp + argc;
  1254. if (maxargs >= 3 && argp < stop)
  1255. year = *argp++;
  1256. else
  1257. year = YearFromTime(lorutime);
  1258. if (maxargs >= 2 && argp < stop)
  1259. month = *argp++;
  1260. else
  1261. month = MonthFromTime(lorutime);
  1262. if (maxargs >= 1 && argp < stop)
  1263. day = *argp++;
  1264. else
  1265. day = DateFromTime(lorutime);
  1266. day = MakeDay(year, month, day); /* day within year */
  1267. result = MakeDate(day, TimeWithinDay(lorutime));
  1268. if (local)
  1269. result = UTC(result);
  1270. result = TIMECLIP(result);
  1271. if (!SetUTCTime(cx, obj, NULL, result))
  1272. return JS_FALSE;
  1273. return js_NewNumberInRootedValue(cx, result, vp);
  1274. }
  1275. static JSBool
  1276. date_setDate(JSContext *cx, uintN argc, jsval *vp)
  1277. {
  1278. return date_makeDate(cx, 1, JS_TRUE, argc, vp);
  1279. }
  1280. static JSBool
  1281. date_setUTCDate(JSContext *cx, uintN argc, jsval *vp)
  1282. {
  1283. return date_makeDate(cx, 1, JS_FALSE, argc, vp);
  1284. }
  1285. static JSBool
  1286. date_setMonth(JSContext *cx, uintN argc, jsval *vp)
  1287. {
  1288. return date_makeDate(cx, 2, JS_TRUE, argc, vp);
  1289. }
  1290. static JSBool
  1291. date_setUTCMonth(JSContext *cx, uintN argc, jsval *vp)
  1292. {
  1293. return date_makeDate(cx, 2, JS_FALSE, argc, vp);
  1294. }
  1295. static JSBool
  1296. date_setFullYear(JSContext *cx, uintN argc, jsval *vp)
  1297. {
  1298. return date_makeDate(cx, 3, JS_TRUE, argc, vp);
  1299. }
  1300. static JSBool
  1301. date_setUTCFullYear(JSContext *cx, uintN argc, jsval *vp)
  1302. {
  1303. return date_makeDate(cx, 3, JS_FALSE, argc, vp);
  1304. }
  1305. static JSBool
  1306. date_setYear(JSContext *cx, uintN argc, jsval *vp)
  1307. {
  1308. JSObject *obj;
  1309. jsdouble t;
  1310. jsdouble year;
  1311. jsdouble day;
  1312. jsdouble result;
  1313. obj = JS_THIS_OBJECT(cx, vp);
  1314. if (!GetUTCTime(cx, obj, vp, &result))
  1315. return JS_FALSE;
  1316. year = js_ValueToNumber(cx, &vp[2]);
  1317. if (JSVAL_IS_NULL(vp[2]))
  1318. return JS_FALSE;
  1319. if (!JSDOUBLE_IS_FINITE(year)) {
  1320. if (!SetUTCTimePtr(cx, obj, NULL, cx->runtime->jsNaN))
  1321. return JS_FALSE;
  1322. return js_NewNumberInRootedValue(cx, *cx->runtime->jsNaN, vp);
  1323. }
  1324. year = js_DoubleToInteger(year);
  1325. if (!JSDOUBLE_IS_FINITE(result)) {
  1326. t = +0.0;
  1327. } else {
  1328. t = LocalTime(result);
  1329. }
  1330. if (year >= 0 && year <= 99)
  1331. year += 1900;
  1332. day = MakeDay(year, MonthFromTime(t), DateFromTime(t));
  1333. result = MakeDate(day, TimeWithinDay(t));
  1334. result = UTC(result);
  1335. result = TIMECLIP(result);
  1336. if (!SetUTCTime(cx, obj, NULL, result))
  1337. return JS_FALSE;
  1338. return js_NewNumberInRootedValue(cx, result, vp);
  1339. }
  1340. /* constants for toString, toUTCString */
  1341. static char js_NaN_date_str[] = "Invalid Date";
  1342. static const char* days[] =
  1343. {
  1344. "Sun","Mon","Tue","Wed","Thu","Fri","Sat"
  1345. };
  1346. static const char* months[] =
  1347. {
  1348. "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  1349. };
  1350. static JSBool
  1351. date_toGMTString(JSContext *cx, uintN argc, jsval *vp)
  1352. {
  1353. char buf[100];
  1354. JSString *str;
  1355. jsdouble utctime;
  1356. if (!GetUTCTime(cx, JS_THIS_OBJECT(cx, vp), vp, &utctime))
  1357. return JS_FALSE;
  1358. if (!JSDOUBLE_IS_FINITE(utctime)) {
  1359. JS_snprintf(buf, sizeof buf, js_NaN_date_str);
  1360. } else {
  1361. /* Avoid dependence on PRMJ_FormatTimeUSEnglish, because it
  1362. * requires a PRMJTime... which only has 16-bit years. Sub-ECMA.
  1363. */
  1364. JS_snprintf(buf, sizeof buf, "%s, %.2d %s %.4d %.2d:%.2d:%.2d GMT",
  1365. days[WeekDay(utctime)],
  1366. DateFromTime(utctime),
  1367. months[MonthFromTime(utctime)],
  1368. YearFromTime(utctime),
  1369. HourFromTime(utctime),
  1370. MinFromTime(utctime),
  1371. SecFromTime(utctime));
  1372. }
  1373. str = JS_NewStringCopyZ(cx, buf);
  1374. if (!str)
  1375. return JS_FALSE;
  1376. *vp = STRING_TO_JSVAL(str);
  1377. return JS_TRUE;
  1378. }
  1379. /* for Date.toLocaleString; interface to PRMJTime date struct.
  1380. */
  1381. static void
  1382. new_explode(jsdouble timeval, PRMJTime *split)
  1383. {
  1384. jsint year = YearFromTime(timeval);
  1385. split->tm_usec = (int32) msFromTime(timeval) * 1000;
  1386. split->tm_sec = (int8) SecFromTime(timeval);
  1387. split->tm_min = (int8) MinFromTime(timeval);
  1388. split->tm_hour = (int8) HourFromTime(timeval);
  1389. split->tm_mday = (int8) DateFromTime(timeval);
  1390. split->tm_mon = (int8) MonthFromTime(timeval);
  1391. split->tm_wday = (int8) WeekDay(timeval);
  1392. split->tm_year = year;
  1393. split->tm_yday = (int16) DayWithinYear(timeval, year);
  1394. /* not sure how this affects things, but it doesn't seem
  1395. to matter. */
  1396. split->tm_isdst = (DaylightSavingTA(timeval) != 0);
  1397. }
  1398. typedef enum formatspec {
  1399. FORMATSPEC_FULL, FORMATSPEC_DATE, FORMATSPEC_TIME
  1400. } formatspec;
  1401. /* helper function */
  1402. static JSBool
  1403. date_format(JSContext *cx, jsdouble date, formatspec format, jsval *rval)
  1404. {
  1405. char buf[100];
  1406. JSString *str;
  1407. char tzbuf[100];
  1408. JSBool usetz;
  1409. size_t i, tzlen;
  1410. PRMJTime split;
  1411. if (!JSDOUBLE_IS_FINITE(date)) {
  1412. JS_snprintf(buf, sizeof buf, js_NaN_date_str);
  1413. } else {
  1414. jsdouble local = LocalTime(date);
  1415. /* offset from GMT in minutes. The offset includes daylight savings,
  1416. if it applies. */
  1417. jsint minutes = (jsint) floor(AdjustTime(date) / msPerMinute);
  1418. /* map 510 minutes to 0830 hours */
  1419. intN offset = (minutes / 60) * 100 + minutes % 60;
  1420. /* print as "Wed Nov 05 19:38:03 GMT-0800 (PST) 1997" The TZA is
  1421. * printed as 'GMT-0800' rather than as 'PST' to avoid
  1422. * operating-system dependence on strftime (which
  1423. * PRMJ_FormatTimeUSEnglish calls, for %Z only.) win32 prints
  1424. * PST as 'Pacific Standard Time.' This way we always know
  1425. * what we're getting, and can parse it if we produce it.
  1426. * The OS TZA string is included as a comment.
  1427. */
  1428. /* get a timezone string from the OS to include as a
  1429. comment. */
  1430. new_explode(date, &split);
  1431. if (PRMJ_FormatTime(tzbuf, sizeof tzbuf, "(%Z)", &split) != 0) {
  1432. /* Decide whether to use the resulting timezone string.
  1433. *
  1434. * Reject it if it contains any non-ASCII, non-alphanumeric
  1435. * characters. It's then likely in some other character
  1436. * encoding, and we probably won't display it correctly.
  1437. */
  1438. usetz = JS_TRUE;
  1439. tzlen = strlen(tzbuf);
  1440. if (tzlen > 100) {
  1441. usetz = JS_FALSE;
  1442. } else {
  1443. for (i = 0; i < tzlen; i++) {
  1444. jschar c = tzbuf[i];
  1445. if (c > 127 ||
  1446. !(isalpha(c) || isdigit(c) ||
  1447. c == ' ' || c == '(' || c == ')')) {
  1448. usetz = JS_FALSE;
  1449. }
  1450. }
  1451. }
  1452. /* Also reject it if it's not parenthesized or if it's '()'. */
  1453. if (tzbuf[0] != '(' || tzbuf[1] == ')')
  1454. usetz = JS_FALSE;
  1455. } else
  1456. usetz = JS_FALSE;
  1457. switch (format) {
  1458. case FORMATSPEC_FULL:
  1459. /*
  1460. * Avoid dependence on PRMJ_FormatTimeUSEnglish, because it
  1461. * requires a PRMJTime... which only has 16-bit years. Sub-ECMA.
  1462. */
  1463. /* Tue Oct 31 2000 09:41:40 GMT-0800 (PST) */
  1464. JS_snprintf(buf, sizeof buf,
  1465. "%s %s %.2d %.4d %.2d:%.2d:%.2d GMT%+.4d%s%s",
  1466. days[WeekDay(local)],
  1467. months[MonthFromTime(local)],
  1468. DateFromTime(local),
  1469. YearFromTime(local),
  1470. HourFromTime(local),
  1471. MinFromTime(local),
  1472. SecFromTime(local),
  1473. offset,
  1474. usetz ? " " : "",
  1475. usetz ? tzbuf : "");
  1476. break;
  1477. case FORMATSPEC_DATE:
  1478. /* Tue Oct 31 2000 */
  1479. JS_snprintf(buf, sizeof buf,
  1480. "%s %s %.2d %.4d",
  1481. days[WeekDay(local)],
  1482. months[MonthFromTime(local)],
  1483. DateFromTime(local),
  1484. YearFromTime(local));
  1485. break;
  1486. case FORMATSPEC_TIME:
  1487. /* 09:41:40 GMT-0800 (PST) */
  1488. JS_snprintf(buf, sizeof buf,
  1489. "%.2d:%.2d:%.2d GMT%+.4d%s%s",
  1490. HourFromTime(local),
  1491. MinFromTime(local),
  1492. SecFromTime(local),
  1493. offset,
  1494. usetz ? " " : "",
  1495. usetz ? tzbuf : "");
  1496. break;
  1497. }
  1498. }
  1499. str = JS_NewStringCopyZ(cx, buf);
  1500. if (!str)
  1501. return JS_FALSE;
  1502. *rval = STRING_TO_JSVAL(str);
  1503. return JS_TRUE;
  1504. }
  1505. static JSBool
  1506. date_toLocaleHelper(JSContext *cx, const char *format, jsval *vp)
  1507. {
  1508. JSObject *obj;
  1509. char buf[100];
  1510. JSString *str;
  1511. PRMJTime split;
  1512. jsdouble utctime;
  1513. obj = JS_THIS_OBJECT(cx, vp);
  1514. if (!GetUTCTime(cx, obj, vp, &utctime))
  1515. return JS_FALSE;
  1516. if (!JSDOUBLE_IS_FINITE(utctime)) {
  1517. JS_snprintf(buf, sizeof buf, js_NaN_date_str);
  1518. } else {
  1519. intN result_len;
  1520. jsdouble local = LocalTime(utctime);
  1521. new_explode(local, &split);
  1522. /* let PRMJTime format it. */
  1523. result_len = PRMJ_FormatTime(buf, sizeof buf, format, &split);
  1524. /* If it failed, default to toString. */
  1525. if (result_len == 0)
  1526. return date_format(cx, utctime, FORMATSPEC_FULL, vp);
  1527. /* Hacked check against undesired 2-digit year 00/00/00…

Large files files are truncated, but you can click here to view the full file