]> git.saurik.com Git - apple/javascriptcore.git/blame - runtime/DateConstructor.cpp
JavaScriptCore-903.5.tar.gz
[apple/javascriptcore.git] / runtime / DateConstructor.cpp
CommitLineData
9dae56ea
A
1/*
2 * Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
14957cd0 3 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2011 Apple Inc. All rights reserved.
9dae56ea
A
4 *
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.
9 *
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.
14 *
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
18 * USA
19 *
20 */
21
22#include "config.h"
23#include "DateConstructor.h"
24
ba379fdc 25#include "DateConversion.h"
9dae56ea 26#include "DateInstance.h"
9dae56ea 27#include "DatePrototype.h"
ba379fdc 28#include "JSFunction.h"
9dae56ea
A
29#include "JSGlobalObject.h"
30#include "JSString.h"
4e4e5a6f 31#include "JSStringBuilder.h"
9dae56ea 32#include "ObjectPrototype.h"
9dae56ea
A
33#include <math.h>
34#include <time.h>
ba379fdc 35#include <wtf/DateMath.h>
9dae56ea
A
36#include <wtf/MathExtras.h>
37
f9bf01c6
A
38#if OS(WINCE) && !PLATFORM(QT)
39extern "C" time_t time(time_t* timer); // Provided by libce.
ba379fdc
A
40#endif
41
9dae56ea
A
42#if HAVE(SYS_TIME_H)
43#include <sys/time.h>
44#endif
45
46#if HAVE(SYS_TIMEB_H)
47#include <sys/timeb.h>
48#endif
49
ba379fdc
A
50using namespace WTF;
51
9dae56ea
A
52namespace JSC {
53
14957cd0
A
54static EncodedJSValue JSC_HOST_CALL dateParse(ExecState*);
55static EncodedJSValue JSC_HOST_CALL dateNow(ExecState*);
56static EncodedJSValue JSC_HOST_CALL dateUTC(ExecState*);
57
58}
59
60#include "DateConstructor.lut.h"
61
62namespace JSC {
63
64const ClassInfo DateConstructor::s_info = { "Function", &InternalFunction::s_info, 0, ExecState::dateConstructorTable };
9dae56ea 65
14957cd0
A
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
71@end
72*/
9dae56ea 73
14957cd0
A
74ASSERT_CLASS_FITS_IN_CELL(DateConstructor);
75
76DateConstructor::DateConstructor(ExecState* exec, JSGlobalObject* globalObject, Structure* structure, DatePrototype* datePrototype)
77 : InternalFunction(&exec->globalData(), globalObject, structure, Identifier(exec, datePrototype->classInfo()->className))
9dae56ea 78{
14957cd0
A
79 putDirectWithoutTransition(exec->globalData(), exec->propertyNames().prototype, datePrototype, DontEnum | DontDelete | ReadOnly);
80 putDirectWithoutTransition(exec->globalData(), exec->propertyNames().length, jsNumber(7), ReadOnly | DontEnum | DontDelete);
81}
9dae56ea 82
14957cd0
A
83bool DateConstructor::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot &slot)
84{
85 return getStaticFunctionSlot<InternalFunction>(exec, ExecState::dateConstructorTable(exec), this, propertyName, slot);
86}
9dae56ea 87
14957cd0
A
88bool DateConstructor::getOwnPropertyDescriptor(ExecState* exec, const Identifier& propertyName, PropertyDescriptor& descriptor)
89{
90 return getStaticFunctionDescriptor<InternalFunction>(exec, ExecState::dateConstructorTable(exec), this, propertyName, descriptor);
9dae56ea
A
91}
92
93// ECMA 15.9.3
14957cd0 94JSObject* constructDate(ExecState* exec, JSGlobalObject* globalObject, const ArgList& args)
9dae56ea
A
95{
96 int numArgs = args.size();
97
98 double value;
99
100 if (numArgs == 0) // new Date() ECMA 15.9.3.3
f9bf01c6 101 value = jsCurrentTime();
9dae56ea 102 else if (numArgs == 1) {
14957cd0 103 if (args.at(0).inherits(&DateInstance::s_info))
ba379fdc 104 value = asDateInstance(args.at(0))->internalNumber();
9dae56ea 105 else {
ba379fdc 106 JSValue primitive = args.at(0).toPrimitive(exec);
9dae56ea 107 if (primitive.isString())
f9bf01c6 108 value = parseDate(exec, primitive.getString(exec));
9dae56ea
A
109 else
110 value = primitive.toNumber(exec);
111 }
112 } else {
14957cd0
A
113 double doubleArguments[7] = {
114 args.at(0).toNumber(exec),
115 args.at(1).toNumber(exec),
116 args.at(2).toNumber(exec),
117 args.at(3).toNumber(exec),
118 args.at(4).toNumber(exec),
119 args.at(5).toNumber(exec),
120 args.at(6).toNumber(exec)
121 };
122 if (isnan(doubleArguments[0])
123 || isnan(doubleArguments[1])
124 || (numArgs >= 3 && isnan(doubleArguments[2]))
125 || (numArgs >= 4 && isnan(doubleArguments[3]))
126 || (numArgs >= 5 && isnan(doubleArguments[4]))
127 || (numArgs >= 6 && isnan(doubleArguments[5]))
128 || (numArgs >= 7 && isnan(doubleArguments[6])))
9dae56ea
A
129 value = NaN;
130 else {
f9bf01c6 131 GregorianDateTime t;
14957cd0 132 int year = JSC::toInt32(doubleArguments[0]);
f9bf01c6 133 t.year = (year >= 0 && year <= 99) ? year : year - 1900;
14957cd0
A
134 t.month = JSC::toInt32(doubleArguments[1]);
135 t.monthDay = (numArgs >= 3) ? JSC::toInt32(doubleArguments[2]) : 1;
136 t.hour = JSC::toInt32(doubleArguments[3]);
137 t.minute = JSC::toInt32(doubleArguments[4]);
138 t.second = JSC::toInt32(doubleArguments[5]);
f9bf01c6 139 t.isDST = -1;
14957cd0 140 double ms = (numArgs >= 7) ? doubleArguments[6] : 0;
f9bf01c6 141 value = gregorianDateTimeToMS(exec, t, ms, false);
9dae56ea
A
142 }
143 }
144
14957cd0 145 return new (exec) DateInstance(exec, globalObject->dateStructure(), value);
9dae56ea
A
146}
147
14957cd0 148static EncodedJSValue JSC_HOST_CALL constructWithDateConstructor(ExecState* exec)
9dae56ea 149{
14957cd0
A
150 ArgList args(exec);
151 return JSValue::encode(constructDate(exec, asInternalFunction(exec->callee())->globalObject(), args));
9dae56ea
A
152}
153
154ConstructType DateConstructor::getConstructData(ConstructData& constructData)
155{
156 constructData.native.function = constructWithDateConstructor;
157 return ConstructTypeHost;
158}
159
160// ECMA 15.9.2
14957cd0 161static EncodedJSValue JSC_HOST_CALL callDate(ExecState* exec)
9dae56ea
A
162{
163 time_t localTime = time(0);
164 tm localTM;
165 getLocalTime(&localTime, &localTM);
f9bf01c6
A
166 GregorianDateTime ts(exec, localTM);
167 DateConversionBuffer date;
168 DateConversionBuffer time;
169 formatDate(ts, date);
170 formatTime(ts, time);
14957cd0 171 return JSValue::encode(jsMakeNontrivialString(exec, date, " ", time));
9dae56ea
A
172}
173
174CallType DateConstructor::getCallData(CallData& callData)
175{
176 callData.native.function = callDate;
177 return CallTypeHost;
178}
179
14957cd0 180static EncodedJSValue JSC_HOST_CALL dateParse(ExecState* exec)
9dae56ea 181{
14957cd0 182 return JSValue::encode(jsNumber(parseDate(exec, exec->argument(0).toString(exec))));
9dae56ea
A
183}
184
14957cd0 185static EncodedJSValue JSC_HOST_CALL dateNow(ExecState*)
9dae56ea 186{
14957cd0 187 return JSValue::encode(jsNumber(jsCurrentTime()));
9dae56ea
A
188}
189
14957cd0 190static EncodedJSValue JSC_HOST_CALL dateUTC(ExecState* exec)
9dae56ea 191{
14957cd0
A
192 double doubleArguments[7] = {
193 exec->argument(0).toNumber(exec),
194 exec->argument(1).toNumber(exec),
195 exec->argument(2).toNumber(exec),
196 exec->argument(3).toNumber(exec),
197 exec->argument(4).toNumber(exec),
198 exec->argument(5).toNumber(exec),
199 exec->argument(6).toNumber(exec)
200 };
201 int n = exec->argumentCount();
202 if (isnan(doubleArguments[0])
203 || isnan(doubleArguments[1])
204 || (n >= 3 && isnan(doubleArguments[2]))
205 || (n >= 4 && isnan(doubleArguments[3]))
206 || (n >= 5 && isnan(doubleArguments[4]))
207 || (n >= 6 && isnan(doubleArguments[5]))
208 || (n >= 7 && isnan(doubleArguments[6])))
209 return JSValue::encode(jsNaN());
9dae56ea
A
210
211 GregorianDateTime t;
14957cd0 212 int year = JSC::toInt32(doubleArguments[0]);
9dae56ea 213 t.year = (year >= 0 && year <= 99) ? year : year - 1900;
14957cd0
A
214 t.month = JSC::toInt32(doubleArguments[1]);
215 t.monthDay = (n >= 3) ? JSC::toInt32(doubleArguments[2]) : 1;
216 t.hour = JSC::toInt32(doubleArguments[3]);
217 t.minute = JSC::toInt32(doubleArguments[4]);
218 t.second = JSC::toInt32(doubleArguments[5]);
219 double ms = (n >= 7) ? doubleArguments[6] : 0;
220 return JSValue::encode(jsNumber(timeClip(gregorianDateTimeToMS(exec, t, ms, true))));
9dae56ea
A
221}
222
223} // namespace JSC