gdi32: Try to parse font names without FreeType.
[wine/zf.git] / dlls / jscript / date.c
blob5ca23f199f353718f78646d14b52ef89c120ac0c
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 BOOL formatAD = TRUE;
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 formatAD = FALSE;
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, formatAD?L"":L" B.C.");
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, formatAD?L"":L" B.C.");
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, formatAD?L"":L" B.C.");
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 BOOL formatAD = TRUE;
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 formatAD = FALSE;
670 year = -year+1;
673 day = date_from_time(date->time);
675 swprintf(buf, ARRAY_SIZE(buf),
676 formatAD ? L"%s, %d %s %d %02d:%02d:%02d UTC" : L"%s, %d %s %d B.C. %02d:%02d:%02d UTC",
677 week, day, month, year, (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 BOOL formatAD = TRUE;
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 formatAD = FALSE;
745 year = -year+1;
748 day = date_from_time(time);
750 swprintf(buf, ARRAY_SIZE(buf), formatAD ? L"%s %s %d %d" : L"%s %s %d %d B.C.", week, month,
751 day, year);
753 date_str = jsstr_alloc(buf);
754 if(!date_str)
755 return E_OUTOFMEMORY;
757 *r = jsval_string(date_str);
759 return S_OK;
762 static HRESULT Date_toDateString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
763 jsval_t *r)
765 DateInstance *date;
767 if(!(date = date_this(jsthis)))
768 return JS_E_DATE_EXPECTED;
770 return dateobj_to_date_string(date, r);
773 /* ECMA-262 3rd Edition 15.9.5.4 */
774 static HRESULT Date_toTimeString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
775 jsval_t *r)
777 DateInstance *date;
778 jsstr_t *date_str;
779 WCHAR buf[32];
780 DOUBLE time;
781 WCHAR sign;
782 int offset;
784 TRACE("\n");
786 if(!(date = date_this(jsthis)))
787 return JS_E_DATE_EXPECTED;
789 if(isnan(date->time)) {
790 if(r)
791 *r = jsval_string(jsstr_nan());
792 return S_OK;
795 time = local_time(date->time, date);
797 if(r) {
798 offset = date->bias +
799 daylight_saving_ta(time, date);
801 if(offset < 0) {
802 sign = '+';
803 offset = -offset;
805 else sign = '-';
807 if(offset)
808 swprintf(buf, ARRAY_SIZE(buf), L"%02d:%02d:%02d UTC%c%02d%02d", (int)hour_from_time(time),
809 (int)min_from_time(time), (int)sec_from_time(time),
810 sign, offset/60, offset%60);
811 else
812 swprintf(buf, ARRAY_SIZE(buf), L"%02d:%02d:%02d UTC", (int)hour_from_time(time),
813 (int)min_from_time(time), (int)sec_from_time(time));
815 date_str = jsstr_alloc(buf);
816 if(!date_str)
817 return E_OUTOFMEMORY;
819 *r = jsval_string(date_str);
821 return S_OK;
824 /* ECMA-262 3rd Edition 15.9.5.6 */
825 static HRESULT Date_toLocaleDateString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
826 jsval_t *r)
828 SYSTEMTIME st;
829 DateInstance *date;
830 jsstr_t *date_str;
831 int len;
833 TRACE("\n");
835 if(!(date = date_this(jsthis)))
836 return JS_E_DATE_EXPECTED;
838 if(isnan(date->time)) {
839 if(r)
840 *r = jsval_string(jsstr_nan());
841 return S_OK;
844 st = create_systemtime(local_time(date->time, date));
846 if(st.wYear<1601 || st.wYear>9999)
847 return dateobj_to_date_string(date, r);
849 if(r) {
850 WCHAR *ptr;
852 len = GetDateFormatW(ctx->lcid, DATE_LONGDATE, &st, NULL, NULL, 0);
853 date_str = jsstr_alloc_buf(len-1, &ptr);
854 if(!date_str)
855 return E_OUTOFMEMORY;
856 GetDateFormatW(ctx->lcid, DATE_LONGDATE, &st, NULL, ptr, len);
858 *r = jsval_string(date_str);
860 return S_OK;
863 /* ECMA-262 3rd Edition 15.9.5.7 */
864 static HRESULT Date_toLocaleTimeString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
865 jsval_t *r)
867 SYSTEMTIME st;
868 DateInstance *date;
869 jsstr_t *date_str;
870 int len;
872 TRACE("\n");
874 if(!(date = date_this(jsthis)))
875 return JS_E_DATE_EXPECTED;
877 if(isnan(date->time)) {
878 if(r)
879 *r = jsval_string(jsstr_nan());
880 return S_OK;
883 st = create_systemtime(local_time(date->time, date));
885 if(st.wYear<1601 || st.wYear>9999)
886 return Date_toTimeString(ctx, jsthis, flags, argc, argv, r);
888 if(r) {
889 WCHAR *ptr;
891 len = GetTimeFormatW(ctx->lcid, 0, &st, NULL, NULL, 0);
892 date_str = jsstr_alloc_buf(len-1, &ptr);
893 if(!date_str)
894 return E_OUTOFMEMORY;
895 GetTimeFormatW(ctx->lcid, 0, &st, NULL, ptr, len);
897 *r = jsval_string(date_str);
899 return S_OK;
902 /* ECMA-262 3rd Edition 15.9.5.9 */
903 static HRESULT Date_getTime(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
904 jsval_t *r)
906 DateInstance *date;
908 TRACE("\n");
910 if(!(date = date_this(jsthis)))
911 return JS_E_DATE_EXPECTED;
913 if(r)
914 *r = jsval_number(date->time);
915 return S_OK;
918 /* ECMA-262 3rd Edition 15.9.5.10 */
919 static HRESULT Date_getFullYear(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
920 jsval_t *r)
922 DateInstance *date;
924 TRACE("\n");
926 if(!(date = date_this(jsthis)))
927 return JS_E_DATE_EXPECTED;
929 if(r) {
930 DOUBLE time = local_time(date->time, date);
932 *r = jsval_number(year_from_time(time));
934 return S_OK;
937 /* ECMA-262 3rd Edition 15.9.5.11 */
938 static HRESULT Date_getUTCFullYear(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
939 jsval_t *r)
941 DateInstance *date;
943 TRACE("\n");
945 if(!(date = date_this(jsthis)))
946 return JS_E_DATE_EXPECTED;
948 if(r)
949 *r = jsval_number(year_from_time(date->time));
950 return S_OK;
953 /* ECMA-262 3rd Edition 15.9.5.12 */
954 static HRESULT Date_getMonth(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
956 DateInstance *date;
958 TRACE("\n");
960 if(!(date = date_this(jsthis)))
961 return JS_E_DATE_EXPECTED;
963 if(r)
964 *r = jsval_number(month_from_time(local_time(date->time, date)));
965 return S_OK;
968 /* ECMA-262 3rd Edition 15.9.5.13 */
969 static HRESULT Date_getUTCMonth(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
970 jsval_t *r)
972 DateInstance *date;
974 TRACE("\n");
976 if(!(date = date_this(jsthis)))
977 return JS_E_DATE_EXPECTED;
979 if(r)
980 *r = jsval_number(month_from_time(date->time));
981 return S_OK;
984 /* ECMA-262 3rd Edition 15.9.5.14 */
985 static HRESULT Date_getDate(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
987 DateInstance *date;
989 TRACE("\n");
991 if(!(date = date_this(jsthis)))
992 return JS_E_DATE_EXPECTED;
994 if(r)
995 *r = jsval_number(date_from_time(local_time(date->time, date)));
996 return S_OK;
999 /* ECMA-262 3rd Edition 15.9.5.15 */
1000 static HRESULT Date_getUTCDate(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1001 jsval_t *r)
1003 DateInstance *date;
1005 TRACE("\n");
1007 if(!(date = date_this(jsthis)))
1008 return JS_E_DATE_EXPECTED;
1010 if(r)
1011 *r = jsval_number(date_from_time(date->time));
1012 return S_OK;
1015 /* ECMA-262 3rd Edition 15.9.5.16 */
1016 static HRESULT Date_getDay(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1017 jsval_t *r)
1019 DateInstance *date;
1021 TRACE("\n");
1023 if(!(date = date_this(jsthis)))
1024 return JS_E_DATE_EXPECTED;
1026 if(r)
1027 *r = jsval_number(week_day(local_time(date->time, date)));
1028 return S_OK;
1031 /* ECMA-262 3rd Edition 15.9.5.17 */
1032 static HRESULT Date_getUTCDay(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1033 jsval_t *r)
1035 DateInstance *date;
1037 TRACE("\n");
1039 if(!(date = date_this(jsthis)))
1040 return JS_E_DATE_EXPECTED;
1042 if(r)
1043 *r = jsval_number(week_day(date->time));
1044 return S_OK;
1047 /* ECMA-262 3rd Edition 15.9.5.18 */
1048 static HRESULT Date_getHours(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1049 jsval_t *r)
1051 DateInstance *date;
1053 TRACE("\n");
1055 if(!(date = date_this(jsthis)))
1056 return JS_E_DATE_EXPECTED;
1058 if(r)
1059 *r = jsval_number(hour_from_time(local_time(date->time, date)));
1060 return S_OK;
1063 /* ECMA-262 3rd Edition 15.9.5.19 */
1064 static HRESULT Date_getUTCHours(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1065 jsval_t *r)
1067 DateInstance *date;
1069 TRACE("\n");
1071 if(!(date = date_this(jsthis)))
1072 return JS_E_DATE_EXPECTED;
1074 if(r)
1075 *r = jsval_number(hour_from_time(date->time));
1076 return S_OK;
1079 /* ECMA-262 3rd Edition 15.9.5.20 */
1080 static HRESULT Date_getMinutes(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1081 jsval_t *r)
1083 DateInstance *date;
1085 TRACE("\n");
1087 if(!(date = date_this(jsthis)))
1088 return JS_E_DATE_EXPECTED;
1090 if(r)
1091 *r = jsval_number(min_from_time(local_time(date->time, date)));
1092 return S_OK;
1095 /* ECMA-262 3rd Edition 15.9.5.21 */
1096 static HRESULT Date_getUTCMinutes(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1097 jsval_t *r)
1099 DateInstance *date;
1101 TRACE("\n");
1103 if(!(date = date_this(jsthis)))
1104 return JS_E_DATE_EXPECTED;
1106 if(r)
1107 *r = jsval_number(min_from_time(date->time));
1108 return S_OK;
1111 /* ECMA-262 3rd Edition 15.9.5.22 */
1112 static HRESULT Date_getSeconds(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
1114 DateInstance *date;
1116 TRACE("\n");
1118 if(!(date = date_this(jsthis)))
1119 return JS_E_DATE_EXPECTED;
1121 if(r)
1122 *r = jsval_number(sec_from_time(local_time(date->time, date)));
1123 return S_OK;
1126 /* ECMA-262 3rd Edition 15.9.5.23 */
1127 static HRESULT Date_getUTCSeconds(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1128 jsval_t *r)
1130 DateInstance *date;
1132 TRACE("\n");
1134 if(!(date = date_this(jsthis)))
1135 return JS_E_DATE_EXPECTED;
1137 if(r)
1138 *r = jsval_number(sec_from_time(date->time));
1139 return S_OK;
1142 /* ECMA-262 3rd Edition 15.9.5.24 */
1143 static HRESULT Date_getMilliseconds(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1144 jsval_t *r)
1146 DateInstance *date;
1148 TRACE("\n");
1150 if(!(date = date_this(jsthis)))
1151 return JS_E_DATE_EXPECTED;
1153 if(r)
1154 *r = jsval_number(ms_from_time(local_time(date->time, date)));
1155 return S_OK;
1158 /* ECMA-262 3rd Edition 15.9.5.25 */
1159 static HRESULT Date_getUTCMilliseconds(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1160 jsval_t *r)
1162 DateInstance *date;
1164 TRACE("\n");
1166 if(!(date = date_this(jsthis)))
1167 return JS_E_DATE_EXPECTED;
1169 if(r)
1170 *r = jsval_number(ms_from_time(date->time));
1171 return S_OK;
1174 /* ECMA-262 3rd Edition 15.9.5.26 */
1175 static HRESULT Date_getTimezoneOffset(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1176 jsval_t *r)
1178 DateInstance *date;
1180 TRACE("\n");
1182 if(!(date = date_this(jsthis)))
1183 return JS_E_DATE_EXPECTED;
1185 if(r)
1186 *r = jsval_number(floor((date->time-local_time(date->time, date))/MS_PER_MINUTE));
1187 return S_OK;
1190 /* ECMA-262 3rd Edition 15.9.5.27 */
1191 static HRESULT Date_setTime(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1192 jsval_t *r)
1194 double n;
1195 HRESULT hres;
1196 DateInstance *date;
1198 TRACE("\n");
1200 if(!(date = date_this(jsthis)))
1201 return JS_E_DATE_EXPECTED;
1203 if(!argc)
1204 return JS_E_MISSING_ARG;
1206 hres = to_number(ctx, argv[0], &n);
1207 if(FAILED(hres))
1208 return hres;
1210 date->time = time_clip(n);
1212 if(r)
1213 *r = jsval_number(date->time);
1214 return S_OK;
1217 /* ECMA-262 3rd Edition 15.9.5.28 */
1218 static HRESULT Date_setMilliseconds(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1219 jsval_t *r)
1221 DateInstance *date;
1222 double n, t;
1223 HRESULT hres;
1225 TRACE("\n");
1227 if(!(date = date_this(jsthis)))
1228 return JS_E_DATE_EXPECTED;
1230 if(!argc)
1231 return JS_E_MISSING_ARG;
1233 hres = to_number(ctx, argv[0], &n);
1234 if(FAILED(hres))
1235 return hres;
1237 t = local_time(date->time, date);
1238 t = make_date(day(t), make_time(hour_from_time(t), min_from_time(t),
1239 sec_from_time(t), n));
1240 date->time = time_clip(utc(t, date));
1242 if(r)
1243 *r = jsval_number(date->time);
1244 return S_OK;
1247 /* ECMA-262 3rd Edition 15.9.5.29 */
1248 static HRESULT Date_setUTCMilliseconds(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1249 jsval_t *r)
1251 DateInstance *date;
1252 double n, t;
1253 HRESULT hres;
1255 TRACE("\n");
1257 if(!(date = date_this(jsthis)))
1258 return JS_E_DATE_EXPECTED;
1260 if(!argc)
1261 return JS_E_MISSING_ARG;
1263 hres = to_number(ctx, argv[0], &n);
1264 if(FAILED(hres))
1265 return hres;
1267 t = date->time;
1268 t = make_date(day(t), make_time(hour_from_time(t), min_from_time(t),
1269 sec_from_time(t), n));
1270 date->time = time_clip(t);
1272 if(r)
1273 *r = jsval_number(date->time);
1274 return S_OK;
1277 /* ECMA-262 3rd Edition 15.9.5.30 */
1278 static HRESULT Date_setSeconds(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1279 jsval_t *r)
1281 DateInstance *date;
1282 double t, sec, ms;
1283 HRESULT hres;
1285 TRACE("\n");
1287 if(!(date = date_this(jsthis)))
1288 return JS_E_DATE_EXPECTED;
1290 if(!argc)
1291 return JS_E_MISSING_ARG;
1293 t = local_time(date->time, date);
1295 hres = to_number(ctx, argv[0], &sec);
1296 if(FAILED(hres))
1297 return hres;
1299 if(argc > 1) {
1300 hres = to_number(ctx, argv[1], &ms);
1301 if(FAILED(hres))
1302 return hres;
1303 }else {
1304 ms = ms_from_time(t);
1307 t = make_date(day(t), make_time(hour_from_time(t),
1308 min_from_time(t), sec, ms));
1309 date->time = time_clip(utc(t, date));
1311 if(r)
1312 *r = jsval_number(date->time);
1313 return S_OK;
1316 /* ECMA-262 3rd Edition 15.9.5.31 */
1317 static HRESULT Date_setUTCSeconds(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1318 jsval_t *r)
1320 DateInstance *date;
1321 double t, sec, ms;
1322 HRESULT hres;
1324 TRACE("\n");
1326 if(!(date = date_this(jsthis)))
1327 return JS_E_DATE_EXPECTED;
1329 if(!argc)
1330 return JS_E_MISSING_ARG;
1332 t = date->time;
1334 hres = to_number(ctx, argv[0], &sec);
1335 if(FAILED(hres))
1336 return hres;
1338 if(argc > 1) {
1339 hres = to_number(ctx, argv[1], &ms);
1340 if(FAILED(hres))
1341 return hres;
1342 }else {
1343 ms = ms_from_time(t);
1346 t = make_date(day(t), make_time(hour_from_time(t),
1347 min_from_time(t), sec, ms));
1348 date->time = time_clip(t);
1350 if(r)
1351 *r = jsval_number(date->time);
1352 return S_OK;
1355 /* ECMA-262 3rd Edition 15.9.5.33 */
1356 static HRESULT Date_setMinutes(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1357 jsval_t *r)
1359 DateInstance *date;
1360 double t, min, sec, ms;
1361 HRESULT hres;
1363 TRACE("\n");
1365 if(!(date = date_this(jsthis)))
1366 return JS_E_DATE_EXPECTED;
1368 if(!argc)
1369 return JS_E_MISSING_ARG;
1371 t = local_time(date->time, date);
1373 hres = to_number(ctx, argv[0], &min);
1374 if(FAILED(hres))
1375 return hres;
1377 if(argc > 1) {
1378 hres = to_number(ctx, argv[1], &sec);
1379 if(FAILED(hres))
1380 return hres;
1381 }else {
1382 sec = sec_from_time(t);
1385 if(argc > 2) {
1386 hres = to_number(ctx, argv[2], &ms);
1387 if(FAILED(hres))
1388 return hres;
1389 }else {
1390 ms = ms_from_time(t);
1393 t = make_date(day(t), make_time(hour_from_time(t),
1394 min, sec, ms));
1395 date->time = time_clip(utc(t, date));
1397 if(r)
1398 *r = jsval_number(date->time);
1399 return S_OK;
1402 /* ECMA-262 3rd Edition 15.9.5.34 */
1403 static HRESULT Date_setUTCMinutes(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1404 jsval_t *r)
1406 DateInstance *date;
1407 double t, min, sec, ms;
1408 HRESULT hres;
1410 TRACE("\n");
1412 if(!(date = date_this(jsthis)))
1413 return JS_E_DATE_EXPECTED;
1415 if(!argc)
1416 return JS_E_MISSING_ARG;
1418 t = date->time;
1420 hres = to_number(ctx, argv[0], &min);
1421 if(FAILED(hres))
1422 return hres;
1424 if(argc > 1) {
1425 hres = to_number(ctx, argv[1], &sec);
1426 if(FAILED(hres))
1427 return hres;
1428 }else {
1429 sec = sec_from_time(t);
1432 if(argc > 2) {
1433 hres = to_number(ctx, argv[2], &ms);
1434 if(FAILED(hres))
1435 return hres;
1436 }else {
1437 ms = ms_from_time(t);
1440 t = make_date(day(t), make_time(hour_from_time(t),
1441 min, sec, ms));
1442 date->time = time_clip(t);
1444 if(r)
1445 *r = jsval_number(date->time);
1446 return S_OK;
1449 /* ECMA-262 3rd Edition 15.9.5.35 */
1450 static HRESULT Date_setHours(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1451 jsval_t *r)
1453 DateInstance *date;
1454 double t, hour, min, sec, ms;
1455 HRESULT hres;
1457 TRACE("\n");
1459 if(!(date = date_this(jsthis)))
1460 return JS_E_DATE_EXPECTED;
1462 if(!argc)
1463 return JS_E_MISSING_ARG;
1465 t = local_time(date->time, date);
1467 hres = to_number(ctx, argv[0], &hour);
1468 if(FAILED(hres))
1469 return hres;
1471 if(argc > 1) {
1472 hres = to_number(ctx, argv[1], &min);
1473 if(FAILED(hres))
1474 return hres;
1475 }else {
1476 min = min_from_time(t);
1479 if(argc > 2) {
1480 hres = to_number(ctx, argv[2], &sec);
1481 if(FAILED(hres))
1482 return hres;
1483 }else {
1484 sec = sec_from_time(t);
1487 if(argc > 3) {
1488 hres = to_number(ctx, argv[3], &ms);
1489 if(FAILED(hres))
1490 return hres;
1491 }else {
1492 ms = ms_from_time(t);
1495 t = make_date(day(t), make_time(hour, min, sec, ms));
1496 date->time = time_clip(utc(t, date));
1498 if(r)
1499 *r = jsval_number(date->time);
1500 return S_OK;
1503 /* ECMA-262 3rd Edition 15.9.5.36 */
1504 static HRESULT Date_setUTCHours(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1505 jsval_t *r)
1507 DateInstance *date;
1508 double t, hour, min, sec, ms;
1509 HRESULT hres;
1511 TRACE("\n");
1513 if(!(date = date_this(jsthis)))
1514 return JS_E_DATE_EXPECTED;
1516 if(!argc)
1517 return JS_E_MISSING_ARG;
1519 t = date->time;
1521 hres = to_number(ctx, argv[0], &hour);
1522 if(FAILED(hres))
1523 return hres;
1525 if(argc > 1) {
1526 hres = to_number(ctx, argv[1], &min);
1527 if(FAILED(hres))
1528 return hres;
1529 }else {
1530 min = min_from_time(t);
1533 if(argc > 2) {
1534 hres = to_number(ctx, argv[2], &sec);
1535 if(FAILED(hres))
1536 return hres;
1537 }else {
1538 sec = sec_from_time(t);
1541 if(argc > 3) {
1542 hres = to_number(ctx, argv[3], &ms);
1543 if(FAILED(hres))
1544 return hres;
1545 }else {
1546 ms = ms_from_time(t);
1549 t = make_date(day(t), make_time(hour, min, sec, ms));
1550 date->time = time_clip(t);
1552 if(r)
1553 *r = jsval_number(date->time);
1554 return S_OK;
1557 /* ECMA-262 3rd Edition 15.9.5.36 */
1558 static HRESULT Date_setDate(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1559 jsval_t *r)
1561 DateInstance *date;
1562 double t, n;
1563 HRESULT hres;
1565 TRACE("\n");
1567 if(!(date = date_this(jsthis)))
1568 return JS_E_DATE_EXPECTED;
1570 if(!argc)
1571 return JS_E_MISSING_ARG;
1573 hres = to_number(ctx, argv[0], &n);
1574 if(FAILED(hres))
1575 return hres;
1577 t = local_time(date->time, date);
1578 t = make_date(make_day(year_from_time(t), month_from_time(t), n), time_within_day(t));
1579 date->time = time_clip(utc(t, date));
1581 if(r)
1582 *r = jsval_number(date->time);
1583 return S_OK;
1586 /* ECMA-262 3rd Edition 15.9.5.37 */
1587 static HRESULT Date_setUTCDate(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1588 jsval_t *r)
1590 DateInstance *date;
1591 double t, n;
1592 HRESULT hres;
1594 TRACE("\n");
1596 if(!(date = date_this(jsthis)))
1597 return JS_E_DATE_EXPECTED;
1599 if(!argc)
1600 return JS_E_MISSING_ARG;
1602 hres = to_number(ctx, argv[0], &n);
1603 if(FAILED(hres))
1604 return hres;
1606 t = date->time;
1607 t = make_date(make_day(year_from_time(t), month_from_time(t), n), time_within_day(t));
1608 date->time = time_clip(t);
1610 if(r)
1611 *r = jsval_number(date->time);
1612 return S_OK;
1615 /* ECMA-262 3rd Edition 15.9.5.38 */
1616 static HRESULT Date_setMonth(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1617 jsval_t *r)
1619 DateInstance *date;
1620 DOUBLE t, month, ddate;
1621 HRESULT hres;
1623 TRACE("\n");
1625 if(!(date = date_this(jsthis)))
1626 return JS_E_DATE_EXPECTED;
1628 if(!argc)
1629 return JS_E_MISSING_ARG;
1631 t = local_time(date->time, date);
1633 hres = to_number(ctx, argv[0], &month);
1634 if(FAILED(hres))
1635 return hres;
1637 if(argc > 1) {
1638 hres = to_number(ctx, argv[1], &ddate);
1639 if(FAILED(hres))
1640 return hres;
1641 }else {
1642 ddate = date_from_time(t);
1645 t = make_date(make_day(year_from_time(t), month, ddate),
1646 time_within_day(t));
1647 date->time = time_clip(utc(t, date));
1649 if(r)
1650 *r = jsval_number(date->time);
1651 return S_OK;
1654 /* ECMA-262 3rd Edition 15.9.5.39 */
1655 static HRESULT Date_setUTCMonth(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1656 jsval_t *r)
1658 DateInstance *date;
1659 double t, month, ddate;
1660 HRESULT hres;
1662 TRACE("\n");
1664 if(!(date = date_this(jsthis)))
1665 return JS_E_DATE_EXPECTED;
1667 if(!argc)
1668 return JS_E_MISSING_ARG;
1670 t = date->time;
1672 hres = to_number(ctx, argv[0], &month);
1673 if(FAILED(hres))
1674 return hres;
1676 if(argc > 1) {
1677 hres = to_number(ctx, argv[1], &ddate);
1678 if(FAILED(hres))
1679 return hres;
1680 }else {
1681 ddate = date_from_time(t);
1684 t = make_date(make_day(year_from_time(t), month, ddate),
1685 time_within_day(t));
1686 date->time = time_clip(t);
1688 if(r)
1689 *r = jsval_number(date->time);
1690 return S_OK;
1693 /* ECMA-262 3rd Edition 15.9.5.40 */
1694 static HRESULT Date_setFullYear(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1695 jsval_t *r)
1697 DateInstance *date;
1698 double t, year, month, ddate;
1699 HRESULT hres;
1701 TRACE("\n");
1703 if(!(date = date_this(jsthis)))
1704 return JS_E_DATE_EXPECTED;
1706 if(!argc)
1707 return JS_E_MISSING_ARG;
1709 t = local_time(date->time, date);
1711 hres = to_number(ctx, argv[0], &year);
1712 if(FAILED(hres))
1713 return hres;
1715 if(argc > 1) {
1716 hres = to_number(ctx, argv[1], &month);
1717 if(FAILED(hres))
1718 return hres;
1719 }else {
1720 month = month_from_time(t);
1723 if(argc > 2) {
1724 hres = to_number(ctx, argv[2], &ddate);
1725 if(FAILED(hres))
1726 return hres;
1727 }else {
1728 ddate = date_from_time(t);
1731 t = make_date(make_day(year, month, ddate), time_within_day(t));
1732 date->time = time_clip(utc(t, date));
1734 if(r)
1735 *r = jsval_number(date->time);
1736 return S_OK;
1739 /* ECMA-262 3rd Edition 15.9.5.41 */
1740 static HRESULT Date_setUTCFullYear(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1741 jsval_t *r)
1743 DateInstance *date;
1744 double t, year, month, ddate;
1745 HRESULT hres;
1747 TRACE("\n");
1749 if(!(date = date_this(jsthis)))
1750 return JS_E_DATE_EXPECTED;
1752 if(!argc)
1753 return JS_E_MISSING_ARG;
1755 t = date->time;
1757 hres = to_number(ctx, argv[0], &year);
1758 if(FAILED(hres))
1759 return hres;
1761 if(argc > 1) {
1762 hres = to_number(ctx, argv[1], &month);
1763 if(FAILED(hres))
1764 return hres;
1765 }else {
1766 month = month_from_time(t);
1769 if(argc > 2) {
1770 hres = to_number(ctx, argv[2], &ddate);
1771 if(FAILED(hres))
1772 return hres;
1773 }else {
1774 ddate = date_from_time(t);
1777 t = make_date(make_day(year, month, ddate), time_within_day(t));
1778 date->time = time_clip(t);
1780 if(r)
1781 *r = jsval_number(date->time);
1782 return S_OK;
1785 /* ECMA-262 3rd Edition B2.4 */
1786 static HRESULT Date_getYear(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1787 jsval_t *r)
1789 DateInstance *date;
1790 DOUBLE t, year;
1792 TRACE("\n");
1794 if(!(date = date_this(jsthis)))
1795 return JS_E_DATE_EXPECTED;
1797 t = local_time(date->time, date);
1798 if(isnan(t)) {
1799 if(r)
1800 *r = jsval_number(NAN);
1801 return S_OK;
1804 year = year_from_time(t);
1805 if(r)
1806 *r = jsval_number((1900<=year && year<2000)?year-1900:year);
1807 return S_OK;
1810 /* ECMA-262 3rd Edition B2.5 */
1811 static HRESULT Date_setYear(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1812 jsval_t *r)
1814 DateInstance *date;
1815 DOUBLE t, year;
1816 HRESULT hres;
1818 TRACE("\n");
1820 if(!(date = date_this(jsthis)))
1821 return JS_E_DATE_EXPECTED;
1823 if(!argc)
1824 return JS_E_MISSING_ARG;
1826 t = local_time(date->time, date);
1828 hres = to_number(ctx, argv[0], &year);
1829 if(FAILED(hres))
1830 return hres;
1832 if(isnan(year)) {
1833 date->time = year;
1834 if(r)
1835 *r = jsval_number(NAN);
1836 return S_OK;
1839 year = year >= 0.0 ? floor(year) : -floor(-year);
1840 if(-1.0 < year && year < 100.0)
1841 year += 1900.0;
1843 date->time = time_clip(utc(make_date(make_day(year, month_from_time(t), date_from_time(t)), time_within_day(t)), date));
1845 if(r)
1846 *r = jsval_number(date->time);
1847 return S_OK;
1850 static HRESULT Date_get_value(script_ctx_t *ctx, jsdisp_t *jsthis, jsval_t *r)
1852 TRACE("\n");
1854 return dateobj_to_string(date_from_jsdisp(jsthis), r);
1857 static const builtin_prop_t Date_props[] = {
1858 {L"getDate", Date_getDate, PROPF_METHOD},
1859 {L"getDay", Date_getDay, PROPF_METHOD},
1860 {L"getFullYear", Date_getFullYear, PROPF_METHOD},
1861 {L"getHours", Date_getHours, PROPF_METHOD},
1862 {L"getMilliseconds", Date_getMilliseconds, PROPF_METHOD},
1863 {L"getMinutes", Date_getMinutes, PROPF_METHOD},
1864 {L"getMonth", Date_getMonth, PROPF_METHOD},
1865 {L"getSeconds", Date_getSeconds, PROPF_METHOD},
1866 {L"getTime", Date_getTime, PROPF_METHOD},
1867 {L"getTimezoneOffset", Date_getTimezoneOffset, PROPF_METHOD},
1868 {L"getUTCDate", Date_getUTCDate, PROPF_METHOD},
1869 {L"getUTCDay", Date_getUTCDay, PROPF_METHOD},
1870 {L"getUTCFullYear", Date_getUTCFullYear, PROPF_METHOD},
1871 {L"getUTCHours", Date_getUTCHours, PROPF_METHOD},
1872 {L"getUTCMilliseconds", Date_getUTCMilliseconds, PROPF_METHOD},
1873 {L"getUTCMinutes", Date_getUTCMinutes, PROPF_METHOD},
1874 {L"getUTCMonth", Date_getUTCMonth, PROPF_METHOD},
1875 {L"getUTCSeconds", Date_getUTCSeconds, PROPF_METHOD},
1876 {L"getYear", Date_getYear, PROPF_METHOD},
1877 {L"setDate", Date_setDate, PROPF_METHOD|1},
1878 {L"setFullYear", Date_setFullYear, PROPF_METHOD|3},
1879 {L"setHours", Date_setHours, PROPF_METHOD|4},
1880 {L"setMilliseconds", Date_setMilliseconds, PROPF_METHOD|1},
1881 {L"setMinutes", Date_setMinutes, PROPF_METHOD|3},
1882 {L"setMonth", Date_setMonth, PROPF_METHOD|2},
1883 {L"setSeconds", Date_setSeconds, PROPF_METHOD|2},
1884 {L"setTime", Date_setTime, PROPF_METHOD|1},
1885 {L"setUTCDate", Date_setUTCDate, PROPF_METHOD|1},
1886 {L"setUTCFullYear", Date_setUTCFullYear, PROPF_METHOD|3},
1887 {L"setUTCHours", Date_setUTCHours, PROPF_METHOD|4},
1888 {L"setUTCMilliseconds", Date_setUTCMilliseconds, PROPF_METHOD|1},
1889 {L"setUTCMinutes", Date_setUTCMinutes, PROPF_METHOD|3},
1890 {L"setUTCMonth", Date_setUTCMonth, PROPF_METHOD|2},
1891 {L"setUTCSeconds", Date_setUTCSeconds, PROPF_METHOD|2},
1892 {L"setYear", Date_setYear, PROPF_METHOD|1},
1893 {L"toDateString", Date_toDateString, PROPF_METHOD},
1894 {L"toGMTString", Date_toGMTString, PROPF_METHOD},
1895 {L"toISOString", Date_toISOString, PROPF_METHOD|PROPF_ES5},
1896 {L"toLocaleDateString", Date_toLocaleDateString, PROPF_METHOD},
1897 {L"toLocaleString", Date_toLocaleString, PROPF_METHOD},
1898 {L"toLocaleTimeString", Date_toLocaleTimeString, PROPF_METHOD},
1899 {L"toString", Date_toString, PROPF_METHOD},
1900 {L"toTimeString", Date_toTimeString, PROPF_METHOD},
1901 {L"toUTCString", Date_toUTCString, PROPF_METHOD},
1902 {L"valueOf", Date_valueOf, PROPF_METHOD},
1905 static const builtin_info_t Date_info = {
1906 JSCLASS_DATE,
1907 {NULL, NULL,0, Date_get_value},
1908 ARRAY_SIZE(Date_props),
1909 Date_props,
1910 NULL,
1911 NULL
1914 static const builtin_info_t DateInst_info = {
1915 JSCLASS_DATE,
1916 {NULL, NULL,0, Date_get_value},
1917 0, NULL,
1918 NULL,
1919 NULL
1922 static HRESULT create_date(script_ctx_t *ctx, jsdisp_t *object_prototype, DOUBLE time, jsdisp_t **ret)
1924 DateInstance *date;
1925 HRESULT hres;
1926 TIME_ZONE_INFORMATION tzi;
1928 GetTimeZoneInformation(&tzi);
1930 date = heap_alloc_zero(sizeof(DateInstance));
1931 if(!date)
1932 return E_OUTOFMEMORY;
1934 if(object_prototype)
1935 hres = init_dispex(&date->dispex, ctx, &Date_info, object_prototype);
1936 else
1937 hres = init_dispex_from_constr(&date->dispex, ctx, &DateInst_info, ctx->date_constr);
1938 if(FAILED(hres)) {
1939 heap_free(date);
1940 return hres;
1943 date->time = time;
1944 date->bias = tzi.Bias;
1945 date->standardDate = tzi.StandardDate;
1946 date->standardBias = tzi.StandardBias;
1947 date->daylightDate = tzi.DaylightDate;
1948 date->daylightBias = tzi.DaylightBias;
1950 *ret = &date->dispex;
1951 return S_OK;
1954 static inline HRESULT date_parse(jsstr_t *input_str, double *ret) {
1955 static const DWORD string_ids[] = { LOCALE_SMONTHNAME12, LOCALE_SMONTHNAME11,
1956 LOCALE_SMONTHNAME10, LOCALE_SMONTHNAME9, LOCALE_SMONTHNAME8,
1957 LOCALE_SMONTHNAME7, LOCALE_SMONTHNAME6, LOCALE_SMONTHNAME5,
1958 LOCALE_SMONTHNAME4, LOCALE_SMONTHNAME3, LOCALE_SMONTHNAME2,
1959 LOCALE_SMONTHNAME1, LOCALE_SDAYNAME7, LOCALE_SDAYNAME1,
1960 LOCALE_SDAYNAME2, LOCALE_SDAYNAME3, LOCALE_SDAYNAME4,
1961 LOCALE_SDAYNAME5, LOCALE_SDAYNAME6 };
1962 WCHAR *strings[ARRAY_SIZE(string_ids)];
1963 WCHAR *parse;
1964 int input_len, parse_len = 0, nest_level = 0, i, size;
1965 int year = 0, month = 0, day = 0, hour = 0, min = 0, sec = 0;
1966 int ms = 0, offset = 0, hour_adjust = 0;
1967 BOOL set_year = FALSE, set_month = FALSE, set_day = FALSE, set_hour = FALSE;
1968 BOOL set_offset = FALSE, set_era = FALSE, ad = TRUE, set_am = FALSE, am = TRUE;
1969 BOOL set_hour_adjust = TRUE;
1970 TIME_ZONE_INFORMATION tzi;
1971 const WCHAR *input;
1972 DateInstance di;
1973 DWORD lcid_en;
1975 input_len = jsstr_length(input_str);
1976 input = jsstr_flatten(input_str);
1977 if(!input)
1978 return E_OUTOFMEMORY;
1980 for(i=0; i<input_len; i++) {
1981 if(input[i] == '(') nest_level++;
1982 else if(input[i] == ')') {
1983 nest_level--;
1984 if(nest_level<0) {
1985 *ret = NAN;
1986 return S_OK;
1989 else if(!nest_level) parse_len++;
1992 parse = heap_alloc((parse_len+1)*sizeof(WCHAR));
1993 if(!parse)
1994 return E_OUTOFMEMORY;
1995 nest_level = 0;
1996 parse_len = 0;
1997 for(i=0; i<input_len; i++) {
1998 if(input[i] == '(') nest_level++;
1999 else if(input[i] == ')') nest_level--;
2000 else if(!nest_level) parse[parse_len++] = towupper(input[i]);
2002 parse[parse_len] = 0;
2004 GetTimeZoneInformation(&tzi);
2005 di.bias = tzi.Bias;
2006 di.standardDate = tzi.StandardDate;
2007 di.standardBias = tzi.StandardBias;
2008 di.daylightDate = tzi.DaylightDate;
2009 di.daylightBias = tzi.DaylightBias;
2011 /* FIXME: Cache strings */
2012 lcid_en = MAKELCID(MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),SORT_DEFAULT);
2013 for(i=0; i<ARRAY_SIZE(string_ids); i++) {
2014 size = GetLocaleInfoW(lcid_en, string_ids[i], NULL, 0);
2015 strings[i] = heap_alloc((size+1)*sizeof(WCHAR));
2016 if(!strings[i]) {
2017 i--;
2018 while(i-- >= 0)
2019 heap_free(strings[i]);
2020 heap_free(parse);
2021 return E_OUTOFMEMORY;
2023 GetLocaleInfoW(lcid_en, string_ids[i], strings[i], size);
2026 for(i=0; i<parse_len;) {
2027 while(iswspace(parse[i])) i++;
2028 if(parse[i] == ',') {
2029 while(parse[i] == ',') i++;
2030 continue;
2033 if(parse[i]>='0' && parse[i]<='9') {
2034 int tmp = wcstol(&parse[i], NULL, 10);
2035 while(parse[i]>='0' && parse[i]<='9') i++;
2036 while(iswspace(parse[i])) i++;
2038 if(parse[i] == ':') {
2039 /* Time */
2040 if(set_hour) break;
2041 set_hour = TRUE;
2043 hour = tmp;
2045 while(parse[i] == ':') i++;
2046 while(iswspace(parse[i])) i++;
2047 if(parse[i]>='0' && parse[i]<='9') {
2048 min = wcstol(&parse[i], NULL, 10);
2049 while(parse[i]>='0' && parse[i]<='9') i++;
2052 while(iswspace(parse[i])) i++;
2053 while(parse[i] == ':') i++;
2054 while(iswspace(parse[i])) i++;
2055 if(parse[i]>='0' && parse[i]<='9') {
2056 sec = wcstol(&parse[i], NULL, 10);
2057 while(parse[i]>='0' && parse[i]<='9') i++;
2060 else if(parse[i]=='-' || parse[i]=='/') {
2061 /* Short or long date */
2062 if(set_day || set_month || set_year) break;
2063 set_day = TRUE;
2064 set_month = TRUE;
2065 set_year = TRUE;
2067 month = tmp-1;
2069 while(iswspace(parse[i])) i++;
2070 while(parse[i]=='-' || parse[i]=='/') i++;
2071 while(iswspace(parse[i])) i++;
2072 if(parse[i]<'0' || parse[i]>'9') break;
2073 day = wcstol(&parse[i], NULL, 10);
2074 while(parse[i]>='0' && parse[i]<='9') i++;
2076 while(parse[i]=='-' || parse[i]=='/') i++;
2077 while(iswspace(parse[i])) i++;
2078 if(parse[i]<'0' || parse[i]>'9') break;
2079 year = wcstol(&parse[i], NULL, 10);
2080 while(parse[i]>='0' && parse[i]<='9') i++;
2082 if(tmp >= 70){
2083 /* long date */
2084 month = day - 1;
2085 day = year;
2086 year = tmp;
2089 else if(tmp<0) break;
2090 else if(tmp<70) {
2091 /* Day */
2092 if(set_day) break;
2093 set_day = TRUE;
2094 day = tmp;
2096 else {
2097 /* Year */
2098 if(set_year) break;
2099 set_year = TRUE;
2100 year = tmp;
2103 else if(parse[i]=='+' || parse[i]=='-') {
2104 /* Timezone offset */
2105 BOOL positive = TRUE;
2107 if(set_offset && set_hour_adjust) break;
2108 set_offset = TRUE;
2109 set_hour_adjust = FALSE;
2111 if(parse[i] == '-') positive = FALSE;
2113 i++;
2114 while(iswspace(parse[i])) i++;
2115 if(parse[i]<'0' || parse[i]>'9') break;
2116 offset = wcstol(&parse[i], NULL, 10);
2117 while(parse[i]>='0' && parse[i]<='9') i++;
2119 if(offset<24) offset *= 60;
2120 else offset = (offset/100)*60 + offset%100;
2122 if(positive) offset = -offset;
2125 else {
2126 if(parse[i]<'A' || parse[i]>'Z') break;
2127 else if(parse[i]=='B' && (parse[i+1]=='C' ||
2128 (parse[i+1]=='.' && parse[i+2]=='C'))) {
2129 /* AD/BC */
2130 if(set_era) break;
2131 set_era = TRUE;
2132 ad = FALSE;
2134 i++;
2135 if(parse[i] == '.') i++;
2136 i++;
2137 if(parse[i] == '.') i++;
2139 else if(parse[i]=='A' && (parse[i+1]=='D' ||
2140 (parse[i+1]=='.' && parse[i+2]=='D'))) {
2141 /* AD/BC */
2142 if(set_era) break;
2143 set_era = TRUE;
2145 i++;
2146 if(parse[i] == '.') i++;
2147 i++;
2148 if(parse[i] == '.') i++;
2150 else if(parse[i+1]<'A' || parse[i+1]>'Z') {
2151 /* Timezone */
2152 if(set_offset) break;
2153 set_offset = TRUE;
2155 if(parse[i] <= 'I') hour_adjust = parse[i]-'A'+2;
2156 else if(parse[i] == 'J') break;
2157 else if(parse[i] <= 'M') hour_adjust = parse[i]-'K'+11;
2158 else if(parse[i] <= 'Y') hour_adjust = parse[i]-'N';
2159 else hour_adjust = 1;
2161 i++;
2162 if(parse[i] == '.') i++;
2164 else if(parse[i]=='A' && parse[i+1]=='M') {
2165 /* AM/PM */
2166 if(set_am) break;
2167 set_am = TRUE;
2168 am = TRUE;
2169 i += 2;
2171 else if(parse[i]=='P' && parse[i+1]=='M') {
2172 /* AM/PM */
2173 if(set_am) break;
2174 set_am = TRUE;
2175 am = FALSE;
2176 i += 2;
2178 else if((parse[i]=='U' && parse[i+1]=='T' && parse[i+2]=='C')
2179 || (parse[i]=='G' && parse[i+1]=='M' && parse[i+2]=='T')) {
2180 /* Timezone */
2181 if(set_offset) break;
2182 set_offset = TRUE;
2183 set_hour_adjust = FALSE;
2185 i += 3;
2187 else {
2188 /* Month or garbage */
2189 unsigned int j;
2191 for(size=i; parse[size]>='A' && parse[size]<='Z'; size++);
2192 size -= i;
2194 for(j=0; j<ARRAY_SIZE(string_ids); j++)
2195 if(!wcsnicmp(&parse[i], strings[j], size)) break;
2197 if(j < 12) {
2198 if(set_month) break;
2199 set_month = TRUE;
2200 month = 11-j;
2202 else if(j == ARRAY_SIZE(string_ids)) break;
2204 i += size;
2209 if(i == parse_len && set_year && set_month && set_day && (!set_am || hour<13)) {
2210 if(set_am) {
2211 if(hour == 12) hour = 0;
2212 if(!am) hour += 12;
2215 if(!ad) year = -year+1;
2216 else if(year<100) year += 1900;
2218 *ret = time_clip(make_date(make_day(year, month, day),
2219 make_time(hour+hour_adjust, min, sec, ms)) + offset*MS_PER_MINUTE);
2221 if(set_hour_adjust)
2222 *ret = utc(*ret, &di);
2223 }else {
2224 *ret = NAN;
2227 for(i=0; i<ARRAY_SIZE(string_ids); i++)
2228 heap_free(strings[i]);
2229 heap_free(parse);
2231 return S_OK;
2234 static HRESULT DateConstr_parse(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
2235 jsval_t *r)
2237 jsstr_t *parse_str;
2238 double n;
2239 HRESULT hres;
2241 TRACE("\n");
2243 if(!argc) {
2244 if(r)
2245 *r = jsval_number(NAN);
2246 return S_OK;
2249 hres = to_string(ctx, argv[0], &parse_str);
2250 if(FAILED(hres))
2251 return hres;
2253 hres = date_parse(parse_str, &n);
2254 jsstr_release(parse_str);
2255 if(FAILED(hres))
2256 return hres;
2258 *r = jsval_number(n);
2259 return S_OK;
2262 static HRESULT date_utc(script_ctx_t *ctx, unsigned argc, jsval_t *argv, double *ret)
2264 double year, month, vdate, hours, minutes, seconds, ms;
2265 HRESULT hres;
2267 TRACE("\n");
2269 if(argc) {
2270 hres = to_number(ctx, argv[0], &year);
2271 if(FAILED(hres))
2272 return hres;
2273 if(0 <= year && year <= 99)
2274 year += 1900;
2275 }else {
2276 year = 1900;
2279 if(argc>1) {
2280 hres = to_number(ctx, argv[1], &month);
2281 if(FAILED(hres))
2282 return hres;
2283 }else {
2284 month = 0;
2287 if(argc>2) {
2288 hres = to_number(ctx, argv[2], &vdate);
2289 if(FAILED(hres))
2290 return hres;
2291 }else {
2292 vdate = 1;
2295 if(argc>3) {
2296 hres = to_number(ctx, argv[3], &hours);
2297 if(FAILED(hres))
2298 return hres;
2299 }else {
2300 hours = 0;
2303 if(argc>4) {
2304 hres = to_number(ctx, argv[4], &minutes);
2305 if(FAILED(hres))
2306 return hres;
2307 }else {
2308 minutes = 0;
2311 if(argc>5) {
2312 hres = to_number(ctx, argv[5], &seconds);
2313 if(FAILED(hres))
2314 return hres;
2315 }else {
2316 seconds = 0;
2319 if(argc>6) {
2320 hres = to_number(ctx, argv[6], &ms);
2321 if(FAILED(hres))
2322 return hres;
2323 } else {
2324 ms = 0;
2327 *ret = time_clip(make_date(make_day(year, month, vdate),
2328 make_time(hours, minutes,seconds, ms)));
2329 return S_OK;
2332 static HRESULT DateConstr_UTC(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
2333 jsval_t *r)
2335 double n;
2336 HRESULT hres;
2338 TRACE("\n");
2340 hres = date_utc(ctx, argc, argv, &n);
2341 if(SUCCEEDED(hres) && r)
2342 *r = jsval_number(n);
2343 return hres;
2346 /* ECMA-262 5.1 Edition 15.9.4.4 */
2347 static HRESULT DateConstr_now(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
2349 TRACE("\n");
2351 if(r) *r = jsval_number(date_now());
2352 return S_OK;
2355 static HRESULT DateConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
2356 jsval_t *r)
2358 jsdisp_t *date;
2359 HRESULT hres;
2361 TRACE("\n");
2363 switch(flags) {
2364 case DISPATCH_CONSTRUCT:
2365 switch(argc) {
2366 /* ECMA-262 3rd Edition 15.9.3.3 */
2367 case 0:
2368 hres = create_date(ctx, NULL, date_now(), &date);
2369 if(FAILED(hres))
2370 return hres;
2371 break;
2373 /* ECMA-262 3rd Edition 15.9.3.2 */
2374 case 1: {
2375 jsval_t prim;
2376 double n;
2378 hres = to_primitive(ctx, argv[0], &prim, NO_HINT);
2379 if(FAILED(hres))
2380 return hres;
2382 if(is_string(prim))
2383 hres = date_parse(get_string(prim), &n);
2384 else
2385 hres = to_number(ctx, prim, &n);
2387 jsval_release(prim);
2388 if(FAILED(hres))
2389 return hres;
2391 hres = create_date(ctx, NULL, time_clip(n), &date);
2392 if(FAILED(hres))
2393 return hres;
2394 break;
2397 /* ECMA-262 3rd Edition 15.9.3.1 */
2398 default: {
2399 double ret_date;
2400 DateInstance *di;
2402 hres = date_utc(ctx, argc, argv, &ret_date);
2403 if(FAILED(hres))
2404 return hres;
2406 hres = create_date(ctx, NULL, ret_date, &date);
2407 if(FAILED(hres))
2408 return hres;
2410 di = date_from_jsdisp(date);
2411 di->time = utc(di->time, di);
2415 *r = jsval_obj(date);
2416 return S_OK;
2418 case INVOKE_FUNC: {
2419 FILETIME system_time, local_time;
2420 LONGLONG lltime;
2422 GetSystemTimeAsFileTime(&system_time);
2423 FileTimeToLocalFileTime(&system_time, &local_time);
2424 lltime = ((LONGLONG)local_time.dwHighDateTime<<32)
2425 + local_time.dwLowDateTime;
2427 return date_to_string(lltime/10000-TIME_EPOCH, FALSE, 0, r);
2430 default:
2431 FIXME("unimplemented flags %x\n", flags);
2432 return E_NOTIMPL;
2435 return S_OK;
2438 static const builtin_prop_t DateConstr_props[] = {
2439 {L"UTC", DateConstr_UTC, PROPF_METHOD},
2440 {L"now", DateConstr_now, PROPF_HTML|PROPF_METHOD},
2441 {L"parse", DateConstr_parse, PROPF_METHOD}
2444 static const builtin_info_t DateConstr_info = {
2445 JSCLASS_FUNCTION,
2446 DEFAULT_FUNCTION_VALUE,
2447 ARRAY_SIZE(DateConstr_props),
2448 DateConstr_props,
2449 NULL,
2450 NULL
2453 HRESULT create_date_constr(script_ctx_t *ctx, jsdisp_t *object_prototype, jsdisp_t **ret)
2455 jsdisp_t *date;
2456 HRESULT hres;
2458 hres = create_date(ctx, object_prototype, 0.0, &date);
2459 if(FAILED(hres))
2460 return hres;
2462 hres = create_builtin_constructor(ctx, DateConstr_value, L"Date", &DateConstr_info,
2463 PROPF_CONSTR|7, date, ret);
2465 jsdisp_release(date);
2466 return hres;