2 * Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
3 * Copyright (C) 2004, 2005, 2006, 2007, 2008 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 "ObjectPrototype.h"
32 #include "PrototypeFunction.h"
35 #include <wtf/DateMath.h>
36 #include <wtf/MathExtras.h>
38 #if PLATFORM(WINCE) && !PLATFORM(QT)
39 extern "C" time_t time(time_t* timer
); //provided by libce
47 #include <sys/timeb.h>
54 // TODO: MakeTime (15.9.11.1) etc. ?
56 ASSERT_CLASS_FITS_IN_CELL(DateConstructor
);
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
&);
62 DateConstructor::DateConstructor(ExecState
* exec
, PassRefPtr
<Structure
> structure
, Structure
* prototypeFunctionStructure
, DatePrototype
* datePrototype
)
63 : InternalFunction(&exec
->globalData(), structure
, Identifier(exec
, datePrototype
->classInfo()->className
))
65 putDirectWithoutTransition(exec
->propertyNames().prototype
, datePrototype
, DontEnum
|DontDelete
|ReadOnly
);
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
);
71 putDirectWithoutTransition(exec
->propertyNames().length
, jsNumber(exec
, 7), ReadOnly
| DontEnum
| DontDelete
);
75 JSObject
* constructDate(ExecState
* exec
, const ArgList
& args
)
77 int numArgs
= args
.size();
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();
87 JSValue primitive
= args
.at(0).toPrimitive(exec
);
88 if (primitive
.isString())
89 value
= parseDate(primitive
.getString());
91 value
= primitive
.toNumber(exec
);
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
))))
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
);
112 double ms
= (numArgs
>= 7) ? args
.at(6).toNumber(exec
) : 0;
113 value
= gregorianDateTimeToMS(t
, ms
, false);
117 DateInstance
* result
= new (exec
) DateInstance(exec
->lexicalGlobalObject()->dateStructure());
118 result
->setInternalValue(jsNumber(exec
, timeClip(value
)));
122 static JSObject
* constructWithDateConstructor(ExecState
* exec
, JSObject
*, const ArgList
& args
)
124 return constructDate(exec
, args
);
127 ConstructType
DateConstructor::getConstructData(ConstructData
& constructData
)
129 constructData
.native
.function
= constructWithDateConstructor
;
130 return ConstructTypeHost
;
134 static JSValue JSC_HOST_CALL
callDate(ExecState
* exec
, JSObject
*, JSValue
, const ArgList
&)
136 time_t localTime
= time(0);
138 getLocalTime(&localTime
, &localTM
);
139 GregorianDateTime
ts(localTM
);
140 return jsNontrivialString(exec
, formatDate(ts
) + " " + formatTime(ts
, false));
143 CallType
DateConstructor::getCallData(CallData
& callData
)
145 callData
.native
.function
= callDate
;
149 static JSValue JSC_HOST_CALL
dateParse(ExecState
* exec
, JSObject
*, JSValue
, const ArgList
& args
)
151 return jsNumber(exec
, parseDate(args
.at(0).toString(exec
)));
154 static JSValue JSC_HOST_CALL
dateNow(ExecState
* exec
, JSObject
*, JSValue
, const ArgList
&)
156 return jsNumber(exec
, getCurrentUTCTime());
159 static JSValue JSC_HOST_CALL
dateUTC(ExecState
* exec
, JSObject
*, JSValue
, const ArgList
& args
)
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
))))
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));