dcomp: Add DCompositionCreateDevice() stub.
[wine.git] / dlls / jscript / date.c
blobefb97aa632e277bf54ae954d27824b8f59f458a7
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
21 #include <limits.h>
22 #include <math.h>
23 #include <assert.h>
25 #include "jscript.h"
27 #include "wine/debug.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
31 /* 1601 to 1970 is 369 years plus 89 leap days */
32 #define TIME_EPOCH ((ULONGLONG)(369 * 365 + 89) * 86400 * 1000)
34 typedef struct {
35 jsdisp_t dispex;
37 /* ECMA-262 3rd Edition 15.9.1.1 */
38 DOUBLE time;
40 LONG bias;
41 SYSTEMTIME standardDate;
42 LONG standardBias;
43 SYSTEMTIME daylightDate;
44 LONG daylightBias;
45 } DateInstance;
47 static inline DateInstance *date_from_jsdisp(jsdisp_t *jsdisp)
49 return CONTAINING_RECORD(jsdisp, DateInstance, dispex);
52 static inline DateInstance *date_this(vdisp_t *jsthis)
54 return is_vclass(jsthis, JSCLASS_DATE) ? date_from_jsdisp(jsthis->u.jsdisp) : NULL;
57 /*ECMA-262 3rd Edition 15.9.1.2 */
58 #define MS_PER_DAY 86400000
59 #define MS_PER_HOUR 3600000
60 #define MS_PER_MINUTE 60000
62 /* ECMA-262 3rd Edition 15.9.1.2 */
63 static inline DOUBLE day(DOUBLE time)
65 return floor(time / MS_PER_DAY);
68 /* ECMA-262 3rd Edition 15.9.1.2 */
69 static inline DOUBLE time_within_day(DOUBLE time)
71 DOUBLE ret;
73 ret = fmod(time, MS_PER_DAY);
74 if(ret < 0)
75 ret += MS_PER_DAY;
77 return ret;
80 /* ECMA-262 3rd Edition 15.9.1.3 */
81 static inline DOUBLE days_in_year(DOUBLE year)
83 int y;
85 if(year != (int)year)
86 return NAN;
88 y = year;
89 if(y%4 != 0) return 365;
90 if(y%100 != 0) return 366;
91 if(y%400 != 0) return 365;
92 return 366;
95 /* ECMA-262 3rd Edition 15.9.1.3 */
96 static inline DOUBLE day_from_year(DOUBLE year)
98 if(year != (int)year)
99 return NAN;
101 return floor(365.0*(year-1970) + floor((year-1969)/4)
102 - floor((year-1901)/100) + floor((year-1601)/400));
105 static inline int day_from_month(int month, int in_leap_year)
107 switch(month)
109 case 0:
110 return 0;
111 case 1:
112 return 31;
113 case 2:
114 return 59+in_leap_year;
115 case 3:
116 return 90+in_leap_year;
117 case 4:
118 return 120+in_leap_year;
119 case 5:
120 return 151+in_leap_year;
121 case 6:
122 return 181+in_leap_year;
123 case 7:
124 return 212+in_leap_year;
125 case 8:
126 return 243+in_leap_year;
127 case 9:
128 return 273+in_leap_year;
129 case 10:
130 return 304+in_leap_year;
131 default:
132 return 334+in_leap_year;
136 /* ECMA-262 3rd Edition 15.9.1.3 */
137 static inline DOUBLE time_from_year(DOUBLE year)
139 return MS_PER_DAY*day_from_year(year);
142 /* ECMA-262 3rd Edition 15.9.1.3 */
143 static inline DOUBLE year_from_time(DOUBLE time)
145 int y;
147 if(isnan(time))
148 return NAN;
150 y = 1970 + time/365.25/MS_PER_DAY;
152 if(time_from_year(y) > time)
153 while(time_from_year(y) > time) y--;
154 else
155 while(time_from_year(y+1)<=time) y++;
157 return y;
160 /* ECMA-262 3rd Edition 15.9.1.3 */
161 static inline int in_leap_year(DOUBLE time)
163 if(days_in_year(year_from_time(time))==366)
164 return 1;
165 return 0;
168 /* ECMA-262 3rd Edition 15.9.1.4 */
169 static inline int day_within_year(DOUBLE time)
171 return day(time) - day_from_year(year_from_time(time));
174 /* ECMA-262 3rd Edition 15.9.1.4 */
175 static inline DOUBLE month_from_time(DOUBLE time)
177 int ily = in_leap_year(time);
178 int dwy = day_within_year(time);
180 if(isnan(time))
181 return NAN;
183 if(0<=dwy && dwy<31) return 0;
184 if(dwy < 59+ily) return 1;
185 if(dwy < 90+ily) return 2;
186 if(dwy < 120+ily) return 3;
187 if(dwy < 151+ily) return 4;
188 if(dwy < 181+ily) return 5;
189 if(dwy < 212+ily) return 6;
190 if(dwy < 243+ily) return 7;
191 if(dwy < 273+ily) return 8;
192 if(dwy < 304+ily) return 9;
193 if(dwy < 334+ily) return 10;
194 return 11;
197 /* ECMA-262 3rd Edition 15.9.1.5 */
198 static inline DOUBLE date_from_time(DOUBLE time)
200 int dwy = day_within_year(time);
201 int ily = in_leap_year(time);
202 int mft = month_from_time(time);
204 if(isnan(time))
205 return NAN;
207 if(mft==0) return dwy+1;
208 if(mft==1) return dwy-30;
209 if(mft==2) return dwy-58-ily;
210 if(mft==3) return dwy-89-ily;
211 if(mft==4) return dwy-119-ily;
212 if(mft==5) return dwy-150-ily;
213 if(mft==6) return dwy-180-ily;
214 if(mft==7) return dwy-211-ily;
215 if(mft==8) return dwy-242-ily;
216 if(mft==9) return dwy-272-ily;
217 if(mft==10) return dwy-303-ily;
218 return dwy-333-ily;
221 /* ECMA-262 3rd Edition 15.9.1.6 */
222 static inline DOUBLE week_day(DOUBLE time)
224 DOUBLE ret;
226 if(isnan(time))
227 return NAN;
229 ret = fmod(day(time)+4, 7);
230 if(ret<0) ret += 7;
232 return ret;
235 static inline DOUBLE convert_time(int year, SYSTEMTIME st)
237 DOUBLE time;
238 int set_week_day;
240 if(st.wMonth == 0)
241 return NAN;
243 if(st.wYear != 0)
244 year = st.wYear;
246 time = time_from_year(year);
247 time += (DOUBLE)day_from_month(st.wMonth-1, in_leap_year(time)) * MS_PER_DAY;
249 if(st.wYear == 0) {
250 set_week_day = st.wDayOfWeek-week_day(time);
251 if(set_week_day < 0)
252 set_week_day += 7;
253 time += set_week_day * MS_PER_DAY;
255 time += (DOUBLE)(st.wDay-1) * 7 * MS_PER_DAY;
256 if(month_from_time(time) != st.wMonth-1)
257 time -= 7 * MS_PER_DAY;
259 else
260 time += st.wDay * MS_PER_DAY;
262 time += st.wHour * MS_PER_HOUR;
263 time += st.wMinute * MS_PER_MINUTE;
265 return time;
268 /* ECMA-262 3rd Edition 15.9.1.9 */
269 static inline DOUBLE daylight_saving_ta(DOUBLE time, DateInstance *date)
271 int year = year_from_time(time);
272 DOUBLE standardTime, daylightTime;
274 if(isnan(time))
275 return 0;
277 standardTime = convert_time(year, date->standardDate);
278 daylightTime = convert_time(year, date->daylightDate);
280 if(isnan(standardTime) || isnan(daylightTime))
281 return 0;
282 else if(standardTime > daylightTime) {
283 if(daylightTime <= time && time < standardTime)
284 return date->daylightBias;
286 return date->standardBias;
288 else {
289 if(standardTime <= time && time < daylightTime)
290 return date->standardBias;
292 return date->daylightBias;
296 /* ECMA-262 3rd Edition 15.9.1.9 */
297 static inline DOUBLE local_time(DOUBLE time, DateInstance *date)
299 return time - (daylight_saving_ta(time, date)+date->bias)*MS_PER_MINUTE;
302 /* ECMA-262 3rd Edition 15.9.1.9 */
303 static inline DOUBLE utc(DOUBLE time, DateInstance *date)
305 time += date->bias * MS_PER_MINUTE;
306 return time + daylight_saving_ta(time, date)*MS_PER_MINUTE;
309 /* ECMA-262 3rd Edition 15.9.1.10 */
310 static inline DOUBLE hour_from_time(DOUBLE time)
312 DOUBLE ret;
314 if(isnan(time))
315 return NAN;
317 ret = fmod(floor(time/MS_PER_HOUR), 24);
318 if(ret<0) ret += 24;
320 return ret;
323 /* ECMA-262 3rd Edition 15.9.1.10 */
324 static inline DOUBLE min_from_time(DOUBLE time)
326 DOUBLE ret;
328 if(isnan(time))
329 return NAN;
331 ret = fmod(floor(time/MS_PER_MINUTE), 60);
332 if(ret<0) ret += 60;
334 return ret;
337 /* ECMA-262 3rd Edition 15.9.1.10 */
338 static inline DOUBLE sec_from_time(DOUBLE time)
340 DOUBLE ret;
342 if(isnan(time))
343 return NAN;
345 ret = fmod(floor(time/1000), 60);
346 if(ret<0) ret += 60;
348 return ret;
351 /* ECMA-262 3rd Edition 15.9.1.10 */
352 static inline DOUBLE ms_from_time(DOUBLE time)
354 DOUBLE ret;
356 if(isnan(time))
357 return NAN;
359 ret = fmod(time, 1000);
360 if(ret<0) ret += 1000;
362 return ret;
365 /* ECMA-262 3rd Edition 15.9.1.11 */
366 static inline DOUBLE make_time(DOUBLE hour, DOUBLE min, DOUBLE sec, DOUBLE ms)
368 return hour*MS_PER_HOUR + min*MS_PER_MINUTE + sec*1000 + ms;
371 /* ECMA-262 3rd Edition 15.9.1.12 */
372 static inline DOUBLE make_day(DOUBLE year, DOUBLE month, DOUBLE day)
374 DOUBLE time;
376 year += floor(month/12);
378 month = fmod(month, 12);
379 if(month<0) month += 12;
381 time = time_from_year(year);
383 day += floor(time / MS_PER_DAY);
384 day += day_from_month(month, in_leap_year(time));
386 return day-1;
389 /* ECMA-262 3rd Edition 15.9.1.13 */
390 static inline DOUBLE make_date(DOUBLE day, DOUBLE time)
392 return day*MS_PER_DAY + time;
395 /* ECMA-262 3rd Edition 15.9.1.14 */
396 static inline DOUBLE time_clip(DOUBLE time)
398 if(8.64e15 < time || time < -8.64e15) {
399 return NAN;
402 return floor(time);
405 static double date_now(void)
407 FILETIME ftime;
408 LONGLONG time;
410 GetSystemTimeAsFileTime(&ftime);
411 time = ((LONGLONG)ftime.dwHighDateTime << 32) + ftime.dwLowDateTime;
413 return time/10000 - TIME_EPOCH;
416 static SYSTEMTIME create_systemtime(DOUBLE time)
418 SYSTEMTIME st;
420 st.wYear = year_from_time(time);
421 st.wMonth = month_from_time(time) + 1;
422 st.wDayOfWeek = week_day(time);
423 st.wDay = date_from_time(time);
424 st.wHour = hour_from_time(time);
425 st.wMinute = min_from_time(time);
426 st.wSecond = sec_from_time(time);
427 st.wMilliseconds = ms_from_time(time);
429 return st;
432 static inline HRESULT date_to_string(DOUBLE time, BOOL show_offset, int offset, jsval_t *r)
434 static const DWORD week_ids[] = { LOCALE_SABBREVDAYNAME7, LOCALE_SABBREVDAYNAME1,
435 LOCALE_SABBREVDAYNAME2, LOCALE_SABBREVDAYNAME3, LOCALE_SABBREVDAYNAME4,
436 LOCALE_SABBREVDAYNAME5, LOCALE_SABBREVDAYNAME6 };
437 static const DWORD month_ids[] = { LOCALE_SABBREVMONTHNAME1, LOCALE_SABBREVMONTHNAME2,
438 LOCALE_SABBREVMONTHNAME3, LOCALE_SABBREVMONTHNAME4,
439 LOCALE_SABBREVMONTHNAME5, LOCALE_SABBREVMONTHNAME6,
440 LOCALE_SABBREVMONTHNAME7, LOCALE_SABBREVMONTHNAME8,
441 LOCALE_SABBREVMONTHNAME9, LOCALE_SABBREVMONTHNAME10,
442 LOCALE_SABBREVMONTHNAME11, LOCALE_SABBREVMONTHNAME12 };
444 const WCHAR *formatEra = L"";
445 WCHAR week[64], month[64];
446 WCHAR buf[192];
447 jsstr_t *date_jsstr;
448 int year, day;
449 DWORD lcid_en;
450 WCHAR sign = '-';
452 if(isnan(time)) {
453 if(r)
454 *r = jsval_string(jsstr_nan());
455 return S_OK;
458 if(r) {
459 lcid_en = MAKELCID(MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),SORT_DEFAULT);
461 week[0] = 0;
462 GetLocaleInfoW(lcid_en, week_ids[(int)week_day(time)], week, ARRAY_SIZE(week));
464 month[0] = 0;
465 GetLocaleInfoW(lcid_en, month_ids[(int)month_from_time(time)], month, ARRAY_SIZE(month));
467 year = year_from_time(time);
468 if(year<0) {
469 formatEra = L" B.C.";
470 year = -year+1;
473 day = date_from_time(time);
475 if(offset < 0) {
476 sign = '+';
477 offset = -offset;
480 if(!show_offset)
481 swprintf(buf, ARRAY_SIZE(buf), L"%s %s %d %02d:%02d:%02d %d%s", week, month, day,
482 (int)hour_from_time(time), (int)min_from_time(time),
483 (int)sec_from_time(time), year, formatEra);
484 else if(offset)
485 swprintf(buf, ARRAY_SIZE(buf), L"%s %s %d %02d:%02d:%02d UTC%c%02d%02d %d%s", week, month, day,
486 (int)hour_from_time(time), (int)min_from_time(time),
487 (int)sec_from_time(time), sign, offset/60, offset%60,
488 year, formatEra);
489 else
490 swprintf(buf, ARRAY_SIZE(buf), L"%s %s %d %02d:%02d:%02d UTC %d%s", week, month, day,
491 (int)hour_from_time(time), (int)min_from_time(time),
492 (int)sec_from_time(time), year, formatEra);
494 date_jsstr = jsstr_alloc(buf);
495 if(!date_jsstr)
496 return E_OUTOFMEMORY;
498 *r = jsval_string(date_jsstr);
500 return S_OK;
503 /* ECMA-262 3rd Edition 15.9.1.2 */
504 static HRESULT dateobj_to_string(DateInstance *date, jsval_t *r)
506 DOUBLE time;
507 int offset;
509 time = local_time(date->time, date);
510 offset = date->bias +
511 daylight_saving_ta(time, date);
513 return date_to_string(time, TRUE, offset, r);
516 static HRESULT Date_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
518 DateInstance *date;
520 TRACE("\n");
522 if(!(date = date_this(jsthis)))
523 return JS_E_DATE_EXPECTED;
525 return dateobj_to_string(date, r);
528 /* ECMA-262 3rd Edition 15.9.1.5 */
529 static HRESULT Date_toLocaleString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
530 jsval_t *r)
532 SYSTEMTIME st;
533 DateInstance *date;
534 jsstr_t *date_str;
535 int date_len, time_len;
537 TRACE("\n");
539 if(!(date = date_this(jsthis)))
540 return JS_E_DATE_EXPECTED;
542 if(isnan(date->time)) {
543 if(r)
544 *r = jsval_string(jsstr_nan());
545 return S_OK;
548 st = create_systemtime(local_time(date->time, date));
550 if(st.wYear<1601 || st.wYear>9999)
551 return dateobj_to_string(date, r);
553 if(r) {
554 WCHAR *ptr;
556 date_len = GetDateFormatW(ctx->lcid, DATE_LONGDATE, &st, NULL, NULL, 0);
557 time_len = GetTimeFormatW(ctx->lcid, 0, &st, NULL, NULL, 0);
559 date_str = jsstr_alloc_buf(date_len+time_len-1, &ptr);
560 if(!date_str)
561 return E_OUTOFMEMORY;
563 GetDateFormatW(ctx->lcid, DATE_LONGDATE, &st, NULL, ptr, date_len);
564 GetTimeFormatW(ctx->lcid, 0, &st, NULL, ptr+date_len, time_len);
565 ptr[date_len-1] = ' ';
567 *r = jsval_string(date_str);
569 return S_OK;
572 static HRESULT Date_toISOString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
573 jsval_t *r)
575 DateInstance *date;
576 WCHAR buf[64], *p = buf;
577 double year;
579 TRACE("\n");
581 if(!(date = date_this(jsthis)))
582 return JS_E_DATE_EXPECTED;
584 year = year_from_time(date->time);
585 if(isnan(year) || year > 999999 || year < -999999) {
586 FIXME("year %lf should throw an exception\n", year);
587 return E_FAIL;
590 if(year < 0) {
591 *p++ = '-';
592 p += swprintf(p, ARRAY_SIZE(buf) - 1, L"%06d", -(int)year);
593 }else if(year > 9999) {
594 *p++ = '+';
595 p += swprintf(p, ARRAY_SIZE(buf) - 1, L"%06d", (int)year);
596 }else {
597 p += swprintf(p, ARRAY_SIZE(buf), L"%04d", (int)year);
600 swprintf(p, ARRAY_SIZE(buf) - (p - buf), L"-%02d-%02dT%02d:%02d:%02d.%03dZ",
601 (int)month_from_time(date->time) + 1, (int)date_from_time(date->time),
602 (int)hour_from_time(date->time), (int)min_from_time(date->time),
603 (int)sec_from_time(date->time), (int)ms_from_time(date->time));
605 if(r) {
606 jsstr_t *ret;
607 if(!(ret = jsstr_alloc(buf)))
608 return E_OUTOFMEMORY;
609 *r = jsval_string(ret);
611 return S_OK;
614 static HRESULT Date_valueOf(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
615 jsval_t *r)
617 DateInstance *date;
619 TRACE("\n");
621 if(!(date = date_this(jsthis)))
622 return JS_E_DATE_EXPECTED;
624 if(r)
625 *r = jsval_number(date->time);
626 return S_OK;
629 static inline HRESULT create_utc_string(script_ctx_t *ctx, vdisp_t *jsthis, jsval_t *r)
631 static const DWORD week_ids[] = { LOCALE_SABBREVDAYNAME7, LOCALE_SABBREVDAYNAME1,
632 LOCALE_SABBREVDAYNAME2, LOCALE_SABBREVDAYNAME3, LOCALE_SABBREVDAYNAME4,
633 LOCALE_SABBREVDAYNAME5, LOCALE_SABBREVDAYNAME6 };
634 static const DWORD month_ids[] = { LOCALE_SABBREVMONTHNAME1, LOCALE_SABBREVMONTHNAME2,
635 LOCALE_SABBREVMONTHNAME3, LOCALE_SABBREVMONTHNAME4,
636 LOCALE_SABBREVMONTHNAME5, LOCALE_SABBREVMONTHNAME6,
637 LOCALE_SABBREVMONTHNAME7, LOCALE_SABBREVMONTHNAME8,
638 LOCALE_SABBREVMONTHNAME9, LOCALE_SABBREVMONTHNAME10,
639 LOCALE_SABBREVMONTHNAME11, LOCALE_SABBREVMONTHNAME12 };
641 const WCHAR *formatEra = L"";
642 WCHAR week[64], month[64];
643 WCHAR buf[192];
644 DateInstance *date;
645 jsstr_t *date_str;
646 int year, day;
647 DWORD lcid_en;
649 if(!(date = date_this(jsthis)))
650 return JS_E_DATE_EXPECTED;
652 if(isnan(date->time)) {
653 if(r)
654 *r = jsval_string(jsstr_nan());
655 return S_OK;
658 if(r) {
659 lcid_en = MAKELCID(MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),SORT_DEFAULT);
661 week[0] = 0;
662 GetLocaleInfoW(lcid_en, week_ids[(int)week_day(date->time)], week, ARRAY_SIZE(week));
664 month[0] = 0;
665 GetLocaleInfoW(lcid_en, month_ids[(int)month_from_time(date->time)], month, ARRAY_SIZE(month));
667 year = year_from_time(date->time);
668 if(year<0) {
669 formatEra = L" B.C.";
670 year = -year+1;
673 day = date_from_time(date->time);
675 swprintf(buf, ARRAY_SIZE(buf),
676 L"%s, %d %s %d%s %02d:%02d:%02d UTC", week, day, month, year, formatEra,
677 (int)hour_from_time(date->time), (int)min_from_time(date->time),
678 (int)sec_from_time(date->time));
680 date_str = jsstr_alloc(buf);
681 if(!date_str)
682 return E_OUTOFMEMORY;
684 *r = jsval_string(date_str);
686 return S_OK;
689 /* ECMA-262 3rd Edition 15.9.5.42 */
690 static HRESULT Date_toUTCString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
691 jsval_t *r)
693 TRACE("\n");
694 return create_utc_string(ctx, jsthis, r);
697 static HRESULT Date_toGMTString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
698 jsval_t *r)
700 TRACE("\n");
701 return create_utc_string(ctx, jsthis, r);
704 /* ECMA-262 3rd Edition 15.9.5.3 */
705 static HRESULT dateobj_to_date_string(DateInstance *date, jsval_t *r)
707 static const DWORD week_ids[] = { LOCALE_SABBREVDAYNAME7, LOCALE_SABBREVDAYNAME1,
708 LOCALE_SABBREVDAYNAME2, LOCALE_SABBREVDAYNAME3, LOCALE_SABBREVDAYNAME4,
709 LOCALE_SABBREVDAYNAME5, LOCALE_SABBREVDAYNAME6 };
710 static const DWORD month_ids[] = { LOCALE_SABBREVMONTHNAME1, LOCALE_SABBREVMONTHNAME2,
711 LOCALE_SABBREVMONTHNAME3, LOCALE_SABBREVMONTHNAME4,
712 LOCALE_SABBREVMONTHNAME5, LOCALE_SABBREVMONTHNAME6,
713 LOCALE_SABBREVMONTHNAME7, LOCALE_SABBREVMONTHNAME8,
714 LOCALE_SABBREVMONTHNAME9, LOCALE_SABBREVMONTHNAME10,
715 LOCALE_SABBREVMONTHNAME11, LOCALE_SABBREVMONTHNAME12 };
717 const WCHAR *formatEra = L"";
718 WCHAR week[64], month[64];
719 WCHAR buf[192];
720 jsstr_t *date_str;
721 DOUBLE time;
722 int year, day;
723 DWORD lcid_en;
725 if(isnan(date->time)) {
726 if(r)
727 *r = jsval_string(jsstr_nan());
728 return S_OK;
731 time = local_time(date->time, date);
733 if(r) {
734 lcid_en = MAKELCID(MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),SORT_DEFAULT);
736 week[0] = 0;
737 GetLocaleInfoW(lcid_en, week_ids[(int)week_day(time)], week, ARRAY_SIZE(week));
739 month[0] = 0;
740 GetLocaleInfoW(lcid_en, month_ids[(int)month_from_time(time)], month, ARRAY_SIZE(month));
742 year = year_from_time(time);
743 if(year<0) {
744 formatEra = L" B.C.";
745 year = -year+1;
748 day = date_from_time(time);
750 swprintf(buf, ARRAY_SIZE(buf), L"%s %s %d %d%s", week, month, day, year, formatEra);
752 date_str = jsstr_alloc(buf);
753 if(!date_str)
754 return E_OUTOFMEMORY;
756 *r = jsval_string(date_str);
758 return S_OK;
761 static HRESULT Date_toDateString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
762 jsval_t *r)
764 DateInstance *date;
766 if(!(date = date_this(jsthis)))
767 return JS_E_DATE_EXPECTED;
769 return dateobj_to_date_string(date, r);
772 /* ECMA-262 3rd Edition 15.9.5.4 */
773 static HRESULT Date_toTimeString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
774 jsval_t *r)
776 DateInstance *date;
777 jsstr_t *date_str;
778 WCHAR buf[32];
779 DOUBLE time;
780 WCHAR sign;
781 int offset;
783 TRACE("\n");
785 if(!(date = date_this(jsthis)))
786 return JS_E_DATE_EXPECTED;
788 if(isnan(date->time)) {
789 if(r)
790 *r = jsval_string(jsstr_nan());
791 return S_OK;
794 time = local_time(date->time, date);
796 if(r) {
797 offset = date->bias +
798 daylight_saving_ta(time, date);
800 if(offset < 0) {
801 sign = '+';
802 offset = -offset;
804 else sign = '-';
806 if(offset)
807 swprintf(buf, ARRAY_SIZE(buf), L"%02d:%02d:%02d UTC%c%02d%02d", (int)hour_from_time(time),
808 (int)min_from_time(time), (int)sec_from_time(time),
809 sign, offset/60, offset%60);
810 else
811 swprintf(buf, ARRAY_SIZE(buf), L"%02d:%02d:%02d UTC", (int)hour_from_time(time),
812 (int)min_from_time(time), (int)sec_from_time(time));
814 date_str = jsstr_alloc(buf);
815 if(!date_str)
816 return E_OUTOFMEMORY;
818 *r = jsval_string(date_str);
820 return S_OK;
823 /* ECMA-262 3rd Edition 15.9.5.6 */
824 static HRESULT Date_toLocaleDateString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
825 jsval_t *r)
827 SYSTEMTIME st;
828 DateInstance *date;
829 jsstr_t *date_str;
830 int len;
832 TRACE("\n");
834 if(!(date = date_this(jsthis)))
835 return JS_E_DATE_EXPECTED;
837 if(isnan(date->time)) {
838 if(r)
839 *r = jsval_string(jsstr_nan());
840 return S_OK;
843 st = create_systemtime(local_time(date->time, date));
845 if(st.wYear<1601 || st.wYear>9999)
846 return dateobj_to_date_string(date, r);
848 if(r) {
849 WCHAR *ptr;
851 len = GetDateFormatW(ctx->lcid, DATE_LONGDATE, &st, NULL, NULL, 0);
852 date_str = jsstr_alloc_buf(len-1, &ptr);
853 if(!date_str)
854 return E_OUTOFMEMORY;
855 GetDateFormatW(ctx->lcid, DATE_LONGDATE, &st, NULL, ptr, len);
857 *r = jsval_string(date_str);
859 return S_OK;
862 /* ECMA-262 3rd Edition 15.9.5.7 */
863 static HRESULT Date_toLocaleTimeString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
864 jsval_t *r)
866 SYSTEMTIME st;
867 DateInstance *date;
868 jsstr_t *date_str;
869 int len;
871 TRACE("\n");
873 if(!(date = date_this(jsthis)))
874 return JS_E_DATE_EXPECTED;
876 if(isnan(date->time)) {
877 if(r)
878 *r = jsval_string(jsstr_nan());
879 return S_OK;
882 st = create_systemtime(local_time(date->time, date));
884 if(st.wYear<1601 || st.wYear>9999)
885 return Date_toTimeString(ctx, jsthis, flags, argc, argv, r);
887 if(r) {
888 WCHAR *ptr;
890 len = GetTimeFormatW(ctx->lcid, 0, &st, NULL, NULL, 0);
891 date_str = jsstr_alloc_buf(len-1, &ptr);
892 if(!date_str)
893 return E_OUTOFMEMORY;
894 GetTimeFormatW(ctx->lcid, 0, &st, NULL, ptr, len);
896 *r = jsval_string(date_str);
898 return S_OK;
901 /* ECMA-262 3rd Edition 15.9.5.9 */
902 static HRESULT Date_getTime(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
903 jsval_t *r)
905 DateInstance *date;
907 TRACE("\n");
909 if(!(date = date_this(jsthis)))
910 return JS_E_DATE_EXPECTED;
912 if(r)
913 *r = jsval_number(date->time);
914 return S_OK;
917 /* ECMA-262 3rd Edition 15.9.5.10 */
918 static HRESULT Date_getFullYear(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
919 jsval_t *r)
921 DateInstance *date;
923 TRACE("\n");
925 if(!(date = date_this(jsthis)))
926 return JS_E_DATE_EXPECTED;
928 if(r) {
929 DOUBLE time = local_time(date->time, date);
931 *r = jsval_number(year_from_time(time));
933 return S_OK;
936 /* ECMA-262 3rd Edition 15.9.5.11 */
937 static HRESULT Date_getUTCFullYear(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
938 jsval_t *r)
940 DateInstance *date;
942 TRACE("\n");
944 if(!(date = date_this(jsthis)))
945 return JS_E_DATE_EXPECTED;
947 if(r)
948 *r = jsval_number(year_from_time(date->time));
949 return S_OK;
952 /* ECMA-262 3rd Edition 15.9.5.12 */
953 static HRESULT Date_getMonth(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
955 DateInstance *date;
957 TRACE("\n");
959 if(!(date = date_this(jsthis)))
960 return JS_E_DATE_EXPECTED;
962 if(r)
963 *r = jsval_number(month_from_time(local_time(date->time, date)));
964 return S_OK;
967 /* ECMA-262 3rd Edition 15.9.5.13 */
968 static HRESULT Date_getUTCMonth(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
969 jsval_t *r)
971 DateInstance *date;
973 TRACE("\n");
975 if(!(date = date_this(jsthis)))
976 return JS_E_DATE_EXPECTED;
978 if(r)
979 *r = jsval_number(month_from_time(date->time));
980 return S_OK;
983 /* ECMA-262 3rd Edition 15.9.5.14 */
984 static HRESULT Date_getDate(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
986 DateInstance *date;
988 TRACE("\n");
990 if(!(date = date_this(jsthis)))
991 return JS_E_DATE_EXPECTED;
993 if(r)
994 *r = jsval_number(date_from_time(local_time(date->time, date)));
995 return S_OK;
998 /* ECMA-262 3rd Edition 15.9.5.15 */
999 static HRESULT Date_getUTCDate(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1000 jsval_t *r)
1002 DateInstance *date;
1004 TRACE("\n");
1006 if(!(date = date_this(jsthis)))
1007 return JS_E_DATE_EXPECTED;
1009 if(r)
1010 *r = jsval_number(date_from_time(date->time));
1011 return S_OK;
1014 /* ECMA-262 3rd Edition 15.9.5.16 */
1015 static HRESULT Date_getDay(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1016 jsval_t *r)
1018 DateInstance *date;
1020 TRACE("\n");
1022 if(!(date = date_this(jsthis)))
1023 return JS_E_DATE_EXPECTED;
1025 if(r)
1026 *r = jsval_number(week_day(local_time(date->time, date)));
1027 return S_OK;
1030 /* ECMA-262 3rd Edition 15.9.5.17 */
1031 static HRESULT Date_getUTCDay(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1032 jsval_t *r)
1034 DateInstance *date;
1036 TRACE("\n");
1038 if(!(date = date_this(jsthis)))
1039 return JS_E_DATE_EXPECTED;
1041 if(r)
1042 *r = jsval_number(week_day(date->time));
1043 return S_OK;
1046 /* ECMA-262 3rd Edition 15.9.5.18 */
1047 static HRESULT Date_getHours(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1048 jsval_t *r)
1050 DateInstance *date;
1052 TRACE("\n");
1054 if(!(date = date_this(jsthis)))
1055 return JS_E_DATE_EXPECTED;
1057 if(r)
1058 *r = jsval_number(hour_from_time(local_time(date->time, date)));
1059 return S_OK;
1062 /* ECMA-262 3rd Edition 15.9.5.19 */
1063 static HRESULT Date_getUTCHours(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1064 jsval_t *r)
1066 DateInstance *date;
1068 TRACE("\n");
1070 if(!(date = date_this(jsthis)))
1071 return JS_E_DATE_EXPECTED;
1073 if(r)
1074 *r = jsval_number(hour_from_time(date->time));
1075 return S_OK;
1078 /* ECMA-262 3rd Edition 15.9.5.20 */
1079 static HRESULT Date_getMinutes(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1080 jsval_t *r)
1082 DateInstance *date;
1084 TRACE("\n");
1086 if(!(date = date_this(jsthis)))
1087 return JS_E_DATE_EXPECTED;
1089 if(r)
1090 *r = jsval_number(min_from_time(local_time(date->time, date)));
1091 return S_OK;
1094 /* ECMA-262 3rd Edition 15.9.5.21 */
1095 static HRESULT Date_getUTCMinutes(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1096 jsval_t *r)
1098 DateInstance *date;
1100 TRACE("\n");
1102 if(!(date = date_this(jsthis)))
1103 return JS_E_DATE_EXPECTED;
1105 if(r)
1106 *r = jsval_number(min_from_time(date->time));
1107 return S_OK;
1110 /* ECMA-262 3rd Edition 15.9.5.22 */
1111 static HRESULT Date_getSeconds(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
1113 DateInstance *date;
1115 TRACE("\n");
1117 if(!(date = date_this(jsthis)))
1118 return JS_E_DATE_EXPECTED;
1120 if(r)
1121 *r = jsval_number(sec_from_time(local_time(date->time, date)));
1122 return S_OK;
1125 /* ECMA-262 3rd Edition 15.9.5.23 */
1126 static HRESULT Date_getUTCSeconds(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1127 jsval_t *r)
1129 DateInstance *date;
1131 TRACE("\n");
1133 if(!(date = date_this(jsthis)))
1134 return JS_E_DATE_EXPECTED;
1136 if(r)
1137 *r = jsval_number(sec_from_time(date->time));
1138 return S_OK;
1141 /* ECMA-262 3rd Edition 15.9.5.24 */
1142 static HRESULT Date_getMilliseconds(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1143 jsval_t *r)
1145 DateInstance *date;
1147 TRACE("\n");
1149 if(!(date = date_this(jsthis)))
1150 return JS_E_DATE_EXPECTED;
1152 if(r)
1153 *r = jsval_number(ms_from_time(local_time(date->time, date)));
1154 return S_OK;
1157 /* ECMA-262 3rd Edition 15.9.5.25 */
1158 static HRESULT Date_getUTCMilliseconds(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1159 jsval_t *r)
1161 DateInstance *date;
1163 TRACE("\n");
1165 if(!(date = date_this(jsthis)))
1166 return JS_E_DATE_EXPECTED;
1168 if(r)
1169 *r = jsval_number(ms_from_time(date->time));
1170 return S_OK;
1173 /* ECMA-262 3rd Edition 15.9.5.26 */
1174 static HRESULT Date_getTimezoneOffset(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1175 jsval_t *r)
1177 DateInstance *date;
1179 TRACE("\n");
1181 if(!(date = date_this(jsthis)))
1182 return JS_E_DATE_EXPECTED;
1184 if(r)
1185 *r = jsval_number(floor((date->time-local_time(date->time, date))/MS_PER_MINUTE));
1186 return S_OK;
1189 /* ECMA-262 3rd Edition 15.9.5.27 */
1190 static HRESULT Date_setTime(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1191 jsval_t *r)
1193 double n;
1194 HRESULT hres;
1195 DateInstance *date;
1197 TRACE("\n");
1199 if(!(date = date_this(jsthis)))
1200 return JS_E_DATE_EXPECTED;
1202 if(!argc)
1203 return JS_E_MISSING_ARG;
1205 hres = to_number(ctx, argv[0], &n);
1206 if(FAILED(hres))
1207 return hres;
1209 date->time = time_clip(n);
1211 if(r)
1212 *r = jsval_number(date->time);
1213 return S_OK;
1216 /* ECMA-262 3rd Edition 15.9.5.28 */
1217 static HRESULT Date_setMilliseconds(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1218 jsval_t *r)
1220 DateInstance *date;
1221 double n, t;
1222 HRESULT hres;
1224 TRACE("\n");
1226 if(!(date = date_this(jsthis)))
1227 return JS_E_DATE_EXPECTED;
1229 if(!argc)
1230 return JS_E_MISSING_ARG;
1232 hres = to_number(ctx, argv[0], &n);
1233 if(FAILED(hres))
1234 return hres;
1236 t = local_time(date->time, date);
1237 t = make_date(day(t), make_time(hour_from_time(t), min_from_time(t),
1238 sec_from_time(t), n));
1239 date->time = time_clip(utc(t, date));
1241 if(r)
1242 *r = jsval_number(date->time);
1243 return S_OK;
1246 /* ECMA-262 3rd Edition 15.9.5.29 */
1247 static HRESULT Date_setUTCMilliseconds(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1248 jsval_t *r)
1250 DateInstance *date;
1251 double n, t;
1252 HRESULT hres;
1254 TRACE("\n");
1256 if(!(date = date_this(jsthis)))
1257 return JS_E_DATE_EXPECTED;
1259 if(!argc)
1260 return JS_E_MISSING_ARG;
1262 hres = to_number(ctx, argv[0], &n);
1263 if(FAILED(hres))
1264 return hres;
1266 t = date->time;
1267 t = make_date(day(t), make_time(hour_from_time(t), min_from_time(t),
1268 sec_from_time(t), n));
1269 date->time = time_clip(t);
1271 if(r)
1272 *r = jsval_number(date->time);
1273 return S_OK;
1276 /* ECMA-262 3rd Edition 15.9.5.30 */
1277 static HRESULT Date_setSeconds(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1278 jsval_t *r)
1280 DateInstance *date;
1281 double t, sec, ms;
1282 HRESULT hres;
1284 TRACE("\n");
1286 if(!(date = date_this(jsthis)))
1287 return JS_E_DATE_EXPECTED;
1289 if(!argc)
1290 return JS_E_MISSING_ARG;
1292 t = local_time(date->time, date);
1294 hres = to_number(ctx, argv[0], &sec);
1295 if(FAILED(hres))
1296 return hres;
1298 if(argc > 1) {
1299 hres = to_number(ctx, argv[1], &ms);
1300 if(FAILED(hres))
1301 return hres;
1302 }else {
1303 ms = ms_from_time(t);
1306 t = make_date(day(t), make_time(hour_from_time(t),
1307 min_from_time(t), sec, ms));
1308 date->time = time_clip(utc(t, date));
1310 if(r)
1311 *r = jsval_number(date->time);
1312 return S_OK;
1315 /* ECMA-262 3rd Edition 15.9.5.31 */
1316 static HRESULT Date_setUTCSeconds(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1317 jsval_t *r)
1319 DateInstance *date;
1320 double t, sec, ms;
1321 HRESULT hres;
1323 TRACE("\n");
1325 if(!(date = date_this(jsthis)))
1326 return JS_E_DATE_EXPECTED;
1328 if(!argc)
1329 return JS_E_MISSING_ARG;
1331 t = date->time;
1333 hres = to_number(ctx, argv[0], &sec);
1334 if(FAILED(hres))
1335 return hres;
1337 if(argc > 1) {
1338 hres = to_number(ctx, argv[1], &ms);
1339 if(FAILED(hres))
1340 return hres;
1341 }else {
1342 ms = ms_from_time(t);
1345 t = make_date(day(t), make_time(hour_from_time(t),
1346 min_from_time(t), sec, ms));
1347 date->time = time_clip(t);
1349 if(r)
1350 *r = jsval_number(date->time);
1351 return S_OK;
1354 /* ECMA-262 3rd Edition 15.9.5.33 */
1355 static HRESULT Date_setMinutes(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1356 jsval_t *r)
1358 DateInstance *date;
1359 double t, min, sec, ms;
1360 HRESULT hres;
1362 TRACE("\n");
1364 if(!(date = date_this(jsthis)))
1365 return JS_E_DATE_EXPECTED;
1367 if(!argc)
1368 return JS_E_MISSING_ARG;
1370 t = local_time(date->time, date);
1372 hres = to_number(ctx, argv[0], &min);
1373 if(FAILED(hres))
1374 return hres;
1376 if(argc > 1) {
1377 hres = to_number(ctx, argv[1], &sec);
1378 if(FAILED(hres))
1379 return hres;
1380 }else {
1381 sec = sec_from_time(t);
1384 if(argc > 2) {
1385 hres = to_number(ctx, argv[2], &ms);
1386 if(FAILED(hres))
1387 return hres;
1388 }else {
1389 ms = ms_from_time(t);
1392 t = make_date(day(t), make_time(hour_from_time(t),
1393 min, sec, ms));
1394 date->time = time_clip(utc(t, date));
1396 if(r)
1397 *r = jsval_number(date->time);
1398 return S_OK;
1401 /* ECMA-262 3rd Edition 15.9.5.34 */
1402 static HRESULT Date_setUTCMinutes(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1403 jsval_t *r)
1405 DateInstance *date;
1406 double t, min, sec, ms;
1407 HRESULT hres;
1409 TRACE("\n");
1411 if(!(date = date_this(jsthis)))
1412 return JS_E_DATE_EXPECTED;
1414 if(!argc)
1415 return JS_E_MISSING_ARG;
1417 t = date->time;
1419 hres = to_number(ctx, argv[0], &min);
1420 if(FAILED(hres))
1421 return hres;
1423 if(argc > 1) {
1424 hres = to_number(ctx, argv[1], &sec);
1425 if(FAILED(hres))
1426 return hres;
1427 }else {
1428 sec = sec_from_time(t);
1431 if(argc > 2) {
1432 hres = to_number(ctx, argv[2], &ms);
1433 if(FAILED(hres))
1434 return hres;
1435 }else {
1436 ms = ms_from_time(t);
1439 t = make_date(day(t), make_time(hour_from_time(t),
1440 min, sec, ms));
1441 date->time = time_clip(t);
1443 if(r)
1444 *r = jsval_number(date->time);
1445 return S_OK;
1448 /* ECMA-262 3rd Edition 15.9.5.35 */
1449 static HRESULT Date_setHours(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1450 jsval_t *r)
1452 DateInstance *date;
1453 double t, hour, min, sec, ms;
1454 HRESULT hres;
1456 TRACE("\n");
1458 if(!(date = date_this(jsthis)))
1459 return JS_E_DATE_EXPECTED;
1461 if(!argc)
1462 return JS_E_MISSING_ARG;
1464 t = local_time(date->time, date);
1466 hres = to_number(ctx, argv[0], &hour);
1467 if(FAILED(hres))
1468 return hres;
1470 if(argc > 1) {
1471 hres = to_number(ctx, argv[1], &min);
1472 if(FAILED(hres))
1473 return hres;
1474 }else {
1475 min = min_from_time(t);
1478 if(argc > 2) {
1479 hres = to_number(ctx, argv[2], &sec);
1480 if(FAILED(hres))
1481 return hres;
1482 }else {
1483 sec = sec_from_time(t);
1486 if(argc > 3) {
1487 hres = to_number(ctx, argv[3], &ms);
1488 if(FAILED(hres))
1489 return hres;
1490 }else {
1491 ms = ms_from_time(t);
1494 t = make_date(day(t), make_time(hour, min, sec, ms));
1495 date->time = time_clip(utc(t, date));
1497 if(r)
1498 *r = jsval_number(date->time);
1499 return S_OK;
1502 /* ECMA-262 3rd Edition 15.9.5.36 */
1503 static HRESULT Date_setUTCHours(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1504 jsval_t *r)
1506 DateInstance *date;
1507 double t, hour, min, sec, ms;
1508 HRESULT hres;
1510 TRACE("\n");
1512 if(!(date = date_this(jsthis)))
1513 return JS_E_DATE_EXPECTED;
1515 if(!argc)
1516 return JS_E_MISSING_ARG;
1518 t = date->time;
1520 hres = to_number(ctx, argv[0], &hour);
1521 if(FAILED(hres))
1522 return hres;
1524 if(argc > 1) {
1525 hres = to_number(ctx, argv[1], &min);
1526 if(FAILED(hres))
1527 return hres;
1528 }else {
1529 min = min_from_time(t);
1532 if(argc > 2) {
1533 hres = to_number(ctx, argv[2], &sec);
1534 if(FAILED(hres))
1535 return hres;
1536 }else {
1537 sec = sec_from_time(t);
1540 if(argc > 3) {
1541 hres = to_number(ctx, argv[3], &ms);
1542 if(FAILED(hres))
1543 return hres;
1544 }else {
1545 ms = ms_from_time(t);
1548 t = make_date(day(t), make_time(hour, min, sec, ms));
1549 date->time = time_clip(t);
1551 if(r)
1552 *r = jsval_number(date->time);
1553 return S_OK;
1556 /* ECMA-262 3rd Edition 15.9.5.36 */
1557 static HRESULT Date_setDate(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1558 jsval_t *r)
1560 DateInstance *date;
1561 double t, n;
1562 HRESULT hres;
1564 TRACE("\n");
1566 if(!(date = date_this(jsthis)))
1567 return JS_E_DATE_EXPECTED;
1569 if(!argc)
1570 return JS_E_MISSING_ARG;
1572 hres = to_number(ctx, argv[0], &n);
1573 if(FAILED(hres))
1574 return hres;
1576 t = local_time(date->time, date);
1577 t = make_date(make_day(year_from_time(t), month_from_time(t), n), time_within_day(t));
1578 date->time = time_clip(utc(t, date));
1580 if(r)
1581 *r = jsval_number(date->time);
1582 return S_OK;
1585 /* ECMA-262 3rd Edition 15.9.5.37 */
1586 static HRESULT Date_setUTCDate(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1587 jsval_t *r)
1589 DateInstance *date;
1590 double t, n;
1591 HRESULT hres;
1593 TRACE("\n");
1595 if(!(date = date_this(jsthis)))
1596 return JS_E_DATE_EXPECTED;
1598 if(!argc)
1599 return JS_E_MISSING_ARG;
1601 hres = to_number(ctx, argv[0], &n);
1602 if(FAILED(hres))
1603 return hres;
1605 t = date->time;
1606 t = make_date(make_day(year_from_time(t), month_from_time(t), n), time_within_day(t));
1607 date->time = time_clip(t);
1609 if(r)
1610 *r = jsval_number(date->time);
1611 return S_OK;
1614 /* ECMA-262 3rd Edition 15.9.5.38 */
1615 static HRESULT Date_setMonth(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1616 jsval_t *r)
1618 DateInstance *date;
1619 DOUBLE t, month, ddate;
1620 HRESULT hres;
1622 TRACE("\n");
1624 if(!(date = date_this(jsthis)))
1625 return JS_E_DATE_EXPECTED;
1627 if(!argc)
1628 return JS_E_MISSING_ARG;
1630 t = local_time(date->time, date);
1632 hres = to_number(ctx, argv[0], &month);
1633 if(FAILED(hres))
1634 return hres;
1636 if(argc > 1) {
1637 hres = to_number(ctx, argv[1], &ddate);
1638 if(FAILED(hres))
1639 return hres;
1640 }else {
1641 ddate = date_from_time(t);
1644 t = make_date(make_day(year_from_time(t), month, ddate),
1645 time_within_day(t));
1646 date->time = time_clip(utc(t, date));
1648 if(r)
1649 *r = jsval_number(date->time);
1650 return S_OK;
1653 /* ECMA-262 3rd Edition 15.9.5.39 */
1654 static HRESULT Date_setUTCMonth(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1655 jsval_t *r)
1657 DateInstance *date;
1658 double t, month, ddate;
1659 HRESULT hres;
1661 TRACE("\n");
1663 if(!(date = date_this(jsthis)))
1664 return JS_E_DATE_EXPECTED;
1666 if(!argc)
1667 return JS_E_MISSING_ARG;
1669 t = date->time;
1671 hres = to_number(ctx, argv[0], &month);
1672 if(FAILED(hres))
1673 return hres;
1675 if(argc > 1) {
1676 hres = to_number(ctx, argv[1], &ddate);
1677 if(FAILED(hres))
1678 return hres;
1679 }else {
1680 ddate = date_from_time(t);
1683 t = make_date(make_day(year_from_time(t), month, ddate),
1684 time_within_day(t));
1685 date->time = time_clip(t);
1687 if(r)
1688 *r = jsval_number(date->time);
1689 return S_OK;
1692 /* ECMA-262 3rd Edition 15.9.5.40 */
1693 static HRESULT Date_setFullYear(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1694 jsval_t *r)
1696 DateInstance *date;
1697 double t, year, month, ddate;
1698 HRESULT hres;
1700 TRACE("\n");
1702 if(!(date = date_this(jsthis)))
1703 return JS_E_DATE_EXPECTED;
1705 if(!argc)
1706 return JS_E_MISSING_ARG;
1708 t = local_time(date->time, date);
1710 hres = to_number(ctx, argv[0], &year);
1711 if(FAILED(hres))
1712 return hres;
1714 if(argc > 1) {
1715 hres = to_number(ctx, argv[1], &month);
1716 if(FAILED(hres))
1717 return hres;
1718 }else {
1719 month = month_from_time(t);
1722 if(argc > 2) {
1723 hres = to_number(ctx, argv[2], &ddate);
1724 if(FAILED(hres))
1725 return hres;
1726 }else {
1727 ddate = date_from_time(t);
1730 t = make_date(make_day(year, month, ddate), time_within_day(t));
1731 date->time = time_clip(utc(t, date));
1733 if(r)
1734 *r = jsval_number(date->time);
1735 return S_OK;
1738 /* ECMA-262 3rd Edition 15.9.5.41 */
1739 static HRESULT Date_setUTCFullYear(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1740 jsval_t *r)
1742 DateInstance *date;
1743 double t, year, month, ddate;
1744 HRESULT hres;
1746 TRACE("\n");
1748 if(!(date = date_this(jsthis)))
1749 return JS_E_DATE_EXPECTED;
1751 if(!argc)
1752 return JS_E_MISSING_ARG;
1754 t = date->time;
1756 hres = to_number(ctx, argv[0], &year);
1757 if(FAILED(hres))
1758 return hres;
1760 if(argc > 1) {
1761 hres = to_number(ctx, argv[1], &month);
1762 if(FAILED(hres))
1763 return hres;
1764 }else {
1765 month = month_from_time(t);
1768 if(argc > 2) {
1769 hres = to_number(ctx, argv[2], &ddate);
1770 if(FAILED(hres))
1771 return hres;
1772 }else {
1773 ddate = date_from_time(t);
1776 t = make_date(make_day(year, month, ddate), time_within_day(t));
1777 date->time = time_clip(t);
1779 if(r)
1780 *r = jsval_number(date->time);
1781 return S_OK;
1784 /* ECMA-262 3rd Edition B2.4 */
1785 static HRESULT Date_getYear(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1786 jsval_t *r)
1788 DateInstance *date;
1789 DOUBLE t, year;
1791 TRACE("\n");
1793 if(!(date = date_this(jsthis)))
1794 return JS_E_DATE_EXPECTED;
1796 t = local_time(date->time, date);
1797 if(isnan(t)) {
1798 if(r)
1799 *r = jsval_number(NAN);
1800 return S_OK;
1803 year = year_from_time(t);
1804 if(r)
1805 *r = jsval_number((1900<=year && year<2000)?year-1900:year);
1806 return S_OK;
1809 /* ECMA-262 3rd Edition B2.5 */
1810 static HRESULT Date_setYear(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1811 jsval_t *r)
1813 DateInstance *date;
1814 DOUBLE t, year;
1815 HRESULT hres;
1817 TRACE("\n");
1819 if(!(date = date_this(jsthis)))
1820 return JS_E_DATE_EXPECTED;
1822 if(!argc)
1823 return JS_E_MISSING_ARG;
1825 t = local_time(date->time, date);
1827 hres = to_number(ctx, argv[0], &year);
1828 if(FAILED(hres))
1829 return hres;
1831 if(isnan(year)) {
1832 date->time = year;
1833 if(r)
1834 *r = jsval_number(NAN);
1835 return S_OK;
1838 year = year >= 0.0 ? floor(year) : -floor(-year);
1839 if(-1.0 < year && year < 100.0)
1840 year += 1900.0;
1842 date->time = time_clip(utc(make_date(make_day(year, month_from_time(t), date_from_time(t)), time_within_day(t)), date));
1844 if(r)
1845 *r = jsval_number(date->time);
1846 return S_OK;
1849 static HRESULT Date_get_value(script_ctx_t *ctx, jsdisp_t *jsthis, jsval_t *r)
1851 TRACE("\n");
1853 return dateobj_to_string(date_from_jsdisp(jsthis), r);
1856 static const builtin_prop_t Date_props[] = {
1857 {L"getDate", Date_getDate, PROPF_METHOD},
1858 {L"getDay", Date_getDay, PROPF_METHOD},
1859 {L"getFullYear", Date_getFullYear, PROPF_METHOD},
1860 {L"getHours", Date_getHours, PROPF_METHOD},
1861 {L"getMilliseconds", Date_getMilliseconds, PROPF_METHOD},
1862 {L"getMinutes", Date_getMinutes, PROPF_METHOD},
1863 {L"getMonth", Date_getMonth, PROPF_METHOD},
1864 {L"getSeconds", Date_getSeconds, PROPF_METHOD},
1865 {L"getTime", Date_getTime, PROPF_METHOD},
1866 {L"getTimezoneOffset", Date_getTimezoneOffset, PROPF_METHOD},
1867 {L"getUTCDate", Date_getUTCDate, PROPF_METHOD},
1868 {L"getUTCDay", Date_getUTCDay, PROPF_METHOD},
1869 {L"getUTCFullYear", Date_getUTCFullYear, PROPF_METHOD},
1870 {L"getUTCHours", Date_getUTCHours, PROPF_METHOD},
1871 {L"getUTCMilliseconds", Date_getUTCMilliseconds, PROPF_METHOD},
1872 {L"getUTCMinutes", Date_getUTCMinutes, PROPF_METHOD},
1873 {L"getUTCMonth", Date_getUTCMonth, PROPF_METHOD},
1874 {L"getUTCSeconds", Date_getUTCSeconds, PROPF_METHOD},
1875 {L"getYear", Date_getYear, PROPF_METHOD},
1876 {L"setDate", Date_setDate, PROPF_METHOD|1},
1877 {L"setFullYear", Date_setFullYear, PROPF_METHOD|3},
1878 {L"setHours", Date_setHours, PROPF_METHOD|4},
1879 {L"setMilliseconds", Date_setMilliseconds, PROPF_METHOD|1},
1880 {L"setMinutes", Date_setMinutes, PROPF_METHOD|3},
1881 {L"setMonth", Date_setMonth, PROPF_METHOD|2},
1882 {L"setSeconds", Date_setSeconds, PROPF_METHOD|2},
1883 {L"setTime", Date_setTime, PROPF_METHOD|1},
1884 {L"setUTCDate", Date_setUTCDate, PROPF_METHOD|1},
1885 {L"setUTCFullYear", Date_setUTCFullYear, PROPF_METHOD|3},
1886 {L"setUTCHours", Date_setUTCHours, PROPF_METHOD|4},
1887 {L"setUTCMilliseconds", Date_setUTCMilliseconds, PROPF_METHOD|1},
1888 {L"setUTCMinutes", Date_setUTCMinutes, PROPF_METHOD|3},
1889 {L"setUTCMonth", Date_setUTCMonth, PROPF_METHOD|2},
1890 {L"setUTCSeconds", Date_setUTCSeconds, PROPF_METHOD|2},
1891 {L"setYear", Date_setYear, PROPF_METHOD|1},
1892 {L"toDateString", Date_toDateString, PROPF_METHOD},
1893 {L"toGMTString", Date_toGMTString, PROPF_METHOD},
1894 {L"toISOString", Date_toISOString, PROPF_METHOD|PROPF_ES5},
1895 {L"toLocaleDateString", Date_toLocaleDateString, PROPF_METHOD},
1896 {L"toLocaleString", Date_toLocaleString, PROPF_METHOD},
1897 {L"toLocaleTimeString", Date_toLocaleTimeString, PROPF_METHOD},
1898 {L"toString", Date_toString, PROPF_METHOD},
1899 {L"toTimeString", Date_toTimeString, PROPF_METHOD},
1900 {L"toUTCString", Date_toUTCString, PROPF_METHOD},
1901 {L"valueOf", Date_valueOf, PROPF_METHOD},
1904 static const builtin_info_t Date_info = {
1905 JSCLASS_DATE,
1906 {NULL, NULL,0, Date_get_value},
1907 ARRAY_SIZE(Date_props),
1908 Date_props,
1909 NULL,
1910 NULL
1913 static const builtin_info_t DateInst_info = {
1914 JSCLASS_DATE,
1915 {NULL, NULL,0, Date_get_value},
1916 0, NULL,
1917 NULL,
1918 NULL
1921 static HRESULT create_date(script_ctx_t *ctx, jsdisp_t *object_prototype, DOUBLE time, jsdisp_t **ret)
1923 DateInstance *date;
1924 HRESULT hres;
1925 TIME_ZONE_INFORMATION tzi;
1927 GetTimeZoneInformation(&tzi);
1929 date = heap_alloc_zero(sizeof(DateInstance));
1930 if(!date)
1931 return E_OUTOFMEMORY;
1933 if(object_prototype)
1934 hres = init_dispex(&date->dispex, ctx, &Date_info, object_prototype);
1935 else
1936 hres = init_dispex_from_constr(&date->dispex, ctx, &DateInst_info, ctx->date_constr);
1937 if(FAILED(hres)) {
1938 heap_free(date);
1939 return hres;
1942 date->time = time;
1943 date->bias = tzi.Bias;
1944 date->standardDate = tzi.StandardDate;
1945 date->standardBias = tzi.StandardBias;
1946 date->daylightDate = tzi.DaylightDate;
1947 date->daylightBias = tzi.DaylightBias;
1949 *ret = &date->dispex;
1950 return S_OK;
1953 static inline HRESULT date_parse(jsstr_t *input_str, double *ret) {
1954 static const DWORD string_ids[] = { LOCALE_SMONTHNAME12, LOCALE_SMONTHNAME11,
1955 LOCALE_SMONTHNAME10, LOCALE_SMONTHNAME9, LOCALE_SMONTHNAME8,
1956 LOCALE_SMONTHNAME7, LOCALE_SMONTHNAME6, LOCALE_SMONTHNAME5,
1957 LOCALE_SMONTHNAME4, LOCALE_SMONTHNAME3, LOCALE_SMONTHNAME2,
1958 LOCALE_SMONTHNAME1, LOCALE_SDAYNAME7, LOCALE_SDAYNAME1,
1959 LOCALE_SDAYNAME2, LOCALE_SDAYNAME3, LOCALE_SDAYNAME4,
1960 LOCALE_SDAYNAME5, LOCALE_SDAYNAME6 };
1961 WCHAR *strings[ARRAY_SIZE(string_ids)];
1962 WCHAR *parse;
1963 int input_len, parse_len = 0, nest_level = 0, i, size;
1964 int year = 0, month = 0, day = 0, hour = 0, min = 0, sec = 0;
1965 int ms = 0, offset = 0, hour_adjust = 0;
1966 BOOL set_year = FALSE, set_month = FALSE, set_day = FALSE, set_hour = FALSE;
1967 BOOL set_offset = FALSE, set_era = FALSE, ad = TRUE, set_am = FALSE, am = TRUE;
1968 BOOL set_hour_adjust = TRUE;
1969 TIME_ZONE_INFORMATION tzi;
1970 const WCHAR *input;
1971 DateInstance di;
1972 DWORD lcid_en;
1974 input_len = jsstr_length(input_str);
1975 input = jsstr_flatten(input_str);
1976 if(!input)
1977 return E_OUTOFMEMORY;
1979 for(i=0; i<input_len; i++) {
1980 if(input[i] == '(') nest_level++;
1981 else if(input[i] == ')') {
1982 nest_level--;
1983 if(nest_level<0) {
1984 *ret = NAN;
1985 return S_OK;
1988 else if(!nest_level) parse_len++;
1991 parse = heap_alloc((parse_len+1)*sizeof(WCHAR));
1992 if(!parse)
1993 return E_OUTOFMEMORY;
1994 nest_level = 0;
1995 parse_len = 0;
1996 for(i=0; i<input_len; i++) {
1997 if(input[i] == '(') nest_level++;
1998 else if(input[i] == ')') nest_level--;
1999 else if(!nest_level) parse[parse_len++] = towupper(input[i]);
2001 parse[parse_len] = 0;
2003 GetTimeZoneInformation(&tzi);
2004 di.bias = tzi.Bias;
2005 di.standardDate = tzi.StandardDate;
2006 di.standardBias = tzi.StandardBias;
2007 di.daylightDate = tzi.DaylightDate;
2008 di.daylightBias = tzi.DaylightBias;
2010 /* FIXME: Cache strings */
2011 lcid_en = MAKELCID(MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),SORT_DEFAULT);
2012 for(i=0; i<ARRAY_SIZE(string_ids); i++) {
2013 size = GetLocaleInfoW(lcid_en, string_ids[i], NULL, 0);
2014 strings[i] = heap_alloc((size+1)*sizeof(WCHAR));
2015 if(!strings[i]) {
2016 i--;
2017 while(i-- >= 0)
2018 heap_free(strings[i]);
2019 heap_free(parse);
2020 return E_OUTOFMEMORY;
2022 GetLocaleInfoW(lcid_en, string_ids[i], strings[i], size);
2025 for(i=0; i<parse_len;) {
2026 while(iswspace(parse[i])) i++;
2027 if(parse[i] == ',') {
2028 while(parse[i] == ',') i++;
2029 continue;
2032 if(parse[i]>='0' && parse[i]<='9') {
2033 int tmp = wcstol(&parse[i], NULL, 10);
2034 while(parse[i]>='0' && parse[i]<='9') i++;
2035 while(iswspace(parse[i])) i++;
2037 if(parse[i] == ':') {
2038 /* Time */
2039 if(set_hour) break;
2040 set_hour = TRUE;
2042 hour = tmp;
2044 while(parse[i] == ':') i++;
2045 while(iswspace(parse[i])) i++;
2046 if(parse[i]>='0' && parse[i]<='9') {
2047 min = wcstol(&parse[i], NULL, 10);
2048 while(parse[i]>='0' && parse[i]<='9') i++;
2051 while(iswspace(parse[i])) i++;
2052 while(parse[i] == ':') i++;
2053 while(iswspace(parse[i])) i++;
2054 if(parse[i]>='0' && parse[i]<='9') {
2055 sec = wcstol(&parse[i], NULL, 10);
2056 while(parse[i]>='0' && parse[i]<='9') i++;
2059 else if(parse[i]=='-' || parse[i]=='/') {
2060 /* Short or long date */
2061 if(set_day || set_month || set_year) break;
2062 set_day = TRUE;
2063 set_month = TRUE;
2064 set_year = TRUE;
2066 month = tmp-1;
2068 while(iswspace(parse[i])) i++;
2069 while(parse[i]=='-' || parse[i]=='/') i++;
2070 while(iswspace(parse[i])) i++;
2071 if(parse[i]<'0' || parse[i]>'9') break;
2072 day = wcstol(&parse[i], NULL, 10);
2073 while(parse[i]>='0' && parse[i]<='9') i++;
2075 while(parse[i]=='-' || parse[i]=='/') i++;
2076 while(iswspace(parse[i])) i++;
2077 if(parse[i]<'0' || parse[i]>'9') break;
2078 year = wcstol(&parse[i], NULL, 10);
2079 while(parse[i]>='0' && parse[i]<='9') i++;
2081 if(tmp >= 70){
2082 /* long date */
2083 month = day - 1;
2084 day = year;
2085 year = tmp;
2088 else if(tmp<0) break;
2089 else if(tmp<70) {
2090 /* Day */
2091 if(set_day) break;
2092 set_day = TRUE;
2093 day = tmp;
2095 else {
2096 /* Year */
2097 if(set_year) break;
2098 set_year = TRUE;
2099 year = tmp;
2102 else if(parse[i]=='+' || parse[i]=='-') {
2103 /* Timezone offset */
2104 BOOL positive = TRUE;
2106 if(set_offset && set_hour_adjust) break;
2107 set_offset = TRUE;
2108 set_hour_adjust = FALSE;
2110 if(parse[i] == '-') positive = FALSE;
2112 i++;
2113 while(iswspace(parse[i])) i++;
2114 if(parse[i]<'0' || parse[i]>'9') break;
2115 offset = wcstol(&parse[i], NULL, 10);
2116 while(parse[i]>='0' && parse[i]<='9') i++;
2118 if(offset<24) offset *= 60;
2119 else offset = (offset/100)*60 + offset%100;
2121 if(positive) offset = -offset;
2124 else {
2125 if(parse[i]<'A' || parse[i]>'Z') break;
2126 else if(parse[i]=='B' && (parse[i+1]=='C' ||
2127 (parse[i+1]=='.' && parse[i+2]=='C'))) {
2128 /* AD/BC */
2129 if(set_era) break;
2130 set_era = TRUE;
2131 ad = FALSE;
2133 i++;
2134 if(parse[i] == '.') i++;
2135 i++;
2136 if(parse[i] == '.') i++;
2138 else if(parse[i]=='A' && (parse[i+1]=='D' ||
2139 (parse[i+1]=='.' && parse[i+2]=='D'))) {
2140 /* AD/BC */
2141 if(set_era) break;
2142 set_era = TRUE;
2144 i++;
2145 if(parse[i] == '.') i++;
2146 i++;
2147 if(parse[i] == '.') i++;
2149 else if(parse[i+1]<'A' || parse[i+1]>'Z') {
2150 /* Timezone */
2151 if(set_offset) break;
2152 set_offset = TRUE;
2154 if(parse[i] <= 'I') hour_adjust = parse[i]-'A'+2;
2155 else if(parse[i] == 'J') break;
2156 else if(parse[i] <= 'M') hour_adjust = parse[i]-'K'+11;
2157 else if(parse[i] <= 'Y') hour_adjust = parse[i]-'N';
2158 else hour_adjust = 1;
2160 i++;
2161 if(parse[i] == '.') i++;
2163 else if(parse[i]=='A' && parse[i+1]=='M') {
2164 /* AM/PM */
2165 if(set_am) break;
2166 set_am = TRUE;
2167 am = TRUE;
2168 i += 2;
2170 else if(parse[i]=='P' && parse[i+1]=='M') {
2171 /* AM/PM */
2172 if(set_am) break;
2173 set_am = TRUE;
2174 am = FALSE;
2175 i += 2;
2177 else if((parse[i]=='U' && parse[i+1]=='T' && parse[i+2]=='C')
2178 || (parse[i]=='G' && parse[i+1]=='M' && parse[i+2]=='T')) {
2179 /* Timezone */
2180 if(set_offset) break;
2181 set_offset = TRUE;
2182 set_hour_adjust = FALSE;
2184 i += 3;
2186 else {
2187 /* Month or garbage */
2188 unsigned int j;
2190 for(size=i; parse[size]>='A' && parse[size]<='Z'; size++);
2191 size -= i;
2193 for(j=0; j<ARRAY_SIZE(string_ids); j++)
2194 if(!wcsnicmp(&parse[i], strings[j], size)) break;
2196 if(j < 12) {
2197 if(set_month) break;
2198 set_month = TRUE;
2199 month = 11-j;
2201 else if(j == ARRAY_SIZE(string_ids)) break;
2203 i += size;
2208 if(i == parse_len && set_year && set_month && set_day && (!set_am || hour<13)) {
2209 if(set_am) {
2210 if(hour == 12) hour = 0;
2211 if(!am) hour += 12;
2214 if(!ad) year = -year+1;
2215 else if(year<100) year += 1900;
2217 *ret = time_clip(make_date(make_day(year, month, day),
2218 make_time(hour+hour_adjust, min, sec, ms)) + offset*MS_PER_MINUTE);
2220 if(set_hour_adjust)
2221 *ret = utc(*ret, &di);
2222 }else {
2223 *ret = NAN;
2226 for(i=0; i<ARRAY_SIZE(string_ids); i++)
2227 heap_free(strings[i]);
2228 heap_free(parse);
2230 return S_OK;
2233 static HRESULT DateConstr_parse(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
2234 jsval_t *r)
2236 jsstr_t *parse_str;
2237 double n;
2238 HRESULT hres;
2240 TRACE("\n");
2242 if(!argc) {
2243 if(r)
2244 *r = jsval_number(NAN);
2245 return S_OK;
2248 hres = to_string(ctx, argv[0], &parse_str);
2249 if(FAILED(hres))
2250 return hres;
2252 hres = date_parse(parse_str, &n);
2253 jsstr_release(parse_str);
2254 if(FAILED(hres))
2255 return hres;
2257 *r = jsval_number(n);
2258 return S_OK;
2261 static HRESULT date_utc(script_ctx_t *ctx, unsigned argc, jsval_t *argv, double *ret)
2263 double year, month, vdate, hours, minutes, seconds, ms;
2264 HRESULT hres;
2266 TRACE("\n");
2268 if(argc) {
2269 hres = to_number(ctx, argv[0], &year);
2270 if(FAILED(hres))
2271 return hres;
2272 if(0 <= year && year <= 99)
2273 year += 1900;
2274 }else {
2275 year = 1900;
2278 if(argc>1) {
2279 hres = to_number(ctx, argv[1], &month);
2280 if(FAILED(hres))
2281 return hres;
2282 }else {
2283 month = 0;
2286 if(argc>2) {
2287 hres = to_number(ctx, argv[2], &vdate);
2288 if(FAILED(hres))
2289 return hres;
2290 }else {
2291 vdate = 1;
2294 if(argc>3) {
2295 hres = to_number(ctx, argv[3], &hours);
2296 if(FAILED(hres))
2297 return hres;
2298 }else {
2299 hours = 0;
2302 if(argc>4) {
2303 hres = to_number(ctx, argv[4], &minutes);
2304 if(FAILED(hres))
2305 return hres;
2306 }else {
2307 minutes = 0;
2310 if(argc>5) {
2311 hres = to_number(ctx, argv[5], &seconds);
2312 if(FAILED(hres))
2313 return hres;
2314 }else {
2315 seconds = 0;
2318 if(argc>6) {
2319 hres = to_number(ctx, argv[6], &ms);
2320 if(FAILED(hres))
2321 return hres;
2322 } else {
2323 ms = 0;
2326 *ret = time_clip(make_date(make_day(year, month, vdate),
2327 make_time(hours, minutes,seconds, ms)));
2328 return S_OK;
2331 static HRESULT DateConstr_UTC(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
2332 jsval_t *r)
2334 double n;
2335 HRESULT hres;
2337 TRACE("\n");
2339 hres = date_utc(ctx, argc, argv, &n);
2340 if(SUCCEEDED(hres) && r)
2341 *r = jsval_number(n);
2342 return hres;
2345 /* ECMA-262 5.1 Edition 15.9.4.4 */
2346 static HRESULT DateConstr_now(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
2348 TRACE("\n");
2350 if(r) *r = jsval_number(date_now());
2351 return S_OK;
2354 static HRESULT DateConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
2355 jsval_t *r)
2357 jsdisp_t *date;
2358 HRESULT hres;
2360 TRACE("\n");
2362 switch(flags) {
2363 case DISPATCH_CONSTRUCT:
2364 switch(argc) {
2365 /* ECMA-262 3rd Edition 15.9.3.3 */
2366 case 0:
2367 hres = create_date(ctx, NULL, date_now(), &date);
2368 if(FAILED(hres))
2369 return hres;
2370 break;
2372 /* ECMA-262 3rd Edition 15.9.3.2 */
2373 case 1: {
2374 jsval_t prim;
2375 double n;
2377 hres = to_primitive(ctx, argv[0], &prim, NO_HINT);
2378 if(FAILED(hres))
2379 return hres;
2381 if(is_string(prim))
2382 hres = date_parse(get_string(prim), &n);
2383 else
2384 hres = to_number(ctx, prim, &n);
2386 jsval_release(prim);
2387 if(FAILED(hres))
2388 return hres;
2390 hres = create_date(ctx, NULL, time_clip(n), &date);
2391 if(FAILED(hres))
2392 return hres;
2393 break;
2396 /* ECMA-262 3rd Edition 15.9.3.1 */
2397 default: {
2398 double ret_date;
2399 DateInstance *di;
2401 hres = date_utc(ctx, argc, argv, &ret_date);
2402 if(FAILED(hres))
2403 return hres;
2405 hres = create_date(ctx, NULL, ret_date, &date);
2406 if(FAILED(hres))
2407 return hres;
2409 di = date_from_jsdisp(date);
2410 di->time = utc(di->time, di);
2414 *r = jsval_obj(date);
2415 return S_OK;
2417 case INVOKE_FUNC: {
2418 FILETIME system_time, local_time;
2419 LONGLONG lltime;
2421 GetSystemTimeAsFileTime(&system_time);
2422 FileTimeToLocalFileTime(&system_time, &local_time);
2423 lltime = ((LONGLONG)local_time.dwHighDateTime<<32)
2424 + local_time.dwLowDateTime;
2426 return date_to_string(lltime/10000-TIME_EPOCH, FALSE, 0, r);
2429 default:
2430 FIXME("unimplemented flags %x\n", flags);
2431 return E_NOTIMPL;
2434 return S_OK;
2437 static const builtin_prop_t DateConstr_props[] = {
2438 {L"UTC", DateConstr_UTC, PROPF_METHOD},
2439 {L"now", DateConstr_now, PROPF_HTML|PROPF_METHOD},
2440 {L"parse", DateConstr_parse, PROPF_METHOD}
2443 static const builtin_info_t DateConstr_info = {
2444 JSCLASS_FUNCTION,
2445 DEFAULT_FUNCTION_VALUE,
2446 ARRAY_SIZE(DateConstr_props),
2447 DateConstr_props,
2448 NULL,
2449 NULL
2452 HRESULT create_date_constr(script_ctx_t *ctx, jsdisp_t *object_prototype, jsdisp_t **ret)
2454 jsdisp_t *date;
2455 HRESULT hres;
2457 hres = create_date(ctx, object_prototype, 0.0, &date);
2458 if(FAILED(hres))
2459 return hres;
2461 hres = create_builtin_constructor(ctx, DateConstr_value, L"Date", &DateConstr_info,
2462 PROPF_CONSTR|7, date, ret);
2464 jsdisp_release(date);
2465 return hres;