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 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
DateConstructor(ExecState * exec,PassRefPtr<Structure> structure,Structure * prototypeFunctionStructure,DatePrototype * datePrototype)60 DateConstructor::DateConstructor(ExecState* exec, PassRefPtr<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
constructDate(ExecState * exec,const ArgList & args)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 = getCurrentUTCTime();
81 else if (numArgs == 1) {
82 if (args.at(0).isObject(&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(primitive.getString());
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(t, ms, false);
112 }
113 }
114
115 DateInstance* result = new (exec) DateInstance(exec->lexicalGlobalObject()->dateStructure());
116 result->setInternalValue(jsNumber(exec, timeClip(value)));
117 return result;
118 }
119
constructWithDateConstructor(ExecState * exec,JSObject *,const ArgList & args)120 static JSObject* constructWithDateConstructor(ExecState* exec, JSObject*, const ArgList& args)
121 {
122 return constructDate(exec, args);
123 }
124
getConstructData(ConstructData & constructData)125 ConstructType DateConstructor::getConstructData(ConstructData& constructData)
126 {
127 constructData.native.function = constructWithDateConstructor;
128 return ConstructTypeHost;
129 }
130
131 // ECMA 15.9.2
callDate(ExecState * exec,JSObject *,JSValue,const ArgList &)132 static JSValue JSC_HOST_CALL callDate(ExecState* exec, JSObject*, JSValue, const ArgList&)
133 {
134 time_t localTime = time(0);
135 tm localTM;
136 getLocalTime(&localTime, &localTM);
137 GregorianDateTime ts(localTM);
138 return jsNontrivialString(exec, formatDate(ts) + " " + formatTime(ts, false));
139 }
140
getCallData(CallData & callData)141 CallType DateConstructor::getCallData(CallData& callData)
142 {
143 callData.native.function = callDate;
144 return CallTypeHost;
145 }
146
dateParse(ExecState * exec,JSObject *,JSValue,const ArgList & args)147 static JSValue JSC_HOST_CALL dateParse(ExecState* exec, JSObject*, JSValue, const ArgList& args)
148 {
149 return jsNumber(exec, parseDate(args.at(0).toString(exec)));
150 }
151
dateNow(ExecState * exec,JSObject *,JSValue,const ArgList &)152 static JSValue JSC_HOST_CALL dateNow(ExecState* exec, JSObject*, JSValue, const ArgList&)
153 {
154 return jsNumber(exec, getCurrentUTCTime());
155 }
156
dateUTC(ExecState * exec,JSObject *,JSValue,const ArgList & args)157 static JSValue JSC_HOST_CALL dateUTC(ExecState* exec, JSObject*, JSValue, const ArgList& args)
158 {
159 int n = args.size();
160 if (isnan(args.at(0).toNumber(exec))
161 || isnan(args.at(1).toNumber(exec))
162 || (n >= 3 && isnan(args.at(2).toNumber(exec)))
163 || (n >= 4 && isnan(args.at(3).toNumber(exec)))
164 || (n >= 5 && isnan(args.at(4).toNumber(exec)))
165 || (n >= 6 && isnan(args.at(5).toNumber(exec)))
166 || (n >= 7 && isnan(args.at(6).toNumber(exec))))
167 return jsNaN(exec);
168
169 GregorianDateTime t;
170 int year = args.at(0).toInt32(exec);
171 t.year = (year >= 0 && year <= 99) ? year : year - 1900;
172 t.month = args.at(1).toInt32(exec);
173 t.monthDay = (n >= 3) ? args.at(2).toInt32(exec) : 1;
174 t.hour = args.at(3).toInt32(exec);
175 t.minute = args.at(4).toInt32(exec);
176 t.second = args.at(5).toInt32(exec);
177 double ms = (n >= 7) ? args.at(6).toNumber(exec) : 0;
178 return jsNumber(exec, gregorianDateTimeToMS(t, ms, true));
179 }
180
181 } // namespace JSC
182