usp10/tests: Check broken value for script tag as well.
[wine.git] / dlls / jscript / date.c
blob7015e0f9677f2336cce22a1c8c5711b2c683313b
1 /*
2 * Copyright 2008 Jacek Caban for CodeWeavers
3 * Copyright 2009 Piotr Caban
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.1 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 St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include "config.h"
21 #include "wine/port.h"
23 #include <limits.h>
24 #include <math.h>
26 #include "jscript.h"
28 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
32 /* 1601 to 1970 is 369 years plus 89 leap days */
33 #define TIME_EPOCH ((ULONGLONG)(369 * 365 + 89) * 86400 * 1000)
35 typedef struct {
36 jsdisp_t dispex;
38 /* ECMA-262 3rd Edition 15.9.1.1 */
39 DOUBLE time;
41 LONG bias;
42 SYSTEMTIME standardDate;
43 LONG standardBias;
44 SYSTEMTIME daylightDate;
45 LONG daylightBias;
46 } DateInstance;
48 static const WCHAR toStringW[] = {'t','o','S','t','r','i','n','g',0};
49 static const WCHAR toLocaleStringW[] = {'t','o','L','o','c','a','l','e','S','t','r','i','n','g',0};
50 static const WCHAR propertyIsEnumerableW[] =
51 {'p','r','o','p','e','r','t','y','I','s','E','n','u','m','e','r','a','b','l','e',0};
52 static const WCHAR isPrototypeOfW[] = {'i','s','P','r','o','t','o','t','y','p','e','O','f',0};
53 static const WCHAR valueOfW[] = {'v','a','l','u','e','O','f',0};
54 static const WCHAR toUTCStringW[] = {'t','o','U','T','C','S','t','r','i','n','g',0};
55 static const WCHAR toGMTStringW[] = {'t','o','G','M','T','S','t','r','i','n','g',0};
56 static const WCHAR toDateStringW[] = {'t','o','D','a','t','e','S','t','r','i','n','g',0};
57 static const WCHAR toTimeStringW[] = {'t','o','T','i','m','e','S','t','r','i','n','g',0};
58 static const WCHAR toLocaleDateStringW[] = {'t','o','L','o','c','a','l','e','D','a','t','e','S','t','r','i','n','g',0};
59 static const WCHAR toLocaleTimeStringW[] = {'t','o','L','o','c','a','l','e','T','i','m','e','S','t','r','i','n','g',0};
60 static const WCHAR getTimeW[] = {'g','e','t','T','i','m','e',0};
61 static const WCHAR getFullYearW[] = {'g','e','t','F','u','l','l','Y','e','a','r',0};
62 static const WCHAR getUTCFullYearW[] = {'g','e','t','U','T','C','F','u','l','l','Y','e','a','r',0};
63 static const WCHAR getMonthW[] = {'g','e','t','M','o','n','t','h',0};
64 static const WCHAR getUTCMonthW[] = {'g','e','t','U','T','C','M','o','n','t','h',0};
65 static const WCHAR getDateW[] = {'g','e','t','D','a','t','e',0};
66 static const WCHAR getUTCDateW[] = {'g','e','t','U','T','C','D','a','t','e',0};
67 static const WCHAR getDayW[] = {'g','e','t','D','a','y',0};
68 static const WCHAR getUTCDayW[] = {'g','e','t','U','T','C','D','a','y',0};
69 static const WCHAR getHoursW[] = {'g','e','t','H','o','u','r','s',0};
70 static const WCHAR getUTCHoursW[] = {'g','e','t','U','T','C','H','o','u','r','s',0};
71 static const WCHAR getMinutesW[] = {'g','e','t','M','i','n','u','t','e','s',0};
72 static const WCHAR getUTCMinutesW[] = {'g','e','t','U','T','C','M','i','n','u','t','e','s',0};
73 static const WCHAR getSecondsW[] = {'g','e','t','S','e','c','o','n','d','s',0};
74 static const WCHAR getUTCSecondsW[] = {'g','e','t','U','T','C','S','e','c','o','n','d','s',0};
75 static const WCHAR getMillisecondsW[] = {'g','e','t','M','i','l','l','i','s','e','c','o','n','d','s',0};
76 static const WCHAR getUTCMillisecondsW[] = {'g','e','t','U','T','C','M','i','l','l','i','s','e','c','o','n','d','s',0};
77 static const WCHAR getTimezoneOffsetW[] = {'g','e','t','T','i','m','e','z','o','n','e','O','f','f','s','e','t',0};
78 static const WCHAR setTimeW[] = {'s','e','t','T','i','m','e',0};
79 static const WCHAR setMillisecondsW[] = {'s','e','t','M','i','l','l','i','s','e','c','o','n','d','s',0};
80 static const WCHAR setUTCMillisecondsW[] = {'s','e','t','U','T','C','M','i','l','l','i','s','e','c','o','n','d','s',0};
81 static const WCHAR setSecondsW[] = {'s','e','t','S','e','c','o','n','d','s',0};
82 static const WCHAR setUTCSecondsW[] = {'s','e','t','U','T','C','S','e','c','o','n','d','s',0};
83 static const WCHAR setMinutesW[] = {'s','e','t','M','i','n','u','t','e','s',0};
84 static const WCHAR setUTCMinutesW[] = {'s','e','t','U','T','C','M','i','n','u','t','e','s',0};
85 static const WCHAR setHoursW[] = {'s','e','t','H','o','u','r','s',0};
86 static const WCHAR setUTCHoursW[] = {'s','e','t','U','T','C','H','o','u','r','s',0};
87 static const WCHAR setDateW[] = {'s','e','t','D','a','t','e',0};
88 static const WCHAR setUTCDateW[] = {'s','e','t','U','T','C','D','a','t','e',0};
89 static const WCHAR setMonthW[] = {'s','e','t','M','o','n','t','h',0};
90 static const WCHAR setUTCMonthW[] = {'s','e','t','U','T','C','M','o','n','t','h',0};
91 static const WCHAR setFullYearW[] = {'s','e','t','F','u','l','l','Y','e','a','r',0};
92 static const WCHAR setUTCFullYearW[] = {'s','e','t','U','T','C','F','u','l','l','Y','e','a','r',0};
93 static const WCHAR getYearW[] = {'g','e','t','Y','e','a','r',0};
94 static const WCHAR setYearW[] = {'s','e','t','Y','e','a','r',0};
96 static const WCHAR UTCW[] = {'U','T','C',0};
97 static const WCHAR parseW[] = {'p','a','r','s','e',0};
99 static inline DateInstance *date_this(vdisp_t *jsthis)
101 return is_vclass(jsthis, JSCLASS_DATE) ? (DateInstance*)jsthis->u.jsdisp : NULL;
104 /*ECMA-262 3rd Edition 15.9.1.2 */
105 #define MS_PER_DAY 86400000
106 #define MS_PER_HOUR 3600000
107 #define MS_PER_MINUTE 60000
109 /* ECMA-262 3rd Edition 15.9.1.2 */
110 static inline DOUBLE day(DOUBLE time)
112 return floor(time / MS_PER_DAY);
115 /* ECMA-262 3rd Edition 15.9.1.2 */
116 static inline DOUBLE time_within_day(DOUBLE time)
118 DOUBLE ret;
120 ret = fmod(time, MS_PER_DAY);
121 if(ret < 0)
122 ret += MS_PER_DAY;
124 return ret;
127 /* ECMA-262 3rd Edition 15.9.1.3 */
128 static inline DOUBLE days_in_year(DOUBLE year)
130 int y;
132 if(year != (int)year)
133 return ret_nan();
135 y = year;
136 if(y%4 != 0) return 365;
137 if(y%100 != 0) return 366;
138 if(y%400 != 0) return 365;
139 return 366;
142 /* ECMA-262 3rd Edition 15.9.1.3 */
143 static inline DOUBLE day_from_year(DOUBLE year)
145 if(year != (int)year)
146 return ret_nan();
148 return floor(365.0*(year-1970) + floor((year-1969)/4)
149 - floor((year-1901)/100) + floor((year-1601)/400));
152 static inline int day_from_month(int month, int in_leap_year)
154 switch(month)
156 case 0:
157 return 0;
158 case 1:
159 return 31;
160 case 2:
161 return 59+in_leap_year;
162 case 3:
163 return 90+in_leap_year;
164 case 4:
165 return 120+in_leap_year;
166 case 5:
167 return 151+in_leap_year;
168 case 6:
169 return 181+in_leap_year;
170 case 7:
171 return 212+in_leap_year;
172 case 8:
173 return 243+in_leap_year;
174 case 9:
175 return 273+in_leap_year;
176 case 10:
177 return 304+in_leap_year;
178 default:
179 return 334+in_leap_year;
183 /* ECMA-262 3rd Edition 15.9.1.3 */
184 static inline DOUBLE time_from_year(DOUBLE year)
186 return MS_PER_DAY*day_from_year(year);
189 /* ECMA-262 3rd Edition 15.9.1.3 */
190 static inline DOUBLE year_from_time(DOUBLE time)
192 int y;
194 if(isnan(time))
195 return ret_nan();
197 y = 1970 + time/365.25/MS_PER_DAY;
199 if(time_from_year(y) > time)
200 while(time_from_year(y) > time) y--;
201 else
202 while(time_from_year(y+1)<=time) y++;
204 return y;
207 /* ECMA-262 3rd Edition 15.9.1.3 */
208 static inline int in_leap_year(DOUBLE time)
210 if(days_in_year(year_from_time(time))==366)
211 return 1;
212 return 0;
215 /* ECMA-262 3rd Edition 15.9.1.4 */
216 static inline int day_within_year(DOUBLE time)
218 return day(time) - day_from_year(year_from_time(time));
221 /* ECMA-262 3rd Edition 15.9.1.4 */
222 static inline DOUBLE month_from_time(DOUBLE time)
224 int ily = in_leap_year(time);
225 int dwy = day_within_year(time);
227 if(isnan(time))
228 return ret_nan();
230 if(0<=dwy && dwy<31) return 0;
231 if(dwy < 59+ily) return 1;
232 if(dwy < 90+ily) return 2;
233 if(dwy < 120+ily) return 3;
234 if(dwy < 151+ily) return 4;
235 if(dwy < 181+ily) return 5;
236 if(dwy < 212+ily) return 6;
237 if(dwy < 243+ily) return 7;
238 if(dwy < 273+ily) return 8;
239 if(dwy < 304+ily) return 9;
240 if(dwy < 334+ily) return 10;
241 return 11;
244 /* ECMA-262 3rd Edition 15.9.1.5 */
245 static inline DOUBLE date_from_time(DOUBLE time)
247 int dwy = day_within_year(time);
248 int ily = in_leap_year(time);
249 int mft = month_from_time(time);
251 if(isnan(time))
252 return ret_nan();
254 if(mft==0) return dwy+1;
255 if(mft==1) return dwy-30;
256 if(mft==2) return dwy-58-ily;
257 if(mft==3) return dwy-89-ily;
258 if(mft==4) return dwy-119-ily;
259 if(mft==5) return dwy-150-ily;
260 if(mft==6) return dwy-180-ily;
261 if(mft==7) return dwy-211-ily;
262 if(mft==8) return dwy-242-ily;
263 if(mft==9) return dwy-272-ily;
264 if(mft==10) return dwy-303-ily;
265 return dwy-333-ily;
268 /* ECMA-262 3rd Edition 15.9.1.6 */
269 static inline DOUBLE week_day(DOUBLE time)
271 DOUBLE ret;
273 if(isnan(time))
274 return ret_nan();
276 ret = fmod(day(time)+4, 7);
277 if(ret<0) ret += 7;
279 return ret;
282 static inline DOUBLE convert_time(int year, SYSTEMTIME st)
284 DOUBLE time;
285 int set_week_day;
287 if(st.wMonth == 0)
288 return ret_nan();
290 if(st.wYear != 0)
291 year = st.wYear;
293 time = time_from_year(year);
294 time += (DOUBLE)day_from_month(st.wMonth-1, in_leap_year(time)) * MS_PER_DAY;
296 if(st.wYear == 0) {
297 set_week_day = st.wDayOfWeek-week_day(time);
298 if(set_week_day < 0)
299 set_week_day += 7;
300 time += set_week_day * MS_PER_DAY;
302 time += (DOUBLE)(st.wDay-1) * 7 * MS_PER_DAY;
303 if(month_from_time(time) != st.wMonth-1)
304 time -= 7 * MS_PER_DAY;
306 else
307 time += st.wDay * MS_PER_DAY;
309 time += st.wHour * MS_PER_HOUR;
310 time += st.wMinute * MS_PER_MINUTE;
312 return time;
315 /* ECMA-262 3rd Edition 15.9.1.9 */
316 static inline DOUBLE daylight_saving_ta(DOUBLE time, DateInstance *date)
318 int year = year_from_time(time);
319 DOUBLE standardTime, daylightTime;
321 if(isnan(time))
322 return 0;
324 standardTime = convert_time(year, date->standardDate);
325 daylightTime = convert_time(year, date->daylightDate);
327 if(isnan(standardTime) || isnan(daylightTime))
328 return 0;
329 else if(standardTime > daylightTime) {
330 if(daylightTime <= time && time < standardTime)
331 return date->daylightBias;
333 return date->standardBias;
335 else {
336 if(standardTime <= time && time < daylightTime)
337 return date->standardBias;
339 return date->daylightBias;
343 /* ECMA-262 3rd Edition 15.9.1.9 */
344 static inline DOUBLE local_time(DOUBLE time, DateInstance *date)
346 return time - (daylight_saving_ta(time, date)+date->bias)*MS_PER_MINUTE;
349 /* ECMA-262 3rd Edition 15.9.1.9 */
350 static inline DOUBLE utc(DOUBLE time, DateInstance *date)
352 time += date->bias * MS_PER_MINUTE;
353 return time + daylight_saving_ta(time, date)*MS_PER_MINUTE;
356 /* ECMA-262 3rd Edition 15.9.1.10 */
357 static inline DOUBLE hour_from_time(DOUBLE time)
359 DOUBLE ret;
361 if(isnan(time))
362 return ret_nan();
364 ret = fmod(floor(time/MS_PER_HOUR), 24);
365 if(ret<0) ret += 24;
367 return ret;
370 /* ECMA-262 3rd Edition 15.9.1.10 */
371 static inline DOUBLE min_from_time(DOUBLE time)
373 DOUBLE ret;
375 if(isnan(time))
376 return ret_nan();
378 ret = fmod(floor(time/MS_PER_MINUTE), 60);
379 if(ret<0) ret += 60;
381 return ret;
384 /* ECMA-262 3rd Edition 15.9.1.10 */
385 static inline DOUBLE sec_from_time(DOUBLE time)
387 DOUBLE ret;
389 if(isnan(time))
390 return ret_nan();
392 ret = fmod(floor(time/1000), 60);
393 if(ret<0) ret += 60;
395 return ret;
398 /* ECMA-262 3rd Edition 15.9.1.10 */
399 static inline DOUBLE ms_from_time(DOUBLE time)
401 DOUBLE ret;
403 if(isnan(time))
404 return ret_nan();
406 ret = fmod(time, 1000);
407 if(ret<0) ret += 1000;
409 return ret;
412 /* ECMA-262 3rd Edition 15.9.1.11 */
413 static inline DOUBLE make_time(DOUBLE hour, DOUBLE min, DOUBLE sec, DOUBLE ms)
415 return hour*MS_PER_HOUR + min*MS_PER_MINUTE + sec*1000 + ms;
418 /* ECMA-262 3rd Edition 15.9.1.12 */
419 static inline DOUBLE make_day(DOUBLE year, DOUBLE month, DOUBLE day)
421 DOUBLE time;
423 year += floor(month/12);
425 month = fmod(month, 12);
426 if(month<0) month += 12;
428 time = time_from_year(year);
430 day += floor(time / MS_PER_DAY);
431 day += day_from_month(month, in_leap_year(time));
433 return day-1;
436 /* ECMA-262 3rd Edition 15.9.1.13 */
437 static inline DOUBLE make_date(DOUBLE day, DOUBLE time)
439 return day*MS_PER_DAY + time;
442 /* ECMA-262 3rd Edition 15.9.1.14 */
443 static inline DOUBLE time_clip(DOUBLE time)
445 if(8.64e15 < time || time < -8.64e15) {
446 return ret_nan();
449 return floor(time);
452 static SYSTEMTIME create_systemtime(DOUBLE time)
454 SYSTEMTIME st;
456 st.wYear = year_from_time(time);
457 st.wMonth = month_from_time(time) + 1;
458 st.wDayOfWeek = week_day(time);
459 st.wDay = date_from_time(time);
460 st.wHour = hour_from_time(time);
461 st.wMinute = min_from_time(time);
462 st.wSecond = sec_from_time(time);
463 st.wMilliseconds = ms_from_time(time);
465 return st;
468 static inline HRESULT date_to_string(DOUBLE time, BOOL show_offset, int offset, VARIANT *retv)
470 static const WCHAR NaNW[] = { 'N','a','N',0 };
471 static const WCHAR formatW[] = { '%','s',' ','%','s',' ','%','d',' ',
472 '%','0','2','d',':','%','0','2','d',':','%','0','2','d',' ',
473 'U','T','C','%','c','%','0','2','d','%','0','2','d',' ','%','d','%','s',0 };
474 static const WCHAR formatUTCW[] = { '%','s',' ','%','s',' ','%','d',' ',
475 '%','0','2','d',':','%','0','2','d',':','%','0','2','d',' ',
476 'U','T','C',' ','%','d','%','s',0 };
477 static const WCHAR formatNoOffsetW[] = { '%','s',' ','%','s',' ',
478 '%','d',' ','%','0','2','d',':','%','0','2','d',':',
479 '%','0','2','d',' ','%','d','%','s',0 };
480 static const WCHAR ADW[] = { 0 };
481 static const WCHAR BCW[] = { ' ','B','.','C','.',0 };
483 static const DWORD week_ids[] = { LOCALE_SABBREVDAYNAME7, LOCALE_SABBREVDAYNAME1,
484 LOCALE_SABBREVDAYNAME2, LOCALE_SABBREVDAYNAME3, LOCALE_SABBREVDAYNAME4,
485 LOCALE_SABBREVDAYNAME5, LOCALE_SABBREVDAYNAME6 };
486 static const DWORD month_ids[] = { LOCALE_SABBREVMONTHNAME1, LOCALE_SABBREVMONTHNAME2,
487 LOCALE_SABBREVMONTHNAME3, LOCALE_SABBREVMONTHNAME4,
488 LOCALE_SABBREVMONTHNAME5, LOCALE_SABBREVMONTHNAME6,
489 LOCALE_SABBREVMONTHNAME7, LOCALE_SABBREVMONTHNAME8,
490 LOCALE_SABBREVMONTHNAME9, LOCALE_SABBREVMONTHNAME10,
491 LOCALE_SABBREVMONTHNAME11, LOCALE_SABBREVMONTHNAME12 };
493 BOOL formatAD = TRUE;
494 BSTR week, month;
495 BSTR date_str;
496 int len, size, year, day;
497 DWORD lcid_en, week_id, month_id;
498 WCHAR sign = '-';
500 if(isnan(time)) {
501 if(retv) {
502 V_VT(retv) = VT_BSTR;
503 V_BSTR(retv) = SysAllocString(NaNW);
504 if(!V_BSTR(retv))
505 return E_OUTOFMEMORY;
507 return S_OK;
510 if(retv) {
511 len = 21;
513 lcid_en = MAKELCID(MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),SORT_DEFAULT);
515 week_id = week_ids[(int)week_day(time)];
516 size = GetLocaleInfoW(lcid_en, week_id, NULL, 0);
517 week = SysAllocStringLen(NULL, size);
518 if(!week)
519 return E_OUTOFMEMORY;
520 GetLocaleInfoW(lcid_en, week_id, week, size);
521 len += size-1;
523 month_id = month_ids[(int)month_from_time(time)];
524 size = GetLocaleInfoW(lcid_en, month_id, NULL, 0);
525 month = SysAllocStringLen(NULL, size);
526 if(!month) {
527 SysFreeString(week);
528 return E_OUTOFMEMORY;
530 GetLocaleInfoW(lcid_en, month_id, month, size);
531 len += size-1;
533 year = year_from_time(time);
534 if(year<0)
535 year = -year+1;
536 do {
537 year /= 10;
538 len++;
539 } while(year);
541 year = year_from_time(time);
542 if(year<0) {
543 formatAD = FALSE;
544 year = -year+1;
545 len += 5;
548 day = date_from_time(time);
549 do {
550 day /= 10;
551 len++;
552 } while(day);
553 day = date_from_time(time);
555 if(!show_offset) len -= 9;
556 else if(offset == 0) len -= 5;
557 else if(offset < 0) {
558 sign = '+';
559 offset = -offset;
562 date_str = SysAllocStringLen(NULL, len);
563 if(!date_str) {
564 SysFreeString(week);
565 SysFreeString(month);
566 return E_OUTOFMEMORY;
569 if(!show_offset)
570 sprintfW(date_str, formatNoOffsetW, week, month, day,
571 (int)hour_from_time(time), (int)min_from_time(time),
572 (int)sec_from_time(time), year, formatAD?ADW:BCW);
573 else if(offset)
574 sprintfW(date_str, formatW, week, month, day,
575 (int)hour_from_time(time), (int)min_from_time(time),
576 (int)sec_from_time(time), sign, offset/60, offset%60,
577 year, formatAD?ADW:BCW);
578 else
579 sprintfW(date_str, formatUTCW, week, month, day,
580 (int)hour_from_time(time), (int)min_from_time(time),
581 (int)sec_from_time(time), year, formatAD?ADW:BCW);
583 SysFreeString(week);
584 SysFreeString(month);
586 V_VT(retv) = VT_BSTR;
587 V_BSTR(retv) = date_str;
589 return S_OK;
592 /* ECMA-262 3rd Edition 15.9.1.2 */
593 static HRESULT dateobj_to_string(DateInstance *date, VARIANT *retv)
595 DOUBLE time;
596 int offset;
598 time = local_time(date->time, date);
599 offset = date->bias +
600 daylight_saving_ta(time, date);
602 return date_to_string(time, TRUE, offset, retv);
605 static HRESULT Date_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
606 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
608 DateInstance *date;
610 TRACE("\n");
612 if(!(date = date_this(jsthis)))
613 return throw_type_error(ctx, ei, JS_E_DATE_EXPECTED, NULL);
615 return dateobj_to_string(date, retv);
618 /* ECMA-262 3rd Edition 15.9.1.5 */
619 static HRESULT Date_toLocaleString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
620 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
622 static const WCHAR NaNW[] = { 'N','a','N',0 };
623 SYSTEMTIME st;
624 DateInstance *date;
625 BSTR date_str;
626 int date_len, time_len;
628 TRACE("\n");
630 if(!(date = date_this(jsthis)))
631 return throw_type_error(ctx, ei, JS_E_DATE_EXPECTED, NULL);
633 if(isnan(date->time)) {
634 if(retv) {
635 V_VT(retv) = VT_BSTR;
636 V_BSTR(retv) = SysAllocString(NaNW);
637 if(!V_BSTR(retv))
638 return E_OUTOFMEMORY;
640 return S_OK;
643 st = create_systemtime(local_time(date->time, date));
645 if(st.wYear<1601 || st.wYear>9999)
646 return dateobj_to_string(date, retv);
648 if(retv) {
649 date_len = GetDateFormatW(ctx->lcid, DATE_LONGDATE, &st, NULL, NULL, 0);
650 time_len = GetTimeFormatW(ctx->lcid, 0, &st, NULL, NULL, 0);
651 date_str = SysAllocStringLen(NULL, date_len+time_len-1);
652 if(!date_str)
653 return E_OUTOFMEMORY;
654 GetDateFormatW(ctx->lcid, DATE_LONGDATE, &st, NULL, date_str, date_len);
655 GetTimeFormatW(ctx->lcid, 0, &st, NULL, &date_str[date_len], time_len);
656 date_str[date_len-1] = ' ';
658 V_VT(retv) = VT_BSTR;
659 V_BSTR(retv) = date_str;
661 return S_OK;
664 static HRESULT Date_valueOf(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
665 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
667 DateInstance *date;
669 TRACE("\n");
671 if(!(date = date_this(jsthis)))
672 return throw_type_error(ctx, ei, JS_E_DATE_EXPECTED, NULL);
674 if(retv)
675 num_set_val(retv, date->time);
676 return S_OK;
679 static inline HRESULT create_utc_string(script_ctx_t *ctx, vdisp_t *jsthis,
680 VARIANT *retv, jsexcept_t *ei)
682 static const WCHAR NaNW[] = { 'N','a','N',0 };
683 static const WCHAR formatADW[] = { '%','s',',',' ','%','d',' ','%','s',' ','%','d',' ',
684 '%','0','2','d',':','%','0','2','d',':','%','0','2','d',' ','U','T','C',0 };
685 static const WCHAR formatBCW[] = { '%','s',',',' ','%','d',' ','%','s',' ','%','d',' ','B','.','C','.',' ',
686 '%','0','2','d',':','%','0','2','d',':','%','0','2','d',' ','U','T','C',0 };
688 static const DWORD week_ids[] = { LOCALE_SABBREVDAYNAME7, LOCALE_SABBREVDAYNAME1,
689 LOCALE_SABBREVDAYNAME2, LOCALE_SABBREVDAYNAME3, LOCALE_SABBREVDAYNAME4,
690 LOCALE_SABBREVDAYNAME5, LOCALE_SABBREVDAYNAME6 };
691 static const DWORD month_ids[] = { LOCALE_SABBREVMONTHNAME1, LOCALE_SABBREVMONTHNAME2,
692 LOCALE_SABBREVMONTHNAME3, LOCALE_SABBREVMONTHNAME4,
693 LOCALE_SABBREVMONTHNAME5, LOCALE_SABBREVMONTHNAME6,
694 LOCALE_SABBREVMONTHNAME7, LOCALE_SABBREVMONTHNAME8,
695 LOCALE_SABBREVMONTHNAME9, LOCALE_SABBREVMONTHNAME10,
696 LOCALE_SABBREVMONTHNAME11, LOCALE_SABBREVMONTHNAME12 };
698 BOOL formatAD = TRUE;
699 BSTR week, month;
700 DateInstance *date;
701 BSTR date_str;
702 int len, size, year, day;
703 DWORD lcid_en, week_id, month_id;
705 if(!(date = date_this(jsthis)))
706 return throw_type_error(ctx, ei, JS_E_DATE_EXPECTED, NULL);
708 if(isnan(date->time)) {
709 if(retv) {
710 V_VT(retv) = VT_BSTR;
711 V_BSTR(retv) = SysAllocString(NaNW);
712 if(!V_BSTR(retv))
713 return E_OUTOFMEMORY;
715 return S_OK;
718 if(retv) {
719 len = 17;
721 lcid_en = MAKELCID(MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),SORT_DEFAULT);
723 week_id = week_ids[(int)week_day(date->time)];
724 size = GetLocaleInfoW(lcid_en, week_id, NULL, 0);
725 week = SysAllocStringLen(NULL, size);
726 if(!week)
727 return E_OUTOFMEMORY;
728 GetLocaleInfoW(lcid_en, week_id, week, size);
729 len += size-1;
731 month_id = month_ids[(int)month_from_time(date->time)];
732 size = GetLocaleInfoW(lcid_en, month_id, NULL, 0);
733 month = SysAllocStringLen(NULL, size);
734 if(!month) {
735 SysFreeString(week);
736 return E_OUTOFMEMORY;
738 GetLocaleInfoW(lcid_en, month_id, month, size);
739 len += size-1;
741 year = year_from_time(date->time);
742 if(year<0)
743 year = -year+1;
744 do {
745 year /= 10;
746 len++;
747 } while(year);
749 year = year_from_time(date->time);
750 if(year<0) {
751 formatAD = FALSE;
752 year = -year+1;
753 len += 5;
756 day = date_from_time(date->time);
757 do {
758 day /= 10;
759 len++;
760 } while(day);
761 day = date_from_time(date->time);
763 date_str = SysAllocStringLen(NULL, len);
764 if(!date_str) {
765 SysFreeString(week);
766 SysFreeString(month);
767 return E_OUTOFMEMORY;
769 sprintfW(date_str, formatAD?formatADW:formatBCW, week, day, month, year,
770 (int)hour_from_time(date->time), (int)min_from_time(date->time),
771 (int)sec_from_time(date->time));
773 SysFreeString(week);
774 SysFreeString(month);
776 V_VT(retv) = VT_BSTR;
777 V_BSTR(retv) = date_str;
779 return S_OK;
782 /* ECMA-262 3rd Edition 15.9.5.42 */
783 static HRESULT Date_toUTCString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
784 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
786 TRACE("\n");
787 return create_utc_string(ctx, jsthis, retv, ei);
790 static HRESULT Date_toGMTString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
791 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
793 TRACE("\n");
794 return create_utc_string(ctx, jsthis, retv, ei);
797 /* ECMA-262 3rd Edition 15.9.5.3 */
798 static HRESULT dateobj_to_date_string(DateInstance *date, VARIANT *retv)
800 static const WCHAR NaNW[] = { 'N','a','N',0 };
801 static const WCHAR formatADW[] = { '%','s',' ','%','s',' ','%','d',' ','%','d',0 };
802 static const WCHAR formatBCW[] = { '%','s',' ','%','s',' ','%','d',' ','%','d',' ','B','.','C','.',0 };
804 static const DWORD week_ids[] = { LOCALE_SABBREVDAYNAME7, LOCALE_SABBREVDAYNAME1,
805 LOCALE_SABBREVDAYNAME2, LOCALE_SABBREVDAYNAME3, LOCALE_SABBREVDAYNAME4,
806 LOCALE_SABBREVDAYNAME5, LOCALE_SABBREVDAYNAME6 };
807 static const DWORD month_ids[] = { LOCALE_SABBREVMONTHNAME1, LOCALE_SABBREVMONTHNAME2,
808 LOCALE_SABBREVMONTHNAME3, LOCALE_SABBREVMONTHNAME4,
809 LOCALE_SABBREVMONTHNAME5, LOCALE_SABBREVMONTHNAME6,
810 LOCALE_SABBREVMONTHNAME7, LOCALE_SABBREVMONTHNAME8,
811 LOCALE_SABBREVMONTHNAME9, LOCALE_SABBREVMONTHNAME10,
812 LOCALE_SABBREVMONTHNAME11, LOCALE_SABBREVMONTHNAME12 };
814 BOOL formatAD = TRUE;
815 BSTR week, month;
816 BSTR date_str;
817 DOUBLE time;
818 int len, size, year, day;
819 DWORD lcid_en, week_id, month_id;
821 if(isnan(date->time)) {
822 if(retv) {
823 V_VT(retv) = VT_BSTR;
824 V_BSTR(retv) = SysAllocString(NaNW);
825 if(!V_BSTR(retv))
826 return E_OUTOFMEMORY;
828 return S_OK;
831 time = local_time(date->time, date);
833 if(retv) {
834 len = 5;
836 lcid_en = MAKELCID(MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),SORT_DEFAULT);
838 week_id = week_ids[(int)week_day(time)];
839 size = GetLocaleInfoW(lcid_en, week_id, NULL, 0);
840 week = SysAllocStringLen(NULL, size);
841 if(!week)
842 return E_OUTOFMEMORY;
843 GetLocaleInfoW(lcid_en, week_id, week, size);
844 len += size-1;
846 month_id = month_ids[(int)month_from_time(time)];
847 size = GetLocaleInfoW(lcid_en, month_id, NULL, 0);
848 month = SysAllocStringLen(NULL, size);
849 if(!month) {
850 SysFreeString(week);
851 return E_OUTOFMEMORY;
853 GetLocaleInfoW(lcid_en, month_id, month, size);
854 len += size-1;
856 year = year_from_time(time);
857 if(year<0)
858 year = -year+1;
859 do {
860 year /= 10;
861 len++;
862 } while(year);
864 year = year_from_time(time);
865 if(year<0) {
866 formatAD = FALSE;
867 year = -year+1;
868 len += 5;
871 day = date_from_time(time);
872 do {
873 day /= 10;
874 len++;
875 } while(day);
876 day = date_from_time(time);
878 date_str = SysAllocStringLen(NULL, len);
879 if(!date_str) {
880 SysFreeString(week);
881 SysFreeString(month);
882 return E_OUTOFMEMORY;
884 sprintfW(date_str, formatAD?formatADW:formatBCW, week, month, day, year);
886 SysFreeString(week);
887 SysFreeString(month);
889 V_VT(retv) = VT_BSTR;
890 V_BSTR(retv) = date_str;
892 return S_OK;
895 static HRESULT Date_toDateString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
896 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
898 DateInstance *date;
900 if(!(date = date_this(jsthis)))
901 return throw_type_error(ctx, ei, JS_E_DATE_EXPECTED, NULL);
903 return dateobj_to_date_string(date, retv);
906 /* ECMA-262 3rd Edition 15.9.5.4 */
907 static HRESULT Date_toTimeString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
908 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
910 static const WCHAR NaNW[] = { 'N','a','N',0 };
911 static const WCHAR formatW[] = { '%','0','2','d',':','%','0','2','d',':','%','0','2','d',
912 ' ','U','T','C','%','c','%','0','2','d','%','0','2','d',0 };
913 static const WCHAR formatUTCW[] = { '%','0','2','d',':','%','0','2','d',
914 ':','%','0','2','d',' ','U','T','C',0 };
915 DateInstance *date;
916 BSTR date_str;
917 DOUBLE time;
918 WCHAR sign;
919 int offset;
921 TRACE("\n");
923 if(!(date = date_this(jsthis)))
924 return throw_type_error(ctx, ei, JS_E_DATE_EXPECTED, NULL);
926 if(isnan(date->time)) {
927 if(retv) {
928 V_VT(retv) = VT_BSTR;
929 V_BSTR(retv) = SysAllocString(NaNW);
930 if(!V_BSTR(retv))
931 return E_OUTOFMEMORY;
933 return S_OK;
936 time = local_time(date->time, date);
938 if(retv) {
939 date_str = SysAllocStringLen(NULL, 17);
940 if(!date_str)
941 return E_OUTOFMEMORY;
943 offset = date->bias +
944 daylight_saving_ta(time, date);
946 if(offset < 0) {
947 sign = '+';
948 offset = -offset;
950 else sign = '-';
952 if(offset)
953 sprintfW(date_str, formatW, (int)hour_from_time(time),
954 (int)min_from_time(time), (int)sec_from_time(time),
955 sign, offset/60, offset%60);
956 else
957 sprintfW(date_str, formatUTCW, (int)hour_from_time(time),
958 (int)min_from_time(time), (int)sec_from_time(time));
960 V_VT(retv) = VT_BSTR;
961 V_BSTR(retv) = date_str;
963 return S_OK;
966 /* ECMA-262 3rd Edition 15.9.5.6 */
967 static HRESULT Date_toLocaleDateString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
968 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
970 static const WCHAR NaNW[] = { 'N','a','N',0 };
971 SYSTEMTIME st;
972 DateInstance *date;
973 BSTR date_str;
974 int len;
976 TRACE("\n");
978 if(!(date = date_this(jsthis)))
979 return throw_type_error(ctx, ei, JS_E_DATE_EXPECTED, NULL);
981 if(isnan(date->time)) {
982 if(retv) {
983 V_VT(retv) = VT_BSTR;
984 V_BSTR(retv) = SysAllocString(NaNW);
985 if(!V_BSTR(retv))
986 return E_OUTOFMEMORY;
988 return S_OK;
991 st = create_systemtime(local_time(date->time, date));
993 if(st.wYear<1601 || st.wYear>9999)
994 return dateobj_to_date_string(date, retv);
996 if(retv) {
997 len = GetDateFormatW(ctx->lcid, DATE_LONGDATE, &st, NULL, NULL, 0);
998 date_str = SysAllocStringLen(NULL, len);
999 if(!date_str)
1000 return E_OUTOFMEMORY;
1001 GetDateFormatW(ctx->lcid, DATE_LONGDATE, &st, NULL, date_str, len);
1003 V_VT(retv) = VT_BSTR;
1004 V_BSTR(retv) = date_str;
1006 return S_OK;
1009 /* ECMA-262 3rd Edition 15.9.5.7 */
1010 static HRESULT Date_toLocaleTimeString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1011 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
1013 static const WCHAR NaNW[] = { 'N','a','N',0 };
1014 SYSTEMTIME st;
1015 DateInstance *date;
1016 BSTR date_str;
1017 int len;
1019 TRACE("\n");
1021 if(!(date = date_this(jsthis)))
1022 return throw_type_error(ctx, ei, JS_E_DATE_EXPECTED, NULL);
1024 if(isnan(date->time)) {
1025 if(retv) {
1026 V_VT(retv) = VT_BSTR;
1027 V_BSTR(retv) = SysAllocString(NaNW);
1028 if(!V_BSTR(retv))
1029 return E_OUTOFMEMORY;
1031 return S_OK;
1034 st = create_systemtime(local_time(date->time, date));
1036 if(st.wYear<1601 || st.wYear>9999)
1037 return Date_toTimeString(ctx, jsthis, flags, dp, retv, ei, caller);
1039 if(retv) {
1040 len = GetTimeFormatW(ctx->lcid, 0, &st, NULL, NULL, 0);
1041 date_str = SysAllocStringLen(NULL, len);
1042 if(!date_str)
1043 return E_OUTOFMEMORY;
1044 GetTimeFormatW(ctx->lcid, 0, &st, NULL, date_str, len);
1046 V_VT(retv) = VT_BSTR;
1047 V_BSTR(retv) = date_str;
1049 return S_OK;
1052 /* ECMA-262 3rd Edition 15.9.5.9 */
1053 static HRESULT Date_getTime(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1054 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
1056 DateInstance *date;
1058 TRACE("\n");
1060 if(!(date = date_this(jsthis)))
1061 return throw_type_error(ctx, ei, JS_E_DATE_EXPECTED, NULL);
1063 if(retv)
1064 num_set_val(retv, date->time);
1065 return S_OK;
1068 /* ECMA-262 3rd Edition 15.9.5.10 */
1069 static HRESULT Date_getFullYear(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1070 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
1072 DateInstance *date;
1074 TRACE("\n");
1076 if(!(date = date_this(jsthis)))
1077 return throw_type_error(ctx, ei, JS_E_DATE_EXPECTED, NULL);
1079 if(retv) {
1080 DOUBLE time = local_time(date->time, date);
1082 num_set_val(retv, year_from_time(time));
1084 return S_OK;
1087 /* ECMA-262 3rd Edition 15.9.5.11 */
1088 static HRESULT Date_getUTCFullYear(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1089 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
1091 DateInstance *date;
1093 TRACE("\n");
1095 if(!(date = date_this(jsthis)))
1096 return throw_type_error(ctx, ei, JS_E_DATE_EXPECTED, NULL);
1098 if(retv)
1099 num_set_val(retv, year_from_time(date->time));
1100 return S_OK;
1103 /* ECMA-262 3rd Edition 15.9.5.12 */
1104 static HRESULT Date_getMonth(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1105 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
1107 DateInstance *date;
1109 TRACE("\n");
1111 if(!(date = date_this(jsthis)))
1112 return throw_type_error(ctx, ei, JS_E_DATE_EXPECTED, NULL);
1114 if(retv) {
1115 DOUBLE time = local_time(date->time, date);
1117 num_set_val(retv, month_from_time(time));
1119 return S_OK;
1122 /* ECMA-262 3rd Edition 15.9.5.13 */
1123 static HRESULT Date_getUTCMonth(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1124 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
1126 DateInstance *date;
1128 TRACE("\n");
1130 if(!(date = date_this(jsthis)))
1131 return throw_type_error(ctx, ei, JS_E_DATE_EXPECTED, NULL);
1133 if(retv)
1134 num_set_val(retv, month_from_time(date->time));
1135 return S_OK;
1138 /* ECMA-262 3rd Edition 15.9.5.14 */
1139 static HRESULT Date_getDate(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1140 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
1142 DateInstance *date;
1144 TRACE("\n");
1146 if(!(date = date_this(jsthis)))
1147 return throw_type_error(ctx, ei, JS_E_DATE_EXPECTED, NULL);
1149 if(retv) {
1150 DOUBLE time = local_time(date->time, date);
1152 num_set_val(retv, date_from_time(time));
1154 return S_OK;
1157 /* ECMA-262 3rd Edition 15.9.5.15 */
1158 static HRESULT Date_getUTCDate(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1159 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
1161 DateInstance *date;
1163 TRACE("\n");
1165 if(!(date = date_this(jsthis)))
1166 return throw_type_error(ctx, ei, JS_E_DATE_EXPECTED, NULL);
1168 if(retv)
1169 num_set_val(retv, date_from_time(date->time));
1170 return S_OK;
1173 /* ECMA-262 3rd Edition 15.9.5.16 */
1174 static HRESULT Date_getDay(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1175 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
1177 DateInstance *date;
1179 TRACE("\n");
1181 if(!(date = date_this(jsthis)))
1182 return throw_type_error(ctx, ei, JS_E_DATE_EXPECTED, NULL);
1184 if(retv) {
1185 DOUBLE time = local_time(date->time, date);
1187 num_set_val(retv, week_day(time));
1189 return S_OK;
1192 /* ECMA-262 3rd Edition 15.9.5.17 */
1193 static HRESULT Date_getUTCDay(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1194 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
1196 DateInstance *date;
1198 TRACE("\n");
1200 if(!(date = date_this(jsthis)))
1201 return throw_type_error(ctx, ei, JS_E_DATE_EXPECTED, NULL);
1203 if(retv)
1204 num_set_val(retv, week_day(date->time));
1205 return S_OK;
1208 /* ECMA-262 3rd Edition 15.9.5.18 */
1209 static HRESULT Date_getHours(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1210 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
1212 DateInstance *date;
1214 TRACE("\n");
1216 if(!(date = date_this(jsthis)))
1217 return throw_type_error(ctx, ei, JS_E_DATE_EXPECTED, NULL);
1219 if(retv) {
1220 DOUBLE time = local_time(date->time, date);
1222 num_set_val(retv, hour_from_time(time));
1224 return S_OK;
1227 /* ECMA-262 3rd Edition 15.9.5.19 */
1228 static HRESULT Date_getUTCHours(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1229 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
1231 DateInstance *date;
1233 TRACE("\n");
1235 if(!(date = date_this(jsthis)))
1236 return throw_type_error(ctx, ei, JS_E_DATE_EXPECTED, NULL);
1238 if(retv)
1239 num_set_val(retv, hour_from_time(date->time));
1240 return S_OK;
1243 /* ECMA-262 3rd Edition 15.9.5.20 */
1244 static HRESULT Date_getMinutes(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1245 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
1247 DateInstance *date;
1249 TRACE("\n");
1251 if(!(date = date_this(jsthis)))
1252 return throw_type_error(ctx, ei, JS_E_DATE_EXPECTED, NULL);
1254 if(retv) {
1255 DOUBLE time = local_time(date->time, date);
1257 num_set_val(retv, min_from_time(time));
1259 return S_OK;
1262 /* ECMA-262 3rd Edition 15.9.5.21 */
1263 static HRESULT Date_getUTCMinutes(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1264 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
1266 DateInstance *date;
1268 TRACE("\n");
1270 if(!(date = date_this(jsthis)))
1271 return throw_type_error(ctx, ei, JS_E_DATE_EXPECTED, NULL);
1273 if(retv)
1274 num_set_val(retv, min_from_time(date->time));
1275 return S_OK;
1278 /* ECMA-262 3rd Edition 15.9.5.22 */
1279 static HRESULT Date_getSeconds(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1280 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
1282 DateInstance *date;
1284 TRACE("\n");
1286 if(!(date = date_this(jsthis)))
1287 return throw_type_error(ctx, ei, JS_E_DATE_EXPECTED, NULL);
1289 if(retv) {
1290 DOUBLE time = local_time(date->time, date);
1292 num_set_val(retv, sec_from_time(time));
1294 return S_OK;
1297 /* ECMA-262 3rd Edition 15.9.5.23 */
1298 static HRESULT Date_getUTCSeconds(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1299 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
1301 DateInstance *date;
1303 TRACE("\n");
1305 if(!(date = date_this(jsthis)))
1306 return throw_type_error(ctx, ei, JS_E_DATE_EXPECTED, NULL);
1308 if(retv)
1309 num_set_val(retv, sec_from_time(date->time));
1310 return S_OK;
1313 /* ECMA-262 3rd Edition 15.9.5.24 */
1314 static HRESULT Date_getMilliseconds(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1315 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
1317 DateInstance *date;
1319 TRACE("\n");
1321 if(!(date = date_this(jsthis)))
1322 return throw_type_error(ctx, ei, JS_E_DATE_EXPECTED, NULL);
1324 if(retv) {
1325 DOUBLE time = local_time(date->time, date);
1327 num_set_val(retv, ms_from_time(time));
1329 return S_OK;
1332 /* ECMA-262 3rd Edition 15.9.5.25 */
1333 static HRESULT Date_getUTCMilliseconds(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1334 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
1336 DateInstance *date;
1338 TRACE("\n");
1340 if(!(date = date_this(jsthis)))
1341 return throw_type_error(ctx, ei, JS_E_DATE_EXPECTED, NULL);
1343 if(retv)
1344 num_set_val(retv, ms_from_time(date->time));
1345 return S_OK;
1348 /* ECMA-262 3rd Edition 15.9.5.26 */
1349 static HRESULT Date_getTimezoneOffset(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1350 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
1352 DateInstance *date;
1354 TRACE("\n");
1356 if(!(date = date_this(jsthis)))
1357 return throw_type_error(ctx, ei, JS_E_DATE_EXPECTED, NULL);
1359 if(retv)
1360 num_set_val(retv, floor(
1361 (date->time-local_time(date->time, date))/MS_PER_MINUTE));
1362 return S_OK;
1365 /* ECMA-262 3rd Edition 15.9.5.27 */
1366 static HRESULT Date_setTime(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1367 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
1369 VARIANT v;
1370 HRESULT hres;
1371 DateInstance *date;
1373 TRACE("\n");
1375 if(!(date = date_this(jsthis)))
1376 return throw_type_error(ctx, ei, JS_E_DATE_EXPECTED, NULL);
1378 if(!arg_cnt(dp))
1379 return throw_type_error(ctx, ei, JS_E_MISSING_ARG, NULL);
1381 hres = to_number(ctx, get_arg(dp, 0), ei, &v);
1382 if(FAILED(hres))
1383 return hres;
1385 date->time = time_clip(num_val(&v));
1387 if(retv)
1388 num_set_val(retv, date->time);
1390 return S_OK;
1393 /* ECMA-262 3rd Edition 15.9.5.28 */
1394 static HRESULT Date_setMilliseconds(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1395 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
1397 VARIANT v;
1398 HRESULT hres;
1399 DateInstance *date;
1400 DOUBLE t;
1402 TRACE("\n");
1404 if(!(date = date_this(jsthis)))
1405 return throw_type_error(ctx, ei, JS_E_DATE_EXPECTED, NULL);
1407 if(!arg_cnt(dp))
1408 return throw_type_error(ctx, ei, JS_E_MISSING_ARG, NULL);
1410 hres = to_number(ctx, get_arg(dp, 0), ei, &v);
1411 if(FAILED(hres))
1412 return hres;
1414 t = local_time(date->time, date);
1415 t = make_date(day(t), make_time(hour_from_time(t), min_from_time(t),
1416 sec_from_time(t), num_val(&v)));
1417 date->time = time_clip(utc(t, date));
1419 if(retv)
1420 num_set_val(retv, date->time);
1422 return S_OK;
1425 /* ECMA-262 3rd Edition 15.9.5.29 */
1426 static HRESULT Date_setUTCMilliseconds(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1427 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
1429 VARIANT v;
1430 HRESULT hres;
1431 DateInstance *date;
1432 DOUBLE t;
1434 TRACE("\n");
1436 if(!(date = date_this(jsthis)))
1437 return throw_type_error(ctx, ei, JS_E_DATE_EXPECTED, NULL);
1439 if(!arg_cnt(dp))
1440 return throw_type_error(ctx, ei, JS_E_MISSING_ARG, NULL);
1442 hres = to_number(ctx, get_arg(dp, 0), ei, &v);
1443 if(FAILED(hres))
1444 return hres;
1446 t = date->time;
1447 t = make_date(day(t), make_time(hour_from_time(t), min_from_time(t),
1448 sec_from_time(t), num_val(&v)));
1449 date->time = time_clip(t);
1451 if(retv)
1452 num_set_val(retv, date->time);
1454 return S_OK;
1457 /* ECMA-262 3rd Edition 15.9.5.30 */
1458 static HRESULT Date_setSeconds(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1459 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
1461 VARIANT v;
1462 HRESULT hres;
1463 DateInstance *date;
1464 DOUBLE t, sec, ms;
1466 TRACE("\n");
1468 if(!(date = date_this(jsthis)))
1469 return throw_type_error(ctx, ei, JS_E_DATE_EXPECTED, NULL);
1471 if(!arg_cnt(dp))
1472 return throw_type_error(ctx, ei, JS_E_MISSING_ARG, NULL);
1474 t = local_time(date->time, date);
1476 hres = to_number(ctx, get_arg(dp, 0), ei, &v);
1477 if(FAILED(hres))
1478 return hres;
1479 sec = num_val(&v);
1481 if(arg_cnt(dp) > 1) {
1482 hres = to_number(ctx, get_arg(dp, 1), ei, &v);
1483 if(FAILED(hres))
1484 return hres;
1485 ms = num_val(&v);
1487 else ms = ms_from_time(t);
1489 t = make_date(day(t), make_time(hour_from_time(t),
1490 min_from_time(t), sec, ms));
1491 date->time = time_clip(utc(t, date));
1493 if(retv)
1494 num_set_val(retv, date->time);
1496 return S_OK;
1499 /* ECMA-262 3rd Edition 15.9.5.31 */
1500 static HRESULT Date_setUTCSeconds(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1501 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
1503 VARIANT v;
1504 HRESULT hres;
1505 DateInstance *date;
1506 DOUBLE t, sec, ms;
1508 TRACE("\n");
1510 if(!(date = date_this(jsthis)))
1511 return throw_type_error(ctx, ei, JS_E_DATE_EXPECTED, NULL);
1513 if(!arg_cnt(dp))
1514 return throw_type_error(ctx, ei, JS_E_MISSING_ARG, NULL);
1516 t = date->time;
1518 hres = to_number(ctx, get_arg(dp, 0), ei, &v);
1519 if(FAILED(hres))
1520 return hres;
1521 sec = num_val(&v);
1523 if(arg_cnt(dp) > 1) {
1524 hres = to_number(ctx, get_arg(dp, 1), ei, &v);
1525 if(FAILED(hres))
1526 return hres;
1527 ms = num_val(&v);
1529 else ms = ms_from_time(t);
1531 t = make_date(day(t), make_time(hour_from_time(t),
1532 min_from_time(t), sec, ms));
1533 date->time = time_clip(t);
1535 if(retv)
1536 num_set_val(retv, date->time);
1538 return S_OK;
1541 /* ECMA-262 3rd Edition 15.9.5.33 */
1542 static HRESULT Date_setMinutes(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1543 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
1545 VARIANT v;
1546 HRESULT hres;
1547 DateInstance *date;
1548 DOUBLE t, min, sec, ms;
1550 TRACE("\n");
1552 if(!(date = date_this(jsthis)))
1553 return throw_type_error(ctx, ei, JS_E_DATE_EXPECTED, NULL);
1555 if(!arg_cnt(dp))
1556 return throw_type_error(ctx, ei, JS_E_MISSING_ARG, NULL);
1558 t = local_time(date->time, date);
1560 hres = to_number(ctx, get_arg(dp, 0), ei, &v);
1561 if(FAILED(hres))
1562 return hres;
1563 min = num_val(&v);
1565 if(arg_cnt(dp) > 1) {
1566 hres = to_number(ctx, get_arg(dp, 1), ei, &v);
1567 if(FAILED(hres))
1568 return hres;
1569 sec = num_val(&v);
1571 else sec = sec_from_time(t);
1573 if(arg_cnt(dp) > 2) {
1574 hres = to_number(ctx, get_arg(dp, 2), ei, &v);
1575 if(FAILED(hres))
1576 return hres;
1577 ms = num_val(&v);
1579 else ms = ms_from_time(t);
1581 t = make_date(day(t), make_time(hour_from_time(t),
1582 min, sec, ms));
1583 date->time = time_clip(utc(t, date));
1585 if(retv)
1586 num_set_val(retv, date->time);
1588 return S_OK;
1591 /* ECMA-262 3rd Edition 15.9.5.34 */
1592 static HRESULT Date_setUTCMinutes(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1593 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
1595 VARIANT v;
1596 HRESULT hres;
1597 DateInstance *date;
1598 DOUBLE t, min, sec, ms;
1600 TRACE("\n");
1602 if(!(date = date_this(jsthis)))
1603 return throw_type_error(ctx, ei, JS_E_DATE_EXPECTED, NULL);
1605 if(!arg_cnt(dp))
1606 return throw_type_error(ctx, ei, JS_E_MISSING_ARG, NULL);
1608 t = date->time;
1610 hres = to_number(ctx, get_arg(dp, 0), ei, &v);
1611 if(FAILED(hres))
1612 return hres;
1613 min = num_val(&v);
1615 if(arg_cnt(dp) > 1) {
1616 hres = to_number(ctx, get_arg(dp, 1), ei, &v);
1617 if(FAILED(hres))
1618 return hres;
1619 sec = num_val(&v);
1621 else sec = sec_from_time(t);
1623 if(arg_cnt(dp) > 2) {
1624 hres = to_number(ctx, get_arg(dp, 2), ei, &v);
1625 if(FAILED(hres))
1626 return hres;
1627 ms = num_val(&v);
1629 else ms = ms_from_time(t);
1631 t = make_date(day(t), make_time(hour_from_time(t),
1632 min, sec, ms));
1633 date->time = time_clip(t);
1635 if(retv)
1636 num_set_val(retv, date->time);
1638 return S_OK;
1641 /* ECMA-262 3rd Edition 15.9.5.35 */
1642 static HRESULT Date_setHours(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1643 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
1645 VARIANT v;
1646 HRESULT hres;
1647 DateInstance *date;
1648 DOUBLE t, hour, min, sec, ms;
1650 TRACE("\n");
1652 if(!(date = date_this(jsthis)))
1653 return throw_type_error(ctx, ei, JS_E_DATE_EXPECTED, NULL);
1655 if(!arg_cnt(dp))
1656 return throw_type_error(ctx, ei, JS_E_MISSING_ARG, NULL);
1658 t = local_time(date->time, date);
1660 hres = to_number(ctx, get_arg(dp, 0), ei, &v);
1661 if(FAILED(hres))
1662 return hres;
1663 hour = num_val(&v);
1665 if(arg_cnt(dp) > 1) {
1666 hres = to_number(ctx, get_arg(dp, 1), ei, &v);
1667 if(FAILED(hres))
1668 return hres;
1669 min = num_val(&v);
1671 else min = min_from_time(t);
1673 if(arg_cnt(dp) > 2) {
1674 hres = to_number(ctx, get_arg(dp, 2), ei, &v);
1675 if(FAILED(hres))
1676 return hres;
1677 sec = num_val(&v);
1679 else sec = sec_from_time(t);
1681 if(arg_cnt(dp) > 3) {
1682 hres = to_number(ctx, get_arg(dp, 3), ei, &v);
1683 if(FAILED(hres))
1684 return hres;
1685 ms = num_val(&v);
1687 else ms = ms_from_time(t);
1689 t = make_date(day(t), make_time(hour, min, sec, ms));
1690 date->time = time_clip(utc(t, date));
1692 if(retv)
1693 num_set_val(retv, date->time);
1695 return S_OK;
1698 /* ECMA-262 3rd Edition 15.9.5.36 */
1699 static HRESULT Date_setUTCHours(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1700 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
1702 DateInstance *date;
1703 VARIANT v;
1704 HRESULT hres;
1705 DOUBLE t, hour, min, sec, ms;
1707 TRACE("\n");
1709 if(!(date = date_this(jsthis)))
1710 return throw_type_error(ctx, ei, JS_E_DATE_EXPECTED, NULL);
1712 if(!arg_cnt(dp))
1713 return throw_type_error(ctx, ei, JS_E_MISSING_ARG, NULL);
1715 t = date->time;
1717 hres = to_number(ctx, get_arg(dp, 0), ei, &v);
1718 if(FAILED(hres))
1719 return hres;
1720 hour = num_val(&v);
1722 if(arg_cnt(dp) > 1) {
1723 hres = to_number(ctx, get_arg(dp, 1), ei, &v);
1724 if(FAILED(hres))
1725 return hres;
1726 min = num_val(&v);
1728 else min = min_from_time(t);
1730 if(arg_cnt(dp) > 2) {
1731 hres = to_number(ctx, get_arg(dp, 2), ei, &v);
1732 if(FAILED(hres))
1733 return hres;
1734 sec = num_val(&v);
1736 else sec = sec_from_time(t);
1738 if(arg_cnt(dp) > 3) {
1739 hres = to_number(ctx, get_arg(dp, 3), ei, &v);
1740 if(FAILED(hres))
1741 return hres;
1742 ms = num_val(&v);
1744 else ms = ms_from_time(t);
1746 t = make_date(day(t), make_time(hour, min, sec, ms));
1747 date->time = time_clip(t);
1749 if(retv)
1750 num_set_val(retv, date->time);
1752 return S_OK;
1755 /* ECMA-262 3rd Edition 15.9.5.36 */
1756 static HRESULT Date_setDate(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1757 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
1759 VARIANT v;
1760 HRESULT hres;
1761 DateInstance *date;
1762 DOUBLE t;
1764 TRACE("\n");
1766 if(!(date = date_this(jsthis)))
1767 return throw_type_error(ctx, ei, JS_E_DATE_EXPECTED, NULL);
1769 if(!arg_cnt(dp))
1770 return throw_type_error(ctx, ei, JS_E_MISSING_ARG, NULL);
1772 hres = to_number(ctx, get_arg(dp, 0), ei, &v);
1773 if(FAILED(hres))
1774 return hres;
1776 t = local_time(date->time, date);
1777 t = make_date(make_day(year_from_time(t), month_from_time(t),
1778 num_val(&v)), time_within_day(t));
1779 date->time = time_clip(utc(t, date));
1781 if(retv)
1782 num_set_val(retv, date->time);
1784 return S_OK;
1787 /* ECMA-262 3rd Edition 15.9.5.37 */
1788 static HRESULT Date_setUTCDate(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1789 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
1791 VARIANT v;
1792 HRESULT hres;
1793 DateInstance *date;
1794 DOUBLE t;
1796 TRACE("\n");
1798 if(!(date = date_this(jsthis)))
1799 return throw_type_error(ctx, ei, JS_E_DATE_EXPECTED, NULL);
1801 if(!arg_cnt(dp))
1802 return throw_type_error(ctx, ei, JS_E_MISSING_ARG, NULL);
1804 hres = to_number(ctx, get_arg(dp, 0), ei, &v);
1805 if(FAILED(hres))
1806 return hres;
1808 t = date->time;
1809 t = make_date(make_day(year_from_time(t), month_from_time(t),
1810 num_val(&v)), time_within_day(t));
1811 date->time = time_clip(t);
1813 if(retv)
1814 num_set_val(retv, date->time);
1816 return S_OK;
1819 /* ECMA-262 3rd Edition 15.9.5.38 */
1820 static HRESULT Date_setMonth(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1821 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
1823 VARIANT v;
1824 HRESULT hres;
1825 DateInstance *date;
1826 DOUBLE t, month, ddate;
1828 TRACE("\n");
1830 if(!(date = date_this(jsthis)))
1831 return throw_type_error(ctx, ei, JS_E_DATE_EXPECTED, NULL);
1833 if(!arg_cnt(dp))
1834 return throw_type_error(ctx, ei, JS_E_MISSING_ARG, NULL);
1836 t = local_time(date->time, date);
1838 hres = to_number(ctx, get_arg(dp, 0), ei, &v);
1839 if(FAILED(hres))
1840 return hres;
1841 month = num_val(&v);
1843 if(arg_cnt(dp) > 1) {
1844 hres = to_number(ctx, get_arg(dp, 1), ei, &v);
1845 if(FAILED(hres))
1846 return hres;
1847 ddate = num_val(&v);
1849 else ddate = date_from_time(t);
1851 t = make_date(make_day(year_from_time(t), month, ddate),
1852 time_within_day(t));
1853 date->time = time_clip(utc(t, date));
1855 if(retv)
1856 num_set_val(retv, date->time);
1858 return S_OK;
1861 /* ECMA-262 3rd Edition 15.9.5.39 */
1862 static HRESULT Date_setUTCMonth(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1863 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
1865 VARIANT v;
1866 HRESULT hres;
1867 DateInstance *date;
1868 DOUBLE t, month, ddate;
1870 TRACE("\n");
1872 if(!(date = date_this(jsthis)))
1873 return throw_type_error(ctx, ei, JS_E_DATE_EXPECTED, NULL);
1875 if(!arg_cnt(dp))
1876 return throw_type_error(ctx, ei, JS_E_MISSING_ARG, NULL);
1878 t = date->time;
1880 hres = to_number(ctx, get_arg(dp, 0), ei, &v);
1881 if(FAILED(hres))
1882 return hres;
1883 month = num_val(&v);
1885 if(arg_cnt(dp) > 1) {
1886 hres = to_number(ctx, get_arg(dp, 1), ei, &v);
1887 if(FAILED(hres))
1888 return hres;
1889 ddate = num_val(&v);
1891 else ddate = date_from_time(t);
1893 t = make_date(make_day(year_from_time(t), month, ddate),
1894 time_within_day(t));
1895 date->time = time_clip(t);
1897 if(retv)
1898 num_set_val(retv, date->time);
1900 return S_OK;
1903 /* ECMA-262 3rd Edition 15.9.5.40 */
1904 static HRESULT Date_setFullYear(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1905 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
1907 VARIANT v;
1908 HRESULT hres;
1909 DateInstance *date;
1910 DOUBLE t, year, month, ddate;
1912 TRACE("\n");
1914 if(!(date = date_this(jsthis)))
1915 return throw_type_error(ctx, ei, JS_E_DATE_EXPECTED, NULL);
1917 if(!arg_cnt(dp))
1918 return throw_type_error(ctx, ei, JS_E_MISSING_ARG, NULL);
1920 t = local_time(date->time, date);
1922 hres = to_number(ctx, get_arg(dp, 0), ei, &v);
1923 if(FAILED(hres))
1924 return hres;
1925 year = num_val(&v);
1927 if(arg_cnt(dp) > 1) {
1928 hres = to_number(ctx, get_arg(dp, 1), ei, &v);
1929 if(FAILED(hres))
1930 return hres;
1931 month = num_val(&v);
1933 else month = month_from_time(t);
1935 if(arg_cnt(dp) > 2) {
1936 hres = to_number(ctx, get_arg(dp, 2), ei, &v);
1937 if(FAILED(hres))
1938 return hres;
1939 ddate = num_val(&v);
1941 else ddate = date_from_time(t);
1943 t = make_date(make_day(year, month, ddate), time_within_day(t));
1944 date->time = time_clip(utc(t, date));
1946 if(retv)
1947 num_set_val(retv, date->time);
1949 return S_OK;
1952 /* ECMA-262 3rd Edition 15.9.5.41 */
1953 static HRESULT Date_setUTCFullYear(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1954 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
1956 VARIANT v;
1957 HRESULT hres;
1958 DateInstance *date;
1959 DOUBLE t, year, month, ddate;
1961 TRACE("\n");
1963 if(!(date = date_this(jsthis)))
1964 return throw_type_error(ctx, ei, JS_E_DATE_EXPECTED, NULL);
1966 if(!arg_cnt(dp))
1967 return throw_type_error(ctx, ei, JS_E_MISSING_ARG, NULL);
1969 t = date->time;
1971 hres = to_number(ctx, get_arg(dp, 0), ei, &v);
1972 if(FAILED(hres))
1973 return hres;
1974 year = num_val(&v);
1976 if(arg_cnt(dp) > 1) {
1977 hres = to_number(ctx, get_arg(dp, 1), ei, &v);
1978 if(FAILED(hres))
1979 return hres;
1980 month = num_val(&v);
1982 else month = month_from_time(t);
1984 if(arg_cnt(dp) > 2) {
1985 hres = to_number(ctx, get_arg(dp, 2), ei, &v);
1986 if(FAILED(hres))
1987 return hres;
1988 ddate = num_val(&v);
1990 else ddate = date_from_time(t);
1992 t = make_date(make_day(year, month, ddate), time_within_day(t));
1993 date->time = time_clip(t);
1995 if(retv)
1996 num_set_val(retv, date->time);
1998 return S_OK;
2001 /* ECMA-262 3rd Edition B2.4 */
2002 static HRESULT Date_getYear(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
2003 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
2005 DateInstance *date;
2006 DOUBLE t, year;
2008 TRACE("\n");
2010 if(!(date = date_this(jsthis)))
2011 return throw_type_error(ctx, ei, JS_E_DATE_EXPECTED, NULL);
2013 t = local_time(date->time, date);
2014 if(isnan(t)) {
2015 if(retv)
2016 num_set_nan(retv);
2017 return S_OK;
2020 year = year_from_time(t);
2021 if(retv)
2022 num_set_val(retv, (1900<=year && year<2000)?year-1900:year);
2024 return S_OK;
2027 /* ECMA-262 3rd Edition B2.5 */
2028 static HRESULT Date_setYear(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
2029 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
2031 DateInstance *date;
2032 DOUBLE t, year;
2033 VARIANT v;
2034 HRESULT hres;
2036 TRACE("\n");
2038 if(!(date = date_this(jsthis)))
2039 return throw_type_error(ctx, ei, JS_E_DATE_EXPECTED, NULL);
2041 if(!arg_cnt(dp))
2042 return throw_type_error(ctx, ei, JS_E_MISSING_ARG, NULL);
2044 t = local_time(date->time, date);
2046 hres = to_number(ctx, get_arg(dp, 0), ei, &v);
2047 if(FAILED(hres))
2048 return hres;
2050 year = num_val(&v);
2051 if(isnan(year)) {
2052 date->time = year;
2053 if(retv)
2054 num_set_nan(retv);
2055 return S_OK;
2058 year = year >= 0.0 ? floor(year) : -floor(-year);
2059 if(-1.0 < year && year < 100.0)
2060 year += 1900.0;
2062 date->time = time_clip(utc(make_date(make_day(year, month_from_time(t), date_from_time(t)), time_within_day(t)), date));
2064 if(retv)
2065 num_set_val(retv, date->time);
2066 return S_OK;
2069 static HRESULT Date_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
2070 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
2072 TRACE("\n");
2074 switch(flags) {
2075 case INVOKE_FUNC:
2076 return throw_type_error(ctx, ei, JS_E_FUNCTION_EXPECTED, NULL);
2077 default:
2078 FIXME("unimplemented flags %x\n", flags);
2079 return E_NOTIMPL;
2082 return S_OK;
2085 static const builtin_prop_t Date_props[] = {
2086 {getDateW, Date_getDate, PROPF_METHOD},
2087 {getDayW, Date_getDay, PROPF_METHOD},
2088 {getFullYearW, Date_getFullYear, PROPF_METHOD},
2089 {getHoursW, Date_getHours, PROPF_METHOD},
2090 {getMillisecondsW, Date_getMilliseconds, PROPF_METHOD},
2091 {getMinutesW, Date_getMinutes, PROPF_METHOD},
2092 {getMonthW, Date_getMonth, PROPF_METHOD},
2093 {getSecondsW, Date_getSeconds, PROPF_METHOD},
2094 {getTimeW, Date_getTime, PROPF_METHOD},
2095 {getTimezoneOffsetW, Date_getTimezoneOffset, PROPF_METHOD},
2096 {getUTCDateW, Date_getUTCDate, PROPF_METHOD},
2097 {getUTCDayW, Date_getUTCDay, PROPF_METHOD},
2098 {getUTCFullYearW, Date_getUTCFullYear, PROPF_METHOD},
2099 {getUTCHoursW, Date_getUTCHours, PROPF_METHOD},
2100 {getUTCMillisecondsW, Date_getUTCMilliseconds, PROPF_METHOD},
2101 {getUTCMinutesW, Date_getUTCMinutes, PROPF_METHOD},
2102 {getUTCMonthW, Date_getUTCMonth, PROPF_METHOD},
2103 {getUTCSecondsW, Date_getUTCSeconds, PROPF_METHOD},
2104 {getYearW, Date_getYear, PROPF_METHOD},
2105 {setDateW, Date_setDate, PROPF_METHOD|1},
2106 {setFullYearW, Date_setFullYear, PROPF_METHOD|3},
2107 {setHoursW, Date_setHours, PROPF_METHOD|4},
2108 {setMillisecondsW, Date_setMilliseconds, PROPF_METHOD|1},
2109 {setMinutesW, Date_setMinutes, PROPF_METHOD|3},
2110 {setMonthW, Date_setMonth, PROPF_METHOD|2},
2111 {setSecondsW, Date_setSeconds, PROPF_METHOD|2},
2112 {setTimeW, Date_setTime, PROPF_METHOD|1},
2113 {setUTCDateW, Date_setUTCDate, PROPF_METHOD|1},
2114 {setUTCFullYearW, Date_setUTCFullYear, PROPF_METHOD|3},
2115 {setUTCHoursW, Date_setUTCHours, PROPF_METHOD|4},
2116 {setUTCMillisecondsW, Date_setUTCMilliseconds, PROPF_METHOD|1},
2117 {setUTCMinutesW, Date_setUTCMinutes, PROPF_METHOD|3},
2118 {setUTCMonthW, Date_setUTCMonth, PROPF_METHOD|2},
2119 {setUTCSecondsW, Date_setUTCSeconds, PROPF_METHOD|2},
2120 {setYearW, Date_setYear, PROPF_METHOD|1},
2121 {toDateStringW, Date_toDateString, PROPF_METHOD},
2122 {toGMTStringW, Date_toGMTString, PROPF_METHOD},
2123 {toLocaleDateStringW, Date_toLocaleDateString, PROPF_METHOD},
2124 {toLocaleStringW, Date_toLocaleString, PROPF_METHOD},
2125 {toLocaleTimeStringW, Date_toLocaleTimeString, PROPF_METHOD},
2126 {toStringW, Date_toString, PROPF_METHOD},
2127 {toTimeStringW, Date_toTimeString, PROPF_METHOD},
2128 {toUTCStringW, Date_toUTCString, PROPF_METHOD},
2129 {valueOfW, Date_valueOf, PROPF_METHOD},
2132 static const builtin_info_t Date_info = {
2133 JSCLASS_DATE,
2134 {NULL, Date_value, 0},
2135 sizeof(Date_props)/sizeof(*Date_props),
2136 Date_props,
2137 NULL,
2138 NULL
2141 static HRESULT create_date(script_ctx_t *ctx, jsdisp_t *object_prototype, DOUBLE time, jsdisp_t **ret)
2143 DateInstance *date;
2144 HRESULT hres;
2145 TIME_ZONE_INFORMATION tzi;
2147 GetTimeZoneInformation(&tzi);
2149 date = heap_alloc_zero(sizeof(DateInstance));
2150 if(!date)
2151 return E_OUTOFMEMORY;
2153 if(object_prototype)
2154 hres = init_dispex(&date->dispex, ctx, &Date_info, object_prototype);
2155 else
2156 hres = init_dispex_from_constr(&date->dispex, ctx, &Date_info, ctx->date_constr);
2157 if(FAILED(hres)) {
2158 heap_free(date);
2159 return hres;
2162 date->time = time;
2163 date->bias = tzi.Bias;
2164 date->standardDate = tzi.StandardDate;
2165 date->standardBias = tzi.StandardBias;
2166 date->daylightDate = tzi.DaylightDate;
2167 date->daylightBias = tzi.DaylightBias;
2169 *ret = &date->dispex;
2170 return S_OK;
2173 static inline HRESULT date_parse(BSTR input, VARIANT *retv) {
2174 static const DWORD string_ids[] = { LOCALE_SMONTHNAME12, LOCALE_SMONTHNAME11,
2175 LOCALE_SMONTHNAME10, LOCALE_SMONTHNAME9, LOCALE_SMONTHNAME8,
2176 LOCALE_SMONTHNAME7, LOCALE_SMONTHNAME6, LOCALE_SMONTHNAME5,
2177 LOCALE_SMONTHNAME4, LOCALE_SMONTHNAME3, LOCALE_SMONTHNAME2,
2178 LOCALE_SMONTHNAME1, LOCALE_SDAYNAME7, LOCALE_SDAYNAME1,
2179 LOCALE_SDAYNAME2, LOCALE_SDAYNAME3, LOCALE_SDAYNAME4,
2180 LOCALE_SDAYNAME5, LOCALE_SDAYNAME6 };
2181 BSTR strings[sizeof(string_ids)/sizeof(DWORD)];
2183 BSTR parse;
2184 int input_len, parse_len = 0, nest_level = 0, i, size;
2185 int year = 0, month = 0, day = 0, hour = 0, min = 0, sec = 0;
2186 int ms = 0, offset = 0, hour_adjust = 0;
2187 BOOL set_year = FALSE, set_month = FALSE, set_day = FALSE, set_hour = FALSE;
2188 BOOL set_offset = FALSE, set_era = FALSE, ad = TRUE, set_am = FALSE, am = TRUE;
2189 BOOL set_hour_adjust = TRUE;
2190 TIME_ZONE_INFORMATION tzi;
2191 DateInstance di;
2192 DWORD lcid_en;
2194 if(retv) num_set_nan(retv);
2196 input_len = SysStringLen(input);
2197 for(i=0; i<input_len; i++) {
2198 if(input[i] == '(') nest_level++;
2199 else if(input[i] == ')') {
2200 nest_level--;
2201 if(nest_level<0)
2202 return S_OK;
2204 else if(!nest_level) parse_len++;
2207 parse = SysAllocStringLen(NULL, parse_len);
2208 if(!parse)
2209 return E_OUTOFMEMORY;
2210 nest_level = 0;
2211 parse_len = 0;
2212 for(i=0; i<input_len; i++) {
2213 if(input[i] == '(') nest_level++;
2214 else if(input[i] == ')') nest_level--;
2215 else if(!nest_level) parse[parse_len++] = toupperW(input[i]);
2218 GetTimeZoneInformation(&tzi);
2219 di.bias = tzi.Bias;
2220 di.standardDate = tzi.StandardDate;
2221 di.standardBias = tzi.StandardBias;
2222 di.daylightDate = tzi.DaylightDate;
2223 di.daylightBias = tzi.DaylightBias;
2225 lcid_en = MAKELCID(MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),SORT_DEFAULT);
2226 for(i=0; i<sizeof(string_ids)/sizeof(DWORD); i++) {
2227 size = GetLocaleInfoW(lcid_en, string_ids[i], NULL, 0);
2228 strings[i] = SysAllocStringLen(NULL, size);
2229 if(!strings[i]) {
2230 i--;
2231 while(i-- >= 0)
2232 SysFreeString(strings[i]);
2233 SysFreeString(parse);
2234 return E_OUTOFMEMORY;
2236 GetLocaleInfoW(lcid_en, string_ids[i], strings[i], size);
2239 for(i=0; i<parse_len;) {
2240 while(isspaceW(parse[i])) i++;
2241 if(parse[i] == ',') {
2242 while(parse[i] == ',') i++;
2243 continue;
2246 if(parse[i]>='0' && parse[i]<='9') {
2247 int tmp = atoiW(&parse[i]);
2248 while(parse[i]>='0' && parse[i]<='9') i++;
2249 while(isspaceW(parse[i])) i++;
2251 if(parse[i] == ':') {
2252 /* Time */
2253 if(set_hour) break;
2254 set_hour = TRUE;
2256 hour = tmp;
2258 while(parse[i] == ':') i++;
2259 while(isspaceW(parse[i])) i++;
2260 if(parse[i]>='0' && parse[i]<='9') {
2261 min = atoiW(&parse[i]);
2262 while(parse[i]>='0' && parse[i]<='9') i++;
2265 while(isspaceW(parse[i])) i++;
2266 while(parse[i] == ':') i++;
2267 while(isspaceW(parse[i])) i++;
2268 if(parse[i]>='0' && parse[i]<='9') {
2269 sec = atoiW(&parse[i]);
2270 while(parse[i]>='0' && parse[i]<='9') i++;
2273 else if(parse[i]=='-' || parse[i]=='/') {
2274 /* Short date */
2275 if(set_day || set_month || set_year) break;
2276 set_day = TRUE;
2277 set_month = TRUE;
2278 set_year = TRUE;
2280 month = tmp-1;
2282 while(isspaceW(parse[i])) i++;
2283 while(parse[i]=='-' || parse[i]=='/') i++;
2284 while(isspaceW(parse[i])) i++;
2285 if(parse[i]<'0' || parse[i]>'9') break;
2286 day = atoiW(&parse[i]);
2287 while(parse[i]>='0' && parse[i]<='9') i++;
2289 while(parse[i]=='-' || parse[i]=='/') i++;
2290 while(isspaceW(parse[i])) i++;
2291 if(parse[i]<'0' || parse[i]>'9') break;
2292 year = atoiW(&parse[i]);
2293 while(parse[i]>='0' && parse[i]<='9') i++;
2295 else if(tmp<0) break;
2296 else if(tmp<70) {
2297 /* Day */
2298 if(set_day) break;
2299 set_day = TRUE;
2300 day = tmp;
2302 else {
2303 /* Year */
2304 if(set_year) break;
2305 set_year = TRUE;
2306 year = tmp;
2309 else {
2310 if(parse[i]<'A' || parse[i]>'Z') break;
2311 else if(parse[i]=='B' && (parse[i+1]=='C' ||
2312 (parse[i+1]=='.' && parse[i+2]=='C'))) {
2313 /* AD/BC */
2314 if(set_era) break;
2315 set_era = TRUE;
2316 ad = FALSE;
2318 i++;
2319 if(parse[i] == '.') i++;
2320 i++;
2321 if(parse[i] == '.') i++;
2323 else if(parse[i]=='A' && (parse[i+1]=='D' ||
2324 (parse[i+1]=='.' && parse[i+2]=='D'))) {
2325 /* AD/BC */
2326 if(set_era) break;
2327 set_era = TRUE;
2329 i++;
2330 if(parse[i] == '.') i++;
2331 i++;
2332 if(parse[i] == '.') i++;
2334 else if(parse[i+1]<'A' || parse[i+1]>'Z') {
2335 /* Timezone */
2336 if(set_offset) break;
2337 set_offset = TRUE;
2339 if(parse[i] <= 'I') hour_adjust = parse[i]-'A'+2;
2340 else if(parse[i] == 'J') break;
2341 else if(parse[i] <= 'M') hour_adjust = parse[i]-'K'+11;
2342 else if(parse[i] <= 'Y') hour_adjust = parse[i]-'N';
2343 else hour_adjust = 1;
2345 i++;
2346 if(parse[i] == '.') i++;
2348 else if(parse[i]=='A' && parse[i+1]=='M') {
2349 /* AM/PM */
2350 if(set_am) break;
2351 set_am = TRUE;
2352 am = TRUE;
2353 i += 2;
2355 else if(parse[i]=='P' && parse[i+1]=='M') {
2356 /* AM/PM */
2357 if(set_am) break;
2358 set_am = TRUE;
2359 am = FALSE;
2360 i += 2;
2362 else if((parse[i]=='U' && parse[i+1]=='T' && parse[i+2]=='C')
2363 || (parse[i]=='G' && parse[i+1]=='M' && parse[i+2]=='T')) {
2364 /* Timezone */
2365 BOOL positive = TRUE;
2367 if(set_offset) break;
2368 set_offset = TRUE;
2369 set_hour_adjust = FALSE;
2371 i += 3;
2372 while(isspaceW(parse[i])) i++;
2373 if(parse[i] == '-') positive = FALSE;
2374 else if(parse[i] != '+') continue;
2376 i++;
2377 while(isspaceW(parse[i])) i++;
2378 if(parse[i]<'0' || parse[i]>'9') break;
2379 offset = atoiW(&parse[i]);
2380 while(parse[i]>='0' && parse[i]<='9') i++;
2382 if(offset<24) offset *= 60;
2383 else offset = (offset/100)*60 + offset%100;
2385 if(positive) offset = -offset;
2387 else {
2388 /* Month or garbage */
2389 int j;
2391 for(size=i; parse[size]>='A' && parse[size]<='Z'; size++);
2392 size -= i;
2394 for(j=0; j<sizeof(string_ids)/sizeof(DWORD); j++)
2395 if(!memicmpW(&parse[i], strings[j], size)) break;
2397 if(j < 12) {
2398 if(set_month) break;
2399 set_month = TRUE;
2400 month = 11-j;
2402 else if(j == sizeof(string_ids)/sizeof(DWORD)) break;
2404 i += size;
2409 if(retv && i==parse_len && set_year && set_month
2410 && set_day && (!set_am || hour<13)) {
2411 if(set_am) {
2412 if(hour == 12) hour = 0;
2413 if(!am) hour += 12;
2416 if(!ad) year = -year+1;
2417 else if(year<100) year += 1900;
2419 V_VT(retv) = VT_R8;
2420 V_R8(retv) = time_clip(make_date(make_day(year, month, day),
2421 make_time(hour+hour_adjust, min, sec, ms)) + offset*MS_PER_MINUTE);
2423 if(set_hour_adjust) V_R8(retv) = utc(V_R8(retv), &di);
2426 for(i=0; i<sizeof(string_ids)/sizeof(DWORD); i++)
2427 SysFreeString(strings[i]);
2428 SysFreeString(parse);
2430 return S_OK;
2433 static HRESULT DateConstr_parse(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
2434 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
2436 BSTR parse_str;
2437 HRESULT hres;
2439 TRACE("\n");
2441 if(!arg_cnt(dp)) {
2442 if(retv)
2443 num_set_nan(retv);
2444 return S_OK;
2447 hres = to_string(ctx, get_arg(dp,0), ei, &parse_str);
2448 if(FAILED(hres))
2449 return hres;
2451 hres = date_parse(parse_str, retv);
2453 SysFreeString(parse_str);
2454 return hres;
2457 static HRESULT date_utc(script_ctx_t *ctx, DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei)
2459 VARIANT year, month, vdate, hours, minutes, seconds, ms;
2460 DOUBLE y;
2461 int arg_no = arg_cnt(dp);
2462 HRESULT hres;
2464 TRACE("\n");
2466 if(arg_no>0) {
2467 hres = to_number(ctx, get_arg(dp, 0), ei, &year);
2468 if(FAILED(hres))
2469 return hres;
2470 y = num_val(&year);
2471 if(0<=y && y<=99)
2472 y += 1900;
2474 else y = 1900;
2476 if(arg_no>1) {
2477 hres = to_number(ctx, get_arg(dp, 1), ei, &month);
2478 if(FAILED(hres))
2479 return hres;
2481 else {
2482 V_VT(&month) = VT_R8;
2483 V_R8(&month) = 0;
2486 if(arg_no>2) {
2487 hres = to_number(ctx, get_arg(dp, 2), ei, &vdate);
2488 if(FAILED(hres))
2489 return hres;
2491 else {
2492 V_VT(&vdate) = VT_R8;
2493 V_R8(&vdate) = 1;
2496 if(arg_no>3) {
2497 hres = to_number(ctx, get_arg(dp, 3), ei, &hours);
2498 if(FAILED(hres))
2499 return hres;
2501 else {
2502 V_VT(&hours) = VT_R8;
2503 V_R8(&hours) = 0;
2506 if(arg_no>4) {
2507 hres = to_number(ctx, get_arg(dp, 4), ei, &minutes);
2508 if(FAILED(hres))
2509 return hres;
2511 else {
2512 V_VT(&minutes) = VT_R8;
2513 V_R8(&minutes) = 0;
2516 if(arg_no>5) {
2517 hres = to_number(ctx, get_arg(dp, 5), ei, &seconds);
2518 if(FAILED(hres))
2519 return hres;
2521 else {
2522 V_VT(&seconds) = VT_R8;
2523 V_R8(&seconds) = 0;
2526 if(arg_no>6) {
2527 hres = to_number(ctx, get_arg(dp, 6), ei, &ms);
2528 if(FAILED(hres))
2529 return hres;
2531 else {
2532 V_VT(&ms) = VT_R8;
2533 V_R8(&ms) = 0;
2536 if(retv) {
2537 V_VT(retv) = VT_R8;
2538 V_R8(retv) = time_clip(make_date(
2539 make_day(y, num_val(&month), num_val(&vdate)),
2540 make_time(num_val(&hours), num_val(&minutes),
2541 num_val(&seconds), num_val(&ms))));
2544 return S_OK;
2547 static HRESULT DateConstr_UTC(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
2548 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
2550 TRACE("\n");
2552 return date_utc(ctx, dp, retv, ei);
2555 static HRESULT DateConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
2556 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
2558 jsdisp_t *date;
2559 HRESULT hres;
2561 TRACE("\n");
2563 switch(flags) {
2564 case DISPATCH_CONSTRUCT:
2565 switch(arg_cnt(dp)) {
2566 /* ECMA-262 3rd Edition 15.9.3.3 */
2567 case 0: {
2568 FILETIME time;
2569 LONGLONG lltime;
2571 GetSystemTimeAsFileTime(&time);
2572 lltime = ((LONGLONG)time.dwHighDateTime<<32)
2573 + time.dwLowDateTime;
2575 hres = create_date(ctx, NULL, lltime/10000-TIME_EPOCH, &date);
2576 if(FAILED(hres))
2577 return hres;
2578 break;
2581 /* ECMA-262 3rd Edition 15.9.3.2 */
2582 case 1: {
2583 VARIANT prim, num;
2585 hres = to_primitive(ctx, get_arg(dp,0), ei, &prim, NO_HINT);
2586 if(FAILED(hres))
2587 return hres;
2589 if(V_VT(&prim) == VT_BSTR)
2590 hres = date_parse(V_BSTR(&prim), &num);
2591 else
2592 hres = to_number(ctx, &prim, ei, &num);
2594 VariantClear(&prim);
2595 if(FAILED(hres))
2596 return hres;
2598 hres = create_date(ctx, NULL, time_clip(num_val(&num)), &date);
2599 if(FAILED(hres))
2600 return hres;
2601 break;
2604 /* ECMA-262 3rd Edition 15.9.3.1 */
2605 default: {
2606 VARIANT ret_date;
2607 DateInstance *di;
2609 hres = date_utc(ctx, dp, &ret_date, ei);
2610 if(FAILED(hres))
2611 return hres;
2613 hres = create_date(ctx, NULL, num_val(&ret_date), &date);
2614 if(FAILED(hres))
2615 return hres;
2617 di = (DateInstance*)date;
2618 di->time = utc(di->time, di);
2622 var_set_jsdisp(retv, date);
2623 return S_OK;
2625 case INVOKE_FUNC: {
2626 FILETIME system_time, local_time;
2627 LONGLONG lltime;
2629 GetSystemTimeAsFileTime(&system_time);
2630 FileTimeToLocalFileTime(&system_time, &local_time);
2631 lltime = ((LONGLONG)local_time.dwHighDateTime<<32)
2632 + local_time.dwLowDateTime;
2634 return date_to_string(lltime/10000-TIME_EPOCH, FALSE, 0, retv);
2637 default:
2638 FIXME("unimplemented flags %x\n", flags);
2639 return E_NOTIMPL;
2642 return S_OK;
2645 static const builtin_prop_t DateConstr_props[] = {
2646 {UTCW, DateConstr_UTC, PROPF_METHOD},
2647 {parseW, DateConstr_parse, PROPF_METHOD}
2650 static const builtin_info_t DateConstr_info = {
2651 JSCLASS_FUNCTION,
2652 {NULL, Function_value, 0},
2653 sizeof(DateConstr_props)/sizeof(*DateConstr_props),
2654 DateConstr_props,
2655 NULL,
2656 NULL
2659 HRESULT create_date_constr(script_ctx_t *ctx, jsdisp_t *object_prototype, jsdisp_t **ret)
2661 jsdisp_t *date;
2662 HRESULT hres;
2664 static const WCHAR DateW[] = {'D','a','t','e',0};
2666 hres = create_date(ctx, object_prototype, 0.0, &date);
2667 if(FAILED(hres))
2668 return hres;
2670 hres = create_builtin_function(ctx, DateConstr_value, DateW, &DateConstr_info,
2671 PROPF_CONSTR|7, date, ret);
2673 jsdisp_release(date);
2674 return hres;