]> git.saurik.com Git - apple/javascriptcore.git/blob - runtime/DateConstructor.cpp
JavaScriptCore-576.tar.gz
[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 OS(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 ASSERT_CLASS_FITS_IN_CELL(DateConstructor);
55
56 static JSValue JSC_HOST_CALL dateParse(ExecState*, JSObject*, JSValue, const ArgList&);
57 static JSValue JSC_HOST_CALL dateNow(ExecState*, JSObject*, JSValue, const ArgList&);
58 static JSValue JSC_HOST_CALL dateUTC(ExecState*, JSObject*, JSValue, const ArgList&);
59
60 DateConstructor::DateConstructor(ExecState* exec, NonNullPassRefPtr<Structure> structure, Structure* prototypeFunctionStructure, DatePrototype* datePrototype)
61 : InternalFunction(&exec->globalData(), structure, Identifier(exec, datePrototype->classInfo()->className))
62 {
63 putDirectWithoutTransition(exec->propertyNames().prototype, datePrototype, DontEnum|DontDelete|ReadOnly);
64
65 putDirectFunctionWithoutTransition(exec, new (exec) NativeFunctionWrapper(exec, prototypeFunctionStructure, 1, exec->propertyNames().parse, dateParse), DontEnum);
66 putDirectFunctionWithoutTransition(exec, new (exec) NativeFunctionWrapper(exec, prototypeFunctionStructure, 7, exec->propertyNames().UTC, dateUTC), DontEnum);
67 putDirectFunctionWithoutTransition(exec, new (exec) NativeFunctionWrapper(exec, prototypeFunctionStructure, 0, exec->propertyNames().now, dateNow), DontEnum);
68
69 putDirectWithoutTransition(exec->propertyNames().length, jsNumber(exec, 7), ReadOnly | DontEnum | DontDelete);
70 }
71
72 // ECMA 15.9.3
73 JSObject* constructDate(ExecState* exec, const ArgList& args)
74 {
75 int numArgs = args.size();
76
77 double value;
78
79 if (numArgs == 0) // new Date() ECMA 15.9.3.3
80 value = jsCurrentTime();
81 else if (numArgs == 1) {
82 if (args.at(0).inherits(&DateInstance::info))
83 value = asDateInstance(args.at(0))->internalNumber();
84 else {
85 JSValue primitive = args.at(0).toPrimitive(exec);
86 if (primitive.isString())
87 value = parseDate(exec, primitive.getString(exec));
88 else
89 value = primitive.toNumber(exec);
90 }
91 } else {
92 if (isnan(args.at(0).toNumber(exec))
93 || isnan(args.at(1).toNumber(exec))
94 || (numArgs >= 3 && isnan(args.at(2).toNumber(exec)))
95 || (numArgs >= 4 && isnan(args.at(3).toNumber(exec)))
96 || (numArgs >= 5 && isnan(args.at(4).toNumber(exec)))
97 || (numArgs >= 6 && isnan(args.at(5).toNumber(exec)))
98 || (numArgs >= 7 && isnan(args.at(6).toNumber(exec))))
99 value = NaN;
100 else {
101 GregorianDateTime t;
102 int year = args.at(0).toInt32(exec);
103 t.year = (year >= 0 && year <= 99) ? year : year - 1900;
104 t.month = args.at(1).toInt32(exec);
105 t.monthDay = (numArgs >= 3) ? args.at(2).toInt32(exec) : 1;
106 t.hour = args.at(3).toInt32(exec);
107 t.minute = args.at(4).toInt32(exec);
108 t.second = args.at(5).toInt32(exec);
109 t.isDST = -1;
110 double ms = (numArgs >= 7) ? args.at(6).toNumber(exec) : 0;
111 value = gregorianDateTimeToMS(exec, t, ms, false);
112 }
113 }
114
115 return new (exec) DateInstance(exec, value);
116 }
117
118 static JSObject* constructWithDateConstructor(ExecState* exec, JSObject*, const ArgList& args)
119 {
120 return constructDate(exec, args);
121 }
122
123 ConstructType DateConstructor::getConstructData(ConstructData& constructData)
124 {
125 constructData.native.function = constructWithDateConstructor;
126 return ConstructTypeHost;
127 }
128
129 // ECMA 15.9.2
130 static JSValue JSC_HOST_CALL callDate(ExecState* exec, JSObject*, JSValue, const ArgList&)
131 {
132 time_t localTime = time(0);
133 tm localTM;
134 getLocalTime(&localTime, &localTM);
135 GregorianDateTime ts(exec, localTM);
136 DateConversionBuffer date;
137 DateConversionBuffer time;
138 formatDate(ts, date);
139 formatTime(ts, time);
140 return jsNontrivialString(exec, makeString(date, " ", time));
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(exec, 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, jsCurrentTime());
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(exec, t, ms, true));
181 }
182
183 } // namespace JSC