2 * Copyright 2008 Jacek Caban for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "wine/debug.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
28 /* 1601 to 1970 is 369 years plus 89 leap days */
29 #define TIME_EPOCH ((ULONGLONG)(369 * 365 + 89) * 86400 * 1000)
34 /* ECMA-262 3rd Edition 15.9.1.1 */
38 SYSTEMTIME standardDate;
40 SYSTEMTIME daylightDate;
44 static const WCHAR toStringW[] = {'t','o','S','t','r','i','n','g',0};
45 static const WCHAR toLocaleStringW[] = {'t','o','L','o','c','a','l','e','S','t','r','i','n','g',0};
46 static const WCHAR hasOwnPropertyW[] = {'h','a','s','O','w','n','P','r','o','p','e','r','t','y',0};
47 static const WCHAR propertyIsEnumerableW[] =
48 {'p','r','o','p','e','r','t','y','I','s','E','n','u','m','e','r','a','b','l','e',0};
49 static const WCHAR isPrototypeOfW[] = {'i','s','P','r','o','t','o','t','y','p','e','O','f',0};
50 static const WCHAR valueOfW[] = {'v','a','l','u','e','O','f',0};
51 static const WCHAR toUTCStringW[] = {'t','o','U','T','C','S','t','r','i','n','g',0};
52 static const WCHAR toDateStringW[] = {'t','o','D','a','t','e','S','t','r','i','n','g',0};
53 static const WCHAR toTimeStringW[] = {'t','o','T','i','m','e','S','t','r','i','n','g',0};
54 static const WCHAR toLocaleDateStringW[] = {'t','o','L','o','c','a','l','e','D','a','t','e','S','t','r','i','n','g',0};
55 static const WCHAR toLocaleTimeStringW[] = {'t','o','L','o','c','a','l','e','T','i','m','e','S','t','r','i','n','g',0};
56 static const WCHAR getTimeW[] = {'g','e','t','T','i','m','e',0};
57 static const WCHAR getFullYearW[] = {'g','e','t','F','u','l','l','Y','e','a','r',0};
58 static const WCHAR getUTCFullYearW[] = {'g','e','t','U','T','C','F','u','l','l','Y','e','a','r',0};
59 static const WCHAR getMonthW[] = {'g','e','t','M','o','n','t','h',0};
60 static const WCHAR getUTCMonthW[] = {'g','e','t','U','T','C','M','o','n','t','h',0};
61 static const WCHAR getDateW[] = {'g','e','t','D','a','t','e',0};
62 static const WCHAR getUTCDateW[] = {'g','e','t','U','T','C','D','a','t','e',0};
63 static const WCHAR getDayW[] = {'g','e','t','D','a','y',0};
64 static const WCHAR getUTCDayW[] = {'g','e','t','U','T','C','D','a','y',0};
65 static const WCHAR getHoursW[] = {'g','e','t','H','o','u','r','s',0};
66 static const WCHAR getUTCHoursW[] = {'g','e','t','U','T','C','H','o','u','r','s',0};
67 static const WCHAR getMinutesW[] = {'g','e','t','M','i','n','u','t','e','s',0};
68 static const WCHAR getUTCMinutesW[] = {'g','e','t','U','T','C','M','i','n','u','t','e','s',0};
69 static const WCHAR getSecondsW[] = {'g','e','t','S','e','c','o','n','d','s',0};
70 static const WCHAR getUTCSecondsW[] = {'g','e','t','U','T','C','S','e','c','o','n','d','s',0};
71 static const WCHAR getMillisecondsW[] = {'g','e','t','M','i','l','l','i','s','e','c','o','n','d','s',0};
72 static const WCHAR getUTCMillisecondsW[] = {'g','e','t','U','T','C','M','i','l','l','i','s','e','c','o','n','d','s',0};
73 static const WCHAR getTimezoneOffsetW[] = {'g','e','t','T','i','m','e','z','o','n','e','O','f','f','s','e','t',0};
74 static const WCHAR setTimeW[] = {'s','e','t','T','i','m','e',0};
75 static const WCHAR setMillisecondsW[] = {'s','e','t','M','i','l','l','i','s','e','c','o','n','d','s',0};
76 static const WCHAR setUTCMillisecondsW[] = {'s','e','t','U','T','C','M','i','l','l','i','s','e','c','o','n','d','s',0};
77 static const WCHAR setSecondsW[] = {'s','e','t','S','e','c','o','n','d','s',0};
78 static const WCHAR setUTCSecondsW[] = {'s','e','t','U','T','C','S','e','c','o','n','d','s',0};
79 static const WCHAR setMinutesW[] = {'s','e','t','M','i','n','u','t','e','s',0};
80 static const WCHAR setUTCMinutesW[] = {'s','e','t','U','T','C','M','i','n','u','t','e','s',0};
81 static const WCHAR setHoursW[] = {'s','e','t','H','o','u','r','s',0};
82 static const WCHAR setUTCHoursW[] = {'s','e','t','U','T','C','H','o','u','r','s',0};
83 static const WCHAR setDateW[] = {'s','e','t','D','a','t','e',0};
84 static const WCHAR setUTCDateW[] = {'s','e','t','U','T','C','D','a','t','e',0};
85 static const WCHAR setMonthW[] = {'s','e','t','M','o','n','t','h',0};
86 static const WCHAR setUTCMonthW[] = {'s','e','t','U','T','C','M','o','n','t','h',0};
87 static const WCHAR setFullYearW[] = {'s','e','t','F','u','l','l','Y','e','a','r',0};
88 static const WCHAR setUTCFullYearW[] = {'s','e','t','U','T','C','F','u','l','l','Y','e','a','r',0};
90 /*ECMA-262 3rd Edition 15.9.1.2 */
91 #define MS_PER_DAY 86400000
92 #define MS_PER_HOUR 3600000
93 #define MS_PER_MINUTE 60000
95 /* ECMA-262 3rd Edition 15.9.1.2 */
96 static inline DOUBLE day(DOUBLE time)
98 return floor(time / MS_PER_DAY);
101 /* ECMA-262 3rd Edition 15.9.1.3 */
102 static inline DOUBLE days_in_year(DOUBLE year)
106 if(year != (int)year)
110 if(y%4 != 0) return 365;
111 if(y%100 != 0) return 366;
112 if(y%400 != 0) return 365;
116 /* ECMA-262 3rd Edition 15.9.1.3 */
117 static inline DOUBLE day_from_year(DOUBLE year)
119 if(year != (int)year)
122 return floor(365.0*(year-1970) + floor((year-1969)/4)
123 - floor((year-1901)/100) + floor((year-1601)/400));
126 static inline int day_from_month(int month, int in_leap_year)
135 return 59+in_leap_year;
137 return 90+in_leap_year;
139 return 120+in_leap_year;
141 return 151+in_leap_year;
143 return 181+in_leap_year;
145 return 212+in_leap_year;
147 return 243+in_leap_year;
149 return 273+in_leap_year;
151 return 304+in_leap_year;
153 return 334+in_leap_year;
157 /* ECMA-262 3rd Edition 15.9.1.3 */
158 static inline DOUBLE time_from_year(DOUBLE year)
160 return MS_PER_DAY*day_from_year(year);
163 /* ECMA-262 3rd Edition 15.9.1.3 */
164 static inline DOUBLE year_from_time(DOUBLE time)
171 y = 1970 + time/365.25/MS_PER_DAY;
173 if(time_from_year(y) > time)
174 while(time_from_year(y) > time) y--;
176 while(time_from_year(y+1)<=time) y++;
181 /* ECMA-262 3rd Edition 15.9.1.3 */
182 static inline int in_leap_year(DOUBLE time)
184 if(days_in_year(year_from_time(time))==366)
189 /* ECMA-262 3rd Edition 15.9.1.4 */
190 static inline int day_within_year(DOUBLE time)
192 return day(time) - day_from_year(year_from_time(time));
195 /* ECMA-262 3rd Edition 15.9.1.4 */
196 static inline DOUBLE month_from_time(DOUBLE time)
198 int ily = in_leap_year(time);
199 int dwy = day_within_year(time);
204 if(0<=dwy && dwy<31) return 0;
205 if(dwy < 59+ily) return 1;
206 if(dwy < 90+ily) return 2;
207 if(dwy < 120+ily) return 3;
208 if(dwy < 151+ily) return 4;
209 if(dwy < 181+ily) return 5;
210 if(dwy < 212+ily) return 6;
211 if(dwy < 243+ily) return 7;
212 if(dwy < 273+ily) return 8;
213 if(dwy < 304+ily) return 9;
214 if(dwy < 334+ily) return 10;
218 /* ECMA-262 3rd Edition 15.9.1.5 */
219 static inline DOUBLE date_from_time(DOUBLE time)
221 int dwy = day_within_year(time);
222 int ily = in_leap_year(time);
223 int mft = month_from_time(time);
228 if(mft==0) return dwy+1;
229 if(mft==1) return dwy-30;
230 if(mft==2) return dwy-58-ily;
231 if(mft==3) return dwy-89-ily;
232 if(mft==4) return dwy-119-ily;
233 if(mft==5) return dwy-150-ily;
234 if(mft==6) return dwy-180-ily;
235 if(mft==7) return dwy-211-ily;
236 if(mft==8) return dwy-242-ily;
237 if(mft==9) return dwy-272-ily;
238 if(mft==10) return dwy-303-ily;
242 /* ECMA-262 3rd Edition 15.9.1.6 */
243 static inline DOUBLE week_day(DOUBLE time)
250 ret = fmod(day(time)+4, 7);
256 static inline DOUBLE convert_time(int year, SYSTEMTIME st)
267 time = time_from_year(year);
268 time += (DOUBLE)day_from_month(st.wMonth-1, in_leap_year(time)) * MS_PER_DAY;
271 set_week_day = st.wDayOfWeek-week_day(time);
274 time += set_week_day * MS_PER_DAY;
276 time += (DOUBLE)(st.wDay-1) * 7 * MS_PER_DAY;
277 if(month_from_time(time) != st.wMonth-1)
278 time -= 7 * MS_PER_DAY;
281 time += st.wDay * MS_PER_DAY;
283 time += st.wHour * MS_PER_HOUR;
284 time += st.wMinute * MS_PER_MINUTE;
289 /* ECMA-262 3rd Edition 15.9.1.9 */
290 static inline DOUBLE daylight_saving_ta(DOUBLE time, DateInstance *date)
292 int year = year_from_time(time);
293 DOUBLE standardTime, daylightTime;
298 standardTime = convert_time(year, date->standardDate);
299 daylightTime = convert_time(year, date->daylightDate);
301 if(isnan(standardTime) || isnan(daylightTime))
303 else if(standardTime > daylightTime) {
304 if(daylightTime <= time && time < standardTime)
305 return date->daylightBias;
307 return date->standardBias;
310 if(standardTime <= time && time < daylightTime)
311 return date->standardBias;
313 return date->daylightBias;
317 /* ECMA-262 3rd Edition 15.9.1.9 */
318 static inline DOUBLE local_time(DOUBLE time, DateInstance *date)
320 return time - (daylight_saving_ta(time, date)+date->bias)*MS_PER_MINUTE;
323 /* ECMA-262 3rd Edition 15.9.1.9 */
324 static inline DOUBLE utc(DOUBLE time, DateInstance *date)
326 time += date->bias * MS_PER_MINUTE;
327 return time + daylight_saving_ta(time, date)*MS_PER_MINUTE;
330 /* ECMA-262 3rd Edition 15.9.1.10 */
331 static inline DOUBLE hour_from_time(DOUBLE time)
338 ret = fmod(floor(time/MS_PER_HOUR), 24);
344 /* ECMA-262 3rd Edition 15.9.1.10 */
345 static inline DOUBLE min_from_time(DOUBLE time)
352 ret = fmod(floor(time/MS_PER_MINUTE), 60);
358 /* ECMA-262 3rd Edition 15.9.1.10 */
359 static inline DOUBLE sec_from_time(DOUBLE time)
366 ret = fmod(floor(time/1000), 60);
372 /* ECMA-262 3rd Edition 15.9.1.10 */
373 static inline DOUBLE ms_from_time(DOUBLE time)
380 ret = fmod(time, 1000);
381 if(ret<0) ret += 1000;
386 /* ECMA-262 3rd Edition 15.9.1.11 */
387 static inline DOUBLE make_time(DOUBLE hour, DOUBLE min, DOUBLE sec, DOUBLE ms)
389 return hour*MS_PER_HOUR + min*MS_PER_MINUTE + sec*1000 + ms;
392 /* ECMA-262 3rd Edition 15.9.1.12 */
393 static inline DOUBLE make_day(DOUBLE year, DOUBLE month, DOUBLE day)
397 year += floor(month/12);
399 month = fmod(month, 12);
400 if(month<0) month += 12;
402 time = time_from_year(year);
404 day += floor(time / MS_PER_DAY);
405 day += day_from_month(month, in_leap_year(time));
410 /* ECMA-262 3rd Edition 15.9.1.13 */
411 static inline DOUBLE make_date(DOUBLE day, DOUBLE time)
413 return day*MS_PER_DAY + time;
416 /* ECMA-262 3rd Edition 15.9.1.14 */
417 static inline DOUBLE time_clip(DOUBLE time)
419 if(8.64e15 < time || time < -8.64e15) {
426 static HRESULT Date_toString(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
427 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
433 static HRESULT Date_toLocaleString(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
434 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
440 static HRESULT Date_hasOwnProperty(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
441 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
447 static HRESULT Date_propertyIsEnumerable(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
448 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
454 static HRESULT Date_isPrototypeOf(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
455 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
461 static HRESULT Date_valueOf(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
462 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
468 static HRESULT Date_toUTCString(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
469 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
475 static HRESULT Date_toDateString(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
476 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
482 static HRESULT Date_toTimeString(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
483 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
489 static HRESULT Date_toLocaleDateString(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
490 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
496 static HRESULT Date_toLocaleTimeString(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
497 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
503 /* ECMA-262 3rd Edition 15.9.5.9 */
504 static HRESULT Date_getTime(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
505 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
509 if(!is_class(dispex, JSCLASS_DATE)) {
510 FIXME("throw TypeError\n");
515 DateInstance *date = (DateInstance*)dispex;
516 num_set_val(retv, date->time);
521 /* ECMA-262 3rd Edition 15.9.5.10 */
522 static HRESULT Date_getFullYear(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
523 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
527 if(!is_class(dispex, JSCLASS_DATE)) {
528 FIXME("throw TypeError\n");
533 DateInstance *date = (DateInstance*)dispex;
534 DOUBLE time = local_time(date->time, date);
536 num_set_val(retv, year_from_time(time));
541 /* ECMA-262 3rd Edition 15.9.5.11 */
542 static HRESULT Date_getUTCFullYear(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
543 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
547 if(!is_class(dispex, JSCLASS_DATE)) {
548 FIXME("throw TypeError\n");
553 DateInstance *date = (DateInstance*)dispex;
554 num_set_val(retv, year_from_time(date->time));
559 /* ECMA-262 3rd Edition 15.9.5.12 */
560 static HRESULT Date_getMonth(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
561 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
565 if(!is_class(dispex, JSCLASS_DATE)) {
566 FIXME("throw TypeError\n");
571 DateInstance *date = (DateInstance*)dispex;
572 DOUBLE time = local_time(date->time, date);
574 num_set_val(retv, month_from_time(time));
579 /* ECMA-262 3rd Edition 15.9.5.13 */
580 static HRESULT Date_getUTCMonth(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
581 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
585 if(!is_class(dispex, JSCLASS_DATE)) {
586 FIXME("throw TypeError\n");
591 DateInstance *date = (DateInstance*)dispex;
592 num_set_val(retv, month_from_time(date->time));
597 /* ECMA-262 3rd Edition 15.9.5.14 */
598 static HRESULT Date_getDate(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
599 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
603 if(!is_class(dispex, JSCLASS_DATE)) {
604 FIXME("throw TypeError\n");
609 DateInstance *date = (DateInstance*)dispex;
610 DOUBLE time = local_time(date->time, date);
612 num_set_val(retv, date_from_time(time));
617 /* ECMA-262 3rd Edition 15.9.5.15 */
618 static HRESULT Date_getUTCDate(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
619 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
623 if(!is_class(dispex, JSCLASS_DATE)) {
624 FIXME("throw TypeError\n");
629 DateInstance *date = (DateInstance*)dispex;
630 num_set_val(retv, date_from_time(date->time));
635 /* ECMA-262 3rd Edition 15.9.5.16 */
636 static HRESULT Date_getDay(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
637 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
641 if(!is_class(dispex, JSCLASS_DATE)) {
642 FIXME("throw TypeError\n");
647 DateInstance *date = (DateInstance*)dispex;
648 DOUBLE time = local_time(date->time, date);
650 num_set_val(retv, week_day(time));
655 /* ECMA-262 3rd Edition 15.9.5.17 */
656 static HRESULT Date_getUTCDay(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
657 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
661 if(!is_class(dispex, JSCLASS_DATE)) {
662 FIXME("throw TypeError\n");
667 DateInstance *date = (DateInstance*)dispex;
668 num_set_val(retv, week_day(date->time));
673 /* ECMA-262 3rd Edition 15.9.5.18 */
674 static HRESULT Date_getHours(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
675 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
679 if(!is_class(dispex, JSCLASS_DATE)) {
680 FIXME("throw TypeError\n");
685 DateInstance *date = (DateInstance*)dispex;
686 DOUBLE time = local_time(date->time, date);
688 num_set_val(retv, hour_from_time(time));
693 /* ECMA-262 3rd Edition 15.9.5.19 */
694 static HRESULT Date_getUTCHours(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
695 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
699 if(!is_class(dispex, JSCLASS_DATE)) {
700 FIXME("throw TypeError\n");
705 DateInstance *date = (DateInstance*)dispex;
706 num_set_val(retv, hour_from_time(date->time));
711 /* ECMA-262 3rd Edition 15.9.5.20 */
712 static HRESULT Date_getMinutes(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
713 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
717 if(!is_class(dispex, JSCLASS_DATE)) {
718 FIXME("throw TypeError\n");
723 DateInstance *date = (DateInstance*)dispex;
724 DOUBLE time = local_time(date->time, date);
726 num_set_val(retv, min_from_time(time));
731 /* ECMA-262 3rd Edition 15.9.5.21 */
732 static HRESULT Date_getUTCMinutes(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
733 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
737 if(!is_class(dispex, JSCLASS_DATE)) {
738 FIXME("throw TypeError\n");
743 DateInstance *date = (DateInstance*)dispex;
744 num_set_val(retv, min_from_time(date->time));
749 /* ECMA-262 3rd Edition 15.9.5.22 */
750 static HRESULT Date_getSeconds(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
751 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
755 if(!is_class(dispex, JSCLASS_DATE)) {
756 FIXME("throw TypeError\n");
761 DateInstance *date = (DateInstance*)dispex;
762 DOUBLE time = local_time(date->time, date);
764 num_set_val(retv, sec_from_time(time));
769 /* ECMA-262 3rd Edition 15.9.5.23 */
770 static HRESULT Date_getUTCSeconds(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
771 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
775 if(!is_class(dispex, JSCLASS_DATE)) {
776 FIXME("throw TypeError\n");
781 DateInstance *date = (DateInstance*)dispex;
782 num_set_val(retv, sec_from_time(date->time));
787 /* ECMA-262 3rd Edition 15.9.5.24 */
788 static HRESULT Date_getMilliseconds(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
789 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
793 if(!is_class(dispex, JSCLASS_DATE)) {
794 FIXME("throw TypeError\n");
799 DateInstance *date = (DateInstance*)dispex;
800 DOUBLE time = local_time(date->time, date);
802 num_set_val(retv, ms_from_time(time));
807 /* ECMA-262 3rd Edition 15.9.5.25 */
808 static HRESULT Date_getUTCMilliseconds(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
809 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
813 if(!is_class(dispex, JSCLASS_DATE)) {
814 FIXME("throw TypeError\n");
819 DateInstance *date = (DateInstance*)dispex;
820 num_set_val(retv, ms_from_time(date->time));
825 /* ECMA-262 3rd Edition 15.9.5.26 */
826 static HRESULT Date_getTimezoneOffset(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
827 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
831 if(!is_class(dispex, JSCLASS_DATE)) {
832 FIXME("throw TypeError\n");
837 DateInstance *date = (DateInstance*)dispex;
838 num_set_val(retv, floor(
839 (date->time-local_time(date->time, date))/MS_PER_MINUTE));
844 /* ECMA-262 3rd Edition 15.9.5.27 */
845 static HRESULT Date_setTime(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
846 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
854 if(!is_class(dispex, JSCLASS_DATE)) {
855 FIXME("throw TypeError\n");
860 FIXME("throw ArgumentNotOptional\n");
861 if(retv) num_set_nan(retv);
865 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
869 date = (DateInstance*)dispex;
870 date->time = time_clip(num_val(&v));
873 num_set_val(retv, date->time);
878 /* ECMA-262 3rd Edition 15.9.5.28 */
879 static HRESULT Date_setMilliseconds(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
880 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
889 if(!is_class(dispex, JSCLASS_DATE)) {
890 FIXME("throw TypeError\n");
895 FIXME("throw ArgumentNotOptional\n");
896 if(retv) num_set_nan(retv);
900 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
904 date = (DateInstance*)dispex;
905 t = local_time(date->time, date);
906 t = make_date(day(t), make_time(hour_from_time(t), min_from_time(t),
907 sec_from_time(t), num_val(&v)));
908 date->time = time_clip(utc(t, date));
911 num_set_val(retv, date->time);
916 /* ECMA-262 3rd Edition 15.9.5.29 */
917 static HRESULT Date_setUTCMilliseconds(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
918 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
927 if(!is_class(dispex, JSCLASS_DATE)) {
928 FIXME("throw TypeError\n");
933 FIXME("throw ArgumentNotOptional\n");
934 if(retv) num_set_nan(retv);
938 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
942 date = (DateInstance*)dispex;
944 t = make_date(day(t), make_time(hour_from_time(t), min_from_time(t),
945 sec_from_time(t), num_val(&v)));
946 date->time = time_clip(t);
949 num_set_val(retv, date->time);
954 /* ECMA-262 3rd Edition 15.9.5.30 */
955 static HRESULT Date_setSeconds(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
956 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
965 if(!is_class(dispex, JSCLASS_DATE)) {
966 FIXME("throw TypeError\n");
971 FIXME("throw ArgumentNotOptional\n");
972 if(retv) num_set_nan(retv);
976 date = (DateInstance*)dispex;
977 t = local_time(date->time, date);
979 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
984 if(arg_cnt(dp) > 1) {
985 hres = to_number(dispex->ctx, get_arg(dp, 1), ei, &v);
990 else ms = ms_from_time(t);
992 t = make_date(day(t), make_time(hour_from_time(t),
993 min_from_time(t), sec, ms));
994 date->time = time_clip(utc(t, date));
997 num_set_val(retv, date->time);
1002 /* ECMA-262 3rd Edition 15.9.5.31 */
1003 static HRESULT Date_setUTCSeconds(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1004 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
1013 if(!is_class(dispex, JSCLASS_DATE)) {
1014 FIXME("throw TypeError\n");
1019 FIXME("throw ArgumentNotOptional\n");
1020 if(retv) num_set_nan(retv);
1024 date = (DateInstance*)dispex;
1027 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
1032 if(arg_cnt(dp) > 1) {
1033 hres = to_number(dispex->ctx, get_arg(dp, 1), ei, &v);
1038 else ms = ms_from_time(t);
1040 t = make_date(day(t), make_time(hour_from_time(t),
1041 min_from_time(t), sec, ms));
1042 date->time = time_clip(t);
1045 num_set_val(retv, date->time);
1050 /* ECMA-262 3rd Edition 15.9.5.33 */
1051 static HRESULT Date_setMinutes(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1052 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
1060 if(!is_class(dispex, JSCLASS_DATE)) {
1061 FIXME("throw TypeError\n");
1066 FIXME("throw ArgumentNotOptional\n");
1067 if(retv) num_set_nan(retv);
1071 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
1075 date = (DateInstance*)dispex;
1076 date->time = time_clip(date->time - (min_from_time(date->time) - num_val(&v))*MS_PER_MINUTE);
1079 num_set_val(retv, date->time);
1084 /* ECMA-262 3rd Edition 15.9.5.34 */
1085 static HRESULT Date_setUTCMinutes(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1086 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
1088 return Date_setMinutes(dispex, lcid, flags, dp, retv, ei, caller);
1091 /* ECMA-262 3rd Edition 15.9.5.35 */
1092 static HRESULT Date_setHours(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1093 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
1101 if(!is_class(dispex, JSCLASS_DATE)) {
1102 FIXME("throw TypeError\n");
1107 FIXME("throw ArgumentNotOptional\n");
1108 if(retv) num_set_nan(retv);
1112 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
1116 date = (DateInstance*)dispex;
1117 date->time = time_clip(date->time
1118 - (hour_from_time(date->time - date->bias*MS_PER_MINUTE)
1119 - num_val(&v))*MS_PER_HOUR);
1122 num_set_val(retv, date->time);
1127 /* ECMA-262 3rd Edition 15.9.5.36 */
1128 static HRESULT Date_setUTCHours(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1129 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
1137 if(!is_class(dispex, JSCLASS_DATE)) {
1138 FIXME("throw TypeError\n");
1143 FIXME("throw ArgumentNotOptional\n");
1144 if(retv) num_set_nan(retv);
1148 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
1152 date = (DateInstance*)dispex;
1153 date->time = time_clip(date->time
1154 - (hour_from_time(date->time) - num_val(&v))*MS_PER_HOUR);
1157 num_set_val(retv, date->time);
1162 /* ECMA-262 3rd Edition 15.9.5.36 */
1163 static HRESULT Date_setDate(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1164 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
1170 /* ECMA-262 3rd Edition 15.9.5.37 */
1171 static HRESULT Date_setUTCDate(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1172 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
1178 static HRESULT Date_setMonth(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1179 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
1185 static HRESULT Date_setUTCMonth(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1186 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
1192 static HRESULT Date_setFullYear(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1193 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
1199 static HRESULT Date_setUTCFullYear(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1200 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
1206 static HRESULT Date_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1207 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
1213 static const builtin_prop_t Date_props[] = {
1214 {getDateW, Date_getDate, PROPF_METHOD},
1215 {getDayW, Date_getDay, PROPF_METHOD},
1216 {getFullYearW, Date_getFullYear, PROPF_METHOD},
1217 {getHoursW, Date_getHours, PROPF_METHOD},
1218 {getMillisecondsW, Date_getMilliseconds, PROPF_METHOD},
1219 {getMinutesW, Date_getMinutes, PROPF_METHOD},
1220 {getMonthW, Date_getMonth, PROPF_METHOD},
1221 {getSecondsW, Date_getSeconds, PROPF_METHOD},
1222 {getTimeW, Date_getTime, PROPF_METHOD},
1223 {getTimezoneOffsetW, Date_getTimezoneOffset, PROPF_METHOD},
1224 {getUTCDateW, Date_getUTCDate, PROPF_METHOD},
1225 {getUTCDayW, Date_getUTCDay, PROPF_METHOD},
1226 {getUTCFullYearW, Date_getUTCFullYear, PROPF_METHOD},
1227 {getUTCHoursW, Date_getUTCHours, PROPF_METHOD},
1228 {getUTCMillisecondsW, Date_getUTCMilliseconds, PROPF_METHOD},
1229 {getUTCMinutesW, Date_getUTCMinutes, PROPF_METHOD},
1230 {getUTCMonthW, Date_getUTCMonth, PROPF_METHOD},
1231 {getUTCSecondsW, Date_getUTCSeconds, PROPF_METHOD},
1232 {hasOwnPropertyW, Date_hasOwnProperty, PROPF_METHOD},
1233 {isPrototypeOfW, Date_isPrototypeOf, PROPF_METHOD},
1234 {propertyIsEnumerableW, Date_propertyIsEnumerable, PROPF_METHOD},
1235 {setDateW, Date_setDate, PROPF_METHOD},
1236 {setFullYearW, Date_setFullYear, PROPF_METHOD},
1237 {setHoursW, Date_setHours, PROPF_METHOD},
1238 {setMillisecondsW, Date_setMilliseconds, PROPF_METHOD},
1239 {setMinutesW, Date_setMinutes, PROPF_METHOD},
1240 {setMonthW, Date_setMonth, PROPF_METHOD},
1241 {setSecondsW, Date_setSeconds, PROPF_METHOD},
1242 {setTimeW, Date_setTime, PROPF_METHOD},
1243 {setUTCDateW, Date_setUTCDate, PROPF_METHOD},
1244 {setUTCFullYearW, Date_setUTCFullYear, PROPF_METHOD},
1245 {setUTCHoursW, Date_setUTCHours, PROPF_METHOD},
1246 {setUTCMillisecondsW, Date_setUTCMilliseconds, PROPF_METHOD},
1247 {setUTCMinutesW, Date_setUTCMinutes, PROPF_METHOD},
1248 {setUTCMonthW, Date_setUTCMonth, PROPF_METHOD},
1249 {setUTCSecondsW, Date_setUTCSeconds, PROPF_METHOD},
1250 {toDateStringW, Date_toDateString, PROPF_METHOD},
1251 {toLocaleDateStringW, Date_toLocaleDateString, PROPF_METHOD},
1252 {toLocaleStringW, Date_toLocaleString, PROPF_METHOD},
1253 {toLocaleTimeStringW, Date_toLocaleTimeString, PROPF_METHOD},
1254 {toStringW, Date_toString, PROPF_METHOD},
1255 {toTimeStringW, Date_toTimeString, PROPF_METHOD},
1256 {toUTCStringW, Date_toUTCString, PROPF_METHOD},
1257 {valueOfW, Date_valueOf, PROPF_METHOD},
1260 static const builtin_info_t Date_info = {
1262 {NULL, Date_value, 0},
1263 sizeof(Date_props)/sizeof(*Date_props),
1269 static HRESULT create_date(script_ctx_t *ctx, BOOL use_constr, DOUBLE time, DispatchEx **ret)
1273 TIME_ZONE_INFORMATION tzi;
1276 dret = GetTimeZoneInformation(&tzi);
1278 date = heap_alloc_zero(sizeof(DateInstance));
1280 return E_OUTOFMEMORY;
1283 hres = init_dispex_from_constr(&date->dispex, ctx, &Date_info, ctx->date_constr);
1285 hres = init_dispex(&date->dispex, ctx, &Date_info, NULL);
1292 date->bias = tzi.Bias;
1293 date->standardDate = tzi.StandardDate;
1294 date->standardBias = tzi.StandardBias;
1295 date->daylightDate = tzi.DaylightDate;
1296 date->daylightBias = tzi.DaylightBias;
1298 *ret = &date->dispex;
1302 static HRESULT DateConstr_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1303 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1311 case DISPATCH_CONSTRUCT:
1312 switch(arg_cnt(dp)) {
1313 /* ECMA-262 3rd Edition 15.9.3.3 */
1318 GetSystemTimeAsFileTime(&time);
1319 lltime = ((LONGLONG)time.dwHighDateTime<<32)
1320 + time.dwLowDateTime;
1322 hres = create_date(dispex->ctx, TRUE, lltime/10000-TIME_EPOCH, &date);
1328 /* ECMA-262 3rd Edition 15.9.3.2 */
1332 hres = to_primitive(dispex->ctx, get_arg(dp,0), ei, &prim);
1336 if(V_VT(&prim) == VT_BSTR) {
1337 FIXME("VT_BSTR not supported\n");
1341 hres = to_number(dispex->ctx, &prim, ei, &num);
1342 VariantClear(&prim);
1346 hres = create_date(dispex->ctx, TRUE, time_clip(num_val(&num)), &date);
1352 /* ECMA-262 3rd Edition 15.9.3.1 */
1354 VARIANT year, month, vdate, hours, minutes, seconds, ms;
1356 int arg_no = arg_cnt(dp), y;
1358 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &year);
1366 hres = to_number(dispex->ctx, get_arg(dp, 1), ei, &month);
1371 hres = to_number(dispex->ctx, get_arg(dp, 2), ei, &vdate);
1376 V_VT(&vdate) = VT_R8;
1381 hres = to_number(dispex->ctx, get_arg(dp, 3), ei, &hours);
1386 V_VT(&hours) = VT_R8;
1391 hres = to_number(dispex->ctx, get_arg(dp, 4), ei, &minutes);
1396 V_VT(&minutes) = VT_R8;
1401 hres = to_number(dispex->ctx, get_arg(dp, 5), ei, &seconds);
1406 V_VT(&seconds) = VT_R8;
1411 hres = to_number(dispex->ctx, get_arg(dp, 6), ei, &ms);
1420 hres = create_date(dispex->ctx, TRUE, time_clip(
1421 make_date(make_day(y, num_val(&month), num_val(&vdate)),
1422 make_time(num_val(&hours), num_val(&minutes),
1423 num_val(&seconds), num_val(&ms)))), &date);
1427 di = (DateInstance*)date;
1428 di->time = utc(di->time, di);
1432 V_VT(retv) = VT_DISPATCH;
1433 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(date);
1437 FIXME("unimplemented flags %x\n", flags);
1444 HRESULT create_date_constr(script_ctx_t *ctx, DispatchEx **ret)
1449 hres = create_date(ctx, FALSE, 0.0, &date);
1453 hres = create_builtin_function(ctx, DateConstr_value, PROPF_CONSTR, date, ret);
1455 jsdisp_release(date);