]> git.saurik.com Git - apple/javascriptcore.git/blob - runtime/DateConstructor.cpp
6d7d934062c354302058c378c3a2b25813a0c5d5
[apple/javascriptcore.git] / runtime / DateConstructor.cpp
1 /*
2 * Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
3 * Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
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
25 #include "DateConversion.h"
26 #include "DateInstance.h"
27 #include "DatePrototype.h"
28 #include "JSFunction.h"
29 #include "JSGlobalObject.h"
30 #include "JSString.h"
31 #include "ObjectPrototype.h"
32 #include "PrototypeFunction.h"
33 #include <math.h>
34 #include <time.h>
35 #include <wtf/DateMath.h>
36 #include <wtf/MathExtras.h>
37
38 #if PLATFORM(WINCE) && !PLATFORM(QT)
39 extern "C" time_t time(time_t* timer); //provided by libce
40 #endif
41
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
50 using namespace WTF;
51
52 namespace JSC {
53
54 // TODO: MakeTime (15.9.11.1) etc. ?
55
56 ASSERT_CLASS_FITS_IN_CELL(DateConstructor);
57
58 static JSValue JSC_HOST_CALL dateParse(ExecState*, JSObject*, JSValue, const ArgList&);
59 static JSValue JSC_HOST_CALL dateNow(ExecState*, JSObject*, JSValue, const ArgList&);
60 static JSValue JSC_HOST_CALL dateUTC(ExecState*, JSObject*, JSValue, const ArgList&);
61
62 DateConstructor::DateConstructor(ExecState* exec, PassRefPtr<Structure> structure, Structure* prototypeFunctionStructure, DatePrototype* datePrototype)
63 : InternalFunction(&exec->globalData(), structure, Identifier(exec, datePrototype->classInfo()->className))
64 {
65 putDirectWithoutTransition(exec->propertyNames().prototype, datePrototype, DontEnum|DontDelete|ReadOnly);
66
67 putDirectFunctionWithoutTransition(exec, new (exec) NativeFunctionWrapper(exec, prototypeFunctionStructure, 1, exec->propertyNames().parse, dateParse), DontEnum);
68 putDirectFunctionWithoutTransition(exec, new (exec) NativeFunctionWrapper(exec, prototypeFunctionStructure, 7, exec->propertyNames().UTC, dateUTC), DontEnum);
69 putDirectFunctionWithoutTransition(exec, new (exec) NativeFunctionWrapper(exec, prototypeFunctionStructure, 0, exec->propertyNames().now, dateNow), DontEnum);
70
71 putDirectWithoutTransition(exec->propertyNames().length, jsNumber(exec, 7), ReadOnly | DontEnum | DontDelete);
72 }
73
74 // ECMA 15.9.3
75 JSObject* constructDate(ExecState* exec, const ArgList& args)
76 {
77 int numArgs = args.size();
78
79 double value;
80
81 if (numArgs == 0) // new Date() ECMA 15.9.3.3
82 value = getCurrentUTCTime();
83 else if (numArgs == 1) {
84 if (args.at(0).isObject(&DateInstance::info))
85 value = asDateInstance(args.at(0))->internalNumber();
86 else {
87 JSValue primitive = args.at(0).toPrimitive(exec);
88 if (primitive.isString())
89 value = parseDate(primitive.getString());
90 else
91 value = primitive.toNumber(exec);
92 }
93 } else {
94 if (isnan(args.at(0).toNumber(exec))
95 || isnan(args.at(1).toNumber(exec))
96 || (numArgs >= 3 && isnan(args.at(2).toNumber(exec)))
97 || (numArgs >= 4 && isnan(args.at(3).toNumber(exec)))
98 || (numArgs >= 5 && isnan(args.at(4).toNumber(exec)))
99 || (numArgs >= 6 && isnan(args.at(5).toNumber(exec)))
100 || (numArgs >= 7 && isnan(args.at(6).toNumber(exec))))
101 value = NaN;
102 else {
103 GregorianDateTime t;
104 int year = args.at(0).toInt32(exec);
105 t.year = (year >= 0 && year <= 99) ? year : year - 1900;
106 t.month = args.at(1).toInt32(exec);
107 t.monthDay = (numArgs >= 3) ? args.at(2).toInt32(exec) : 1;
108 t.hour = args.at(3).toInt32(exec);
109 t.minute = args.at(4).toInt32(exec);
110 t.second = args.at(5).toInt32(exec);
111 t.isDST = -1;
112 double ms = (numArgs >= 7) ? args.at(6).toNumber(exec) : 0;
113 value = gregorianDateTimeToMS(t, ms, false);
114 }
115 }
116
117 DateInstance* result = new (exec) DateInstance(exec->lexicalGlobalObject()->dateStructure());
118 result->setInternalValue(jsNumber(exec, timeClip(value)));
119 return result;
120 }
121
122 static JSObject* constructWithDateConstructor(ExecState* exec, JSObject*, const ArgList& args)
123 {
124 return constructDate(exec, args);
125 }
126
127 ConstructType DateConstructor::getConstructData(ConstructData& constructData)
128 {
129 constructData.native.function = constructWithDateConstructor;
130 return ConstructTypeHost;
131 }
132
133 // ECMA 15.9.2
134 static JSValue JSC_HOST_CALL callDate(ExecState* exec, JSObject*, JSValue, const ArgList&)
135 {
136 time_t localTime = time(0);
137 tm localTM;
138 getLocalTime(&localTime, &localTM);
139 GregorianDateTime ts(localTM);
140 return jsNontrivialString(exec, formatDate(ts) + " " + formatTime(ts, false));
141 }
142
143 CallType DateConstructor::getCallData(CallData& callData)
144 {
145 callData.native.function = callDate;
146 return CallTypeHost;
147 }
148
149 static JSValue JSC_HOST_CALL dateParse(ExecState* exec, JSObject*, JSValue, const ArgList& args)
150 {
151 return jsNumber(exec, parseDate(args.at(0).toString(exec)));
152 }
153
154 static JSValue JSC_HOST_CALL dateNow(ExecState* exec, JSObject*, JSValue, const ArgList&)
155 {
156 return jsNumber(exec, getCurrentUTCTime());
157 }
158
159 static JSValue JSC_HOST_CALL dateUTC(ExecState* exec, JSObject*, JSValue, const ArgList& args)
160 {
161 int n = args.size();
162 if (isnan(args.at(0).toNumber(exec))
163 || isnan(args.at(1).toNumber(exec))
164 || (n >= 3 && isnan(args.at(2).toNumber(exec)))
165 || (n >= 4 && isnan(args.at(3).toNumber(exec)))
166 || (n >= 5 && isnan(args.at(4).toNumber(exec)))
167 || (n >= 6 && isnan(args.at(5).toNumber(exec)))
168 || (n >= 7 && isnan(args.at(6).toNumber(exec))))
169 return jsNaN(exec);
170
171 GregorianDateTime t;
172 int year = args.at(0).toInt32(exec);
173 t.year = (year >= 0 && year <= 99) ? year : year - 1900;
174 t.month = args.at(1).toInt32(exec);
175 t.monthDay = (n >= 3) ? args.at(2).toInt32(exec) : 1;
176 t.hour = args.at(3).toInt32(exec);
177 t.minute = args.at(4).toInt32(exec);
178 t.second = args.at(5).toInt32(exec);
179 double ms = (n >= 7) ? args.at(6).toNumber(exec) : 0;
180 return jsNumber(exec, gregorianDateTimeToMS(t, ms, true));
181 }
182
183 } // namespace JSC