2009-06-10 18:29:44 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
2013-04-16 13:47:10 -07:00
|
|
|
* vim: set ts=8 sts=4 et sw=4 tw=99:
|
2012-05-21 04:12:37 -07:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* JS date methods.
|
2013-08-13 15:34:12 -07:00
|
|
|
*
|
2007-03-22 10:30:00 -07:00
|
|
|
* "For example, OS/360 devotes 26 bytes of the permanently
|
|
|
|
* resident date-turnover routine to the proper handling of
|
|
|
|
* December 31 on leap years (when it is Day 366). That
|
|
|
|
* might have been left to the operator."
|
|
|
|
*
|
|
|
|
* Frederick Brooks, 'The Second-System Effect'.
|
|
|
|
*/
|
|
|
|
|
2013-08-13 15:34:12 -07:00
|
|
|
#include "jsdate.h"
|
|
|
|
|
2013-12-08 18:52:54 -08:00
|
|
|
#include "mozilla/ArrayUtils.h"
|
2013-08-13 15:34:12 -07:00
|
|
|
#include "mozilla/FloatingPoint.h"
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <string.h>
|
2011-10-10 23:00:28 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "jsapi.h"
|
|
|
|
#include "jscntxt.h"
|
|
|
|
#include "jsnum.h"
|
|
|
|
#include "jsobj.h"
|
2013-07-23 17:34:18 -07:00
|
|
|
#include "jsprf.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "jsstr.h"
|
2013-07-23 17:34:18 -07:00
|
|
|
#include "jstypes.h"
|
|
|
|
#include "jsutil.h"
|
2014-08-18 14:18:38 -07:00
|
|
|
#include "jswrapper.h"
|
2013-07-23 17:34:18 -07:00
|
|
|
#include "prmjtime.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-12-17 13:50:14 -08:00
|
|
|
#include "js/Conversions.h"
|
2013-08-13 15:34:12 -07:00
|
|
|
#include "js/Date.h"
|
2012-11-27 15:12:20 -08:00
|
|
|
#include "vm/DateTime.h"
|
2011-05-03 12:18:36 -07:00
|
|
|
#include "vm/GlobalObject.h"
|
2013-06-02 15:30:43 -07:00
|
|
|
#include "vm/Interpreter.h"
|
2013-01-11 00:43:00 -08:00
|
|
|
#include "vm/String.h"
|
2012-03-14 15:29:29 -07:00
|
|
|
#include "vm/StringBuffer.h"
|
2011-05-03 12:18:36 -07:00
|
|
|
|
2010-04-10 16:16:35 -07:00
|
|
|
#include "jsobjinlines.h"
|
|
|
|
|
2010-04-07 13:18:50 -07:00
|
|
|
using namespace js;
|
|
|
|
|
2012-11-06 17:35:18 -08:00
|
|
|
using mozilla::ArrayLength;
|
2013-05-01 13:55:13 -07:00
|
|
|
using mozilla::IsFinite;
|
|
|
|
using mozilla::IsNaN;
|
2014-06-10 02:23:39 -07:00
|
|
|
|
|
|
|
using JS::AutoCheckCannotGC;
|
2013-09-19 18:42:56 -07:00
|
|
|
using JS::GenericNaN;
|
2014-12-17 13:50:14 -08:00
|
|
|
using JS::ToInteger;
|
2012-11-06 17:35:18 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/*
|
|
|
|
* The JS 'Date' object is patterned after the Java 'Date' object.
|
2013-03-21 16:38:25 -07:00
|
|
|
* Here is a script:
|
2007-03-22 10:30:00 -07:00
|
|
|
*
|
|
|
|
* today = new Date();
|
|
|
|
*
|
|
|
|
* print(today.toLocaleString());
|
|
|
|
*
|
|
|
|
* weekDay = today.getDay();
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* These Java (and ECMA-262) methods are supported:
|
|
|
|
*
|
|
|
|
* UTC
|
|
|
|
* getDate (getUTCDate)
|
|
|
|
* getDay (getUTCDay)
|
|
|
|
* getHours (getUTCHours)
|
|
|
|
* getMinutes (getUTCMinutes)
|
|
|
|
* getMonth (getUTCMonth)
|
|
|
|
* getSeconds (getUTCSeconds)
|
|
|
|
* getMilliseconds (getUTCMilliseconds)
|
|
|
|
* getTime
|
|
|
|
* getTimezoneOffset
|
|
|
|
* getYear
|
|
|
|
* getFullYear (getUTCFullYear)
|
|
|
|
* parse
|
|
|
|
* setDate (setUTCDate)
|
|
|
|
* setHours (setUTCHours)
|
|
|
|
* setMinutes (setUTCMinutes)
|
|
|
|
* setMonth (setUTCMonth)
|
|
|
|
* setSeconds (setUTCSeconds)
|
|
|
|
* setMilliseconds (setUTCMilliseconds)
|
|
|
|
* setTime
|
|
|
|
* setYear (setFullYear, setUTCFullYear)
|
|
|
|
* toGMTString (toUTCString)
|
|
|
|
* toLocaleString
|
|
|
|
* toString
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* These Java methods are not supported
|
|
|
|
*
|
|
|
|
* setDay
|
|
|
|
* before
|
|
|
|
* after
|
|
|
|
* equals
|
|
|
|
* hashCode
|
|
|
|
*/
|
|
|
|
|
2013-10-10 05:18:01 -07:00
|
|
|
static inline double
|
2012-07-06 13:52:53 -07:00
|
|
|
Day(double t)
|
|
|
|
{
|
|
|
|
return floor(t / msPerDay);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-02-24 14:19:52 -08:00
|
|
|
static double
|
|
|
|
TimeWithinDay(double t)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-07-06 13:52:53 -07:00
|
|
|
double result = fmod(t, msPerDay);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (result < 0)
|
|
|
|
result += msPerDay;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-07-06 13:52:53 -07:00
|
|
|
/* ES5 15.9.1.3. */
|
2013-10-10 05:18:01 -07:00
|
|
|
static inline bool
|
2012-07-09 14:28:20 -07:00
|
|
|
IsLeapYear(double year)
|
2010-08-18 19:44:28 -07:00
|
|
|
{
|
2014-10-01 10:17:51 -07:00
|
|
|
MOZ_ASSERT(ToInteger(year) == year);
|
2012-07-09 14:28:20 -07:00
|
|
|
return fmod(year, 4) == 0 && (fmod(year, 100) != 0 || fmod(year, 400) == 0);
|
2010-08-18 19:44:28 -07:00
|
|
|
}
|
|
|
|
|
2013-10-10 05:18:01 -07:00
|
|
|
static inline double
|
2012-07-09 14:28:20 -07:00
|
|
|
DaysInYear(double year)
|
2010-08-18 19:44:28 -07:00
|
|
|
{
|
2013-05-01 13:55:13 -07:00
|
|
|
if (!IsFinite(year))
|
2013-09-19 18:42:56 -07:00
|
|
|
return GenericNaN();
|
2010-08-18 19:44:28 -07:00
|
|
|
return IsLeapYear(year) ? 366 : 365;
|
|
|
|
}
|
|
|
|
|
2013-10-10 05:18:01 -07:00
|
|
|
static inline double
|
2012-07-09 14:28:20 -07:00
|
|
|
DayFromYear(double y)
|
2010-08-18 19:44:28 -07:00
|
|
|
{
|
2012-07-06 13:52:53 -07:00
|
|
|
return 365 * (y - 1970) +
|
|
|
|
floor((y - 1969) / 4.0) -
|
|
|
|
floor((y - 1901) / 100.0) +
|
|
|
|
floor((y - 1601) / 400.0);
|
2010-08-18 19:44:28 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-10-10 05:18:01 -07:00
|
|
|
static inline double
|
2012-07-09 14:28:20 -07:00
|
|
|
TimeFromYear(double y)
|
2012-07-06 13:52:53 -07:00
|
|
|
{
|
|
|
|
return DayFromYear(y) * msPerDay;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-07-09 14:28:20 -07:00
|
|
|
static double
|
2012-02-24 14:19:52 -08:00
|
|
|
YearFromTime(double t)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-05-01 13:55:13 -07:00
|
|
|
if (!IsFinite(t))
|
2013-09-19 18:42:56 -07:00
|
|
|
return GenericNaN();
|
2012-07-09 14:28:20 -07:00
|
|
|
|
2014-10-01 10:17:51 -07:00
|
|
|
MOZ_ASSERT(ToInteger(t) == t);
|
2012-07-09 14:28:20 -07:00
|
|
|
|
|
|
|
double y = floor(t / (msPerDay * 365.2425)) + 1970;
|
2012-07-06 13:52:53 -07:00
|
|
|
double t2 = TimeFromYear(y);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-08-17 10:42:57 -07:00
|
|
|
/*
|
|
|
|
* Adjust the year if the approximation was wrong. Since the year was
|
|
|
|
* computed using the average number of ms per year, it will usually
|
|
|
|
* be wrong for dates within several hours of a year transition.
|
|
|
|
*/
|
2007-03-22 10:30:00 -07:00
|
|
|
if (t2 > t) {
|
|
|
|
y--;
|
|
|
|
} else {
|
|
|
|
if (t2 + msPerDay * DaysInYear(y) <= t)
|
|
|
|
y++;
|
|
|
|
}
|
|
|
|
return y;
|
|
|
|
}
|
|
|
|
|
2013-10-10 05:18:01 -07:00
|
|
|
static inline int
|
2012-12-04 04:24:45 -08:00
|
|
|
DaysInFebruary(double year)
|
2012-07-06 13:52:53 -07:00
|
|
|
{
|
|
|
|
return IsLeapYear(year) ? 29 : 28;
|
|
|
|
}
|
2009-08-26 18:13:48 -07:00
|
|
|
|
2012-07-06 13:52:53 -07:00
|
|
|
/* ES5 15.9.1.4. */
|
2013-10-10 05:18:01 -07:00
|
|
|
static inline double
|
2012-07-09 14:28:20 -07:00
|
|
|
DayWithinYear(double t, double year)
|
2009-08-26 18:13:48 -07:00
|
|
|
{
|
2014-10-01 10:17:53 -07:00
|
|
|
MOZ_ASSERT_IF(IsFinite(t), YearFromTime(t) == year);
|
2012-07-09 14:28:20 -07:00
|
|
|
return Day(t) - DayFromYear(year);
|
2009-08-26 18:13:48 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-07-09 14:28:20 -07:00
|
|
|
static double
|
2012-02-24 14:19:52 -08:00
|
|
|
MonthFromTime(double t)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-05-01 13:55:13 -07:00
|
|
|
if (!IsFinite(t))
|
2013-09-19 18:42:56 -07:00
|
|
|
return GenericNaN();
|
2012-07-09 14:28:20 -07:00
|
|
|
|
|
|
|
double year = YearFromTime(t);
|
|
|
|
double d = DayWithinYear(t, year);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-07-06 13:52:53 -07:00
|
|
|
int step;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (d < (step = 31))
|
|
|
|
return 0;
|
2010-08-18 19:44:28 -07:00
|
|
|
if (d < (step += DaysInFebruary(year)))
|
2007-03-22 10:30:00 -07:00
|
|
|
return 1;
|
|
|
|
if (d < (step += 31))
|
|
|
|
return 2;
|
|
|
|
if (d < (step += 30))
|
|
|
|
return 3;
|
|
|
|
if (d < (step += 31))
|
|
|
|
return 4;
|
|
|
|
if (d < (step += 30))
|
|
|
|
return 5;
|
|
|
|
if (d < (step += 31))
|
|
|
|
return 6;
|
|
|
|
if (d < (step += 31))
|
|
|
|
return 7;
|
|
|
|
if (d < (step += 30))
|
|
|
|
return 8;
|
|
|
|
if (d < (step += 31))
|
|
|
|
return 9;
|
|
|
|
if (d < (step += 30))
|
|
|
|
return 10;
|
|
|
|
return 11;
|
|
|
|
}
|
|
|
|
|
2012-07-06 13:52:53 -07:00
|
|
|
/* ES5 15.9.1.5. */
|
2012-07-09 14:28:20 -07:00
|
|
|
static double
|
2012-02-24 14:19:52 -08:00
|
|
|
DateFromTime(double t)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-05-01 13:55:13 -07:00
|
|
|
if (!IsFinite(t))
|
2013-09-19 18:42:56 -07:00
|
|
|
return GenericNaN();
|
2012-12-04 04:24:45 -08:00
|
|
|
|
2012-07-09 14:28:20 -07:00
|
|
|
double year = YearFromTime(t);
|
|
|
|
double d = DayWithinYear(t, year);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-07-06 13:52:53 -07:00
|
|
|
int next;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (d <= (next = 30))
|
|
|
|
return d + 1;
|
2012-07-06 13:52:53 -07:00
|
|
|
int step = next;
|
2010-08-18 19:44:28 -07:00
|
|
|
if (d <= (next += DaysInFebruary(year)))
|
2007-03-22 10:30:00 -07:00
|
|
|
return d - step;
|
|
|
|
step = next;
|
|
|
|
if (d <= (next += 31))
|
|
|
|
return d - step;
|
|
|
|
step = next;
|
|
|
|
if (d <= (next += 30))
|
|
|
|
return d - step;
|
|
|
|
step = next;
|
|
|
|
if (d <= (next += 31))
|
|
|
|
return d - step;
|
|
|
|
step = next;
|
|
|
|
if (d <= (next += 30))
|
|
|
|
return d - step;
|
|
|
|
step = next;
|
|
|
|
if (d <= (next += 31))
|
|
|
|
return d - step;
|
|
|
|
step = next;
|
|
|
|
if (d <= (next += 31))
|
|
|
|
return d - step;
|
|
|
|
step = next;
|
|
|
|
if (d <= (next += 30))
|
|
|
|
return d - step;
|
|
|
|
step = next;
|
|
|
|
if (d <= (next += 31))
|
|
|
|
return d - step;
|
|
|
|
step = next;
|
|
|
|
if (d <= (next += 30))
|
|
|
|
return d - step;
|
|
|
|
step = next;
|
|
|
|
return d - step;
|
|
|
|
}
|
|
|
|
|
2012-07-06 13:52:53 -07:00
|
|
|
/* ES5 15.9.1.6. */
|
2012-02-28 15:11:11 -08:00
|
|
|
static int
|
2012-02-24 14:19:52 -08:00
|
|
|
WeekDay(double t)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-07-09 14:28:20 -07:00
|
|
|
/*
|
|
|
|
* We can't assert TimeClip(t) == t because we call this function with
|
|
|
|
* local times, which can be offset outside TimeClip's permitted range.
|
|
|
|
*/
|
2014-10-01 10:17:51 -07:00
|
|
|
MOZ_ASSERT(ToInteger(t) == t);
|
2012-07-06 13:52:53 -07:00
|
|
|
int result = (int(Day(t)) + 4) % 7;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (result < 0)
|
|
|
|
result += 7;
|
2012-07-06 13:52:53 -07:00
|
|
|
return result;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-10-10 05:18:01 -07:00
|
|
|
static inline int
|
2012-07-06 13:52:53 -07:00
|
|
|
DayFromMonth(int month, bool isLeapYear)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* The following array contains the day of year for the first day of
|
|
|
|
* each month, where index 0 is January, and day 0 is January 1.
|
|
|
|
*/
|
|
|
|
static const int firstDayOfMonth[2][13] = {
|
|
|
|
{0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365},
|
|
|
|
{0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366}
|
|
|
|
};
|
|
|
|
|
2014-10-01 10:17:51 -07:00
|
|
|
MOZ_ASSERT(0 <= month && month <= 12);
|
2012-07-06 13:52:53 -07:00
|
|
|
return firstDayOfMonth[isLeapYear][month];
|
|
|
|
}
|
|
|
|
|
2012-07-09 14:28:20 -07:00
|
|
|
template<typename T>
|
2013-10-10 05:18:01 -07:00
|
|
|
static inline int
|
2015-01-06 15:35:02 -08:00
|
|
|
DayFromMonth(T month, bool isLeapYear) = delete;
|
2012-07-09 14:28:20 -07:00
|
|
|
|
2012-07-06 13:52:53 -07:00
|
|
|
/* ES5 15.9.1.12 (out of order to accommodate DaylightSavingTA). */
|
2012-02-24 14:19:52 -08:00
|
|
|
static double
|
|
|
|
MakeDay(double year, double month, double date)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-07-09 14:28:20 -07:00
|
|
|
/* Step 1. */
|
2013-05-01 13:55:13 -07:00
|
|
|
if (!IsFinite(year) || !IsFinite(month) || !IsFinite(date))
|
2013-09-19 18:42:56 -07:00
|
|
|
return GenericNaN();
|
2012-07-06 13:52:53 -07:00
|
|
|
|
2012-07-09 14:28:20 -07:00
|
|
|
/* Steps 2-4. */
|
|
|
|
double y = ToInteger(year);
|
|
|
|
double m = ToInteger(month);
|
|
|
|
double dt = ToInteger(date);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-07-09 14:28:20 -07:00
|
|
|
/* Step 5. */
|
|
|
|
double ym = y + floor(m / 12);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-07-09 14:28:20 -07:00
|
|
|
/* Step 6. */
|
|
|
|
int mn = int(fmod(m, 12.0));
|
|
|
|
if (mn < 0)
|
|
|
|
mn += 12;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-07-09 14:28:20 -07:00
|
|
|
/* Steps 7-8. */
|
|
|
|
bool leap = IsLeapYear(ym);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-07-09 14:28:20 -07:00
|
|
|
double yearday = floor(TimeFromYear(ym) / msPerDay);
|
|
|
|
double monthday = DayFromMonth(mn, leap);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-07-09 14:28:20 -07:00
|
|
|
return yearday + monthday + dt - 1;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-07-06 13:52:53 -07:00
|
|
|
/* ES5 15.9.1.13 (out of order to accommodate DaylightSavingTA). */
|
2013-10-10 05:18:01 -07:00
|
|
|
static inline double
|
2012-07-06 13:52:53 -07:00
|
|
|
MakeDate(double day, double time)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-07-06 13:52:53 -07:00
|
|
|
/* Step 1. */
|
2013-05-01 13:55:13 -07:00
|
|
|
if (!IsFinite(day) || !IsFinite(time))
|
2013-09-19 18:42:56 -07:00
|
|
|
return GenericNaN();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-07-06 13:52:53 -07:00
|
|
|
/* Step 2. */
|
|
|
|
return day * msPerDay + time;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-05-01 20:44:11 -07:00
|
|
|
JS_PUBLIC_API(double)
|
|
|
|
JS::MakeDate(double year, unsigned month, unsigned day)
|
|
|
|
{
|
|
|
|
return TimeClip(::MakeDate(MakeDay(year, month, day), 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
JS_PUBLIC_API(double)
|
|
|
|
JS::YearFromTime(double time)
|
|
|
|
{
|
|
|
|
return ::YearFromTime(time);
|
|
|
|
}
|
|
|
|
|
|
|
|
JS_PUBLIC_API(double)
|
|
|
|
JS::MonthFromTime(double time)
|
|
|
|
{
|
|
|
|
return ::MonthFromTime(time);
|
|
|
|
}
|
|
|
|
|
|
|
|
JS_PUBLIC_API(double)
|
|
|
|
JS::DayFromTime(double time)
|
|
|
|
{
|
|
|
|
return DateFromTime(time);
|
|
|
|
}
|
|
|
|
|
2012-07-09 14:28:20 -07:00
|
|
|
/*
|
|
|
|
* Find a year for which any given date will fall on the same weekday.
|
|
|
|
*
|
|
|
|
* This function should be used with caution when used other than
|
|
|
|
* for determining DST; it hasn't been proven not to produce an
|
|
|
|
* incorrect year for times near year boundaries.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
EquivalentYearForDST(int year)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Years and leap years on which Jan 1 is a Sunday, Monday, etc.
|
|
|
|
*
|
|
|
|
* yearStartingWith[0][i] is an example non-leap year where
|
|
|
|
* Jan 1 appears on Sunday (i == 0), Monday (i == 1), etc.
|
|
|
|
*
|
|
|
|
* yearStartingWith[1][i] is an example leap year where
|
|
|
|
* Jan 1 appears on Sunday (i == 0), Monday (i == 1), etc.
|
|
|
|
*/
|
|
|
|
static const int yearStartingWith[2][7] = {
|
|
|
|
{1978, 1973, 1974, 1975, 1981, 1971, 1977},
|
|
|
|
{1984, 1996, 1980, 1992, 1976, 1988, 1972}
|
|
|
|
};
|
|
|
|
|
|
|
|
int day = int(DayFromYear(year) + 4) % 7;
|
|
|
|
if (day < 0)
|
|
|
|
day += 7;
|
|
|
|
|
|
|
|
return yearStartingWith[IsLeapYear(year)][day];
|
|
|
|
}
|
|
|
|
|
2012-07-06 13:52:53 -07:00
|
|
|
/* ES5 15.9.1.8. */
|
2012-02-24 14:19:52 -08:00
|
|
|
static double
|
2012-11-27 15:12:20 -08:00
|
|
|
DaylightSavingTA(double t, DateTimeInfo *dtInfo)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-05-01 13:55:13 -07:00
|
|
|
if (!IsFinite(t))
|
2013-09-19 18:42:56 -07:00
|
|
|
return GenericNaN();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If earlier than 1970 or after 2038, potentially beyond the ken of
|
|
|
|
* many OSes, map it to an equivalent year before asking.
|
|
|
|
*/
|
|
|
|
if (t < 0.0 || t > 2145916800000.0) {
|
2012-07-09 14:28:20 -07:00
|
|
|
int year = EquivalentYearForDST(int(YearFromTime(t)));
|
2012-02-24 14:19:52 -08:00
|
|
|
double day = MakeDay(year, MonthFromTime(t), DateFromTime(t));
|
2007-03-22 10:30:00 -07:00
|
|
|
t = MakeDate(day, TimeWithinDay(t));
|
|
|
|
}
|
|
|
|
|
2013-01-25 16:50:24 -08:00
|
|
|
int64_t utcMilliseconds = static_cast<int64_t>(t);
|
|
|
|
int64_t offsetMilliseconds = dtInfo->getDSTOffsetMilliseconds(utcMilliseconds);
|
2012-02-24 14:19:52 -08:00
|
|
|
return static_cast<double>(offsetMilliseconds);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-02-24 14:19:52 -08:00
|
|
|
static double
|
2012-11-27 15:12:20 -08:00
|
|
|
AdjustTime(double date, DateTimeInfo *dtInfo)
|
2009-01-06 09:18:35 -08:00
|
|
|
{
|
2012-11-27 15:12:20 -08:00
|
|
|
double t = DaylightSavingTA(date, dtInfo) + dtInfo->localTZA();
|
|
|
|
t = (dtInfo->localTZA() >= 0) ? fmod(t, msPerDay) : -fmod(msPerDay - t, msPerDay);
|
2009-01-06 09:18:35 -08:00
|
|
|
return t;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-07-06 13:52:53 -07:00
|
|
|
/* ES5 15.9.1.9. */
|
2012-02-24 14:19:52 -08:00
|
|
|
static double
|
2012-11-27 15:12:20 -08:00
|
|
|
LocalTime(double t, DateTimeInfo *dtInfo)
|
2010-05-26 17:00:28 -07:00
|
|
|
{
|
2012-11-27 15:12:20 -08:00
|
|
|
return t + AdjustTime(t, dtInfo);
|
2010-05-26 17:00:28 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-02-24 14:19:52 -08:00
|
|
|
static double
|
2012-11-27 15:12:20 -08:00
|
|
|
UTC(double t, DateTimeInfo *dtInfo)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-11-27 15:12:20 -08:00
|
|
|
return t - AdjustTime(t - dtInfo->localTZA(), dtInfo);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-07-06 13:52:53 -07:00
|
|
|
/* ES5 15.9.1.10. */
|
2012-07-09 14:28:20 -07:00
|
|
|
static double
|
2012-02-24 14:19:52 -08:00
|
|
|
HourFromTime(double t)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-07-09 14:28:20 -07:00
|
|
|
double result = fmod(floor(t/msPerHour), HoursPerDay);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (result < 0)
|
2012-07-09 14:28:20 -07:00
|
|
|
result += HoursPerDay;
|
2007-03-22 10:30:00 -07:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-07-09 14:28:20 -07:00
|
|
|
static double
|
2012-02-24 14:19:52 -08:00
|
|
|
MinFromTime(double t)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-07-09 14:28:20 -07:00
|
|
|
double result = fmod(floor(t / msPerMinute), MinutesPerHour);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (result < 0)
|
2012-07-09 14:28:20 -07:00
|
|
|
result += MinutesPerHour;
|
2007-03-22 10:30:00 -07:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-07-09 14:28:20 -07:00
|
|
|
static double
|
2012-02-24 14:19:52 -08:00
|
|
|
SecFromTime(double t)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-07-09 14:28:20 -07:00
|
|
|
double result = fmod(floor(t / msPerSecond), SecondsPerMinute);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (result < 0)
|
2012-07-09 14:28:20 -07:00
|
|
|
result += SecondsPerMinute;
|
2007-03-22 10:30:00 -07:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-07-09 14:28:20 -07:00
|
|
|
static double
|
2012-02-24 14:19:52 -08:00
|
|
|
msFromTime(double t)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-07-09 14:28:20 -07:00
|
|
|
double result = fmod(t, msPerSecond);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (result < 0)
|
2012-07-09 14:28:20 -07:00
|
|
|
result += msPerSecond;
|
2012-07-06 13:52:53 -07:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ES5 15.9.1.11. */
|
|
|
|
static double
|
|
|
|
MakeTime(double hour, double min, double sec, double ms)
|
|
|
|
{
|
|
|
|
/* Step 1. */
|
2013-05-01 13:55:13 -07:00
|
|
|
if (!IsFinite(hour) ||
|
|
|
|
!IsFinite(min) ||
|
|
|
|
!IsFinite(sec) ||
|
|
|
|
!IsFinite(ms))
|
2012-07-06 13:52:53 -07:00
|
|
|
{
|
2013-09-19 18:42:56 -07:00
|
|
|
return GenericNaN();
|
2012-07-06 13:52:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Step 2. */
|
|
|
|
double h = ToInteger(hour);
|
|
|
|
|
|
|
|
/* Step 3. */
|
|
|
|
double m = ToInteger(min);
|
|
|
|
|
|
|
|
/* Step 4. */
|
|
|
|
double s = ToInteger(sec);
|
|
|
|
|
|
|
|
/* Step 5. */
|
|
|
|
double milli = ToInteger(ms);
|
|
|
|
|
|
|
|
/* Steps 6-7. */
|
|
|
|
return h * msPerHour + m * msPerMinute + s * msPerSecond + milli;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/**
|
|
|
|
* end of ECMA 'support' functions
|
|
|
|
*/
|
2008-08-08 09:02:50 -07:00
|
|
|
|
2013-08-08 15:53:04 -07:00
|
|
|
static bool
|
2012-07-30 04:19:09 -07:00
|
|
|
date_convert(JSContext *cx, HandleObject obj, JSType hint, MutableHandleValue vp)
|
2011-04-01 15:24:21 -07:00
|
|
|
{
|
2014-10-01 10:17:51 -07:00
|
|
|
MOZ_ASSERT(hint == JSTYPE_NUMBER || hint == JSTYPE_STRING || hint == JSTYPE_VOID);
|
|
|
|
MOZ_ASSERT(obj->is<DateObject>());
|
2011-04-01 15:24:21 -07:00
|
|
|
|
2014-12-18 03:02:30 -08:00
|
|
|
return JS::OrdinaryToPrimitive(cx, obj, hint == JSTYPE_VOID ? JSTYPE_STRING : hint, vp);
|
2011-04-01 15:24:21 -07:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/* for use by date_parse */
|
|
|
|
|
2013-06-17 18:31:47 -07:00
|
|
|
static const char* const wtb[] = {
|
2007-03-22 10:30:00 -07:00
|
|
|
"am", "pm",
|
|
|
|
"monday", "tuesday", "wednesday", "thursday", "friday",
|
|
|
|
"saturday", "sunday",
|
|
|
|
"january", "february", "march", "april", "may", "june",
|
|
|
|
"july", "august", "september", "october", "november", "december",
|
|
|
|
"gmt", "ut", "utc",
|
|
|
|
"est", "edt",
|
|
|
|
"cst", "cdt",
|
|
|
|
"mst", "mdt",
|
|
|
|
"pst", "pdt"
|
|
|
|
/* time zone table needs to be expanded */
|
|
|
|
};
|
|
|
|
|
2013-06-17 18:31:47 -07:00
|
|
|
static const int ttb[] = {
|
2007-03-22 10:30:00 -07:00
|
|
|
-1, -2, 0, 0, 0, 0, 0, 0, 0, /* AM/PM */
|
|
|
|
2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
|
|
|
|
10000 + 0, 10000 + 0, 10000 + 0, /* GMT/UT/UTC */
|
|
|
|
10000 + 5 * 60, 10000 + 4 * 60, /* EST/EDT */
|
|
|
|
10000 + 6 * 60, 10000 + 5 * 60, /* CST/CDT */
|
|
|
|
10000 + 7 * 60, 10000 + 6 * 60, /* MST/MDT */
|
|
|
|
10000 + 8 * 60, 10000 + 7 * 60 /* PST/PDT */
|
|
|
|
};
|
|
|
|
|
2014-06-10 02:23:39 -07:00
|
|
|
template <typename CharT>
|
2013-08-08 15:53:04 -07:00
|
|
|
static bool
|
2014-06-10 02:23:39 -07:00
|
|
|
RegionMatches(const char *s1, int s1off, const CharT *s2, int s2off, int count)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
while (count > 0 && s1[s1off] && s2[s2off]) {
|
2014-06-10 02:23:39 -07:00
|
|
|
if (unicode::ToLowerCase(s1[s1off]) != unicode::ToLowerCase(s2[s2off]))
|
|
|
|
break;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
s1off++;
|
|
|
|
s2off++;
|
|
|
|
count--;
|
|
|
|
}
|
|
|
|
|
2014-06-10 02:23:39 -07:00
|
|
|
return count == 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* find UTC time from given date... no 1900 correction! */
|
2012-02-24 14:19:52 -08:00
|
|
|
static double
|
|
|
|
date_msecFromDate(double year, double mon, double mday, double hour,
|
|
|
|
double min, double sec, double msec)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-07-09 14:28:20 -07:00
|
|
|
return MakeDate(MakeDay(year, mon, mday), MakeTime(hour, min, sec, msec));
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2007-08-13 13:45:32 -07:00
|
|
|
|
|
|
|
/* compute the time in msec (unclipped) from the given args */
|
2007-03-22 10:30:00 -07:00
|
|
|
#define MAXARGS 7
|
2007-08-01 21:33:52 -07:00
|
|
|
|
2013-08-08 15:53:04 -07:00
|
|
|
static bool
|
2011-09-08 21:02:26 -07:00
|
|
|
date_msecFromArgs(JSContext *cx, CallArgs args, double *rval)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-02-28 15:11:11 -08:00
|
|
|
unsigned loop;
|
2012-02-24 14:19:52 -08:00
|
|
|
double array[MAXARGS];
|
|
|
|
double msec_time;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
for (loop = 0; loop < MAXARGS; loop++) {
|
2011-09-08 21:02:26 -07:00
|
|
|
if (loop < args.length()) {
|
2012-02-24 14:19:52 -08:00
|
|
|
double d;
|
2013-06-26 16:26:45 -07:00
|
|
|
if (!ToNumber(cx, args[loop], &d))
|
2013-08-06 23:59:54 -07:00
|
|
|
return false;
|
2007-08-13 13:45:32 -07:00
|
|
|
/* return NaN if any arg is not finite */
|
2013-05-01 13:55:13 -07:00
|
|
|
if (!IsFinite(d)) {
|
2013-09-19 18:42:56 -07:00
|
|
|
*rval = GenericNaN();
|
2013-08-06 23:59:54 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2012-04-19 16:18:24 -07:00
|
|
|
array[loop] = ToInteger(d);
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
2007-08-13 13:45:32 -07:00
|
|
|
if (loop == 2) {
|
|
|
|
array[loop] = 1; /* Default the date argument to 1. */
|
|
|
|
} else {
|
|
|
|
array[loop] = 0;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* adjust 2-digit years into the 20th century */
|
|
|
|
if (array[0] >= 0 && array[0] <= 99)
|
|
|
|
array[0] += 1900;
|
|
|
|
|
2007-08-13 13:45:32 -07:00
|
|
|
msec_time = date_msecFromDate(array[0], array[1], array[2],
|
|
|
|
array[3], array[4], array[5], array[6]);
|
|
|
|
*rval = msec_time;
|
2013-08-06 23:59:54 -07:00
|
|
|
return true;
|
2007-08-13 13:45:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* See ECMA 15.9.4.[3-10];
|
|
|
|
*/
|
2013-08-02 00:41:57 -07:00
|
|
|
static bool
|
2012-02-28 15:11:11 -08:00
|
|
|
date_UTC(JSContext *cx, unsigned argc, Value *vp)
|
2007-08-13 13:45:32 -07:00
|
|
|
{
|
2011-09-08 21:02:26 -07:00
|
|
|
CallArgs args = CallArgsFromVp(argc, vp);
|
2007-08-13 13:45:32 -07:00
|
|
|
|
2012-02-24 14:19:52 -08:00
|
|
|
double msec_time;
|
2011-09-08 21:02:26 -07:00
|
|
|
if (!date_msecFromArgs(cx, args, &msec_time))
|
2013-08-02 00:41:57 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-04-19 16:22:08 -07:00
|
|
|
msec_time = TimeClip(msec_time);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-09-08 21:02:26 -07:00
|
|
|
args.rval().setNumber(msec_time);
|
2013-08-02 00:41:57 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2009-10-18 11:23:27 -07:00
|
|
|
/*
|
2009-08-26 18:13:48 -07:00
|
|
|
* Read and convert decimal digits from s[*i] into *result
|
2011-04-11 01:38:27 -07:00
|
|
|
* while *i < limit.
|
|
|
|
*
|
2009-08-26 18:13:48 -07:00
|
|
|
* Succeed if any digits are converted. Advance *i only
|
|
|
|
* as digits are consumed.
|
|
|
|
*/
|
2014-06-10 02:23:39 -07:00
|
|
|
template <typename CharT>
|
2013-08-08 15:53:04 -07:00
|
|
|
static bool
|
2014-06-10 02:23:39 -07:00
|
|
|
ParseDigits(size_t *result, const CharT *s, size_t *i, size_t limit)
|
2009-08-26 18:13:48 -07:00
|
|
|
{
|
|
|
|
size_t init = *i;
|
|
|
|
*result = 0;
|
2014-06-10 02:23:39 -07:00
|
|
|
while (*i < limit && ('0' <= s[*i] && s[*i] <= '9')) {
|
2009-08-26 18:13:48 -07:00
|
|
|
*result *= 10;
|
|
|
|
*result += (s[*i] - '0');
|
|
|
|
++(*i);
|
|
|
|
}
|
2014-04-18 11:13:44 -07:00
|
|
|
return *i != init;
|
2009-08-26 18:13:48 -07:00
|
|
|
}
|
|
|
|
|
2011-04-11 01:38:27 -07:00
|
|
|
/*
|
2009-08-26 18:13:48 -07:00
|
|
|
* Read and convert decimal digits to the right of a decimal point,
|
|
|
|
* representing a fractional integer, from s[*i] into *result
|
2011-04-11 01:38:27 -07:00
|
|
|
* while *i < limit.
|
|
|
|
*
|
2009-08-26 18:13:48 -07:00
|
|
|
* Succeed if any digits are converted. Advance *i only
|
|
|
|
* as digits are consumed.
|
|
|
|
*/
|
2014-06-10 02:23:39 -07:00
|
|
|
template <typename CharT>
|
2013-08-08 15:53:04 -07:00
|
|
|
static bool
|
2014-06-10 02:23:39 -07:00
|
|
|
ParseFractional(double *result, const CharT *s, size_t *i, size_t limit)
|
2009-08-26 18:13:48 -07:00
|
|
|
{
|
2012-02-24 14:19:52 -08:00
|
|
|
double factor = 0.1;
|
2009-08-26 18:13:48 -07:00
|
|
|
size_t init = *i;
|
|
|
|
*result = 0.0;
|
2014-06-10 02:23:39 -07:00
|
|
|
while (*i < limit && ('0' <= s[*i] && s[*i] <= '9')) {
|
2009-08-26 18:13:48 -07:00
|
|
|
*result += (s[*i] - '0') * factor;
|
|
|
|
factor *= 0.1;
|
|
|
|
++(*i);
|
|
|
|
}
|
2014-04-18 11:13:44 -07:00
|
|
|
return *i != init;
|
2009-08-26 18:13:48 -07:00
|
|
|
}
|
|
|
|
|
2011-04-11 01:38:27 -07:00
|
|
|
/*
|
|
|
|
* Read and convert exactly n decimal digits from s[*i]
|
|
|
|
* to s[min(*i+n,limit)] into *result.
|
2009-08-26 18:13:48 -07:00
|
|
|
*
|
|
|
|
* Succeed if exactly n digits are converted. Advance *i only
|
|
|
|
* on success.
|
|
|
|
*/
|
2014-06-10 02:23:39 -07:00
|
|
|
template <typename CharT>
|
2013-08-08 15:53:04 -07:00
|
|
|
static bool
|
2014-06-10 02:23:39 -07:00
|
|
|
ParseDigitsN(size_t n, size_t *result, const CharT *s, size_t *i, size_t limit)
|
2009-08-26 18:13:48 -07:00
|
|
|
{
|
|
|
|
size_t init = *i;
|
|
|
|
|
2014-06-10 02:23:39 -07:00
|
|
|
if (ParseDigits(result, s, i, Min(limit, init + n)))
|
2014-04-18 11:13:44 -07:00
|
|
|
return (*i - init) == n;
|
2011-04-11 01:38:27 -07:00
|
|
|
|
2009-08-26 18:13:48 -07:00
|
|
|
*i = init;
|
2013-08-06 23:59:54 -07:00
|
|
|
return false;
|
2009-08-26 18:13:48 -07:00
|
|
|
}
|
|
|
|
|
2012-07-09 14:28:20 -07:00
|
|
|
static int
|
|
|
|
DaysInMonth(int year, int month)
|
|
|
|
{
|
|
|
|
bool leap = IsLeapYear(year);
|
|
|
|
int result = int(DayFromMonth(month, leap) - DayFromMonth(month - 1, leap));
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2011-04-11 01:38:27 -07:00
|
|
|
/*
|
2009-08-26 18:13:48 -07:00
|
|
|
* Parse a string in one of the date-time formats given by the W3C
|
|
|
|
* "NOTE-datetime" specification. These formats make up a restricted
|
|
|
|
* profile of the ISO 8601 format. Quoted here:
|
|
|
|
*
|
|
|
|
* The formats are as follows. Exactly the components shown here
|
|
|
|
* must be present, with exactly this punctuation. Note that the "T"
|
|
|
|
* appears literally in the string, to indicate the beginning of the
|
|
|
|
* time element, as specified in ISO 8601.
|
|
|
|
*
|
|
|
|
* Any combination of the date formats with the time formats is
|
|
|
|
* allowed, and also either the date or the time can be missing.
|
|
|
|
*
|
|
|
|
* The specification is silent on the meaning when fields are
|
|
|
|
* ommitted so the interpretations are a guess, but hopefully a
|
|
|
|
* reasonable one. We default the month to January, the day to the
|
|
|
|
* 1st, and hours minutes and seconds all to 0. If the date is
|
|
|
|
* missing entirely then we assume 1970-01-01 so that the time can
|
|
|
|
* be aded to a date later. If the time is missing then we assume
|
|
|
|
* 00:00 UTC. If the time is present but the time zone field is
|
|
|
|
* missing then we use local time.
|
2011-04-11 01:38:27 -07:00
|
|
|
*
|
2009-08-26 18:13:48 -07:00
|
|
|
* Date part:
|
|
|
|
*
|
|
|
|
* Year:
|
|
|
|
* YYYY (eg 1997)
|
|
|
|
*
|
|
|
|
* Year and month:
|
|
|
|
* YYYY-MM (eg 1997-07)
|
|
|
|
*
|
|
|
|
* Complete date:
|
|
|
|
* YYYY-MM-DD (eg 1997-07-16)
|
|
|
|
*
|
|
|
|
* Time part:
|
|
|
|
*
|
|
|
|
* Hours and minutes:
|
|
|
|
* Thh:mmTZD (eg T19:20+01:00)
|
2011-04-11 01:38:27 -07:00
|
|
|
*
|
2009-08-26 18:13:48 -07:00
|
|
|
* Hours, minutes and seconds:
|
|
|
|
* Thh:mm:ssTZD (eg T19:20:30+01:00)
|
|
|
|
*
|
|
|
|
* Hours, minutes, seconds and a decimal fraction of a second:
|
|
|
|
* Thh:mm:ss.sTZD (eg T19:20:30.45+01:00)
|
|
|
|
*
|
|
|
|
* where:
|
|
|
|
*
|
|
|
|
* YYYY = four-digit year or six digit year as +YYYYYY or -YYYYYY
|
|
|
|
* MM = two-digit month (01=January, etc.)
|
|
|
|
* DD = two-digit day of month (01 through 31)
|
|
|
|
* hh = two digits of hour (00 through 23) (am/pm NOT allowed)
|
|
|
|
* mm = two digits of minute (00 through 59)
|
|
|
|
* ss = two digits of second (00 through 59)
|
|
|
|
* s = one or more digits representing a decimal fraction of a second
|
|
|
|
* TZD = time zone designator (Z or +hh:mm or -hh:mm or missing for local)
|
|
|
|
*/
|
2014-06-10 02:23:39 -07:00
|
|
|
template <typename CharT>
|
2013-08-08 15:53:04 -07:00
|
|
|
static bool
|
2014-06-10 02:23:39 -07:00
|
|
|
ParseISODate(const CharT *s, size_t length, double *result, DateTimeInfo *dtInfo)
|
2009-08-26 18:13:48 -07:00
|
|
|
{
|
|
|
|
size_t i = 0;
|
|
|
|
int tzMul = 1;
|
|
|
|
int dateMul = 1;
|
|
|
|
size_t year = 1970;
|
|
|
|
size_t month = 1;
|
|
|
|
size_t day = 1;
|
|
|
|
size_t hour = 0;
|
|
|
|
size_t min = 0;
|
|
|
|
size_t sec = 0;
|
2012-02-24 14:19:52 -08:00
|
|
|
double frac = 0;
|
2013-08-06 23:59:54 -07:00
|
|
|
bool isLocalTime = false;
|
2009-08-26 18:13:48 -07:00
|
|
|
size_t tzHour = 0;
|
|
|
|
size_t tzMin = 0;
|
|
|
|
|
2014-06-10 02:23:39 -07:00
|
|
|
#define PEEK(ch) (i < length && s[i] == ch)
|
2009-08-26 18:13:48 -07:00
|
|
|
|
2014-06-10 02:23:39 -07:00
|
|
|
#define NEED(ch) \
|
|
|
|
if (i >= length || s[i] != ch) { return false; } else { ++i; }
|
2009-08-26 18:13:48 -07:00
|
|
|
|
2014-06-10 02:23:39 -07:00
|
|
|
#define DONE_DATE_UNLESS(ch) \
|
|
|
|
if (i >= length || s[i] != ch) { goto done_date; } else { ++i; }
|
2009-08-26 18:13:48 -07:00
|
|
|
|
2014-06-10 02:23:39 -07:00
|
|
|
#define DONE_UNLESS(ch) \
|
|
|
|
if (i >= length || s[i] != ch) { goto done; } else { ++i; }
|
2009-08-26 18:13:48 -07:00
|
|
|
|
2014-06-10 02:23:39 -07:00
|
|
|
#define NEED_NDIGITS(n, field) \
|
|
|
|
if (!ParseDigitsN(n, &field, s, &i, length)) { return false; }
|
2009-08-26 18:13:48 -07:00
|
|
|
|
|
|
|
if (PEEK('+') || PEEK('-')) {
|
|
|
|
if (PEEK('-'))
|
|
|
|
dateMul = -1;
|
|
|
|
++i;
|
|
|
|
NEED_NDIGITS(6, year);
|
|
|
|
} else if (!PEEK('T')) {
|
|
|
|
NEED_NDIGITS(4, year);
|
|
|
|
}
|
|
|
|
DONE_DATE_UNLESS('-');
|
|
|
|
NEED_NDIGITS(2, month);
|
|
|
|
DONE_DATE_UNLESS('-');
|
|
|
|
NEED_NDIGITS(2, day);
|
|
|
|
|
|
|
|
done_date:
|
|
|
|
DONE_UNLESS('T');
|
|
|
|
NEED_NDIGITS(2, hour);
|
|
|
|
NEED(':');
|
|
|
|
NEED_NDIGITS(2, min);
|
|
|
|
|
|
|
|
if (PEEK(':')) {
|
|
|
|
++i;
|
|
|
|
NEED_NDIGITS(2, sec);
|
|
|
|
if (PEEK('.')) {
|
|
|
|
++i;
|
2014-06-10 02:23:39 -07:00
|
|
|
if (!ParseFractional(&frac, s, &i, length))
|
2014-06-10 02:19:28 -07:00
|
|
|
return false;
|
2009-08-26 18:13:48 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PEEK('Z')) {
|
|
|
|
++i;
|
|
|
|
} else if (PEEK('+') || PEEK('-')) {
|
|
|
|
if (PEEK('-'))
|
|
|
|
tzMul = -1;
|
|
|
|
++i;
|
|
|
|
NEED_NDIGITS(2, tzHour);
|
2011-08-28 23:16:37 -07:00
|
|
|
/*
|
|
|
|
* Non-standard extension to the ISO date format (permitted by ES5):
|
|
|
|
* allow "-0700" as a time zone offset, not just "-07:00".
|
|
|
|
*/
|
|
|
|
if (PEEK(':'))
|
2014-06-10 02:19:28 -07:00
|
|
|
++i;
|
2009-08-26 18:13:48 -07:00
|
|
|
NEED_NDIGITS(2, tzMin);
|
|
|
|
} else {
|
2013-08-06 23:59:54 -07:00
|
|
|
isLocalTime = true;
|
2009-08-26 18:13:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
if (year > 275943 // ceil(1e8/365) + 1970
|
|
|
|
|| (month == 0 || month > 12)
|
2009-09-05 08:59:11 -07:00
|
|
|
|| (day == 0 || day > size_t(DaysInMonth(year,month)))
|
2011-04-11 01:38:27 -07:00
|
|
|
|| hour > 24
|
2009-08-26 18:13:48 -07:00
|
|
|
|| ((hour == 24) && (min > 0 || sec > 0))
|
2011-04-11 01:38:27 -07:00
|
|
|
|| min > 59
|
2009-08-26 18:13:48 -07:00
|
|
|
|| sec > 59
|
|
|
|
|| tzHour > 23
|
2011-04-11 01:38:27 -07:00
|
|
|
|| tzMin > 59)
|
2014-06-10 02:19:28 -07:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2009-08-26 18:13:48 -07:00
|
|
|
|
2014-06-10 02:23:39 -07:00
|
|
|
if (i != length)
|
2014-06-10 02:19:28 -07:00
|
|
|
return false;
|
2009-08-26 18:13:48 -07:00
|
|
|
|
|
|
|
month -= 1; /* convert month to 0-based */
|
|
|
|
|
2014-06-10 02:19:28 -07:00
|
|
|
double msec = date_msecFromDate(dateMul * double(year), month, day,
|
|
|
|
hour, min, sec, frac * 1000.0);
|
2009-08-26 18:13:48 -07:00
|
|
|
|
2014-06-10 02:19:28 -07:00
|
|
|
if (isLocalTime)
|
2012-11-27 15:12:20 -08:00
|
|
|
msec = UTC(msec, dtInfo);
|
2014-06-10 02:19:28 -07:00
|
|
|
else
|
|
|
|
msec -= tzMul * (tzHour * msPerHour + tzMin * msPerMinute);
|
2009-08-26 18:13:48 -07:00
|
|
|
|
|
|
|
if (msec < -8.64e15 || msec > 8.64e15)
|
2014-06-10 02:19:28 -07:00
|
|
|
return false;
|
2009-08-26 18:13:48 -07:00
|
|
|
|
|
|
|
*result = msec;
|
2013-08-06 23:59:54 -07:00
|
|
|
return true;
|
2009-08-26 18:13:48 -07:00
|
|
|
|
|
|
|
#undef PEEK
|
|
|
|
#undef NEED
|
|
|
|
#undef DONE_UNLESS
|
|
|
|
#undef NEED_NDIGITS
|
|
|
|
}
|
|
|
|
|
2014-06-10 02:23:39 -07:00
|
|
|
template <typename CharT>
|
2013-08-08 15:53:04 -07:00
|
|
|
static bool
|
2014-06-10 02:23:39 -07:00
|
|
|
ParseDate(const CharT *s, size_t length, double *result, DateTimeInfo *dtInfo)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-06-10 02:23:39 -07:00
|
|
|
if (ParseISODate(s, length, result, dtInfo))
|
2014-06-10 02:19:27 -07:00
|
|
|
return true;
|
|
|
|
|
2014-06-10 02:23:39 -07:00
|
|
|
if (length == 0)
|
2014-06-10 02:19:27 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
int year = -1;
|
|
|
|
int mon = -1;
|
|
|
|
int mday = -1;
|
|
|
|
int hour = -1;
|
|
|
|
int min = -1;
|
|
|
|
int sec = -1;
|
2014-06-10 02:19:27 -07:00
|
|
|
int tzOffset = -1;
|
2014-06-10 02:19:27 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
int prevc = 0;
|
2014-06-10 02:19:27 -07:00
|
|
|
|
2014-06-10 02:19:27 -07:00
|
|
|
bool seenPlusMinus = false;
|
|
|
|
bool seenMonthName = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-06-10 02:19:27 -07:00
|
|
|
size_t i = 0;
|
2014-06-10 02:23:39 -07:00
|
|
|
while (i < length) {
|
2014-06-10 02:19:27 -07:00
|
|
|
int c = s[i];
|
2007-03-22 10:30:00 -07:00
|
|
|
i++;
|
|
|
|
if (c <= ' ' || c == ',' || c == '-') {
|
2014-06-10 02:19:27 -07:00
|
|
|
if (c == '-' && '0' <= s[i] && s[i] <= '9')
|
|
|
|
prevc = c;
|
2007-03-22 10:30:00 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (c == '(') { /* comments) */
|
|
|
|
int depth = 1;
|
2014-06-10 02:23:39 -07:00
|
|
|
while (i < length) {
|
2007-03-22 10:30:00 -07:00
|
|
|
c = s[i];
|
|
|
|
i++;
|
2014-06-10 02:19:27 -07:00
|
|
|
if (c == '(') {
|
|
|
|
depth++;
|
|
|
|
} else if (c == ')') {
|
2007-03-22 10:30:00 -07:00
|
|
|
if (--depth <= 0)
|
|
|
|
break;
|
2014-06-10 02:19:27 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ('0' <= c && c <= '9') {
|
2014-06-10 02:19:27 -07:00
|
|
|
int n = c - '0';
|
2014-06-10 02:23:39 -07:00
|
|
|
while (i < length && '0' <= (c = s[i]) && c <= '9') {
|
2007-03-22 10:30:00 -07:00
|
|
|
n = n * 10 + c - '0';
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
2014-06-10 02:19:27 -07:00
|
|
|
/*
|
|
|
|
* Allow TZA before the year, so 'Wed Nov 05 21:49:11 GMT-0800 1997'
|
|
|
|
* works.
|
|
|
|
*
|
|
|
|
* Uses of seenPlusMinus allow ':' in TZA, so Java no-timezone style
|
|
|
|
* of GMT+4:30 works.
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
if ((prevc == '+' || prevc == '-')/* && year>=0 */) {
|
2014-06-10 02:19:27 -07:00
|
|
|
/* Make ':' case below change tzOffset. */
|
|
|
|
seenPlusMinus = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/* offset */
|
|
|
|
if (n < 24)
|
|
|
|
n = n * 60; /* EG. "GMT-3" */
|
|
|
|
else
|
|
|
|
n = n % 100 + n / 100 * 60; /* eg "GMT-0430" */
|
2014-06-10 02:19:27 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (prevc == '+') /* plus means east of GMT */
|
|
|
|
n = -n;
|
2014-06-10 02:19:27 -07:00
|
|
|
|
|
|
|
if (tzOffset != 0 && tzOffset != -1)
|
2014-06-10 02:19:22 -07:00
|
|
|
return false;
|
2014-06-10 02:19:27 -07:00
|
|
|
|
|
|
|
tzOffset = n;
|
2007-03-22 10:30:00 -07:00
|
|
|
} else if (prevc == '/' && mon >= 0 && mday >= 0 && year < 0) {
|
2014-06-10 02:23:39 -07:00
|
|
|
if (c <= ' ' || c == ',' || c == '/' || i >= length)
|
2007-03-22 10:30:00 -07:00
|
|
|
year = n;
|
|
|
|
else
|
2014-06-10 02:19:22 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
} else if (c == ':') {
|
|
|
|
if (hour < 0)
|
|
|
|
hour = /*byte*/ n;
|
|
|
|
else if (min < 0)
|
|
|
|
min = /*byte*/ n;
|
|
|
|
else
|
2014-06-10 02:19:22 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
} else if (c == '/') {
|
2014-06-10 02:19:27 -07:00
|
|
|
/*
|
|
|
|
* Until it is determined that mon is the actual month, keep
|
|
|
|
* it as 1-based rather than 0-based.
|
|
|
|
*/
|
2007-03-22 10:30:00 -07:00
|
|
|
if (mon < 0)
|
|
|
|
mon = /*byte*/ n;
|
|
|
|
else if (mday < 0)
|
|
|
|
mday = /*byte*/ n;
|
|
|
|
else
|
2014-06-10 02:19:22 -07:00
|
|
|
return false;
|
2014-06-10 02:23:39 -07:00
|
|
|
} else if (i < length && c != ',' && c > ' ' && c != '-' && c != '(') {
|
2014-06-10 02:19:22 -07:00
|
|
|
return false;
|
2014-06-10 02:19:27 -07:00
|
|
|
} else if (seenPlusMinus && n < 60) { /* handle GMT-3:30 */
|
|
|
|
if (tzOffset < 0)
|
|
|
|
tzOffset -= n;
|
2007-03-22 10:30:00 -07:00
|
|
|
else
|
2014-06-10 02:19:27 -07:00
|
|
|
tzOffset += n;
|
2007-03-22 10:30:00 -07:00
|
|
|
} else if (hour >= 0 && min < 0) {
|
|
|
|
min = /*byte*/ n;
|
|
|
|
} else if (prevc == ':' && min >= 0 && sec < 0) {
|
|
|
|
sec = /*byte*/ n;
|
|
|
|
} else if (mon < 0) {
|
|
|
|
mon = /*byte*/n;
|
|
|
|
} else if (mon >= 0 && mday < 0) {
|
|
|
|
mday = /*byte*/ n;
|
|
|
|
} else if (mon >= 0 && mday >= 0 && year < 0) {
|
|
|
|
year = n;
|
|
|
|
} else {
|
2014-06-10 02:19:22 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
prevc = 0;
|
|
|
|
} else if (c == '/' || c == ':' || c == '+' || c == '-') {
|
|
|
|
prevc = c;
|
|
|
|
} else {
|
|
|
|
size_t st = i - 1;
|
|
|
|
int k;
|
2014-06-10 02:23:39 -07:00
|
|
|
while (i < length) {
|
2007-03-22 10:30:00 -07:00
|
|
|
c = s[i];
|
|
|
|
if (!(('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z')))
|
|
|
|
break;
|
|
|
|
i++;
|
|
|
|
}
|
2014-06-10 02:19:27 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (i <= st + 1)
|
2014-06-10 02:19:22 -07:00
|
|
|
return false;
|
2014-06-10 02:19:27 -07:00
|
|
|
|
2014-06-10 02:19:22 -07:00
|
|
|
for (k = ArrayLength(wtb); --k >= 0;) {
|
2014-06-10 02:23:39 -07:00
|
|
|
if (RegionMatches(wtb[k], 0, s, st, i - st)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
int action = ttb[k];
|
|
|
|
if (action != 0) {
|
|
|
|
if (action < 0) {
|
|
|
|
/*
|
|
|
|
* AM/PM. Count 12:30 AM as 00:30, 12:30 PM as
|
|
|
|
* 12:30, instead of blindly adding 12 if PM.
|
|
|
|
*/
|
2014-10-01 10:17:51 -07:00
|
|
|
MOZ_ASSERT(action == -1 || action == -2);
|
2014-06-10 02:19:22 -07:00
|
|
|
if (hour > 12 || hour < 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (action == -1 && hour == 12) /* am */
|
|
|
|
hour = 0;
|
|
|
|
else if (action == -2 && hour != 12) /* pm */
|
|
|
|
hour += 12;
|
2007-03-22 10:30:00 -07:00
|
|
|
} else if (action <= 13) { /* month! */
|
2014-06-10 02:19:27 -07:00
|
|
|
/*
|
|
|
|
* Adjust mon to be 1-based until the final values
|
|
|
|
* for mon, mday and year are adjusted below.
|
|
|
|
*/
|
|
|
|
if (seenMonthName)
|
2014-06-10 02:19:22 -07:00
|
|
|
return false;
|
|
|
|
|
2014-06-10 02:19:27 -07:00
|
|
|
seenMonthName = true;
|
2014-06-10 02:19:27 -07:00
|
|
|
int temp = /*byte*/ (action - 2) + 1;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (mon < 0) {
|
|
|
|
mon = temp;
|
|
|
|
} else if (mday < 0) {
|
|
|
|
mday = mon;
|
|
|
|
mon = temp;
|
|
|
|
} else if (year < 0) {
|
|
|
|
year = mon;
|
|
|
|
mon = temp;
|
|
|
|
} else {
|
2014-06-10 02:19:22 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
} else {
|
2014-06-10 02:19:27 -07:00
|
|
|
tzOffset = action - 10000;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2014-06-10 02:19:22 -07:00
|
|
|
}
|
2014-06-10 02:19:27 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (k < 0)
|
2014-06-10 02:19:22 -07:00
|
|
|
return false;
|
2014-06-10 02:19:27 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
prevc = 0;
|
|
|
|
}
|
|
|
|
}
|
2014-06-10 02:19:22 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (year < 0 || mon < 0 || mday < 0)
|
2014-06-10 02:19:22 -07:00
|
|
|
return false;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/*
|
2014-06-10 02:19:27 -07:00
|
|
|
* Case 1. The input string contains an English month name.
|
|
|
|
* The form of the string can be month f l, or f month l, or
|
|
|
|
* f l month which each evaluate to the same date.
|
|
|
|
* If f and l are both greater than or equal to 70, or
|
|
|
|
* both less than 70, the date is invalid.
|
|
|
|
* The year is taken to be the greater of the values f, l.
|
|
|
|
* If the year is greater than or equal to 70 and less than 100,
|
|
|
|
* it is considered to be the number of years after 1900.
|
|
|
|
* Case 2. The input string is of the form "f/m/l" where f, m and l are
|
|
|
|
* integers, e.g. 7/16/45.
|
|
|
|
* Adjust the mon, mday and year values to achieve 100% MSIE
|
|
|
|
* compatibility.
|
|
|
|
* a. If 0 <= f < 70, f/m/l is interpreted as month/day/year.
|
|
|
|
* i. If year < 100, it is the number of years after 1900
|
|
|
|
* ii. If year >= 100, it is the number of years after 0.
|
|
|
|
* b. If 70 <= f < 100
|
|
|
|
* i. If m < 70, f/m/l is interpreted as
|
|
|
|
* year/month/day where year is the number of years after
|
|
|
|
* 1900.
|
|
|
|
* ii. If m >= 70, the date is invalid.
|
|
|
|
* c. If f >= 100
|
|
|
|
* i. If m < 70, f/m/l is interpreted as
|
|
|
|
* year/month/day where year is the number of years after 0.
|
|
|
|
* ii. If m >= 70, the date is invalid.
|
|
|
|
*/
|
|
|
|
if (seenMonthName) {
|
2014-06-10 02:19:22 -07:00
|
|
|
if ((mday >= 70 && year >= 70) || (mday < 70 && year < 70))
|
|
|
|
return false;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (mday > year) {
|
2014-06-10 02:19:27 -07:00
|
|
|
int temp = year;
|
2007-03-22 10:30:00 -07:00
|
|
|
year = mday;
|
|
|
|
mday = temp;
|
|
|
|
}
|
|
|
|
if (year >= 70 && year < 100) {
|
|
|
|
year += 1900;
|
|
|
|
}
|
|
|
|
} else if (mon < 70) { /* (a) month/day/year */
|
|
|
|
if (year < 100) {
|
|
|
|
year += 1900;
|
|
|
|
}
|
|
|
|
} else if (mon < 100) { /* (b) year/month/day */
|
|
|
|
if (mday < 70) {
|
2014-06-10 02:19:27 -07:00
|
|
|
int temp = year;
|
2007-03-22 10:30:00 -07:00
|
|
|
year = mon + 1900;
|
|
|
|
mon = mday;
|
|
|
|
mday = temp;
|
|
|
|
} else {
|
2014-06-10 02:19:22 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
} else { /* (c) year/month/day */
|
|
|
|
if (mday < 70) {
|
2014-06-10 02:19:27 -07:00
|
|
|
int temp = year;
|
2007-03-22 10:30:00 -07:00
|
|
|
year = mon;
|
|
|
|
mon = mday;
|
|
|
|
mday = temp;
|
|
|
|
} else {
|
2014-06-10 02:19:22 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
2014-06-10 02:19:22 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
mon -= 1; /* convert month to 0-based */
|
|
|
|
if (sec < 0)
|
|
|
|
sec = 0;
|
|
|
|
if (min < 0)
|
|
|
|
min = 0;
|
|
|
|
if (hour < 0)
|
|
|
|
hour = 0;
|
|
|
|
|
2014-06-10 02:19:27 -07:00
|
|
|
double msec = date_msecFromDate(year, mon, mday, hour, min, sec, 0);
|
2009-08-26 18:13:48 -07:00
|
|
|
|
2014-06-10 02:19:27 -07:00
|
|
|
if (tzOffset == -1) /* no time zone specified, have to use local */
|
2012-11-27 15:12:20 -08:00
|
|
|
msec = UTC(msec, dtInfo);
|
2014-06-10 02:19:22 -07:00
|
|
|
else
|
2014-06-10 02:19:27 -07:00
|
|
|
msec += tzOffset * msPerMinute;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
*result = msec;
|
2013-08-06 23:59:54 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-06-10 02:23:39 -07:00
|
|
|
static bool
|
|
|
|
ParseDate(JSLinearString *s, double *result, DateTimeInfo *dtInfo)
|
|
|
|
{
|
|
|
|
AutoCheckCannotGC nogc;
|
|
|
|
return s->hasLatin1Chars()
|
|
|
|
? ParseDate(s->latin1Chars(nogc), s->length(), result, dtInfo)
|
|
|
|
: ParseDate(s->twoByteChars(nogc), s->length(), result, dtInfo);
|
|
|
|
}
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
static bool
|
2012-02-28 15:11:11 -08:00
|
|
|
date_parse(JSContext *cx, unsigned argc, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-01-11 00:43:00 -08:00
|
|
|
CallArgs args = CallArgsFromVp(argc, vp);
|
|
|
|
if (args.length() == 0) {
|
2014-03-24 05:49:44 -07:00
|
|
|
args.rval().setNaN();
|
2009-10-28 04:57:31 -07:00
|
|
|
return true;
|
2008-08-08 09:02:50 -07:00
|
|
|
}
|
2013-01-11 00:43:00 -08:00
|
|
|
|
2013-06-26 16:26:45 -07:00
|
|
|
JSString *str = ToString<CanGC>(cx, args[0]);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!str)
|
2013-01-11 00:43:00 -08:00
|
|
|
return false;
|
|
|
|
|
2013-04-30 15:39:19 -07:00
|
|
|
JSLinearString *linearStr = str->ensureLinear(cx);
|
2010-12-06 10:26:58 -08:00
|
|
|
if (!linearStr)
|
|
|
|
return false;
|
|
|
|
|
2013-01-11 00:43:00 -08:00
|
|
|
double result;
|
2014-06-10 02:23:39 -07:00
|
|
|
if (!ParseDate(linearStr, &result, &cx->runtime()->dateTimeInfo)) {
|
2014-03-24 05:49:44 -07:00
|
|
|
args.rval().setNaN();
|
2009-10-28 04:57:31 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-04-19 16:22:08 -07:00
|
|
|
result = TimeClip(result);
|
2014-03-24 05:49:44 -07:00
|
|
|
args.rval().setNumber(result);
|
2010-07-14 23:19:36 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-02-24 14:19:52 -08:00
|
|
|
static inline double
|
2009-02-26 00:49:39 -08:00
|
|
|
NowAsMillis()
|
|
|
|
{
|
2012-02-24 14:19:52 -08:00
|
|
|
return (double) (PRMJ_Now() / PRMJ_USEC_PER_MSEC);
|
2009-02-26 00:49:39 -08:00
|
|
|
}
|
|
|
|
|
2014-09-16 07:01:09 -07:00
|
|
|
bool
|
|
|
|
js::date_now(JSContext *cx, unsigned argc, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-03-24 05:49:44 -07:00
|
|
|
CallArgs args = CallArgsFromVp(argc, vp);
|
|
|
|
args.rval().setDouble(NowAsMillis());
|
2013-08-02 00:41:57 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-06-20 20:17:15 -07:00
|
|
|
void
|
2014-09-15 08:49:37 -07:00
|
|
|
DateObject::setUTCTime(double t)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-06-20 20:17:15 -07:00
|
|
|
for (size_t ind = COMPONENTS_START_SLOT; ind < RESERVED_SLOTS; ind++)
|
|
|
|
setReservedSlot(ind, UndefinedValue());
|
2009-10-24 11:42:29 -07:00
|
|
|
|
2013-06-20 20:17:15 -07:00
|
|
|
setFixedSlot(UTC_TIME_SLOT, DoubleValue(t));
|
2014-09-15 08:49:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DateObject::setUTCTime(double t, MutableHandleValue vp)
|
|
|
|
{
|
|
|
|
setUTCTime(t);
|
|
|
|
vp.setDouble(t);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-06-20 20:17:15 -07:00
|
|
|
void
|
|
|
|
DateObject::fillLocalTimeSlots(DateTimeInfo *dtInfo)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-07-13 15:56:08 -07:00
|
|
|
/* Check if the cache is already populated. */
|
2013-06-20 20:17:15 -07:00
|
|
|
if (!getReservedSlot(LOCAL_TIME_SLOT).isUndefined() &&
|
|
|
|
getReservedSlot(TZA_SLOT).toDouble() == dtInfo->localTZA())
|
2012-07-13 15:56:08 -07:00
|
|
|
{
|
2012-10-07 18:01:54 -07:00
|
|
|
return;
|
2012-07-13 15:56:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Remember timezone used to generate the local cache. */
|
2013-06-20 20:17:15 -07:00
|
|
|
setReservedSlot(TZA_SLOT, DoubleValue(dtInfo->localTZA()));
|
2012-07-13 15:56:08 -07:00
|
|
|
|
2013-06-20 20:17:15 -07:00
|
|
|
double utcTime = UTCTime().toNumber();
|
2010-08-17 10:42:57 -07:00
|
|
|
|
2013-05-01 13:55:13 -07:00
|
|
|
if (!IsFinite(utcTime)) {
|
2013-06-20 20:17:15 -07:00
|
|
|
for (size_t ind = COMPONENTS_START_SLOT; ind < RESERVED_SLOTS; ind++)
|
|
|
|
setReservedSlot(ind, DoubleValue(utcTime));
|
2012-10-07 18:01:54 -07:00
|
|
|
return;
|
2010-08-17 10:42:57 -07:00
|
|
|
}
|
|
|
|
|
2012-11-27 15:12:20 -08:00
|
|
|
double localTime = LocalTime(utcTime, dtInfo);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-06-20 20:17:15 -07:00
|
|
|
setReservedSlot(LOCAL_TIME_SLOT, DoubleValue(localTime));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-07-09 14:28:20 -07:00
|
|
|
int year = (int) floor(localTime /(msPerDay * 365.2425)) + 1970;
|
|
|
|
double yearStartTime = TimeFromYear(year);
|
2010-08-17 10:42:57 -07:00
|
|
|
|
|
|
|
/* Adjust the year in case the approximation was wrong, as in YearFromTime. */
|
2012-03-01 18:54:01 -08:00
|
|
|
int yearDays;
|
2010-08-17 10:42:57 -07:00
|
|
|
if (yearStartTime > localTime) {
|
|
|
|
year--;
|
|
|
|
yearStartTime -= (msPerDay * DaysInYear(year));
|
|
|
|
yearDays = DaysInYear(year);
|
|
|
|
} else {
|
|
|
|
yearDays = DaysInYear(year);
|
2012-02-24 14:19:52 -08:00
|
|
|
double nextStart = yearStartTime + (msPerDay * yearDays);
|
2010-08-17 10:42:57 -07:00
|
|
|
if (nextStart <= localTime) {
|
|
|
|
year++;
|
|
|
|
yearStartTime = nextStart;
|
|
|
|
yearDays = DaysInYear(year);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-06-20 20:17:15 -07:00
|
|
|
setReservedSlot(LOCAL_YEAR_SLOT, Int32Value(year));
|
2010-08-17 10:42:57 -07:00
|
|
|
|
Bug 708735 - Use <stdint.h> types in JSAPI and throughout SpiderMonkey. Continue to provide the {u,}int{8,16,32,64} and JS{Uint,Int}{8,16,32,64} integer types through a single header, however, for a simpler backout strategy -- and also to ease the transition for embedders. r=timeless on switching the jsd API to use the <stdint.h> types, r=luke, r=dmandelin
2011-12-08 19:54:10 -08:00
|
|
|
uint64_t yearTime = uint64_t(localTime - yearStartTime);
|
2012-03-01 18:54:01 -08:00
|
|
|
int yearSeconds = uint32_t(yearTime / 1000);
|
2010-08-17 10:42:57 -07:00
|
|
|
|
2012-03-01 18:54:01 -08:00
|
|
|
int day = yearSeconds / int(SecondsPerDay);
|
2010-08-17 10:42:57 -07:00
|
|
|
|
2012-03-01 18:54:01 -08:00
|
|
|
int step = -1, next = 30;
|
|
|
|
int month;
|
2010-08-17 10:42:57 -07:00
|
|
|
|
|
|
|
do {
|
|
|
|
if (day <= next) {
|
|
|
|
month = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
step = next;
|
|
|
|
next += ((yearDays == 366) ? 29 : 28);
|
|
|
|
if (day <= next) {
|
|
|
|
month = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
step = next;
|
|
|
|
if (day <= (next += 31)) {
|
|
|
|
month = 2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
step = next;
|
|
|
|
if (day <= (next += 30)) {
|
|
|
|
month = 3;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
step = next;
|
|
|
|
if (day <= (next += 31)) {
|
|
|
|
month = 4;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
step = next;
|
|
|
|
if (day <= (next += 30)) {
|
|
|
|
month = 5;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
step = next;
|
|
|
|
if (day <= (next += 31)) {
|
|
|
|
month = 6;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
step = next;
|
|
|
|
if (day <= (next += 31)) {
|
|
|
|
month = 7;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
step = next;
|
|
|
|
if (day <= (next += 30)) {
|
|
|
|
month = 8;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
step = next;
|
|
|
|
if (day <= (next += 31)) {
|
|
|
|
month = 9;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
step = next;
|
|
|
|
if (day <= (next += 30)) {
|
|
|
|
month = 10;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
step = next;
|
|
|
|
month = 11;
|
|
|
|
} while (0);
|
|
|
|
|
2013-06-20 20:17:15 -07:00
|
|
|
setReservedSlot(LOCAL_MONTH_SLOT, Int32Value(month));
|
|
|
|
setReservedSlot(LOCAL_DATE_SLOT, Int32Value(day - step));
|
2010-08-17 10:42:57 -07:00
|
|
|
|
2012-03-01 18:54:01 -08:00
|
|
|
int weekday = WeekDay(localTime);
|
2013-06-20 20:17:15 -07:00
|
|
|
setReservedSlot(LOCAL_DAY_SLOT, Int32Value(weekday));
|
2010-08-17 10:42:57 -07:00
|
|
|
|
2012-03-01 18:54:01 -08:00
|
|
|
int seconds = yearSeconds % 60;
|
2013-06-20 20:17:15 -07:00
|
|
|
setReservedSlot(LOCAL_SECONDS_SLOT, Int32Value(seconds));
|
2010-08-17 10:42:57 -07:00
|
|
|
|
2012-03-01 18:54:01 -08:00
|
|
|
int minutes = (yearSeconds / 60) % 60;
|
2013-06-20 20:17:15 -07:00
|
|
|
setReservedSlot(LOCAL_MINUTES_SLOT, Int32Value(minutes));
|
2010-08-17 10:42:57 -07:00
|
|
|
|
2012-03-01 18:54:01 -08:00
|
|
|
int hours = (yearSeconds / (60 * 60)) % 24;
|
2013-06-20 20:17:15 -07:00
|
|
|
setReservedSlot(LOCAL_HOURS_SLOT, Int32Value(hours));
|
2010-08-17 10:42:57 -07:00
|
|
|
}
|
|
|
|
|
2012-10-07 18:03:20 -07:00
|
|
|
inline double
|
2013-06-20 20:17:15 -07:00
|
|
|
DateObject::cachedLocalTime(DateTimeInfo *dtInfo)
|
2011-01-26 13:37:45 -08:00
|
|
|
{
|
2013-06-20 20:17:15 -07:00
|
|
|
fillLocalTimeSlots(dtInfo);
|
|
|
|
return getReservedSlot(LOCAL_TIME_SLOT).toDouble();
|
2011-01-26 13:37:45 -08:00
|
|
|
}
|
|
|
|
|
2014-01-24 20:14:56 -08:00
|
|
|
MOZ_ALWAYS_INLINE bool
|
2013-05-28 11:23:04 -07:00
|
|
|
IsDate(HandleValue v)
|
2012-07-03 17:44:22 -07:00
|
|
|
{
|
2013-06-20 20:17:15 -07:00
|
|
|
return v.isObject() && v.toObject().is<DateObject>();
|
2012-07-03 17:44:22 -07:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/*
|
|
|
|
* See ECMA 15.9.5.4 thru 15.9.5.23
|
|
|
|
*/
|
2014-01-24 20:14:56 -08:00
|
|
|
/* static */ MOZ_ALWAYS_INLINE bool
|
2013-06-20 20:17:15 -07:00
|
|
|
DateObject::getTime_impl(JSContext *cx, CallArgs args)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-06-20 20:17:15 -07:00
|
|
|
args.rval().set(args.thisv().toObject().as<DateObject>().UTCTime());
|
2011-01-26 13:37:45 -08:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
static bool
|
2012-07-03 17:44:22 -07:00
|
|
|
date_getTime(JSContext *cx, unsigned argc, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-09-08 21:02:26 -07:00
|
|
|
CallArgs args = CallArgsFromVp(argc, vp);
|
2013-06-20 20:17:15 -07:00
|
|
|
return CallNonGenericMethod<IsDate, DateObject::getTime_impl>(cx, args);
|
2012-07-03 17:44:22 -07:00
|
|
|
}
|
2011-09-08 21:18:23 -07:00
|
|
|
|
2014-01-24 20:14:56 -08:00
|
|
|
/* static */ MOZ_ALWAYS_INLINE bool
|
2013-06-20 20:17:15 -07:00
|
|
|
DateObject::getYear_impl(JSContext *cx, CallArgs args)
|
2012-07-03 17:44:22 -07:00
|
|
|
{
|
2013-06-20 20:17:15 -07:00
|
|
|
DateObject *dateObj = &args.thisv().toObject().as<DateObject>();
|
|
|
|
dateObj->fillLocalTimeSlots(&cx->runtime()->dateTimeInfo);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-06-20 20:17:15 -07:00
|
|
|
Value yearVal = dateObj->getReservedSlot(LOCAL_YEAR_SLOT);
|
2010-08-17 10:42:57 -07:00
|
|
|
if (yearVal.isInt32()) {
|
2007-03-22 10:30:00 -07:00
|
|
|
/* Follow ECMA-262 to the letter, contrary to IE JScript. */
|
2012-03-01 18:54:01 -08:00
|
|
|
int year = yearVal.toInt32() - 1900;
|
2011-09-08 21:02:26 -07:00
|
|
|
args.rval().setInt32(year);
|
2010-08-17 10:42:57 -07:00
|
|
|
} else {
|
2012-07-30 04:19:09 -07:00
|
|
|
args.rval().set(yearVal);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-01-26 13:37:45 -08:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
static bool
|
2012-07-03 17:44:22 -07:00
|
|
|
date_getYear(JSContext *cx, unsigned argc, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-09-08 21:02:26 -07:00
|
|
|
CallArgs args = CallArgsFromVp(argc, vp);
|
2013-06-20 20:17:15 -07:00
|
|
|
return CallNonGenericMethod<IsDate, DateObject::getYear_impl>(cx, args);
|
2012-07-03 17:44:22 -07:00
|
|
|
}
|
2011-09-08 21:18:23 -07:00
|
|
|
|
2014-01-24 20:14:56 -08:00
|
|
|
/* static */ MOZ_ALWAYS_INLINE bool
|
2013-06-20 20:17:15 -07:00
|
|
|
DateObject::getFullYear_impl(JSContext *cx, CallArgs args)
|
2012-07-03 17:44:22 -07:00
|
|
|
{
|
2013-06-20 20:17:15 -07:00
|
|
|
DateObject *dateObj = &args.thisv().toObject().as<DateObject>();
|
|
|
|
dateObj->fillLocalTimeSlots(&cx->runtime()->dateTimeInfo);
|
2010-08-17 10:42:57 -07:00
|
|
|
|
2013-06-20 20:17:15 -07:00
|
|
|
args.rval().set(dateObj->getReservedSlot(LOCAL_YEAR_SLOT));
|
2011-09-08 21:18:23 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
static bool
|
2012-07-03 17:44:22 -07:00
|
|
|
date_getFullYear(JSContext *cx, unsigned argc, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-09-08 21:02:26 -07:00
|
|
|
CallArgs args = CallArgsFromVp(argc, vp);
|
2013-06-20 20:17:15 -07:00
|
|
|
return CallNonGenericMethod<IsDate, DateObject::getFullYear_impl>(cx, args);
|
2012-07-03 17:44:22 -07:00
|
|
|
}
|
2011-09-08 21:02:26 -07:00
|
|
|
|
2014-01-24 20:14:56 -08:00
|
|
|
/* static */ MOZ_ALWAYS_INLINE bool
|
2013-06-20 20:17:15 -07:00
|
|
|
DateObject::getUTCFullYear_impl(JSContext *cx, CallArgs args)
|
2012-07-03 17:44:22 -07:00
|
|
|
{
|
2013-06-20 20:17:15 -07:00
|
|
|
double result = args.thisv().toObject().as<DateObject>().UTCTime().toNumber();
|
2013-05-01 13:55:13 -07:00
|
|
|
if (IsFinite(result))
|
2007-03-22 10:30:00 -07:00
|
|
|
result = YearFromTime(result);
|
|
|
|
|
2011-09-08 21:02:26 -07:00
|
|
|
args.rval().setNumber(result);
|
2011-01-26 13:37:45 -08:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
static bool
|
2012-07-03 17:44:22 -07:00
|
|
|
date_getUTCFullYear(JSContext *cx, unsigned argc, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-09-08 21:02:26 -07:00
|
|
|
CallArgs args = CallArgsFromVp(argc, vp);
|
2013-06-20 20:17:15 -07:00
|
|
|
return CallNonGenericMethod<IsDate, DateObject::getUTCFullYear_impl>(cx, args);
|
2012-07-03 17:44:22 -07:00
|
|
|
}
|
2011-09-08 21:18:23 -07:00
|
|
|
|
2014-01-24 20:14:56 -08:00
|
|
|
/* static */ MOZ_ALWAYS_INLINE bool
|
2013-06-20 20:17:15 -07:00
|
|
|
DateObject::getMonth_impl(JSContext *cx, CallArgs args)
|
2012-07-03 17:44:22 -07:00
|
|
|
{
|
2013-06-20 20:17:15 -07:00
|
|
|
DateObject *dateObj = &args.thisv().toObject().as<DateObject>();
|
|
|
|
dateObj->fillLocalTimeSlots(&cx->runtime()->dateTimeInfo);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-06-20 20:17:15 -07:00
|
|
|
args.rval().set(dateObj->getReservedSlot(LOCAL_MONTH_SLOT));
|
2011-01-26 13:37:45 -08:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
static bool
|
2012-07-03 17:44:22 -07:00
|
|
|
date_getMonth(JSContext *cx, unsigned argc, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-09-08 21:02:26 -07:00
|
|
|
CallArgs args = CallArgsFromVp(argc, vp);
|
2013-06-20 20:17:15 -07:00
|
|
|
return CallNonGenericMethod<IsDate, DateObject::getMonth_impl>(cx, args);
|
2012-07-03 17:44:22 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-01-24 20:14:56 -08:00
|
|
|
/* static */ MOZ_ALWAYS_INLINE bool
|
2013-06-20 20:17:15 -07:00
|
|
|
DateObject::getUTCMonth_impl(JSContext *cx, CallArgs args)
|
2012-07-03 17:44:22 -07:00
|
|
|
{
|
2013-06-20 20:17:15 -07:00
|
|
|
double d = args.thisv().toObject().as<DateObject>().UTCTime().toNumber();
|
2012-07-09 14:28:20 -07:00
|
|
|
args.rval().setNumber(MonthFromTime(d));
|
2011-01-26 13:37:45 -08:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
static bool
|
2012-07-03 17:44:22 -07:00
|
|
|
date_getUTCMonth(JSContext *cx, unsigned argc, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-09-08 21:02:26 -07:00
|
|
|
CallArgs args = CallArgsFromVp(argc, vp);
|
2013-06-20 20:17:15 -07:00
|
|
|
return CallNonGenericMethod<IsDate, DateObject::getUTCMonth_impl>(cx, args);
|
2012-07-03 17:44:22 -07:00
|
|
|
}
|
2011-09-08 21:18:23 -07:00
|
|
|
|
2014-01-24 20:14:56 -08:00
|
|
|
/* static */ MOZ_ALWAYS_INLINE bool
|
2013-06-20 20:17:15 -07:00
|
|
|
DateObject::getDate_impl(JSContext *cx, CallArgs args)
|
2012-07-03 17:44:22 -07:00
|
|
|
{
|
2013-06-20 20:17:15 -07:00
|
|
|
DateObject *dateObj = &args.thisv().toObject().as<DateObject>();
|
|
|
|
dateObj->fillLocalTimeSlots(&cx->runtime()->dateTimeInfo);
|
2011-01-26 13:37:45 -08:00
|
|
|
|
2013-06-20 20:17:15 -07:00
|
|
|
args.rval().set(dateObj->getReservedSlot(LOCAL_DATE_SLOT));
|
2011-01-26 13:37:45 -08:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
static bool
|
2012-07-03 17:44:22 -07:00
|
|
|
date_getDate(JSContext *cx, unsigned argc, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-09-08 21:02:26 -07:00
|
|
|
CallArgs args = CallArgsFromVp(argc, vp);
|
2013-06-20 20:17:15 -07:00
|
|
|
return CallNonGenericMethod<IsDate, DateObject::getDate_impl>(cx, args);
|
2012-07-03 17:44:22 -07:00
|
|
|
}
|
2011-09-08 21:02:26 -07:00
|
|
|
|
2014-01-24 20:14:56 -08:00
|
|
|
/* static */ MOZ_ALWAYS_INLINE bool
|
2013-06-20 20:17:15 -07:00
|
|
|
DateObject::getUTCDate_impl(JSContext *cx, CallArgs args)
|
2012-07-03 17:44:22 -07:00
|
|
|
{
|
2013-06-20 20:17:15 -07:00
|
|
|
double result = args.thisv().toObject().as<DateObject>().UTCTime().toNumber();
|
2013-05-01 13:55:13 -07:00
|
|
|
if (IsFinite(result))
|
2007-03-22 10:30:00 -07:00
|
|
|
result = DateFromTime(result);
|
|
|
|
|
2011-09-08 21:02:26 -07:00
|
|
|
args.rval().setNumber(result);
|
2011-01-26 13:37:45 -08:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
static bool
|
2012-07-03 17:44:22 -07:00
|
|
|
date_getUTCDate(JSContext *cx, unsigned argc, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-09-08 21:02:26 -07:00
|
|
|
CallArgs args = CallArgsFromVp(argc, vp);
|
2013-06-20 20:17:15 -07:00
|
|
|
return CallNonGenericMethod<IsDate, DateObject::getUTCDate_impl>(cx, args);
|
2012-07-03 17:44:22 -07:00
|
|
|
}
|
2011-09-08 21:18:23 -07:00
|
|
|
|
2014-01-24 20:14:56 -08:00
|
|
|
/* static */ MOZ_ALWAYS_INLINE bool
|
2013-06-20 20:17:15 -07:00
|
|
|
DateObject::getDay_impl(JSContext *cx, CallArgs args)
|
2012-07-03 17:44:22 -07:00
|
|
|
{
|
2013-06-20 20:17:15 -07:00
|
|
|
DateObject *dateObj = &args.thisv().toObject().as<DateObject>();
|
|
|
|
dateObj->fillLocalTimeSlots(&cx->runtime()->dateTimeInfo);
|
2011-01-26 13:37:45 -08:00
|
|
|
|
2013-06-20 20:17:15 -07:00
|
|
|
args.rval().set(dateObj->getReservedSlot(LOCAL_DAY_SLOT));
|
2011-01-26 13:37:45 -08:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
static bool
|
2012-07-03 17:44:22 -07:00
|
|
|
date_getDay(JSContext *cx, unsigned argc, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-09-08 21:02:26 -07:00
|
|
|
CallArgs args = CallArgsFromVp(argc, vp);
|
2013-06-20 20:17:15 -07:00
|
|
|
return CallNonGenericMethod<IsDate, DateObject::getDay_impl>(cx, args);
|
2012-07-03 17:44:22 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-01-24 20:14:56 -08:00
|
|
|
/* static */ MOZ_ALWAYS_INLINE bool
|
2013-06-20 20:17:15 -07:00
|
|
|
DateObject::getUTCDay_impl(JSContext *cx, CallArgs args)
|
2012-07-03 17:44:22 -07:00
|
|
|
{
|
2013-06-20 20:17:15 -07:00
|
|
|
double result = args.thisv().toObject().as<DateObject>().UTCTime().toNumber();
|
2013-05-01 13:55:13 -07:00
|
|
|
if (IsFinite(result))
|
2007-03-22 10:30:00 -07:00
|
|
|
result = WeekDay(result);
|
|
|
|
|
2011-09-08 21:02:26 -07:00
|
|
|
args.rval().setNumber(result);
|
2011-01-26 13:37:45 -08:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
static bool
|
2012-07-03 17:44:22 -07:00
|
|
|
date_getUTCDay(JSContext *cx, unsigned argc, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-09-08 21:02:26 -07:00
|
|
|
CallArgs args = CallArgsFromVp(argc, vp);
|
2013-06-20 20:17:15 -07:00
|
|
|
return CallNonGenericMethod<IsDate, DateObject::getUTCDay_impl>(cx, args);
|
2012-07-03 17:44:22 -07:00
|
|
|
}
|
2011-09-08 21:18:23 -07:00
|
|
|
|
2014-01-24 20:14:56 -08:00
|
|
|
/* static */ MOZ_ALWAYS_INLINE bool
|
2013-06-20 20:17:15 -07:00
|
|
|
DateObject::getHours_impl(JSContext *cx, CallArgs args)
|
2012-07-03 17:44:22 -07:00
|
|
|
{
|
2013-06-20 20:17:15 -07:00
|
|
|
DateObject *dateObj = &args.thisv().toObject().as<DateObject>();
|
|
|
|
dateObj->fillLocalTimeSlots(&cx->runtime()->dateTimeInfo);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-06-20 20:17:15 -07:00
|
|
|
args.rval().set(dateObj->getReservedSlot(LOCAL_HOURS_SLOT));
|
2011-01-26 13:37:45 -08:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
static bool
|
2012-07-03 17:44:22 -07:00
|
|
|
date_getHours(JSContext *cx, unsigned argc, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-09-08 21:02:26 -07:00
|
|
|
CallArgs args = CallArgsFromVp(argc, vp);
|
2013-06-20 20:17:15 -07:00
|
|
|
return CallNonGenericMethod<IsDate, DateObject::getHours_impl>(cx, args);
|
2012-07-03 17:44:22 -07:00
|
|
|
}
|
2011-09-08 21:02:26 -07:00
|
|
|
|
2014-01-24 20:14:56 -08:00
|
|
|
/* static */ MOZ_ALWAYS_INLINE bool
|
2013-06-20 20:17:15 -07:00
|
|
|
DateObject::getUTCHours_impl(JSContext *cx, CallArgs args)
|
2012-07-03 17:44:22 -07:00
|
|
|
{
|
2013-06-20 20:17:15 -07:00
|
|
|
double result = args.thisv().toObject().as<DateObject>().UTCTime().toNumber();
|
2013-05-01 13:55:13 -07:00
|
|
|
if (IsFinite(result))
|
2007-03-22 10:30:00 -07:00
|
|
|
result = HourFromTime(result);
|
|
|
|
|
2011-09-08 21:02:26 -07:00
|
|
|
args.rval().setNumber(result);
|
2013-08-02 00:41:57 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
static bool
|
2012-07-03 17:44:22 -07:00
|
|
|
date_getUTCHours(JSContext *cx, unsigned argc, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-09-08 21:02:26 -07:00
|
|
|
CallArgs args = CallArgsFromVp(argc, vp);
|
2013-06-20 20:17:15 -07:00
|
|
|
return CallNonGenericMethod<IsDate, DateObject::getUTCHours_impl>(cx, args);
|
2012-07-03 17:44:22 -07:00
|
|
|
}
|
2011-09-08 21:18:23 -07:00
|
|
|
|
2014-01-24 20:14:56 -08:00
|
|
|
/* static */ MOZ_ALWAYS_INLINE bool
|
2013-06-20 20:17:15 -07:00
|
|
|
DateObject::getMinutes_impl(JSContext *cx, CallArgs args)
|
2012-07-03 17:44:22 -07:00
|
|
|
{
|
2013-06-20 20:17:15 -07:00
|
|
|
DateObject *dateObj = &args.thisv().toObject().as<DateObject>();
|
|
|
|
dateObj->fillLocalTimeSlots(&cx->runtime()->dateTimeInfo);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-06-20 20:17:15 -07:00
|
|
|
args.rval().set(dateObj->getReservedSlot(LOCAL_MINUTES_SLOT));
|
2011-01-26 13:37:45 -08:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
static bool
|
2012-07-03 17:44:22 -07:00
|
|
|
date_getMinutes(JSContext *cx, unsigned argc, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-09-08 21:02:26 -07:00
|
|
|
CallArgs args = CallArgsFromVp(argc, vp);
|
2013-06-20 20:17:15 -07:00
|
|
|
return CallNonGenericMethod<IsDate, DateObject::getMinutes_impl>(cx, args);
|
2012-07-03 17:44:22 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-01-24 20:14:56 -08:00
|
|
|
/* static */ MOZ_ALWAYS_INLINE bool
|
2013-06-20 20:17:15 -07:00
|
|
|
DateObject::getUTCMinutes_impl(JSContext *cx, CallArgs args)
|
2012-07-03 17:44:22 -07:00
|
|
|
{
|
2013-06-20 20:17:15 -07:00
|
|
|
double result = args.thisv().toObject().as<DateObject>().UTCTime().toNumber();
|
2013-05-01 13:55:13 -07:00
|
|
|
if (IsFinite(result))
|
2007-03-22 10:30:00 -07:00
|
|
|
result = MinFromTime(result);
|
|
|
|
|
2011-09-08 21:02:26 -07:00
|
|
|
args.rval().setNumber(result);
|
2011-01-26 13:37:45 -08:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
static bool
|
2012-07-03 17:44:22 -07:00
|
|
|
date_getUTCMinutes(JSContext *cx, unsigned argc, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-09-08 21:02:26 -07:00
|
|
|
CallArgs args = CallArgsFromVp(argc, vp);
|
2013-06-20 20:17:15 -07:00
|
|
|
return CallNonGenericMethod<IsDate, DateObject::getUTCMinutes_impl>(cx, args);
|
2012-07-03 17:44:22 -07:00
|
|
|
}
|
2011-09-08 21:18:23 -07:00
|
|
|
|
2012-07-03 17:44:22 -07:00
|
|
|
/* Date.getSeconds is mapped to getUTCSeconds */
|
2011-01-26 13:37:45 -08:00
|
|
|
|
2014-01-24 20:14:56 -08:00
|
|
|
/* static */ MOZ_ALWAYS_INLINE bool
|
2013-06-20 20:17:15 -07:00
|
|
|
DateObject::getUTCSeconds_impl(JSContext *cx, CallArgs args)
|
2012-07-03 17:44:22 -07:00
|
|
|
{
|
2013-06-20 20:17:15 -07:00
|
|
|
DateObject *dateObj = &args.thisv().toObject().as<DateObject>();
|
|
|
|
dateObj->fillLocalTimeSlots(&cx->runtime()->dateTimeInfo);
|
2012-07-03 17:44:22 -07:00
|
|
|
|
2013-06-20 20:17:15 -07:00
|
|
|
args.rval().set(dateObj->getReservedSlot(LOCAL_SECONDS_SLOT));
|
2011-01-26 13:37:45 -08:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
static bool
|
2012-07-03 17:44:22 -07:00
|
|
|
date_getUTCSeconds(JSContext *cx, unsigned argc, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-09-08 21:02:26 -07:00
|
|
|
CallArgs args = CallArgsFromVp(argc, vp);
|
2013-06-20 20:17:15 -07:00
|
|
|
return CallNonGenericMethod<IsDate, DateObject::getUTCSeconds_impl>(cx, args);
|
2012-07-03 17:44:22 -07:00
|
|
|
}
|
2011-09-08 21:02:26 -07:00
|
|
|
|
2012-07-03 17:44:22 -07:00
|
|
|
/* Date.getMilliseconds is mapped to getUTCMilliseconds */
|
|
|
|
|
2014-01-24 20:14:56 -08:00
|
|
|
/* static */ MOZ_ALWAYS_INLINE bool
|
2013-06-20 20:17:15 -07:00
|
|
|
DateObject::getUTCMilliseconds_impl(JSContext *cx, CallArgs args)
|
2012-07-03 17:44:22 -07:00
|
|
|
{
|
2013-06-20 20:17:15 -07:00
|
|
|
double result = args.thisv().toObject().as<DateObject>().UTCTime().toNumber();
|
2013-05-01 13:55:13 -07:00
|
|
|
if (IsFinite(result))
|
2007-03-22 10:30:00 -07:00
|
|
|
result = msFromTime(result);
|
|
|
|
|
2011-09-08 21:02:26 -07:00
|
|
|
args.rval().setNumber(result);
|
2011-01-26 13:37:45 -08:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
static bool
|
2012-07-03 17:44:22 -07:00
|
|
|
date_getUTCMilliseconds(JSContext *cx, unsigned argc, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-09-08 21:02:26 -07:00
|
|
|
CallArgs args = CallArgsFromVp(argc, vp);
|
2013-06-20 20:17:15 -07:00
|
|
|
return CallNonGenericMethod<IsDate, DateObject::getUTCMilliseconds_impl>(cx, args);
|
2012-07-03 17:44:22 -07:00
|
|
|
}
|
2011-09-08 21:18:23 -07:00
|
|
|
|
2014-01-24 20:14:56 -08:00
|
|
|
/* static */ MOZ_ALWAYS_INLINE bool
|
2013-06-20 20:17:15 -07:00
|
|
|
DateObject::getTimezoneOffset_impl(JSContext *cx, CallArgs args)
|
2012-07-03 17:44:22 -07:00
|
|
|
{
|
2013-06-20 20:17:15 -07:00
|
|
|
DateObject *dateObj = &args.thisv().toObject().as<DateObject>();
|
|
|
|
double utctime = dateObj->UTCTime().toNumber();
|
|
|
|
double localtime = dateObj->cachedLocalTime(&cx->runtime()->dateTimeInfo);
|
2007-08-01 21:33:52 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/*
|
2007-08-01 21:33:52 -07:00
|
|
|
* Return the time zone offset in minutes for the current locale that is
|
|
|
|
* appropriate for this time. This value would be a constant except for
|
|
|
|
* daylight savings time.
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
2011-09-08 21:18:23 -07:00
|
|
|
double result = (utctime - localtime) / msPerMinute;
|
2011-09-08 21:02:26 -07:00
|
|
|
args.rval().setNumber(result);
|
2011-01-26 13:37:45 -08:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
static bool
|
2012-07-03 17:44:22 -07:00
|
|
|
date_getTimezoneOffset(JSContext *cx, unsigned argc, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-09-08 21:02:26 -07:00
|
|
|
CallArgs args = CallArgsFromVp(argc, vp);
|
2013-06-20 20:17:15 -07:00
|
|
|
return CallNonGenericMethod<IsDate, DateObject::getTimezoneOffset_impl>(cx, args);
|
2012-07-03 17:44:22 -07:00
|
|
|
}
|
2011-01-26 13:37:45 -08:00
|
|
|
|
2014-01-24 20:14:56 -08:00
|
|
|
MOZ_ALWAYS_INLINE bool
|
2012-07-03 17:44:22 -07:00
|
|
|
date_setTime_impl(JSContext *cx, CallArgs args)
|
|
|
|
{
|
2013-06-20 20:17:15 -07:00
|
|
|
Rooted<DateObject*> dateObj(cx, &args.thisv().toObject().as<DateObject>());
|
2011-09-08 21:02:26 -07:00
|
|
|
if (args.length() == 0) {
|
2014-09-15 08:49:37 -07:00
|
|
|
dateObj->setUTCTime(GenericNaN(), args.rval());
|
2009-10-24 11:42:29 -07:00
|
|
|
return true;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-02-24 14:19:52 -08:00
|
|
|
double result;
|
2013-06-26 16:26:45 -07:00
|
|
|
if (!ToNumber(cx, args[0], &result))
|
2009-10-24 11:42:29 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-09-15 08:49:37 -07:00
|
|
|
dateObj->setUTCTime(TimeClip(result), args.rval());
|
2012-10-07 18:01:35 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
static bool
|
2012-07-03 17:44:22 -07:00
|
|
|
date_setTime(JSContext *cx, unsigned argc, Value *vp)
|
|
|
|
{
|
|
|
|
CallArgs args = CallArgsFromVp(argc, vp);
|
2012-08-16 11:40:05 -07:00
|
|
|
return CallNonGenericMethod<IsDate, date_setTime_impl>(cx, args);
|
2012-07-03 17:44:22 -07:00
|
|
|
}
|
|
|
|
|
2012-07-06 13:53:10 -07:00
|
|
|
static bool
|
|
|
|
GetMsecsOrDefault(JSContext *cx, const CallArgs &args, unsigned i, double t, double *millis)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-07-06 13:53:10 -07:00
|
|
|
if (args.length() <= i) {
|
|
|
|
*millis = msFromTime(t);
|
2009-10-24 11:42:29 -07:00
|
|
|
return true;
|
|
|
|
}
|
2013-06-26 16:26:45 -07:00
|
|
|
return ToNumber(cx, args[i], millis);
|
2012-07-06 13:53:10 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-07-06 13:53:10 -07:00
|
|
|
static bool
|
|
|
|
GetSecsOrDefault(JSContext *cx, const CallArgs &args, unsigned i, double t, double *sec)
|
|
|
|
{
|
|
|
|
if (args.length() <= i) {
|
|
|
|
*sec = SecFromTime(t);
|
2012-04-06 18:06:14 -07:00
|
|
|
return true;
|
|
|
|
}
|
2013-06-26 16:26:45 -07:00
|
|
|
return ToNumber(cx, args[i], sec);
|
2012-07-06 13:53:10 -07:00
|
|
|
}
|
2012-04-06 18:06:14 -07:00
|
|
|
|
2012-07-06 13:53:10 -07:00
|
|
|
static bool
|
|
|
|
GetMinsOrDefault(JSContext *cx, const CallArgs &args, unsigned i, double t, double *mins)
|
|
|
|
{
|
|
|
|
if (args.length() <= i) {
|
|
|
|
*mins = MinFromTime(t);
|
2012-04-06 18:06:14 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2013-06-26 16:26:45 -07:00
|
|
|
return ToNumber(cx, args[i], mins);
|
2012-07-06 13:53:10 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-07-06 13:53:10 -07:00
|
|
|
/* ES5 15.9.5.28. */
|
2014-01-24 20:14:56 -08:00
|
|
|
MOZ_ALWAYS_INLINE bool
|
2012-07-03 17:44:22 -07:00
|
|
|
date_setMilliseconds_impl(JSContext *cx, CallArgs args)
|
2012-07-06 13:53:10 -07:00
|
|
|
{
|
2013-06-20 20:17:15 -07:00
|
|
|
Rooted<DateObject*> dateObj(cx, &args.thisv().toObject().as<DateObject>());
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-07-06 13:53:10 -07:00
|
|
|
/* Step 1. */
|
2013-06-20 20:17:15 -07:00
|
|
|
double t = LocalTime(dateObj->UTCTime().toNumber(), &cx->runtime()->dateTimeInfo);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-07-06 13:53:10 -07:00
|
|
|
/* Step 2. */
|
|
|
|
double milli;
|
2013-06-26 16:26:45 -07:00
|
|
|
if (!ToNumber(cx, args.get(0), &milli))
|
2012-07-06 13:53:10 -07:00
|
|
|
return false;
|
|
|
|
double time = MakeTime(HourFromTime(t), MinFromTime(t), SecFromTime(t), milli);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-07-06 13:53:10 -07:00
|
|
|
/* Step 3. */
|
2013-06-10 14:22:18 -07:00
|
|
|
double u = TimeClip(UTC(MakeDate(Day(t), time), &cx->runtime()->dateTimeInfo));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-07-06 13:53:10 -07:00
|
|
|
/* Steps 4-5. */
|
2014-09-15 08:49:37 -07:00
|
|
|
dateObj->setUTCTime(u, args.rval());
|
2012-10-07 18:01:35 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
static bool
|
2012-07-03 17:44:22 -07:00
|
|
|
date_setMilliseconds(JSContext *cx, unsigned argc, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-07-06 13:53:10 -07:00
|
|
|
CallArgs args = CallArgsFromVp(argc, vp);
|
2012-08-16 11:40:05 -07:00
|
|
|
return CallNonGenericMethod<IsDate, date_setMilliseconds_impl>(cx, args);
|
2012-07-03 17:44:22 -07:00
|
|
|
}
|
2012-07-06 13:53:10 -07:00
|
|
|
|
2012-07-03 17:44:22 -07:00
|
|
|
/* ES5 15.9.5.29. */
|
2014-01-24 20:14:56 -08:00
|
|
|
MOZ_ALWAYS_INLINE bool
|
2012-07-03 17:44:22 -07:00
|
|
|
date_setUTCMilliseconds_impl(JSContext *cx, CallArgs args)
|
|
|
|
{
|
2013-06-20 20:17:15 -07:00
|
|
|
Rooted<DateObject*> dateObj(cx, &args.thisv().toObject().as<DateObject>());
|
2012-07-06 13:53:10 -07:00
|
|
|
|
|
|
|
/* Step 1. */
|
2013-06-20 20:17:15 -07:00
|
|
|
double t = dateObj->UTCTime().toNumber();
|
2012-07-06 13:53:10 -07:00
|
|
|
|
|
|
|
/* Step 2. */
|
|
|
|
double milli;
|
2013-06-26 16:26:45 -07:00
|
|
|
if (!ToNumber(cx, args.get(0), &milli))
|
2012-07-06 13:53:10 -07:00
|
|
|
return false;
|
|
|
|
double time = MakeTime(HourFromTime(t), MinFromTime(t), SecFromTime(t), milli);
|
|
|
|
|
|
|
|
/* Step 3. */
|
|
|
|
double v = TimeClip(MakeDate(Day(t), time));
|
|
|
|
|
|
|
|
/* Steps 4-5. */
|
2014-09-15 08:49:37 -07:00
|
|
|
dateObj->setUTCTime(v, args.rval());
|
2012-10-07 18:01:35 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
static bool
|
2012-07-03 17:44:22 -07:00
|
|
|
date_setUTCMilliseconds(JSContext *cx, unsigned argc, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-07-06 13:53:10 -07:00
|
|
|
CallArgs args = CallArgsFromVp(argc, vp);
|
2012-08-16 11:40:05 -07:00
|
|
|
return CallNonGenericMethod<IsDate, date_setUTCMilliseconds_impl>(cx, args);
|
2012-07-03 17:44:22 -07:00
|
|
|
}
|
2012-07-06 13:53:10 -07:00
|
|
|
|
2012-07-03 17:44:22 -07:00
|
|
|
/* ES5 15.9.5.30. */
|
2014-01-24 20:14:56 -08:00
|
|
|
MOZ_ALWAYS_INLINE bool
|
2012-07-03 17:44:22 -07:00
|
|
|
date_setSeconds_impl(JSContext *cx, CallArgs args)
|
|
|
|
{
|
2013-06-20 20:17:15 -07:00
|
|
|
Rooted<DateObject*> dateObj(cx, &args.thisv().toObject().as<DateObject>());
|
2012-07-06 13:53:10 -07:00
|
|
|
|
|
|
|
/* Step 1. */
|
2013-06-20 20:17:15 -07:00
|
|
|
double t = LocalTime(dateObj->UTCTime().toNumber(), &cx->runtime()->dateTimeInfo);
|
2012-07-06 13:53:10 -07:00
|
|
|
|
|
|
|
/* Step 2. */
|
|
|
|
double s;
|
2013-06-26 16:26:45 -07:00
|
|
|
if (!ToNumber(cx, args.get(0), &s))
|
2012-07-06 13:53:10 -07:00
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Step 3. */
|
|
|
|
double milli;
|
|
|
|
if (!GetMsecsOrDefault(cx, args, 1, t, &milli))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Step 4. */
|
|
|
|
double date = MakeDate(Day(t), MakeTime(HourFromTime(t), MinFromTime(t), s, milli));
|
|
|
|
|
|
|
|
/* Step 5. */
|
2013-06-10 14:22:18 -07:00
|
|
|
double u = TimeClip(UTC(date, &cx->runtime()->dateTimeInfo));
|
2012-07-06 13:53:10 -07:00
|
|
|
|
|
|
|
/* Steps 6-7. */
|
2014-09-15 08:49:37 -07:00
|
|
|
dateObj->setUTCTime(u, args.rval());
|
2012-10-07 18:01:35 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-07-06 13:53:10 -07:00
|
|
|
/* ES5 15.9.5.31. */
|
2013-08-02 00:41:57 -07:00
|
|
|
static bool
|
2012-07-03 17:44:22 -07:00
|
|
|
date_setSeconds(JSContext *cx, unsigned argc, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-07-06 13:53:10 -07:00
|
|
|
CallArgs args = CallArgsFromVp(argc, vp);
|
2012-08-16 11:40:05 -07:00
|
|
|
return CallNonGenericMethod<IsDate, date_setSeconds_impl>(cx, args);
|
2012-07-03 17:44:22 -07:00
|
|
|
}
|
2012-07-06 13:53:10 -07:00
|
|
|
|
2014-01-24 20:14:56 -08:00
|
|
|
MOZ_ALWAYS_INLINE bool
|
2012-07-03 17:44:22 -07:00
|
|
|
date_setUTCSeconds_impl(JSContext *cx, CallArgs args)
|
|
|
|
{
|
2013-06-20 20:17:15 -07:00
|
|
|
Rooted<DateObject*> dateObj(cx, &args.thisv().toObject().as<DateObject>());
|
2012-07-06 13:53:10 -07:00
|
|
|
|
|
|
|
/* Step 1. */
|
2013-06-20 20:17:15 -07:00
|
|
|
double t = dateObj->UTCTime().toNumber();
|
2012-07-06 13:53:10 -07:00
|
|
|
|
|
|
|
/* Step 2. */
|
|
|
|
double s;
|
2013-06-26 16:26:45 -07:00
|
|
|
if (!ToNumber(cx, args.get(0), &s))
|
2012-07-06 13:53:10 -07:00
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Step 3. */
|
|
|
|
double milli;
|
|
|
|
if (!GetMsecsOrDefault(cx, args, 1, t, &milli))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Step 4. */
|
|
|
|
double date = MakeDate(Day(t), MakeTime(HourFromTime(t), MinFromTime(t), s, milli));
|
|
|
|
|
|
|
|
/* Step 5. */
|
|
|
|
double v = TimeClip(date);
|
|
|
|
|
|
|
|
/* Steps 6-7. */
|
2014-09-15 08:49:37 -07:00
|
|
|
dateObj->setUTCTime(v, args.rval());
|
2012-10-07 18:01:35 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-07-06 13:53:10 -07:00
|
|
|
/* ES5 15.9.5.32. */
|
2013-08-02 00:41:57 -07:00
|
|
|
static bool
|
2012-07-03 17:44:22 -07:00
|
|
|
date_setUTCSeconds(JSContext *cx, unsigned argc, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-07-06 13:53:10 -07:00
|
|
|
CallArgs args = CallArgsFromVp(argc, vp);
|
2012-08-16 11:40:05 -07:00
|
|
|
return CallNonGenericMethod<IsDate, date_setUTCSeconds_impl>(cx, args);
|
2012-07-03 17:44:22 -07:00
|
|
|
}
|
2012-07-06 13:53:10 -07:00
|
|
|
|
2014-01-24 20:14:56 -08:00
|
|
|
MOZ_ALWAYS_INLINE bool
|
2012-07-03 17:44:22 -07:00
|
|
|
date_setMinutes_impl(JSContext *cx, CallArgs args)
|
|
|
|
{
|
2013-06-20 20:17:15 -07:00
|
|
|
Rooted<DateObject*> dateObj(cx, &args.thisv().toObject().as<DateObject>());
|
2012-07-06 13:53:10 -07:00
|
|
|
|
|
|
|
/* Step 1. */
|
2013-06-20 20:17:15 -07:00
|
|
|
double t = LocalTime(dateObj->UTCTime().toNumber(), &cx->runtime()->dateTimeInfo);
|
2012-07-06 13:53:10 -07:00
|
|
|
|
|
|
|
/* Step 2. */
|
|
|
|
double m;
|
2013-06-26 16:26:45 -07:00
|
|
|
if (!ToNumber(cx, args.get(0), &m))
|
2012-07-06 13:53:10 -07:00
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Step 3. */
|
|
|
|
double s;
|
|
|
|
if (!GetSecsOrDefault(cx, args, 1, t, &s))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Step 4. */
|
|
|
|
double milli;
|
|
|
|
if (!GetMsecsOrDefault(cx, args, 2, t, &milli))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Step 5. */
|
|
|
|
double date = MakeDate(Day(t), MakeTime(HourFromTime(t), m, s, milli));
|
|
|
|
|
|
|
|
/* Step 6. */
|
2013-06-10 14:22:18 -07:00
|
|
|
double u = TimeClip(UTC(date, &cx->runtime()->dateTimeInfo));
|
2012-07-06 13:53:10 -07:00
|
|
|
|
|
|
|
/* Steps 7-8. */
|
2014-09-15 08:49:37 -07:00
|
|
|
dateObj->setUTCTime(u, args.rval());
|
2012-10-07 18:01:35 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-07-06 13:53:10 -07:00
|
|
|
/* ES5 15.9.5.33. */
|
2013-08-02 00:41:57 -07:00
|
|
|
static bool
|
2012-07-03 17:44:22 -07:00
|
|
|
date_setMinutes(JSContext *cx, unsigned argc, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-07-06 13:53:10 -07:00
|
|
|
CallArgs args = CallArgsFromVp(argc, vp);
|
2012-08-16 11:40:05 -07:00
|
|
|
return CallNonGenericMethod<IsDate, date_setMinutes_impl>(cx, args);
|
2012-07-03 17:44:22 -07:00
|
|
|
}
|
2012-07-06 13:53:10 -07:00
|
|
|
|
2014-01-24 20:14:56 -08:00
|
|
|
MOZ_ALWAYS_INLINE bool
|
2012-07-03 17:44:22 -07:00
|
|
|
date_setUTCMinutes_impl(JSContext *cx, CallArgs args)
|
|
|
|
{
|
2013-06-20 20:17:15 -07:00
|
|
|
Rooted<DateObject*> dateObj(cx, &args.thisv().toObject().as<DateObject>());
|
2012-07-06 13:53:10 -07:00
|
|
|
|
|
|
|
/* Step 1. */
|
2013-06-20 20:17:15 -07:00
|
|
|
double t = dateObj->UTCTime().toNumber();
|
2012-07-06 13:53:10 -07:00
|
|
|
|
|
|
|
/* Step 2. */
|
|
|
|
double m;
|
2013-06-26 16:26:45 -07:00
|
|
|
if (!ToNumber(cx, args.get(0), &m))
|
2012-07-06 13:53:10 -07:00
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Step 3. */
|
|
|
|
double s;
|
|
|
|
if (!GetSecsOrDefault(cx, args, 1, t, &s))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Step 4. */
|
|
|
|
double milli;
|
|
|
|
if (!GetMsecsOrDefault(cx, args, 2, t, &milli))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Step 5. */
|
|
|
|
double date = MakeDate(Day(t), MakeTime(HourFromTime(t), m, s, milli));
|
|
|
|
|
|
|
|
/* Step 6. */
|
|
|
|
double v = TimeClip(date);
|
|
|
|
|
|
|
|
/* Steps 7-8. */
|
2014-09-15 08:49:37 -07:00
|
|
|
dateObj->setUTCTime(v, args.rval());
|
2012-10-07 18:01:35 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-07-06 13:53:10 -07:00
|
|
|
/* ES5 15.9.5.34. */
|
2013-08-02 00:41:57 -07:00
|
|
|
static bool
|
2012-07-03 17:44:22 -07:00
|
|
|
date_setUTCMinutes(JSContext *cx, unsigned argc, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-07-06 13:53:10 -07:00
|
|
|
CallArgs args = CallArgsFromVp(argc, vp);
|
2012-08-16 11:40:05 -07:00
|
|
|
return CallNonGenericMethod<IsDate, date_setUTCMinutes_impl>(cx, args);
|
2012-07-03 17:44:22 -07:00
|
|
|
}
|
2012-07-06 13:53:10 -07:00
|
|
|
|
2014-01-24 20:14:56 -08:00
|
|
|
MOZ_ALWAYS_INLINE bool
|
2012-07-03 17:44:22 -07:00
|
|
|
date_setHours_impl(JSContext *cx, CallArgs args)
|
|
|
|
{
|
2013-06-20 20:17:15 -07:00
|
|
|
Rooted<DateObject*> dateObj(cx, &args.thisv().toObject().as<DateObject>());
|
2012-07-06 13:53:10 -07:00
|
|
|
|
|
|
|
/* Step 1. */
|
2013-06-20 20:17:15 -07:00
|
|
|
double t = LocalTime(dateObj->UTCTime().toNumber(), &cx->runtime()->dateTimeInfo);
|
2012-07-06 13:53:10 -07:00
|
|
|
|
|
|
|
/* Step 2. */
|
|
|
|
double h;
|
2013-06-26 16:26:45 -07:00
|
|
|
if (!ToNumber(cx, args.get(0), &h))
|
2012-07-06 13:53:10 -07:00
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Step 3. */
|
|
|
|
double m;
|
|
|
|
if (!GetMinsOrDefault(cx, args, 1, t, &m))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Step 4. */
|
|
|
|
double s;
|
|
|
|
if (!GetSecsOrDefault(cx, args, 2, t, &s))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Step 5. */
|
|
|
|
double milli;
|
|
|
|
if (!GetMsecsOrDefault(cx, args, 3, t, &milli))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Step 6. */
|
|
|
|
double date = MakeDate(Day(t), MakeTime(h, m, s, milli));
|
|
|
|
|
|
|
|
/* Step 6. */
|
2013-06-10 14:22:18 -07:00
|
|
|
double u = TimeClip(UTC(date, &cx->runtime()->dateTimeInfo));
|
2012-07-06 13:53:10 -07:00
|
|
|
|
|
|
|
/* Steps 7-8. */
|
2014-09-15 08:49:37 -07:00
|
|
|
dateObj->setUTCTime(u, args.rval());
|
2012-10-07 18:01:35 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-07-06 13:53:10 -07:00
|
|
|
/* ES5 15.9.5.35. */
|
2013-08-02 00:41:57 -07:00
|
|
|
static bool
|
2012-07-03 17:44:22 -07:00
|
|
|
date_setHours(JSContext *cx, unsigned argc, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-07-06 13:53:10 -07:00
|
|
|
CallArgs args = CallArgsFromVp(argc, vp);
|
2012-08-16 11:40:05 -07:00
|
|
|
return CallNonGenericMethod<IsDate, date_setHours_impl>(cx, args);
|
2012-07-03 17:44:22 -07:00
|
|
|
}
|
2012-07-06 13:53:10 -07:00
|
|
|
|
2014-01-24 20:14:56 -08:00
|
|
|
MOZ_ALWAYS_INLINE bool
|
2012-07-03 17:44:22 -07:00
|
|
|
date_setUTCHours_impl(JSContext *cx, CallArgs args)
|
|
|
|
{
|
2013-06-20 20:17:15 -07:00
|
|
|
Rooted<DateObject*> dateObj(cx, &args.thisv().toObject().as<DateObject>());
|
2012-07-06 13:53:10 -07:00
|
|
|
|
|
|
|
/* Step 1. */
|
2013-06-20 20:17:15 -07:00
|
|
|
double t = dateObj->UTCTime().toNumber();
|
2012-07-06 13:53:10 -07:00
|
|
|
|
|
|
|
/* Step 2. */
|
|
|
|
double h;
|
2013-06-26 16:26:45 -07:00
|
|
|
if (!ToNumber(cx, args.get(0), &h))
|
2012-07-06 13:53:10 -07:00
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Step 3. */
|
|
|
|
double m;
|
|
|
|
if (!GetMinsOrDefault(cx, args, 1, t, &m))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Step 4. */
|
|
|
|
double s;
|
|
|
|
if (!GetSecsOrDefault(cx, args, 2, t, &s))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Step 5. */
|
|
|
|
double milli;
|
|
|
|
if (!GetMsecsOrDefault(cx, args, 3, t, &milli))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Step 6. */
|
|
|
|
double newDate = MakeDate(Day(t), MakeTime(h, m, s, milli));
|
|
|
|
|
|
|
|
/* Step 7. */
|
|
|
|
double v = TimeClip(newDate);
|
|
|
|
|
|
|
|
/* Steps 8-9. */
|
2014-09-15 08:49:37 -07:00
|
|
|
dateObj->setUTCTime(v, args.rval());
|
2012-10-07 18:01:35 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-07-06 13:53:11 -07:00
|
|
|
/* ES5 15.9.5.36. */
|
2013-08-02 00:41:57 -07:00
|
|
|
static bool
|
2012-07-03 17:44:22 -07:00
|
|
|
date_setUTCHours(JSContext *cx, unsigned argc, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-09-08 21:02:26 -07:00
|
|
|
CallArgs args = CallArgsFromVp(argc, vp);
|
2012-08-16 11:40:05 -07:00
|
|
|
return CallNonGenericMethod<IsDate, date_setUTCHours_impl>(cx, args);
|
2012-07-03 17:44:22 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-01-24 20:14:56 -08:00
|
|
|
MOZ_ALWAYS_INLINE bool
|
2012-07-03 17:44:22 -07:00
|
|
|
date_setDate_impl(JSContext *cx, CallArgs args)
|
|
|
|
{
|
2013-06-20 20:17:15 -07:00
|
|
|
Rooted<DateObject*> dateObj(cx, &args.thisv().toObject().as<DateObject>());
|
2011-01-26 13:37:45 -08:00
|
|
|
|
2012-07-06 13:53:11 -07:00
|
|
|
/* Step 1. */
|
2013-06-20 20:17:15 -07:00
|
|
|
double t = LocalTime(dateObj->UTCTime().toNumber(), &cx->runtime()->dateTimeInfo);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-07-06 13:53:11 -07:00
|
|
|
/* Step 2. */
|
2013-06-20 20:17:15 -07:00
|
|
|
double date;
|
2013-06-26 16:26:45 -07:00
|
|
|
if (!ToNumber(cx, args.get(0), &date))
|
2012-07-06 13:53:11 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-07-06 13:53:11 -07:00
|
|
|
/* Step 3. */
|
2013-06-20 20:17:15 -07:00
|
|
|
double newDate = MakeDate(MakeDay(YearFromTime(t), MonthFromTime(t), date), TimeWithinDay(t));
|
2012-04-06 18:06:14 -07:00
|
|
|
|
2012-07-06 13:53:11 -07:00
|
|
|
/* Step 4. */
|
2013-06-10 14:22:18 -07:00
|
|
|
double u = TimeClip(UTC(newDate, &cx->runtime()->dateTimeInfo));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-07-06 13:53:11 -07:00
|
|
|
/* Steps 5-6. */
|
2014-09-15 08:49:37 -07:00
|
|
|
dateObj->setUTCTime(u, args.rval());
|
2012-10-07 18:01:35 -07:00
|
|
|
return true;
|
2012-07-06 13:53:11 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-07-06 13:53:11 -07:00
|
|
|
/* ES5 15.9.5.37. */
|
2013-08-02 00:41:57 -07:00
|
|
|
static bool
|
2012-07-03 17:44:22 -07:00
|
|
|
date_setDate(JSContext *cx, unsigned argc, Value *vp)
|
2012-07-06 13:53:11 -07:00
|
|
|
{
|
|
|
|
CallArgs args = CallArgsFromVp(argc, vp);
|
2012-08-16 11:40:05 -07:00
|
|
|
return CallNonGenericMethod<IsDate, date_setDate_impl>(cx, args);
|
2012-07-03 17:44:22 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-01-24 20:14:56 -08:00
|
|
|
MOZ_ALWAYS_INLINE bool
|
2012-07-03 17:44:22 -07:00
|
|
|
date_setUTCDate_impl(JSContext *cx, CallArgs args)
|
|
|
|
{
|
2013-06-20 20:17:15 -07:00
|
|
|
Rooted<DateObject*> dateObj(cx, &args.thisv().toObject().as<DateObject>());
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-07-06 13:53:11 -07:00
|
|
|
/* Step 1. */
|
2013-06-20 20:17:15 -07:00
|
|
|
double t = dateObj->UTCTime().toNumber();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-07-06 13:53:11 -07:00
|
|
|
/* Step 2. */
|
2013-06-20 20:17:15 -07:00
|
|
|
double date;
|
2013-06-26 16:26:45 -07:00
|
|
|
if (!ToNumber(cx, args.get(0), &date))
|
2012-07-06 13:53:11 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-07-06 13:53:11 -07:00
|
|
|
/* Step 3. */
|
2013-06-20 20:17:15 -07:00
|
|
|
double newDate = MakeDate(MakeDay(YearFromTime(t), MonthFromTime(t), date), TimeWithinDay(t));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-07-06 13:53:11 -07:00
|
|
|
/* Step 4. */
|
|
|
|
double v = TimeClip(newDate);
|
|
|
|
|
|
|
|
/* Steps 5-6. */
|
2014-09-15 08:49:37 -07:00
|
|
|
dateObj->setUTCTime(v, args.rval());
|
2012-10-07 18:01:35 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
static bool
|
2012-07-03 17:44:22 -07:00
|
|
|
date_setUTCDate(JSContext *cx, unsigned argc, Value *vp)
|
|
|
|
{
|
|
|
|
CallArgs args = CallArgsFromVp(argc, vp);
|
2012-08-16 11:40:05 -07:00
|
|
|
return CallNonGenericMethod<IsDate, date_setUTCDate_impl>(cx, args);
|
2012-07-03 17:44:22 -07:00
|
|
|
}
|
|
|
|
|
2012-07-06 13:53:11 -07:00
|
|
|
static bool
|
|
|
|
GetDateOrDefault(JSContext *cx, const CallArgs &args, unsigned i, double t, double *date)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-07-06 13:53:11 -07:00
|
|
|
if (args.length() <= i) {
|
|
|
|
*date = DateFromTime(t);
|
|
|
|
return true;
|
|
|
|
}
|
2013-06-26 16:26:45 -07:00
|
|
|
return ToNumber(cx, args[i], date);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-07-06 13:53:11 -07:00
|
|
|
static bool
|
|
|
|
GetMonthOrDefault(JSContext *cx, const CallArgs &args, unsigned i, double t, double *month)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-07-06 13:53:11 -07:00
|
|
|
if (args.length() <= i) {
|
|
|
|
*month = MonthFromTime(t);
|
|
|
|
return true;
|
|
|
|
}
|
2013-06-26 16:26:45 -07:00
|
|
|
return ToNumber(cx, args[i], month);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-07-06 13:53:11 -07:00
|
|
|
/* ES5 15.9.5.38. */
|
2014-01-24 20:14:56 -08:00
|
|
|
MOZ_ALWAYS_INLINE bool
|
2012-07-03 17:44:22 -07:00
|
|
|
date_setMonth_impl(JSContext *cx, CallArgs args)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-06-20 20:17:15 -07:00
|
|
|
Rooted<DateObject*> dateObj(cx, &args.thisv().toObject().as<DateObject>());
|
2012-07-06 13:53:11 -07:00
|
|
|
|
|
|
|
/* Step 1. */
|
2013-06-20 20:17:15 -07:00
|
|
|
double t = LocalTime(dateObj->UTCTime().toNumber(), &cx->runtime()->dateTimeInfo);
|
2012-07-06 13:53:11 -07:00
|
|
|
|
|
|
|
/* Step 2. */
|
|
|
|
double m;
|
2013-06-26 16:26:45 -07:00
|
|
|
if (!ToNumber(cx, args.get(0), &m))
|
2012-07-06 13:53:11 -07:00
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Step 3. */
|
2013-06-20 20:17:15 -07:00
|
|
|
double date;
|
|
|
|
if (!GetDateOrDefault(cx, args, 1, t, &date))
|
2012-07-06 13:53:11 -07:00
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Step 4. */
|
2013-06-20 20:17:15 -07:00
|
|
|
double newDate = MakeDate(MakeDay(YearFromTime(t), m, date), TimeWithinDay(t));
|
2012-07-06 13:53:11 -07:00
|
|
|
|
|
|
|
/* Step 5. */
|
2013-06-10 14:22:18 -07:00
|
|
|
double u = TimeClip(UTC(newDate, &cx->runtime()->dateTimeInfo));
|
2012-07-06 13:53:11 -07:00
|
|
|
|
|
|
|
/* Steps 6-7. */
|
2014-09-15 08:49:37 -07:00
|
|
|
dateObj->setUTCTime(u, args.rval());
|
2012-10-07 18:01:35 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
static bool
|
2012-07-03 17:44:22 -07:00
|
|
|
date_setMonth(JSContext *cx, unsigned argc, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-07-06 13:53:11 -07:00
|
|
|
CallArgs args = CallArgsFromVp(argc, vp);
|
2012-08-16 11:40:05 -07:00
|
|
|
return CallNonGenericMethod<IsDate, date_setMonth_impl>(cx, args);
|
2012-07-03 17:44:22 -07:00
|
|
|
}
|
2012-07-06 13:53:11 -07:00
|
|
|
|
2012-07-03 17:44:22 -07:00
|
|
|
/* ES5 15.9.5.39. */
|
2014-01-24 20:14:56 -08:00
|
|
|
MOZ_ALWAYS_INLINE bool
|
2012-07-03 17:44:22 -07:00
|
|
|
date_setUTCMonth_impl(JSContext *cx, CallArgs args)
|
|
|
|
{
|
2013-06-20 20:17:15 -07:00
|
|
|
Rooted<DateObject*> dateObj(cx, &args.thisv().toObject().as<DateObject>());
|
2012-07-06 13:53:11 -07:00
|
|
|
|
|
|
|
/* Step 1. */
|
2013-06-20 20:17:15 -07:00
|
|
|
double t = dateObj->UTCTime().toNumber();
|
2012-07-06 13:53:11 -07:00
|
|
|
|
|
|
|
/* Step 2. */
|
|
|
|
double m;
|
2013-06-26 16:26:45 -07:00
|
|
|
if (!ToNumber(cx, args.get(0), &m))
|
2012-07-06 13:53:11 -07:00
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Step 3. */
|
2013-06-20 20:17:15 -07:00
|
|
|
double date;
|
|
|
|
if (!GetDateOrDefault(cx, args, 1, t, &date))
|
2012-07-06 13:53:11 -07:00
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Step 4. */
|
2013-06-20 20:17:15 -07:00
|
|
|
double newDate = MakeDate(MakeDay(YearFromTime(t), m, date), TimeWithinDay(t));
|
2012-07-06 13:53:11 -07:00
|
|
|
|
|
|
|
/* Step 5. */
|
|
|
|
double v = TimeClip(newDate);
|
|
|
|
|
|
|
|
/* Steps 6-7. */
|
2014-09-15 08:49:37 -07:00
|
|
|
dateObj->setUTCTime(v, args.rval());
|
2012-10-07 18:01:35 -07:00
|
|
|
return true;
|
2012-07-06 13:53:11 -07:00
|
|
|
}
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
static bool
|
2012-07-03 17:44:22 -07:00
|
|
|
date_setUTCMonth(JSContext *cx, unsigned argc, Value *vp)
|
|
|
|
{
|
|
|
|
CallArgs args = CallArgsFromVp(argc, vp);
|
2012-08-16 11:40:05 -07:00
|
|
|
return CallNonGenericMethod<IsDate, date_setUTCMonth_impl>(cx, args);
|
2012-07-03 17:44:22 -07:00
|
|
|
}
|
|
|
|
|
2012-07-06 13:53:11 -07:00
|
|
|
static double
|
2013-06-20 20:17:15 -07:00
|
|
|
ThisLocalTimeOrZero(Handle<DateObject*> dateObj, DateTimeInfo *dtInfo)
|
2012-07-06 13:53:11 -07:00
|
|
|
{
|
2013-06-20 20:17:15 -07:00
|
|
|
double t = dateObj->UTCTime().toNumber();
|
2013-05-01 13:55:13 -07:00
|
|
|
if (IsNaN(t))
|
2012-07-06 13:53:11 -07:00
|
|
|
return +0;
|
2012-11-27 15:12:20 -08:00
|
|
|
return LocalTime(t, dtInfo);
|
2012-07-06 13:53:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static double
|
2013-06-20 20:17:15 -07:00
|
|
|
ThisUTCTimeOrZero(Handle<DateObject*> dateObj)
|
2012-07-06 13:53:11 -07:00
|
|
|
{
|
2013-06-20 20:17:15 -07:00
|
|
|
double t = dateObj->as<DateObject>().UTCTime().toNumber();
|
2013-05-01 13:55:13 -07:00
|
|
|
return IsNaN(t) ? +0 : t;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-07-06 13:53:11 -07:00
|
|
|
/* ES5 15.9.5.40. */
|
2014-01-24 20:14:56 -08:00
|
|
|
MOZ_ALWAYS_INLINE bool
|
2012-07-03 17:44:22 -07:00
|
|
|
date_setFullYear_impl(JSContext *cx, CallArgs args)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-06-20 20:17:15 -07:00
|
|
|
Rooted<DateObject*> dateObj(cx, &args.thisv().toObject().as<DateObject>());
|
2012-07-06 13:53:11 -07:00
|
|
|
|
|
|
|
/* Step 1. */
|
2013-06-20 20:17:15 -07:00
|
|
|
double t = ThisLocalTimeOrZero(dateObj, &cx->runtime()->dateTimeInfo);
|
2012-07-06 13:53:11 -07:00
|
|
|
|
|
|
|
/* Step 2. */
|
|
|
|
double y;
|
2013-06-26 16:26:45 -07:00
|
|
|
if (!ToNumber(cx, args.get(0), &y))
|
2012-07-06 13:53:11 -07:00
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Step 3. */
|
|
|
|
double m;
|
|
|
|
if (!GetMonthOrDefault(cx, args, 1, t, &m))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Step 4. */
|
2013-06-20 20:17:15 -07:00
|
|
|
double date;
|
|
|
|
if (!GetDateOrDefault(cx, args, 2, t, &date))
|
2012-07-06 13:53:11 -07:00
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Step 5. */
|
2013-06-20 20:17:15 -07:00
|
|
|
double newDate = MakeDate(MakeDay(y, m, date), TimeWithinDay(t));
|
2012-07-06 13:53:11 -07:00
|
|
|
|
|
|
|
/* Step 6. */
|
2013-06-10 14:22:18 -07:00
|
|
|
double u = TimeClip(UTC(newDate, &cx->runtime()->dateTimeInfo));
|
2012-07-06 13:53:11 -07:00
|
|
|
|
|
|
|
/* Steps 7-8. */
|
2014-09-15 08:49:37 -07:00
|
|
|
dateObj->setUTCTime(u, args.rval());
|
2012-10-07 18:01:35 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
static bool
|
2012-07-03 17:44:22 -07:00
|
|
|
date_setFullYear(JSContext *cx, unsigned argc, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-07-06 13:53:11 -07:00
|
|
|
CallArgs args = CallArgsFromVp(argc, vp);
|
2012-08-16 11:40:05 -07:00
|
|
|
return CallNonGenericMethod<IsDate, date_setFullYear_impl>(cx, args);
|
2012-07-03 17:44:22 -07:00
|
|
|
}
|
2012-07-06 13:53:11 -07:00
|
|
|
|
2012-07-03 17:44:22 -07:00
|
|
|
/* ES5 15.9.5.41. */
|
2014-01-24 20:14:56 -08:00
|
|
|
MOZ_ALWAYS_INLINE bool
|
2012-07-03 17:44:22 -07:00
|
|
|
date_setUTCFullYear_impl(JSContext *cx, CallArgs args)
|
|
|
|
{
|
2013-06-20 20:17:15 -07:00
|
|
|
Rooted<DateObject*> dateObj(cx, &args.thisv().toObject().as<DateObject>());
|
2012-07-06 13:53:11 -07:00
|
|
|
|
|
|
|
/* Step 1. */
|
2013-06-20 20:17:15 -07:00
|
|
|
double t = ThisUTCTimeOrZero(dateObj);
|
2012-07-06 13:53:11 -07:00
|
|
|
|
|
|
|
/* Step 2. */
|
|
|
|
double y;
|
2013-06-26 16:26:45 -07:00
|
|
|
if (!ToNumber(cx, args.get(0), &y))
|
2012-07-06 13:53:11 -07:00
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Step 3. */
|
|
|
|
double m;
|
|
|
|
if (!GetMonthOrDefault(cx, args, 1, t, &m))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Step 4. */
|
2013-06-20 20:17:15 -07:00
|
|
|
double date;
|
|
|
|
if (!GetDateOrDefault(cx, args, 2, t, &date))
|
2012-07-06 13:53:11 -07:00
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Step 5. */
|
2013-06-20 20:17:15 -07:00
|
|
|
double newDate = MakeDate(MakeDay(y, m, date), TimeWithinDay(t));
|
2012-07-06 13:53:11 -07:00
|
|
|
|
|
|
|
/* Step 6. */
|
|
|
|
double v = TimeClip(newDate);
|
|
|
|
|
|
|
|
/* Steps 7-8. */
|
2014-09-15 08:49:37 -07:00
|
|
|
dateObj->setUTCTime(v, args.rval());
|
2012-10-07 18:01:35 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
static bool
|
2012-07-03 17:44:22 -07:00
|
|
|
date_setUTCFullYear(JSContext *cx, unsigned argc, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-09-08 21:02:26 -07:00
|
|
|
CallArgs args = CallArgsFromVp(argc, vp);
|
2012-08-16 11:40:05 -07:00
|
|
|
return CallNonGenericMethod<IsDate, date_setUTCFullYear_impl>(cx, args);
|
2012-07-03 17:44:22 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-07-03 17:44:22 -07:00
|
|
|
/* ES5 Annex B.2.5. */
|
2014-01-24 20:14:56 -08:00
|
|
|
MOZ_ALWAYS_INLINE bool
|
2012-07-03 17:44:22 -07:00
|
|
|
date_setYear_impl(JSContext *cx, CallArgs args)
|
|
|
|
{
|
2013-06-20 20:17:15 -07:00
|
|
|
Rooted<DateObject*> dateObj(cx, &args.thisv().toObject().as<DateObject>());
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-07-06 13:53:11 -07:00
|
|
|
/* Step 1. */
|
2013-06-20 20:17:15 -07:00
|
|
|
double t = ThisLocalTimeOrZero(dateObj, &cx->runtime()->dateTimeInfo);
|
2011-09-08 21:18:23 -07:00
|
|
|
|
2012-07-06 13:53:11 -07:00
|
|
|
/* Step 2. */
|
|
|
|
double y;
|
2013-06-26 16:26:45 -07:00
|
|
|
if (!ToNumber(cx, args.get(0), &y))
|
2009-10-24 11:42:29 -07:00
|
|
|
return false;
|
2012-07-06 13:53:11 -07:00
|
|
|
|
|
|
|
/* Step 3. */
|
2013-05-01 13:55:13 -07:00
|
|
|
if (IsNaN(y)) {
|
2014-09-15 08:49:37 -07:00
|
|
|
dateObj->setUTCTime(GenericNaN(), args.rval());
|
2009-10-24 11:42:29 -07:00
|
|
|
return true;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-07-06 13:53:11 -07:00
|
|
|
/* Step 4. */
|
|
|
|
double yint = ToInteger(y);
|
|
|
|
if (0 <= yint && yint <= 99)
|
|
|
|
yint += 1900;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-07-06 13:53:11 -07:00
|
|
|
/* Step 5. */
|
|
|
|
double day = MakeDay(yint, MonthFromTime(t), DateFromTime(t));
|
|
|
|
|
|
|
|
/* Step 6. */
|
2013-06-10 14:22:18 -07:00
|
|
|
double u = UTC(MakeDate(day, TimeWithinDay(t)), &cx->runtime()->dateTimeInfo);
|
2012-07-06 13:53:11 -07:00
|
|
|
|
|
|
|
/* Steps 7-8. */
|
2014-09-15 08:49:37 -07:00
|
|
|
dateObj->setUTCTime(TimeClip(u), args.rval());
|
2012-10-07 18:01:35 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
static bool
|
2012-07-03 17:44:22 -07:00
|
|
|
date_setYear(JSContext *cx, unsigned argc, Value *vp)
|
|
|
|
{
|
|
|
|
CallArgs args = CallArgsFromVp(argc, vp);
|
2012-08-16 11:40:05 -07:00
|
|
|
return CallNonGenericMethod<IsDate, date_setYear_impl>(cx, args);
|
2012-07-03 17:44:22 -07:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/* constants for toString, toUTCString */
|
2013-06-17 18:31:47 -07:00
|
|
|
static const char js_NaN_date_str[] = "Invalid Date";
|
|
|
|
static const char * const days[] =
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
"Sun","Mon","Tue","Wed","Thu","Fri","Sat"
|
|
|
|
};
|
2013-06-17 18:31:47 -07:00
|
|
|
static const char * const months[] =
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
|
|
|
|
};
|
|
|
|
|
2008-10-17 11:57:34 -07:00
|
|
|
|
|
|
|
// Avoid dependence on PRMJ_FormatTimeUSEnglish, because it
|
|
|
|
// requires a PRMJTime... which only has 16-bit years. Sub-ECMA.
|
|
|
|
static void
|
2012-02-24 14:19:52 -08:00
|
|
|
print_gmt_string(char* buf, size_t size, double utctime)
|
2008-10-17 11:57:34 -07:00
|
|
|
{
|
2014-10-01 10:17:51 -07:00
|
|
|
MOZ_ASSERT(TimeClip(utctime) == utctime);
|
2008-10-17 11:57:34 -07:00
|
|
|
JS_snprintf(buf, size, "%s, %.2d %s %.4d %.2d:%.2d:%.2d GMT",
|
2012-07-09 14:28:20 -07:00
|
|
|
days[int(WeekDay(utctime))],
|
|
|
|
int(DateFromTime(utctime)),
|
|
|
|
months[int(MonthFromTime(utctime))],
|
|
|
|
int(YearFromTime(utctime)),
|
|
|
|
int(HourFromTime(utctime)),
|
|
|
|
int(MinFromTime(utctime)),
|
|
|
|
int(SecFromTime(utctime)));
|
2008-10-17 11:57:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-02-24 14:19:52 -08:00
|
|
|
print_iso_string(char* buf, size_t size, double utctime)
|
2008-10-17 11:57:34 -07:00
|
|
|
{
|
2014-10-01 10:17:51 -07:00
|
|
|
MOZ_ASSERT(TimeClip(utctime) == utctime);
|
2008-10-17 11:57:34 -07:00
|
|
|
JS_snprintf(buf, size, "%.4d-%.2d-%.2dT%.2d:%.2d:%.2d.%.3dZ",
|
2012-07-09 14:28:20 -07:00
|
|
|
int(YearFromTime(utctime)),
|
|
|
|
int(MonthFromTime(utctime)) + 1,
|
|
|
|
int(DateFromTime(utctime)),
|
|
|
|
int(HourFromTime(utctime)),
|
|
|
|
int(MinFromTime(utctime)),
|
|
|
|
int(SecFromTime(utctime)),
|
|
|
|
int(msFromTime(utctime)));
|
2008-10-17 11:57:34 -07:00
|
|
|
}
|
|
|
|
|
2014-10-26 08:54:25 -07:00
|
|
|
static void
|
|
|
|
print_iso_extended_string(char* buf, size_t size, double utctime)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(TimeClip(utctime) == utctime);
|
|
|
|
JS_snprintf(buf, size, "%+.6d-%.2d-%.2dT%.2d:%.2d:%.2d.%.3dZ",
|
|
|
|
int(YearFromTime(utctime)),
|
|
|
|
int(MonthFromTime(utctime)) + 1,
|
|
|
|
int(DateFromTime(utctime)),
|
|
|
|
int(HourFromTime(utctime)),
|
|
|
|
int(MinFromTime(utctime)),
|
|
|
|
int(SecFromTime(utctime)),
|
|
|
|
int(msFromTime(utctime)));
|
|
|
|
}
|
|
|
|
|
2012-07-06 15:35:45 -07:00
|
|
|
/* ES5 B.2.6. */
|
2014-01-24 20:14:56 -08:00
|
|
|
MOZ_ALWAYS_INLINE bool
|
2012-07-03 17:44:22 -07:00
|
|
|
date_toGMTString_impl(JSContext *cx, CallArgs args)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-06-20 20:17:15 -07:00
|
|
|
double utctime = args.thisv().toObject().as<DateObject>().UTCTime().toNumber();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-01-26 13:37:45 -08:00
|
|
|
char buf[100];
|
2013-05-01 13:55:13 -07:00
|
|
|
if (!IsFinite(utctime))
|
2007-03-22 10:30:00 -07:00
|
|
|
JS_snprintf(buf, sizeof buf, js_NaN_date_str);
|
2012-07-06 15:35:45 -07:00
|
|
|
else
|
|
|
|
print_gmt_string(buf, sizeof buf, utctime);
|
2011-01-26 13:37:45 -08:00
|
|
|
|
2013-04-30 15:40:48 -07:00
|
|
|
JSString *str = JS_NewStringCopyZ(cx, buf);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!str)
|
2011-01-26 13:37:45 -08:00
|
|
|
return false;
|
2011-09-08 21:18:23 -07:00
|
|
|
args.rval().setString(str);
|
2011-01-26 13:37:45 -08:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
static bool
|
2012-07-03 17:44:22 -07:00
|
|
|
date_toGMTString(JSContext *cx, unsigned argc, Value *vp)
|
2008-10-17 11:57:34 -07:00
|
|
|
{
|
2012-07-06 15:35:45 -07:00
|
|
|
CallArgs args = CallArgsFromVp(argc, vp);
|
2012-08-16 11:40:05 -07:00
|
|
|
return CallNonGenericMethod<IsDate, date_toGMTString_impl>(cx, args);
|
2012-07-03 17:44:22 -07:00
|
|
|
}
|
2012-07-06 15:35:45 -07:00
|
|
|
|
2014-10-26 08:54:25 -07:00
|
|
|
/* ES6 draft 2015-01-15 20.3.4.36. */
|
2014-01-24 20:14:56 -08:00
|
|
|
MOZ_ALWAYS_INLINE bool
|
2012-07-03 17:44:22 -07:00
|
|
|
date_toISOString_impl(JSContext *cx, CallArgs args)
|
|
|
|
{
|
2013-06-20 20:17:15 -07:00
|
|
|
double utctime = args.thisv().toObject().as<DateObject>().UTCTime().toNumber();
|
2013-05-01 13:55:13 -07:00
|
|
|
if (!IsFinite(utctime)) {
|
2015-02-27 07:08:15 -08:00
|
|
|
JS_ReportErrorNumber(cx, js::GetErrorMessage, nullptr, JSMSG_INVALID_DATE);
|
2012-07-06 15:35:45 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
char buf[100];
|
2014-10-26 08:54:25 -07:00
|
|
|
int year = int(YearFromTime(utctime));
|
|
|
|
if (year < 0 || year > 9999)
|
|
|
|
print_iso_extended_string(buf, sizeof buf, utctime);
|
|
|
|
else
|
|
|
|
print_iso_string(buf, sizeof buf, utctime);
|
2012-07-06 15:35:45 -07:00
|
|
|
|
2013-04-30 15:40:48 -07:00
|
|
|
JSString *str = JS_NewStringCopyZ(cx, buf);
|
2012-07-06 15:35:45 -07:00
|
|
|
if (!str)
|
|
|
|
return false;
|
|
|
|
args.rval().setString(str);
|
|
|
|
return true;
|
|
|
|
|
2008-10-17 11:57:34 -07:00
|
|
|
}
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
static bool
|
2012-07-03 17:44:22 -07:00
|
|
|
date_toISOString(JSContext *cx, unsigned argc, Value *vp)
|
|
|
|
{
|
|
|
|
CallArgs args = CallArgsFromVp(argc, vp);
|
2012-08-16 11:40:05 -07:00
|
|
|
return CallNonGenericMethod<IsDate, date_toISOString_impl>(cx, args);
|
2012-07-03 17:44:22 -07:00
|
|
|
}
|
|
|
|
|
2010-08-13 10:42:31 -07:00
|
|
|
/* ES5 15.9.5.44. */
|
2013-08-02 00:41:57 -07:00
|
|
|
static bool
|
2012-02-28 15:11:11 -08:00
|
|
|
date_toJSON(JSContext *cx, unsigned argc, Value *vp)
|
2010-08-13 10:42:31 -07:00
|
|
|
{
|
2012-08-12 18:50:49 -07:00
|
|
|
CallArgs args = CallArgsFromVp(argc, vp);
|
|
|
|
|
2010-08-13 10:42:31 -07:00
|
|
|
/* Step 1. */
|
2012-08-12 18:50:49 -07:00
|
|
|
RootedObject obj(cx, ToObject(cx, args.thisv()));
|
2010-08-13 10:42:31 -07:00
|
|
|
if (!obj)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Step 2. */
|
2013-01-18 09:36:24 -08:00
|
|
|
RootedValue tv(cx, ObjectValue(*obj));
|
2013-02-07 18:13:41 -08:00
|
|
|
if (!ToPrimitive(cx, JSTYPE_NUMBER, &tv))
|
2010-08-13 10:42:31 -07:00
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Step 3. */
|
2013-05-01 13:55:13 -07:00
|
|
|
if (tv.isDouble() && !IsFinite(tv.toDouble())) {
|
2012-08-12 18:50:49 -07:00
|
|
|
args.rval().setNull();
|
2010-08-13 10:42:31 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Step 4. */
|
2012-07-30 04:19:09 -07:00
|
|
|
RootedValue toISO(cx);
|
2014-12-18 06:35:42 -08:00
|
|
|
if (!GetProperty(cx, obj, obj, cx->names().toISOString, &toISO))
|
2010-08-13 10:42:31 -07:00
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Step 5. */
|
2014-06-06 08:15:21 -07:00
|
|
|
if (!IsCallable(toISO)) {
|
2015-02-27 07:08:15 -08:00
|
|
|
JS_ReportErrorFlagsAndNumber(cx, JSREPORT_ERROR, js::GetErrorMessage, nullptr,
|
2010-08-13 10:42:31 -07:00
|
|
|
JSMSG_BAD_TOISOSTRING_PROP);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Step 6. */
|
2013-06-20 23:28:06 -07:00
|
|
|
InvokeArgs args2(cx);
|
|
|
|
if (!args2.init(0))
|
2010-08-13 10:42:31 -07:00
|
|
|
return false;
|
|
|
|
|
2013-06-20 23:28:06 -07:00
|
|
|
args2.setCallee(toISO);
|
|
|
|
args2.setThis(ObjectValue(*obj));
|
2010-08-13 10:42:31 -07:00
|
|
|
|
2013-06-20 23:28:06 -07:00
|
|
|
if (!Invoke(cx, args2))
|
2010-08-13 10:42:31 -07:00
|
|
|
return false;
|
2013-06-20 23:28:06 -07:00
|
|
|
args.rval().set(args2.rval());
|
2010-08-13 10:42:31 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-03-21 16:38:25 -07:00
|
|
|
/* for Date.toLocaleFormat; interface to PRMJTime date struct.
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
|
|
|
static void
|
2012-11-27 15:12:20 -08:00
|
|
|
new_explode(double timeval, PRMJTime *split, DateTimeInfo *dtInfo)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-07-09 14:28:20 -07:00
|
|
|
double year = YearFromTime(timeval);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
Bug 708735 - Use <stdint.h> types in JSAPI and throughout SpiderMonkey. Continue to provide the {u,}int{8,16,32,64} and JS{Uint,Int}{8,16,32,64} integer types through a single header, however, for a simpler backout strategy -- and also to ease the transition for embedders. r=timeless on switching the jsd API to use the <stdint.h> types, r=luke, r=dmandelin
2011-12-08 19:54:10 -08:00
|
|
|
split->tm_usec = int32_t(msFromTime(timeval)) * 1000;
|
|
|
|
split->tm_sec = int8_t(SecFromTime(timeval));
|
|
|
|
split->tm_min = int8_t(MinFromTime(timeval));
|
|
|
|
split->tm_hour = int8_t(HourFromTime(timeval));
|
|
|
|
split->tm_mday = int8_t(DateFromTime(timeval));
|
|
|
|
split->tm_mon = int8_t(MonthFromTime(timeval));
|
|
|
|
split->tm_wday = int8_t(WeekDay(timeval));
|
2007-11-12 22:39:16 -08:00
|
|
|
split->tm_year = year;
|
Bug 708735 - Use <stdint.h> types in JSAPI and throughout SpiderMonkey. Continue to provide the {u,}int{8,16,32,64} and JS{Uint,Int}{8,16,32,64} integer types through a single header, however, for a simpler backout strategy -- and also to ease the transition for embedders. r=timeless on switching the jsd API to use the <stdint.h> types, r=luke, r=dmandelin
2011-12-08 19:54:10 -08:00
|
|
|
split->tm_yday = int16_t(DayWithinYear(timeval, year));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/* not sure how this affects things, but it doesn't seem
|
|
|
|
to matter. */
|
2012-11-27 15:12:20 -08:00
|
|
|
split->tm_isdst = (DaylightSavingTA(timeval, dtInfo) != 0);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
typedef enum formatspec {
|
|
|
|
FORMATSPEC_FULL, FORMATSPEC_DATE, FORMATSPEC_TIME
|
|
|
|
} formatspec;
|
|
|
|
|
|
|
|
/* helper function */
|
2013-08-08 15:53:04 -07:00
|
|
|
static bool
|
2013-01-11 00:43:01 -08:00
|
|
|
date_format(JSContext *cx, double date, formatspec format, MutableHandleValue rval)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
char buf[100];
|
|
|
|
char tzbuf[100];
|
2013-08-08 15:53:04 -07:00
|
|
|
bool usetz;
|
2007-03-22 10:30:00 -07:00
|
|
|
size_t i, tzlen;
|
|
|
|
PRMJTime split;
|
|
|
|
|
2013-05-01 13:55:13 -07:00
|
|
|
if (!IsFinite(date)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
JS_snprintf(buf, sizeof buf, js_NaN_date_str);
|
|
|
|
} else {
|
2014-10-01 10:17:51 -07:00
|
|
|
MOZ_ASSERT(TimeClip(date) == date);
|
2012-07-09 14:28:20 -07:00
|
|
|
|
2013-06-10 14:22:18 -07:00
|
|
|
double local = LocalTime(date, &cx->runtime()->dateTimeInfo);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/* offset from GMT in minutes. The offset includes daylight savings,
|
|
|
|
if it applies. */
|
2013-06-10 14:22:18 -07:00
|
|
|
int minutes = (int) floor(AdjustTime(date, &cx->runtime()->dateTimeInfo) / msPerMinute);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/* map 510 minutes to 0830 hours */
|
2012-02-28 15:11:11 -08:00
|
|
|
int offset = (minutes / 60) * 100 + minutes % 60;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/* print as "Wed Nov 05 19:38:03 GMT-0800 (PST) 1997" The TZA is
|
|
|
|
* printed as 'GMT-0800' rather than as 'PST' to avoid
|
|
|
|
* operating-system dependence on strftime (which
|
|
|
|
* PRMJ_FormatTimeUSEnglish calls, for %Z only.) win32 prints
|
|
|
|
* PST as 'Pacific Standard Time.' This way we always know
|
|
|
|
* what we're getting, and can parse it if we produce it.
|
|
|
|
* The OS TZA string is included as a comment.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* get a timezone string from the OS to include as a
|
|
|
|
comment. */
|
2013-06-10 14:22:18 -07:00
|
|
|
new_explode(date, &split, &cx->runtime()->dateTimeInfo);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (PRMJ_FormatTime(tzbuf, sizeof tzbuf, "(%Z)", &split) != 0) {
|
|
|
|
|
|
|
|
/* Decide whether to use the resulting timezone string.
|
|
|
|
*
|
|
|
|
* Reject it if it contains any non-ASCII, non-alphanumeric
|
|
|
|
* characters. It's then likely in some other character
|
|
|
|
* encoding, and we probably won't display it correctly.
|
|
|
|
*/
|
2013-08-06 23:59:54 -07:00
|
|
|
usetz = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
tzlen = strlen(tzbuf);
|
|
|
|
if (tzlen > 100) {
|
2013-08-06 23:59:54 -07:00
|
|
|
usetz = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
|
|
|
for (i = 0; i < tzlen; i++) {
|
2014-07-21 21:43:21 -07:00
|
|
|
char16_t c = tzbuf[i];
|
2007-03-22 10:30:00 -07:00
|
|
|
if (c > 127 ||
|
|
|
|
!(isalpha(c) || isdigit(c) ||
|
|
|
|
c == ' ' || c == '(' || c == ')')) {
|
2013-08-06 23:59:54 -07:00
|
|
|
usetz = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Also reject it if it's not parenthesized or if it's '()'. */
|
|
|
|
if (tzbuf[0] != '(' || tzbuf[1] == ')')
|
2013-08-06 23:59:54 -07:00
|
|
|
usetz = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
} else
|
2013-08-06 23:59:54 -07:00
|
|
|
usetz = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
switch (format) {
|
|
|
|
case FORMATSPEC_FULL:
|
|
|
|
/*
|
|
|
|
* Avoid dependence on PRMJ_FormatTimeUSEnglish, because it
|
|
|
|
* requires a PRMJTime... which only has 16-bit years. Sub-ECMA.
|
|
|
|
*/
|
|
|
|
/* Tue Oct 31 2000 09:41:40 GMT-0800 (PST) */
|
|
|
|
JS_snprintf(buf, sizeof buf,
|
|
|
|
"%s %s %.2d %.4d %.2d:%.2d:%.2d GMT%+.4d%s%s",
|
2012-07-09 14:28:20 -07:00
|
|
|
days[int(WeekDay(local))],
|
|
|
|
months[int(MonthFromTime(local))],
|
|
|
|
int(DateFromTime(local)),
|
|
|
|
int(YearFromTime(local)),
|
|
|
|
int(HourFromTime(local)),
|
|
|
|
int(MinFromTime(local)),
|
|
|
|
int(SecFromTime(local)),
|
2007-03-22 10:30:00 -07:00
|
|
|
offset,
|
|
|
|
usetz ? " " : "",
|
|
|
|
usetz ? tzbuf : "");
|
|
|
|
break;
|
|
|
|
case FORMATSPEC_DATE:
|
|
|
|
/* Tue Oct 31 2000 */
|
|
|
|
JS_snprintf(buf, sizeof buf,
|
|
|
|
"%s %s %.2d %.4d",
|
2012-07-09 14:28:20 -07:00
|
|
|
days[int(WeekDay(local))],
|
|
|
|
months[int(MonthFromTime(local))],
|
|
|
|
int(DateFromTime(local)),
|
|
|
|
int(YearFromTime(local)));
|
2007-03-22 10:30:00 -07:00
|
|
|
break;
|
|
|
|
case FORMATSPEC_TIME:
|
|
|
|
/* 09:41:40 GMT-0800 (PST) */
|
|
|
|
JS_snprintf(buf, sizeof buf,
|
|
|
|
"%.2d:%.2d:%.2d GMT%+.4d%s%s",
|
2012-07-09 14:28:20 -07:00
|
|
|
int(HourFromTime(local)),
|
|
|
|
int(MinFromTime(local)),
|
|
|
|
int(SecFromTime(local)),
|
2007-03-22 10:30:00 -07:00
|
|
|
offset,
|
|
|
|
usetz ? " " : "",
|
|
|
|
usetz ? tzbuf : "");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-30 15:40:48 -07:00
|
|
|
JSString *str = JS_NewStringCopyZ(cx, buf);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!str)
|
2013-01-11 00:43:00 -08:00
|
|
|
return false;
|
2013-01-11 00:43:01 -08:00
|
|
|
rval.setString(str);
|
2013-01-11 00:43:00 -08:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-09-08 21:18:23 -07:00
|
|
|
static bool
|
2013-03-21 16:38:25 -07:00
|
|
|
ToLocaleFormatHelper(JSContext *cx, HandleObject obj, const char *format, MutableHandleValue rval)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-06-20 20:17:15 -07:00
|
|
|
double utctime = obj->as<DateObject>().UTCTime().toNumber();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-09-08 21:18:23 -07:00
|
|
|
char buf[100];
|
2013-05-01 13:55:13 -07:00
|
|
|
if (!IsFinite(utctime)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
JS_snprintf(buf, sizeof buf, js_NaN_date_str);
|
|
|
|
} else {
|
2012-02-28 15:11:11 -08:00
|
|
|
int result_len;
|
2013-06-10 14:22:18 -07:00
|
|
|
double local = LocalTime(utctime, &cx->runtime()->dateTimeInfo);
|
2011-09-08 21:18:23 -07:00
|
|
|
PRMJTime split;
|
2013-06-10 14:22:18 -07:00
|
|
|
new_explode(local, &split, &cx->runtime()->dateTimeInfo);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-07-06 15:26:11 -07:00
|
|
|
/* Let PRMJTime format it. */
|
2007-03-22 10:30:00 -07:00
|
|
|
result_len = PRMJ_FormatTime(buf, sizeof buf, format, &split);
|
|
|
|
|
|
|
|
/* If it failed, default to toString. */
|
|
|
|
if (result_len == 0)
|
2013-01-11 00:43:01 -08:00
|
|
|
return date_format(cx, utctime, FORMATSPEC_FULL, rval);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/* Hacked check against undesired 2-digit year 00/00/00 form. */
|
|
|
|
if (strcmp(format, "%x") == 0 && result_len >= 6 &&
|
|
|
|
/* Format %x means use OS settings, which may have 2-digit yr, so
|
|
|
|
hack end of 3/11/22 or 11.03.22 or 11Mar22 to use 4-digit yr...*/
|
|
|
|
!isdigit(buf[result_len - 3]) &&
|
|
|
|
isdigit(buf[result_len - 2]) && isdigit(buf[result_len - 1]) &&
|
|
|
|
/* ...but not if starts with 4-digit year, like 2022/3/11. */
|
|
|
|
!(isdigit(buf[0]) && isdigit(buf[1]) &&
|
|
|
|
isdigit(buf[2]) && isdigit(buf[3]))) {
|
2014-10-23 10:53:31 -07:00
|
|
|
double localtime = obj->as<DateObject>().cachedLocalTime(&cx->runtime()->dateTimeInfo);
|
|
|
|
int year = IsNaN(localtime) ? 0 : (int) YearFromTime(localtime);
|
2007-03-22 10:30:00 -07:00
|
|
|
JS_snprintf(buf + (result_len - 2), (sizeof buf) - (result_len - 2),
|
2014-10-23 10:53:31 -07:00
|
|
|
"%d", year);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-06-10 14:22:18 -07:00
|
|
|
if (cx->runtime()->localeCallbacks && cx->runtime()->localeCallbacks->localeToUnicode)
|
|
|
|
return cx->runtime()->localeCallbacks->localeToUnicode(cx, buf, rval);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-04-30 15:40:48 -07:00
|
|
|
JSString *str = JS_NewStringCopyZ(cx, buf);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!str)
|
2011-01-26 13:37:45 -08:00
|
|
|
return false;
|
2013-01-11 00:43:01 -08:00
|
|
|
rval.setString(str);
|
2011-01-26 13:37:45 -08:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-08-08 15:17:08 -07:00
|
|
|
#if !EXPOSE_INTL_API
|
2012-07-06 15:26:11 -07:00
|
|
|
static bool
|
2013-06-20 20:17:15 -07:00
|
|
|
ToLocaleStringHelper(JSContext *cx, Handle<DateObject*> dateObj, MutableHandleValue rval)
|
2011-09-08 21:18:23 -07:00
|
|
|
{
|
2011-01-26 13:37:45 -08:00
|
|
|
/*
|
|
|
|
* Use '%#c' for windows, because '%c' is backward-compatible and non-y2k
|
|
|
|
* with msvc; '%#c' requests that a full year be used in the result string.
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
2013-06-20 20:17:15 -07:00
|
|
|
return ToLocaleFormatHelper(cx, dateObj,
|
2007-03-22 10:30:00 -07:00
|
|
|
#if defined(_WIN32) && !defined(__MWERKS__)
|
2012-07-06 15:26:11 -07:00
|
|
|
"%#c"
|
2007-03-22 10:30:00 -07:00
|
|
|
#else
|
2012-07-06 15:26:11 -07:00
|
|
|
"%c"
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
2013-01-11 00:43:01 -08:00
|
|
|
, rval);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-07-06 15:26:11 -07:00
|
|
|
/* ES5 15.9.5.5. */
|
2014-01-24 20:14:56 -08:00
|
|
|
MOZ_ALWAYS_INLINE bool
|
2012-07-03 17:44:22 -07:00
|
|
|
date_toLocaleString_impl(JSContext *cx, CallArgs args)
|
2011-10-04 10:48:36 -07:00
|
|
|
{
|
2013-06-20 20:17:15 -07:00
|
|
|
Rooted<DateObject*> dateObj(cx, &args.thisv().toObject().as<DateObject>());
|
|
|
|
return ToLocaleStringHelper(cx, dateObj, args.rval());
|
2011-10-04 10:48:36 -07:00
|
|
|
}
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
static bool
|
2012-07-03 17:44:22 -07:00
|
|
|
date_toLocaleString(JSContext *cx, unsigned argc, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-07-06 15:26:11 -07:00
|
|
|
CallArgs args = CallArgsFromVp(argc, vp);
|
2012-08-16 11:40:05 -07:00
|
|
|
return CallNonGenericMethod<IsDate, date_toLocaleString_impl>(cx, args);
|
2012-07-03 17:44:22 -07:00
|
|
|
}
|
2012-07-06 15:26:11 -07:00
|
|
|
|
2012-07-03 17:44:22 -07:00
|
|
|
/* ES5 15.9.5.6. */
|
2014-01-24 20:14:56 -08:00
|
|
|
MOZ_ALWAYS_INLINE bool
|
2012-07-03 17:44:22 -07:00
|
|
|
date_toLocaleDateString_impl(JSContext *cx, CallArgs args)
|
|
|
|
{
|
2011-01-26 13:37:45 -08:00
|
|
|
/*
|
|
|
|
* Use '%#x' for windows, because '%x' is backward-compatible and non-y2k
|
|
|
|
* with msvc; '%#x' requests that a full year be used in the result string.
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
2012-07-06 15:26:11 -07:00
|
|
|
static const char format[] =
|
2007-03-22 10:30:00 -07:00
|
|
|
#if defined(_WIN32) && !defined(__MWERKS__)
|
|
|
|
"%#x"
|
|
|
|
#else
|
|
|
|
"%x"
|
|
|
|
#endif
|
2012-07-06 15:26:11 -07:00
|
|
|
;
|
|
|
|
|
2013-06-20 20:17:15 -07:00
|
|
|
Rooted<DateObject*> dateObj(cx, &args.thisv().toObject().as<DateObject>());
|
|
|
|
return ToLocaleFormatHelper(cx, dateObj, format, args.rval());
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
static bool
|
2012-07-03 17:44:22 -07:00
|
|
|
date_toLocaleDateString(JSContext *cx, unsigned argc, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-07-06 15:26:11 -07:00
|
|
|
CallArgs args = CallArgsFromVp(argc, vp);
|
2012-08-16 11:40:05 -07:00
|
|
|
return CallNonGenericMethod<IsDate, date_toLocaleDateString_impl>(cx, args);
|
2012-07-03 17:44:22 -07:00
|
|
|
}
|
2012-07-06 15:26:11 -07:00
|
|
|
|
2012-07-03 17:44:22 -07:00
|
|
|
/* ES5 15.9.5.7. */
|
2014-01-24 20:14:56 -08:00
|
|
|
MOZ_ALWAYS_INLINE bool
|
2012-07-03 17:44:22 -07:00
|
|
|
date_toLocaleTimeString_impl(JSContext *cx, CallArgs args)
|
|
|
|
{
|
2013-06-20 20:17:15 -07:00
|
|
|
Rooted<DateObject*> dateObj(cx, &args.thisv().toObject().as<DateObject>());
|
|
|
|
return ToLocaleFormatHelper(cx, dateObj, "%X", args.rval());
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
static bool
|
2012-07-03 17:44:22 -07:00
|
|
|
date_toLocaleTimeString(JSContext *cx, unsigned argc, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-09-08 21:02:26 -07:00
|
|
|
CallArgs args = CallArgsFromVp(argc, vp);
|
2012-08-16 11:40:05 -07:00
|
|
|
return CallNonGenericMethod<IsDate, date_toLocaleTimeString_impl>(cx, args);
|
2012-07-03 17:44:22 -07:00
|
|
|
}
|
2013-08-08 15:17:08 -07:00
|
|
|
#endif /* !EXPOSE_INTL_API */
|
2011-09-08 21:18:23 -07:00
|
|
|
|
2014-01-24 20:14:56 -08:00
|
|
|
MOZ_ALWAYS_INLINE bool
|
2012-07-03 17:44:22 -07:00
|
|
|
date_toLocaleFormat_impl(JSContext *cx, CallArgs args)
|
|
|
|
{
|
2013-06-20 20:17:15 -07:00
|
|
|
Rooted<DateObject*> dateObj(cx, &args.thisv().toObject().as<DateObject>());
|
2012-07-03 17:44:22 -07:00
|
|
|
|
2013-03-21 16:38:25 -07:00
|
|
|
if (args.length() == 0) {
|
|
|
|
/*
|
|
|
|
* Use '%#c' for windows, because '%c' is backward-compatible and non-y2k
|
|
|
|
* with msvc; '%#c' requests that a full year be used in the result string.
|
|
|
|
*/
|
2013-06-20 20:17:15 -07:00
|
|
|
return ToLocaleFormatHelper(cx, dateObj,
|
2013-03-21 16:38:25 -07:00
|
|
|
#if defined(_WIN32) && !defined(__MWERKS__)
|
|
|
|
"%#c"
|
|
|
|
#else
|
|
|
|
"%c"
|
|
|
|
#endif
|
|
|
|
, args.rval());
|
|
|
|
}
|
2012-07-06 15:26:11 -07:00
|
|
|
|
2013-06-26 16:26:45 -07:00
|
|
|
RootedString fmt(cx, ToString<CanGC>(cx, args[0]));
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!fmt)
|
2011-01-26 13:37:45 -08:00
|
|
|
return false;
|
2011-09-08 21:18:23 -07:00
|
|
|
|
2010-11-11 12:40:29 -08:00
|
|
|
JSAutoByteString fmtbytes(cx, fmt);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!fmtbytes)
|
2011-01-26 13:37:45 -08:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-06-20 20:17:15 -07:00
|
|
|
return ToLocaleFormatHelper(cx, dateObj, fmtbytes.ptr(), args.rval());
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
static bool
|
2012-07-03 17:44:22 -07:00
|
|
|
date_toLocaleFormat(JSContext *cx, unsigned argc, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-09-08 21:02:26 -07:00
|
|
|
CallArgs args = CallArgsFromVp(argc, vp);
|
2012-08-16 11:40:05 -07:00
|
|
|
return CallNonGenericMethod<IsDate, date_toLocaleFormat_impl>(cx, args);
|
2012-07-03 17:44:22 -07:00
|
|
|
}
|
2011-09-08 21:02:26 -07:00
|
|
|
|
2012-07-03 17:44:22 -07:00
|
|
|
/* ES5 15.9.5.4. */
|
2014-01-24 20:14:56 -08:00
|
|
|
MOZ_ALWAYS_INLINE bool
|
2012-07-03 17:44:22 -07:00
|
|
|
date_toTimeString_impl(JSContext *cx, CallArgs args)
|
|
|
|
{
|
2013-06-20 20:17:15 -07:00
|
|
|
return date_format(cx, args.thisv().toObject().as<DateObject>().UTCTime().toNumber(),
|
2013-01-11 00:43:01 -08:00
|
|
|
FORMATSPEC_TIME, args.rval());
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
static bool
|
2012-07-03 17:44:22 -07:00
|
|
|
date_toTimeString(JSContext *cx, unsigned argc, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-09-08 21:02:26 -07:00
|
|
|
CallArgs args = CallArgsFromVp(argc, vp);
|
2012-08-16 11:40:05 -07:00
|
|
|
return CallNonGenericMethod<IsDate, date_toTimeString_impl>(cx, args);
|
2012-07-03 17:44:22 -07:00
|
|
|
}
|
2011-09-08 21:02:26 -07:00
|
|
|
|
2012-07-03 17:44:22 -07:00
|
|
|
/* ES5 15.9.5.3. */
|
2014-01-24 20:14:56 -08:00
|
|
|
MOZ_ALWAYS_INLINE bool
|
2012-07-03 17:44:22 -07:00
|
|
|
date_toDateString_impl(JSContext *cx, CallArgs args)
|
|
|
|
{
|
2013-06-20 20:17:15 -07:00
|
|
|
return date_format(cx, args.thisv().toObject().as<DateObject>().UTCTime().toNumber(),
|
2013-01-11 00:43:01 -08:00
|
|
|
FORMATSPEC_DATE, args.rval());
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
static bool
|
2012-07-03 17:44:22 -07:00
|
|
|
date_toDateString(JSContext *cx, unsigned argc, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-09-08 21:02:26 -07:00
|
|
|
CallArgs args = CallArgsFromVp(argc, vp);
|
2012-08-16 11:40:05 -07:00
|
|
|
return CallNonGenericMethod<IsDate, date_toDateString_impl>(cx, args);
|
2012-07-03 17:44:22 -07:00
|
|
|
}
|
2011-09-08 21:02:26 -07:00
|
|
|
|
2012-07-03 17:44:22 -07:00
|
|
|
#if JS_HAS_TOSOURCE
|
2014-01-24 20:14:56 -08:00
|
|
|
MOZ_ALWAYS_INLINE bool
|
2012-07-03 17:44:22 -07:00
|
|
|
date_toSource_impl(JSContext *cx, CallArgs args)
|
|
|
|
{
|
2011-12-06 02:31:00 -08:00
|
|
|
StringBuffer sb(cx);
|
2012-06-01 23:39:09 -07:00
|
|
|
if (!sb.append("(new Date(") ||
|
2013-06-20 20:17:15 -07:00
|
|
|
!NumberValueToStringBuffer(cx, args.thisv().toObject().as<DateObject>().UTCTime(), sb) ||
|
2011-12-06 02:31:00 -08:00
|
|
|
!sb.append("))"))
|
|
|
|
{
|
2011-01-26 13:37:45 -08:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-04-30 15:40:48 -07:00
|
|
|
JSString *str = sb.finishString();
|
2010-12-09 02:22:15 -08:00
|
|
|
if (!str)
|
2011-01-26 13:37:45 -08:00
|
|
|
return false;
|
2011-09-08 21:02:26 -07:00
|
|
|
args.rval().setString(str);
|
2011-01-26 13:37:45 -08:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2012-07-03 17:44:22 -07:00
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
static bool
|
2012-07-03 17:44:22 -07:00
|
|
|
date_toSource(JSContext *cx, unsigned argc, Value *vp)
|
|
|
|
{
|
|
|
|
CallArgs args = CallArgsFromVp(argc, vp);
|
2012-08-16 11:40:05 -07:00
|
|
|
return CallNonGenericMethod<IsDate, date_toSource_impl>(cx, args);
|
2012-07-03 17:44:22 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
|
|
|
|
2014-01-24 20:14:56 -08:00
|
|
|
MOZ_ALWAYS_INLINE bool
|
2012-07-03 17:44:22 -07:00
|
|
|
date_toString_impl(JSContext *cx, CallArgs args)
|
|
|
|
{
|
2013-06-20 20:17:15 -07:00
|
|
|
return date_format(cx, args.thisv().toObject().as<DateObject>().UTCTime().toNumber(),
|
2013-01-11 00:43:01 -08:00
|
|
|
FORMATSPEC_FULL, args.rval());
|
2012-07-03 17:44:22 -07:00
|
|
|
}
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
static bool
|
2012-02-28 15:11:11 -08:00
|
|
|
date_toString(JSContext *cx, unsigned argc, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-09-08 21:02:26 -07:00
|
|
|
CallArgs args = CallArgsFromVp(argc, vp);
|
2012-08-16 11:40:05 -07:00
|
|
|
return CallNonGenericMethod<IsDate, date_toString_impl>(cx, args);
|
2012-07-03 17:44:22 -07:00
|
|
|
}
|
2011-09-08 21:02:26 -07:00
|
|
|
|
2014-01-24 20:14:56 -08:00
|
|
|
MOZ_ALWAYS_INLINE bool
|
2012-07-03 17:44:22 -07:00
|
|
|
date_valueOf_impl(JSContext *cx, CallArgs args)
|
|
|
|
{
|
2013-06-20 20:17:15 -07:00
|
|
|
Rooted<DateObject*> dateObj(cx, &args.thisv().toObject().as<DateObject>());
|
|
|
|
args.rval().set(dateObj->UTCTime());
|
2012-07-03 17:44:22 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-09-16 07:01:09 -07:00
|
|
|
bool
|
|
|
|
js::date_valueOf(JSContext *cx, unsigned argc, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-10-07 11:58:45 -07:00
|
|
|
CallArgs args = CallArgsFromVp(argc, vp);
|
2012-08-16 11:40:05 -07:00
|
|
|
return CallNonGenericMethod<IsDate, date_valueOf_impl>(cx, args);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-04-22 14:15:49 -07:00
|
|
|
static const JSFunctionSpec date_static_methods[] = {
|
2011-06-10 19:03:57 -07:00
|
|
|
JS_FN("UTC", date_UTC, MAXARGS,0),
|
|
|
|
JS_FN("parse", date_parse, 1,0),
|
2011-11-22 14:41:42 -08:00
|
|
|
JS_FN("now", date_now, 0,0),
|
2007-08-01 21:33:52 -07:00
|
|
|
JS_FS_END
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
2013-04-22 14:15:49 -07:00
|
|
|
static const JSFunctionSpec date_methods[] = {
|
2011-06-10 19:03:57 -07:00
|
|
|
JS_FN("getTime", date_getTime, 0,0),
|
|
|
|
JS_FN("getTimezoneOffset", date_getTimezoneOffset, 0,0),
|
|
|
|
JS_FN("getYear", date_getYear, 0,0),
|
|
|
|
JS_FN("getFullYear", date_getFullYear, 0,0),
|
|
|
|
JS_FN("getUTCFullYear", date_getUTCFullYear, 0,0),
|
|
|
|
JS_FN("getMonth", date_getMonth, 0,0),
|
|
|
|
JS_FN("getUTCMonth", date_getUTCMonth, 0,0),
|
|
|
|
JS_FN("getDate", date_getDate, 0,0),
|
|
|
|
JS_FN("getUTCDate", date_getUTCDate, 0,0),
|
|
|
|
JS_FN("getDay", date_getDay, 0,0),
|
|
|
|
JS_FN("getUTCDay", date_getUTCDay, 0,0),
|
|
|
|
JS_FN("getHours", date_getHours, 0,0),
|
|
|
|
JS_FN("getUTCHours", date_getUTCHours, 0,0),
|
|
|
|
JS_FN("getMinutes", date_getMinutes, 0,0),
|
|
|
|
JS_FN("getUTCMinutes", date_getUTCMinutes, 0,0),
|
|
|
|
JS_FN("getSeconds", date_getUTCSeconds, 0,0),
|
|
|
|
JS_FN("getUTCSeconds", date_getUTCSeconds, 0,0),
|
|
|
|
JS_FN("getMilliseconds", date_getUTCMilliseconds, 0,0),
|
|
|
|
JS_FN("getUTCMilliseconds", date_getUTCMilliseconds, 0,0),
|
|
|
|
JS_FN("setTime", date_setTime, 1,0),
|
|
|
|
JS_FN("setYear", date_setYear, 1,0),
|
|
|
|
JS_FN("setFullYear", date_setFullYear, 3,0),
|
|
|
|
JS_FN("setUTCFullYear", date_setUTCFullYear, 3,0),
|
|
|
|
JS_FN("setMonth", date_setMonth, 2,0),
|
|
|
|
JS_FN("setUTCMonth", date_setUTCMonth, 2,0),
|
|
|
|
JS_FN("setDate", date_setDate, 1,0),
|
|
|
|
JS_FN("setUTCDate", date_setUTCDate, 1,0),
|
|
|
|
JS_FN("setHours", date_setHours, 4,0),
|
|
|
|
JS_FN("setUTCHours", date_setUTCHours, 4,0),
|
|
|
|
JS_FN("setMinutes", date_setMinutes, 3,0),
|
|
|
|
JS_FN("setUTCMinutes", date_setUTCMinutes, 3,0),
|
|
|
|
JS_FN("setSeconds", date_setSeconds, 2,0),
|
|
|
|
JS_FN("setUTCSeconds", date_setUTCSeconds, 2,0),
|
|
|
|
JS_FN("setMilliseconds", date_setMilliseconds, 1,0),
|
|
|
|
JS_FN("setUTCMilliseconds", date_setUTCMilliseconds, 1,0),
|
|
|
|
JS_FN("toUTCString", date_toGMTString, 0,0),
|
|
|
|
JS_FN("toLocaleFormat", date_toLocaleFormat, 0,0),
|
2013-08-08 15:17:08 -07:00
|
|
|
#if EXPOSE_INTL_API
|
2013-09-26 07:11:38 -07:00
|
|
|
JS_SELF_HOSTED_FN(js_toLocaleString_str, "Date_toLocaleString", 0,0),
|
|
|
|
JS_SELF_HOSTED_FN("toLocaleDateString", "Date_toLocaleDateString", 0,0),
|
|
|
|
JS_SELF_HOSTED_FN("toLocaleTimeString", "Date_toLocaleTimeString", 0,0),
|
2013-03-21 16:38:25 -07:00
|
|
|
#else
|
|
|
|
JS_FN(js_toLocaleString_str, date_toLocaleString, 0,0),
|
|
|
|
JS_FN("toLocaleDateString", date_toLocaleDateString, 0,0),
|
|
|
|
JS_FN("toLocaleTimeString", date_toLocaleTimeString, 0,0),
|
|
|
|
#endif
|
2013-03-27 00:40:00 -07:00
|
|
|
JS_FN("toDateString", date_toDateString, 0,0),
|
|
|
|
JS_FN("toTimeString", date_toTimeString, 0,0),
|
|
|
|
JS_FN("toISOString", date_toISOString, 0,0),
|
|
|
|
JS_FN(js_toJSON_str, date_toJSON, 1,0),
|
|
|
|
#if JS_HAS_TOSOURCE
|
|
|
|
JS_FN(js_toSource_str, date_toSource, 0,0),
|
|
|
|
#endif
|
|
|
|
JS_FN(js_toString_str, date_toString, 0,0),
|
|
|
|
JS_FN(js_valueOf_str, date_valueOf, 0,0),
|
2007-08-01 21:33:52 -07:00
|
|
|
JS_FS_END
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
bool
|
2015-02-27 07:08:15 -08:00
|
|
|
js::DateConstructor(JSContext *cx, unsigned argc, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-09-08 21:02:26 -07:00
|
|
|
CallArgs args = CallArgsFromVp(argc, vp);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-09-08 21:02:26 -07:00
|
|
|
/* Date called as function. */
|
2013-07-03 22:59:43 -07:00
|
|
|
if (!args.isConstructing())
|
2013-01-11 00:43:01 -08:00
|
|
|
return date_format(cx, NowAsMillis(), FORMATSPEC_FULL, args.rval());
|
2010-08-16 12:35:04 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/* Date called as constructor. */
|
2012-02-24 14:19:52 -08:00
|
|
|
double d;
|
2011-09-08 21:02:26 -07:00
|
|
|
if (args.length() == 0) {
|
2012-06-10 16:44:50 -07:00
|
|
|
/* ES5 15.9.3.3. */
|
2009-10-24 11:42:29 -07:00
|
|
|
d = NowAsMillis();
|
2011-09-08 21:02:26 -07:00
|
|
|
} else if (args.length() == 1) {
|
2012-06-10 16:44:50 -07:00
|
|
|
/* ES5 15.9.3.2. */
|
|
|
|
|
|
|
|
/* Step 1. */
|
2013-06-26 16:26:45 -07:00
|
|
|
if (!ToPrimitive(cx, args[0]))
|
2012-06-10 16:44:50 -07:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (args[0].isString()) {
|
|
|
|
/* Step 2. */
|
2013-04-30 15:40:48 -07:00
|
|
|
JSString *str = args[0].toString();
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!str)
|
2010-08-16 12:35:04 -07:00
|
|
|
return false;
|
2013-01-11 00:43:00 -08:00
|
|
|
|
2013-04-30 15:39:19 -07:00
|
|
|
JSLinearString *linearStr = str->ensureLinear(cx);
|
2010-12-06 10:26:58 -08:00
|
|
|
if (!linearStr)
|
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-06-10 02:23:39 -07:00
|
|
|
if (!ParseDate(linearStr, &d, &cx->runtime()->dateTimeInfo))
|
2013-09-19 18:42:56 -07:00
|
|
|
d = GenericNaN();
|
2009-10-24 11:42:29 -07:00
|
|
|
else
|
2012-04-19 16:22:08 -07:00
|
|
|
d = TimeClip(d);
|
2012-06-10 16:44:50 -07:00
|
|
|
} else {
|
|
|
|
/* Step 3. */
|
2013-06-26 16:26:45 -07:00
|
|
|
if (!ToNumber(cx, args[0], &d))
|
2012-06-10 16:44:50 -07:00
|
|
|
return false;
|
|
|
|
d = TimeClip(d);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
} else {
|
2012-02-24 14:19:52 -08:00
|
|
|
double msec_time;
|
2011-09-08 21:02:26 -07:00
|
|
|
if (!date_msecFromArgs(cx, args, &msec_time))
|
2010-08-16 12:35:04 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-05-01 13:55:13 -07:00
|
|
|
if (IsFinite(msec_time)) {
|
2013-06-10 14:22:18 -07:00
|
|
|
msec_time = UTC(msec_time, &cx->runtime()->dateTimeInfo);
|
2012-04-19 16:22:08 -07:00
|
|
|
msec_time = TimeClip(msec_time);
|
2007-08-13 13:45:32 -07:00
|
|
|
}
|
2009-10-24 11:42:29 -07:00
|
|
|
d = msec_time;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2010-08-10 16:38:08 -07:00
|
|
|
|
2015-02-27 07:08:15 -08:00
|
|
|
JSObject *obj = NewDateObjectMsec(cx, d);
|
2010-08-10 16:38:08 -07:00
|
|
|
if (!obj)
|
2010-08-16 12:35:04 -07:00
|
|
|
return false;
|
2010-08-10 16:38:08 -07:00
|
|
|
|
2011-09-08 21:02:26 -07:00
|
|
|
args.rval().setObject(*obj);
|
2010-08-16 12:35:04 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-02-04 07:51:54 -08:00
|
|
|
static bool
|
|
|
|
FinishDateClassInit(JSContext *cx, HandleObject ctor, HandleObject proto)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-02-04 07:51:54 -08:00
|
|
|
proto->as<DateObject>().setUTCTime(GenericNaN());
|
2009-10-24 11:42:29 -07:00
|
|
|
|
2010-06-26 14:08:58 -07:00
|
|
|
/*
|
2011-05-03 12:18:36 -07:00
|
|
|
* Date.prototype.toGMTString has the same initial value as
|
|
|
|
* Date.prototype.toUTCString.
|
2010-06-26 14:08:58 -07:00
|
|
|
*/
|
2012-07-30 04:19:09 -07:00
|
|
|
RootedValue toUTCStringFun(cx);
|
2012-09-11 10:32:33 -07:00
|
|
|
RootedId toUTCStringId(cx, NameToId(cx->names().toUTCString));
|
|
|
|
RootedId toGMTStringId(cx, NameToId(cx->names().toGMTString));
|
2014-12-17 13:55:32 -08:00
|
|
|
return NativeGetProperty(cx, proto.as<DateObject>(), toUTCStringId, &toUTCStringFun) &&
|
|
|
|
NativeDefineProperty(cx, proto.as<DateObject>(), toGMTStringId, toUTCStringFun,
|
|
|
|
nullptr, nullptr, 0);
|
2014-02-04 07:51:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
const Class DateObject::class_ = {
|
|
|
|
js_Date_str,
|
|
|
|
JSCLASS_HAS_RESERVED_SLOTS(RESERVED_SLOTS) |
|
|
|
|
JSCLASS_HAS_CACHED_PROTO(JSProto_Date),
|
2014-11-22 10:23:39 -08:00
|
|
|
nullptr, /* addProperty */
|
|
|
|
nullptr, /* delProperty */
|
|
|
|
nullptr, /* getProperty */
|
|
|
|
nullptr, /* setProperty */
|
|
|
|
nullptr, /* enumerate */
|
|
|
|
nullptr, /* resolve */
|
2014-02-04 07:51:54 -08:00
|
|
|
date_convert,
|
2014-11-22 10:23:39 -08:00
|
|
|
nullptr, /* finalize */
|
|
|
|
nullptr, /* call */
|
|
|
|
nullptr, /* hasInstance */
|
|
|
|
nullptr, /* construct */
|
|
|
|
nullptr, /* trace */
|
2011-05-03 12:18:36 -07:00
|
|
|
{
|
2015-02-27 07:08:15 -08:00
|
|
|
GenericCreateConstructor<DateConstructor, MAXARGS, JSFunction::FinalizeKind>,
|
2014-09-18 20:06:32 -07:00
|
|
|
GenericCreatePrototype,
|
2014-02-04 07:51:54 -08:00
|
|
|
date_static_methods,
|
|
|
|
date_methods,
|
2014-05-23 16:53:03 -07:00
|
|
|
nullptr,
|
2014-02-04 07:51:54 -08:00
|
|
|
FinishDateClassInit
|
2010-06-26 14:08:58 -07:00
|
|
|
}
|
2014-02-04 07:51:54 -08:00
|
|
|
};
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
JS_FRIEND_API(JSObject *)
|
2015-02-27 07:08:15 -08:00
|
|
|
js::NewDateObjectMsec(JSContext *cx, double msec_time)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-06-20 20:17:15 -07:00
|
|
|
JSObject *obj = NewBuiltinClassInstance(cx, &DateObject::class_);
|
2011-10-10 11:41:03 -07:00
|
|
|
if (!obj)
|
2013-10-07 09:43:08 -07:00
|
|
|
return nullptr;
|
2013-06-20 20:17:15 -07:00
|
|
|
obj->as<DateObject>().setUTCTime(msec_time);
|
2007-03-22 10:30:00 -07:00
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS_FRIEND_API(JSObject *)
|
2015-02-27 07:08:15 -08:00
|
|
|
js::NewDateObject(JSContext *cx, int year, int mon, int mday,
|
|
|
|
int hour, int min, int sec)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-10-01 10:17:51 -07:00
|
|
|
MOZ_ASSERT(mon < 12);
|
2012-09-20 23:22:35 -07:00
|
|
|
double msec_time = date_msecFromDate(year, mon, mday, hour, min, sec, 0);
|
2015-02-27 07:08:15 -08:00
|
|
|
return NewDateObjectMsec(cx, UTC(msec_time, &cx->runtime()->dateTimeInfo));
|
2012-07-13 15:56:08 -07:00
|
|
|
}
|
|
|
|
|
2013-08-08 15:53:04 -07:00
|
|
|
JS_FRIEND_API(bool)
|
2014-10-23 10:53:31 -07:00
|
|
|
js::DateIsValid(JSContext *cx, JSObject *objArg)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-10-23 10:53:31 -07:00
|
|
|
RootedObject obj(cx, objArg);
|
|
|
|
if (!ObjectClassIs(obj, ESClass_Date, cx))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
RootedValue unboxed(cx);
|
|
|
|
if (!Unbox(cx, obj, &unboxed)) {
|
|
|
|
// This can't actually happen, so we don't force consumers to deal with
|
|
|
|
// a clunky out-param API. Do something sane-ish if it does happen.
|
|
|
|
cx->clearPendingException();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return !IsNaN(unboxed.toNumber());
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-02-24 14:19:52 -08:00
|
|
|
JS_FRIEND_API(double)
|
2014-10-23 10:53:31 -07:00
|
|
|
js::DateGetMsecSinceEpoch(JSContext *cx, JSObject *objArg)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-10-23 10:53:31 -07:00
|
|
|
RootedObject obj(cx, objArg);
|
|
|
|
if (!ObjectClassIs(obj, ESClass_Date, cx))
|
2014-08-18 14:18:38 -07:00
|
|
|
return 0;
|
2014-10-23 10:53:31 -07:00
|
|
|
|
|
|
|
RootedValue unboxed(cx);
|
|
|
|
if (!Unbox(cx, obj, &unboxed)) {
|
|
|
|
// This can't actually happen, so we don't force consumers to deal with
|
|
|
|
// a clunky out-param API. Do something sane-ish if it does happen.
|
|
|
|
cx->clearPendingException();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return unboxed.toNumber();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|