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 OS(WINCE) && !PLATFORM(QT)
39 extern "C" time_t time(time_t* timer
); // Provided by libce.
47 #include <sys/timeb.h>
54 ASSERT_CLASS_FITS_IN_CELL(DateConstructor
);
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
&);
60 DateConstructor::DateConstructor(ExecState
* exec
, NonNullPassRefPtr
<Structure
> structure
, Structure
* prototypeFunctionStructure
, DatePrototype
* datePrototype
)
61 : InternalFunction(&exec
->globalData(), structure
, Identifier(exec
, datePrototype
->classInfo()->className
))
63 putDirectWithoutTransition(exec
->propertyNames().prototype
, datePrototype
, DontEnum
|DontDelete
|ReadOnly
);
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
);
69 putDirectWithoutTransition(exec
->propertyNames().length
, jsNumber(exec
, 7), ReadOnly
| DontEnum
| DontDelete
);
73 JSObject
* constructDate(ExecState
* exec
, const ArgList
& args
)
75 int numArgs
= args
.size();
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();
85 JSValue primitive
= args
.at(0).toPrimitive(exec
);
86 if (primitive
.isString())
87 value
= parseDate(exec
, primitive
.getString(exec
));
89 value
= primitive
.toNumber(exec
);
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
))))
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
);
110 double ms
= (numArgs
>= 7) ? args
.at(6).toNumber(exec
) : 0;
111 value
= gregorianDateTimeToMS(exec
, t
, ms
, false);
115 return new (exec
) DateInstance(exec
, value
);
118 static JSObject
* constructWithDateConstructor(ExecState
* exec
, JSObject
*, const ArgList
& args
)
120 return constructDate(exec
, args
);
123 ConstructType
DateConstructor::getConstructData(ConstructData
& constructData
)
125 constructData
.native
.function
= constructWithDateConstructor
;
126 return ConstructTypeHost
;
130 static JSValue JSC_HOST_CALL
callDate(ExecState
* exec
, JSObject
*, JSValue
, const ArgList
&)
132 time_t localTime
= time(0);
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
));
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(exec
, args
.at(0).toString(exec
)));
154 static JSValue JSC_HOST_CALL
dateNow(ExecState
* exec
, JSObject
*, JSValue
, const ArgList
&)
156 return jsNumber(exec
, jsCurrentTime());
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(exec
, t
, ms
, true));