2 * Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
3 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2011 Apple Inc. All rights reserved.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
23 #include "DateConstructor.h"
25 #include "DateConversion.h"
26 #include "DateInstance.h"
27 #include "DatePrototype.h"
28 #include "JSFunction.h"
29 #include "JSGlobalObject.h"
31 #include "JSStringBuilder.h"
32 #include "ObjectPrototype.h"
35 #include <wtf/DateMath.h>
36 #include <wtf/MathExtras.h>
38 #if OS(WINCE) && !PLATFORM(QT)
39 extern "C" time_t time(time_t* timer); // Provided by libce.
47 #include <sys/timeb.h>
54 static EncodedJSValue JSC_HOST_CALL dateParse(ExecState*);
55 static EncodedJSValue JSC_HOST_CALL dateNow(ExecState*);
56 static EncodedJSValue JSC_HOST_CALL dateUTC(ExecState*);
60 #include "DateConstructor.lut.h"
64 const ClassInfo DateConstructor::s_info = { "Function", &InternalFunction::s_info, 0, ExecState::dateConstructorTable };
66 /* Source for DateConstructor.lut.h
67 @begin dateConstructorTable
68 parse dateParse DontEnum|Function 1
69 UTC dateUTC DontEnum|Function 7
70 now dateNow DontEnum|Function 0
74 ASSERT_CLASS_FITS_IN_CELL(DateConstructor);
76 DateConstructor::DateConstructor(JSGlobalObject* globalObject, Structure* structure)
77 : InternalFunction(globalObject, structure)
81 void DateConstructor::finishCreation(ExecState* exec, DatePrototype* datePrototype)
83 Base::finishCreation(exec->globalData(), Identifier(exec, datePrototype->classInfo()->className));
84 putDirectWithoutTransition(exec->globalData(), exec->propertyNames().prototype, datePrototype, DontEnum | DontDelete | ReadOnly);
85 putDirectWithoutTransition(exec->globalData(), exec->propertyNames().length, jsNumber(7), ReadOnly | DontEnum | DontDelete);
88 bool DateConstructor::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot &slot)
90 return getStaticFunctionSlot<InternalFunction>(exec, ExecState::dateConstructorTable(exec), this, propertyName, slot);
93 bool DateConstructor::getOwnPropertyDescriptor(ExecState* exec, const Identifier& propertyName, PropertyDescriptor& descriptor)
95 return getStaticFunctionDescriptor<InternalFunction>(exec, ExecState::dateConstructorTable(exec), this, propertyName, descriptor);
99 JSObject* constructDate(ExecState* exec, JSGlobalObject* globalObject, const ArgList& args)
101 int numArgs = args.size();
105 if (numArgs == 0) // new Date() ECMA 15.9.3.3
106 value = jsCurrentTime();
107 else if (numArgs == 1) {
108 if (args.at(0).inherits(&DateInstance::s_info))
109 value = asDateInstance(args.at(0))->internalNumber();
111 JSValue primitive = args.at(0).toPrimitive(exec);
112 if (primitive.isString())
113 value = parseDate(exec, primitive.getString(exec));
115 value = primitive.toNumber(exec);
118 double doubleArguments[7] = {
119 args.at(0).toNumber(exec),
120 args.at(1).toNumber(exec),
121 args.at(2).toNumber(exec),
122 args.at(3).toNumber(exec),
123 args.at(4).toNumber(exec),
124 args.at(5).toNumber(exec),
125 args.at(6).toNumber(exec)
127 if (isnan(doubleArguments[0])
128 || isnan(doubleArguments[1])
129 || (numArgs >= 3 && isnan(doubleArguments[2]))
130 || (numArgs >= 4 && isnan(doubleArguments[3]))
131 || (numArgs >= 5 && isnan(doubleArguments[4]))
132 || (numArgs >= 6 && isnan(doubleArguments[5]))
133 || (numArgs >= 7 && isnan(doubleArguments[6])))
134 value = std::numeric_limits<double>::quiet_NaN();
137 int year = JSC::toInt32(doubleArguments[0]);
138 t.year = (year >= 0 && year <= 99) ? year : year - 1900;
139 t.month = JSC::toInt32(doubleArguments[1]);
140 t.monthDay = (numArgs >= 3) ? JSC::toInt32(doubleArguments[2]) : 1;
141 t.hour = JSC::toInt32(doubleArguments[3]);
142 t.minute = JSC::toInt32(doubleArguments[4]);
143 t.second = JSC::toInt32(doubleArguments[5]);
145 double ms = (numArgs >= 7) ? doubleArguments[6] : 0;
146 value = gregorianDateTimeToMS(exec, t, ms, false);
150 return DateInstance::create(exec, globalObject->dateStructure(), value);
153 static EncodedJSValue JSC_HOST_CALL constructWithDateConstructor(ExecState* exec)
156 return JSValue::encode(constructDate(exec, asInternalFunction(exec->callee())->globalObject(), args));
159 ConstructType DateConstructor::getConstructData(ConstructData& constructData)
161 constructData.native.function = constructWithDateConstructor;
162 return ConstructTypeHost;
166 static EncodedJSValue JSC_HOST_CALL callDate(ExecState* exec)
168 time_t localTime = time(0);
170 getLocalTime(&localTime, &localTM);
171 GregorianDateTime ts(exec, localTM);
172 DateConversionBuffer date;
173 DateConversionBuffer time;
174 formatDate(ts, date);
175 formatTime(ts, time);
176 return JSValue::encode(jsMakeNontrivialString(exec, date, " ", time));
179 CallType DateConstructor::getCallData(CallData& callData)
181 callData.native.function = callDate;
185 static EncodedJSValue JSC_HOST_CALL dateParse(ExecState* exec)
187 return JSValue::encode(jsNumber(parseDate(exec, exec->argument(0).toString(exec))));
190 static EncodedJSValue JSC_HOST_CALL dateNow(ExecState*)
192 return JSValue::encode(jsNumber(jsCurrentTime()));
195 static EncodedJSValue JSC_HOST_CALL dateUTC(ExecState* exec)
197 double doubleArguments[7] = {
198 exec->argument(0).toNumber(exec),
199 exec->argument(1).toNumber(exec),
200 exec->argument(2).toNumber(exec),
201 exec->argument(3).toNumber(exec),
202 exec->argument(4).toNumber(exec),
203 exec->argument(5).toNumber(exec),
204 exec->argument(6).toNumber(exec)
206 int n = exec->argumentCount();
207 if (isnan(doubleArguments[0])
208 || isnan(doubleArguments[1])
209 || (n >= 3 && isnan(doubleArguments[2]))
210 || (n >= 4 && isnan(doubleArguments[3]))
211 || (n >= 5 && isnan(doubleArguments[4]))
212 || (n >= 6 && isnan(doubleArguments[5]))
213 || (n >= 7 && isnan(doubleArguments[6])))
214 return JSValue::encode(jsNaN());
217 int year = JSC::toInt32(doubleArguments[0]);
218 t.year = (year >= 0 && year <= 99) ? year : year - 1900;
219 t.month = JSC::toInt32(doubleArguments[1]);
220 t.monthDay = (n >= 3) ? JSC::toInt32(doubleArguments[2]) : 1;
221 t.hour = JSC::toInt32(doubleArguments[3]);
222 t.minute = JSC::toInt32(doubleArguments[4]);
223 t.second = JSC::toInt32(doubleArguments[5]);
224 double ms = (n >= 7) ? doubleArguments[6] : 0;
225 return JSValue::encode(jsNumber(timeClip(gregorianDateTimeToMS(exec, t, ms, true))));