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 "DateInstance.h"
27 #include "DatePrototype.h"
28 #include "JSGlobalObject.h"
30 #include "ObjectPrototype.h"
31 #include "PrototypeFunction.h"
34 #include <wtf/MathExtras.h>
41 #include <sys/timeb.h>
46 // TODO: MakeTime (15.9.11.1) etc. ?
48 ASSERT_CLASS_FITS_IN_CELL(DateConstructor
);
50 static JSValuePtr
dateParse(ExecState
*, JSObject
*, JSValuePtr
, const ArgList
&);
51 static JSValuePtr
dateNow(ExecState
*, JSObject
*, JSValuePtr
, const ArgList
&);
52 static JSValuePtr
dateUTC(ExecState
*, JSObject
*, JSValuePtr
, const ArgList
&);
54 DateConstructor::DateConstructor(ExecState
* exec
, PassRefPtr
<Structure
> structure
, Structure
* prototypeFunctionStructure
, DatePrototype
* datePrototype
)
55 : InternalFunction(&exec
->globalData(), structure
, Identifier(exec
, datePrototype
->classInfo()->className
))
57 putDirectWithoutTransition(exec
->propertyNames().prototype
, datePrototype
, DontEnum
|DontDelete
|ReadOnly
);
59 putDirectFunctionWithoutTransition(exec
, new (exec
) PrototypeFunction(exec
, prototypeFunctionStructure
, 1, exec
->propertyNames().parse
, dateParse
), DontEnum
);
60 putDirectFunctionWithoutTransition(exec
, new (exec
) PrototypeFunction(exec
, prototypeFunctionStructure
, 7, exec
->propertyNames().UTC
, dateUTC
), DontEnum
);
61 putDirectFunctionWithoutTransition(exec
, new (exec
) PrototypeFunction(exec
, prototypeFunctionStructure
, 0, exec
->propertyNames().now
, dateNow
), DontEnum
);
63 putDirectWithoutTransition(exec
->propertyNames().length
, jsNumber(exec
, 7), ReadOnly
| DontEnum
| DontDelete
);
67 JSObject
* constructDate(ExecState
* exec
, const ArgList
& args
)
69 int numArgs
= args
.size();
73 if (numArgs
== 0) // new Date() ECMA 15.9.3.3
74 value
= getCurrentUTCTime();
75 else if (numArgs
== 1) {
76 if (args
.at(exec
, 0).isObject(&DateInstance::info
))
77 value
= asDateInstance(args
.at(exec
, 0))->internalNumber();
79 JSValuePtr primitive
= args
.at(exec
, 0).toPrimitive(exec
);
80 if (primitive
.isString())
81 value
= parseDate(primitive
.getString());
83 value
= primitive
.toNumber(exec
);
86 if (isnan(args
.at(exec
, 0).toNumber(exec
))
87 || isnan(args
.at(exec
, 1).toNumber(exec
))
88 || (numArgs
>= 3 && isnan(args
.at(exec
, 2).toNumber(exec
)))
89 || (numArgs
>= 4 && isnan(args
.at(exec
, 3).toNumber(exec
)))
90 || (numArgs
>= 5 && isnan(args
.at(exec
, 4).toNumber(exec
)))
91 || (numArgs
>= 6 && isnan(args
.at(exec
, 5).toNumber(exec
)))
92 || (numArgs
>= 7 && isnan(args
.at(exec
, 6).toNumber(exec
))))
96 int year
= args
.at(exec
, 0).toInt32(exec
);
97 t
.year
= (year
>= 0 && year
<= 99) ? year
: year
- 1900;
98 t
.month
= args
.at(exec
, 1).toInt32(exec
);
99 t
.monthDay
= (numArgs
>= 3) ? args
.at(exec
, 2).toInt32(exec
) : 1;
100 t
.hour
= args
.at(exec
, 3).toInt32(exec
);
101 t
.minute
= args
.at(exec
, 4).toInt32(exec
);
102 t
.second
= args
.at(exec
, 5).toInt32(exec
);
104 double ms
= (numArgs
>= 7) ? args
.at(exec
, 6).toNumber(exec
) : 0;
105 value
= gregorianDateTimeToMS(t
, ms
, false);
109 DateInstance
* result
= new (exec
) DateInstance(exec
->lexicalGlobalObject()->dateStructure());
110 result
->setInternalValue(jsNumber(exec
, timeClip(value
)));
114 static JSObject
* constructWithDateConstructor(ExecState
* exec
, JSObject
*, const ArgList
& args
)
116 return constructDate(exec
, args
);
119 ConstructType
DateConstructor::getConstructData(ConstructData
& constructData
)
121 constructData
.native
.function
= constructWithDateConstructor
;
122 return ConstructTypeHost
;
126 static JSValuePtr
callDate(ExecState
* exec
, JSObject
*, JSValuePtr
, const ArgList
&)
128 time_t localTime
= time(0);
130 getLocalTime(&localTime
, &localTM
);
131 GregorianDateTime
ts(localTM
);
132 return jsNontrivialString(exec
, formatDate(ts
) + " " + formatTime(ts
, false));
135 CallType
DateConstructor::getCallData(CallData
& callData
)
137 callData
.native
.function
= callDate
;
141 static JSValuePtr
dateParse(ExecState
* exec
, JSObject
*, JSValuePtr
, const ArgList
& args
)
143 return jsNumber(exec
, parseDate(args
.at(exec
, 0).toString(exec
)));
146 static JSValuePtr
dateNow(ExecState
* exec
, JSObject
*, JSValuePtr
, const ArgList
&)
148 return jsNumber(exec
, getCurrentUTCTime());
151 static JSValuePtr
dateUTC(ExecState
* exec
, JSObject
*, JSValuePtr
, const ArgList
& args
)
154 if (isnan(args
.at(exec
, 0).toNumber(exec
))
155 || isnan(args
.at(exec
, 1).toNumber(exec
))
156 || (n
>= 3 && isnan(args
.at(exec
, 2).toNumber(exec
)))
157 || (n
>= 4 && isnan(args
.at(exec
, 3).toNumber(exec
)))
158 || (n
>= 5 && isnan(args
.at(exec
, 4).toNumber(exec
)))
159 || (n
>= 6 && isnan(args
.at(exec
, 5).toNumber(exec
)))
160 || (n
>= 7 && isnan(args
.at(exec
, 6).toNumber(exec
))))
164 int year
= args
.at(exec
, 0).toInt32(exec
);
165 t
.year
= (year
>= 0 && year
<= 99) ? year
: year
- 1900;
166 t
.month
= args
.at(exec
, 1).toInt32(exec
);
167 t
.monthDay
= (n
>= 3) ? args
.at(exec
, 2).toInt32(exec
) : 1;
168 t
.hour
= args
.at(exec
, 3).toInt32(exec
);
169 t
.minute
= args
.at(exec
, 4).toInt32(exec
);
170 t
.second
= args
.at(exec
, 5).toInt32(exec
);
171 double ms
= (n
>= 7) ? args
.at(exec
, 6).toNumber(exec
) : 0;
172 return jsNumber(exec
, gregorianDateTimeToMS(t
, ms
, true));