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 "wine/port.h"
28 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
32 /* 1601 to 1970 is 369 years plus 89 leap days */
33 #define TIME_EPOCH ((ULONGLONG)(369 * 365 + 89) * 86400 * 1000)
38 /* ECMA-262 3rd Edition 15.9.1.1 */
42 SYSTEMTIME standardDate;
44 SYSTEMTIME daylightDate;
48 static const WCHAR toStringW[] = {'t','o','S','t','r','i','n','g',0};
49 static const WCHAR toLocaleStringW[] = {'t','o','L','o','c','a','l','e','S','t','r','i','n','g',0};
50 static const WCHAR propertyIsEnumerableW[] =
51 {'p','r','o','p','e','r','t','y','I','s','E','n','u','m','e','r','a','b','l','e',0};
52 static const WCHAR isPrototypeOfW[] = {'i','s','P','r','o','t','o','t','y','p','e','O','f',0};
53 static const WCHAR valueOfW[] = {'v','a','l','u','e','O','f',0};
54 static const WCHAR toUTCStringW[] = {'t','o','U','T','C','S','t','r','i','n','g',0};
55 static const WCHAR toGMTStringW[] = {'t','o','G','M','T','S','t','r','i','n','g',0};
56 static const WCHAR toDateStringW[] = {'t','o','D','a','t','e','S','t','r','i','n','g',0};
57 static const WCHAR toTimeStringW[] = {'t','o','T','i','m','e','S','t','r','i','n','g',0};
58 static const WCHAR toLocaleDateStringW[] = {'t','o','L','o','c','a','l','e','D','a','t','e','S','t','r','i','n','g',0};
59 static const WCHAR toLocaleTimeStringW[] = {'t','o','L','o','c','a','l','e','T','i','m','e','S','t','r','i','n','g',0};
60 static const WCHAR getTimeW[] = {'g','e','t','T','i','m','e',0};
61 static const WCHAR getFullYearW[] = {'g','e','t','F','u','l','l','Y','e','a','r',0};
62 static const WCHAR getUTCFullYearW[] = {'g','e','t','U','T','C','F','u','l','l','Y','e','a','r',0};
63 static const WCHAR getMonthW[] = {'g','e','t','M','o','n','t','h',0};
64 static const WCHAR getUTCMonthW[] = {'g','e','t','U','T','C','M','o','n','t','h',0};
65 static const WCHAR getDateW[] = {'g','e','t','D','a','t','e',0};
66 static const WCHAR getUTCDateW[] = {'g','e','t','U','T','C','D','a','t','e',0};
67 static const WCHAR getDayW[] = {'g','e','t','D','a','y',0};
68 static const WCHAR getUTCDayW[] = {'g','e','t','U','T','C','D','a','y',0};
69 static const WCHAR getHoursW[] = {'g','e','t','H','o','u','r','s',0};
70 static const WCHAR getUTCHoursW[] = {'g','e','t','U','T','C','H','o','u','r','s',0};
71 static const WCHAR getMinutesW[] = {'g','e','t','M','i','n','u','t','e','s',0};
72 static const WCHAR getUTCMinutesW[] = {'g','e','t','U','T','C','M','i','n','u','t','e','s',0};
73 static const WCHAR getSecondsW[] = {'g','e','t','S','e','c','o','n','d','s',0};
74 static const WCHAR getUTCSecondsW[] = {'g','e','t','U','T','C','S','e','c','o','n','d','s',0};
75 static const WCHAR getMillisecondsW[] = {'g','e','t','M','i','l','l','i','s','e','c','o','n','d','s',0};
76 static const WCHAR getUTCMillisecondsW[] = {'g','e','t','U','T','C','M','i','l','l','i','s','e','c','o','n','d','s',0};
77 static const WCHAR getTimezoneOffsetW[] = {'g','e','t','T','i','m','e','z','o','n','e','O','f','f','s','e','t',0};
78 static const WCHAR setTimeW[] = {'s','e','t','T','i','m','e',0};
79 static const WCHAR setMillisecondsW[] = {'s','e','t','M','i','l','l','i','s','e','c','o','n','d','s',0};
80 static const WCHAR setUTCMillisecondsW[] = {'s','e','t','U','T','C','M','i','l','l','i','s','e','c','o','n','d','s',0};
81 static const WCHAR setSecondsW[] = {'s','e','t','S','e','c','o','n','d','s',0};
82 static const WCHAR setUTCSecondsW[] = {'s','e','t','U','T','C','S','e','c','o','n','d','s',0};
83 static const WCHAR setMinutesW[] = {'s','e','t','M','i','n','u','t','e','s',0};
84 static const WCHAR setUTCMinutesW[] = {'s','e','t','U','T','C','M','i','n','u','t','e','s',0};
85 static const WCHAR setHoursW[] = {'s','e','t','H','o','u','r','s',0};
86 static const WCHAR setUTCHoursW[] = {'s','e','t','U','T','C','H','o','u','r','s',0};
87 static const WCHAR setDateW[] = {'s','e','t','D','a','t','e',0};
88 static const WCHAR setUTCDateW[] = {'s','e','t','U','T','C','D','a','t','e',0};
89 static const WCHAR setMonthW[] = {'s','e','t','M','o','n','t','h',0};
90 static const WCHAR setUTCMonthW[] = {'s','e','t','U','T','C','M','o','n','t','h',0};
91 static const WCHAR setFullYearW[] = {'s','e','t','F','u','l','l','Y','e','a','r',0};
92 static const WCHAR setUTCFullYearW[] = {'s','e','t','U','T','C','F','u','l','l','Y','e','a','r',0};
93 static const WCHAR getYearW[] = {'g','e','t','Y','e','a','r',0};
94 static const WCHAR setYearW[] = {'s','e','t','Y','e','a','r',0};
96 static const WCHAR UTCW[] = {'U','T','C',0};
97 static const WCHAR parseW[] = {'p','a','r','s','e',0};
99 static inline DateInstance *date_this(vdisp_t *jsthis)
101 return is_vclass(jsthis, JSCLASS_DATE) ? (DateInstance*)jsthis->u.jsdisp : NULL;
104 /*ECMA-262 3rd Edition 15.9.1.2 */
105 #define MS_PER_DAY 86400000
106 #define MS_PER_HOUR 3600000
107 #define MS_PER_MINUTE 60000
109 /* ECMA-262 3rd Edition 15.9.1.2 */
110 static inline DOUBLE day(DOUBLE time)
112 return floor(time / MS_PER_DAY);
115 /* ECMA-262 3rd Edition 15.9.1.2 */
116 static inline DOUBLE time_within_day(DOUBLE time)
120 ret = fmod(time, MS_PER_DAY);
127 /* ECMA-262 3rd Edition 15.9.1.3 */
128 static inline DOUBLE days_in_year(DOUBLE year)
132 if(year != (int)year)
136 if(y%4 != 0) return 365;
137 if(y%100 != 0) return 366;
138 if(y%400 != 0) return 365;
142 /* ECMA-262 3rd Edition 15.9.1.3 */
143 static inline DOUBLE day_from_year(DOUBLE year)
145 if(year != (int)year)
148 return floor(365.0*(year-1970) + floor((year-1969)/4)
149 - floor((year-1901)/100) + floor((year-1601)/400));
152 static inline int day_from_month(int month, int in_leap_year)
161 return 59+in_leap_year;
163 return 90+in_leap_year;
165 return 120+in_leap_year;
167 return 151+in_leap_year;
169 return 181+in_leap_year;
171 return 212+in_leap_year;
173 return 243+in_leap_year;
175 return 273+in_leap_year;
177 return 304+in_leap_year;
179 return 334+in_leap_year;
183 /* ECMA-262 3rd Edition 15.9.1.3 */
184 static inline DOUBLE time_from_year(DOUBLE year)
186 return MS_PER_DAY*day_from_year(year);
189 /* ECMA-262 3rd Edition 15.9.1.3 */
190 static inline DOUBLE year_from_time(DOUBLE time)
197 y = 1970 + time/365.25/MS_PER_DAY;
199 if(time_from_year(y) > time)
200 while(time_from_year(y) > time) y--;
202 while(time_from_year(y+1)<=time) y++;
207 /* ECMA-262 3rd Edition 15.9.1.3 */
208 static inline int in_leap_year(DOUBLE time)
210 if(days_in_year(year_from_time(time))==366)
215 /* ECMA-262 3rd Edition 15.9.1.4 */
216 static inline int day_within_year(DOUBLE time)
218 return day(time) - day_from_year(year_from_time(time));
221 /* ECMA-262 3rd Edition 15.9.1.4 */
222 static inline DOUBLE month_from_time(DOUBLE time)
224 int ily = in_leap_year(time);
225 int dwy = day_within_year(time);
230 if(0<=dwy && dwy<31) return 0;
231 if(dwy < 59+ily) return 1;
232 if(dwy < 90+ily) return 2;
233 if(dwy < 120+ily) return 3;
234 if(dwy < 151+ily) return 4;
235 if(dwy < 181+ily) return 5;
236 if(dwy < 212+ily) return 6;
237 if(dwy < 243+ily) return 7;
238 if(dwy < 273+ily) return 8;
239 if(dwy < 304+ily) return 9;
240 if(dwy < 334+ily) return 10;
244 /* ECMA-262 3rd Edition 15.9.1.5 */
245 static inline DOUBLE date_from_time(DOUBLE time)
247 int dwy = day_within_year(time);
248 int ily = in_leap_year(time);
249 int mft = month_from_time(time);
254 if(mft==0) return dwy+1;
255 if(mft==1) return dwy-30;
256 if(mft==2) return dwy-58-ily;
257 if(mft==3) return dwy-89-ily;
258 if(mft==4) return dwy-119-ily;
259 if(mft==5) return dwy-150-ily;
260 if(mft==6) return dwy-180-ily;
261 if(mft==7) return dwy-211-ily;
262 if(mft==8) return dwy-242-ily;
263 if(mft==9) return dwy-272-ily;
264 if(mft==10) return dwy-303-ily;
268 /* ECMA-262 3rd Edition 15.9.1.6 */
269 static inline DOUBLE week_day(DOUBLE time)
276 ret = fmod(day(time)+4, 7);
282 static inline DOUBLE convert_time(int year, SYSTEMTIME st)
293 time = time_from_year(year);
294 time += (DOUBLE)day_from_month(st.wMonth-1, in_leap_year(time)) * MS_PER_DAY;
297 set_week_day = st.wDayOfWeek-week_day(time);
300 time += set_week_day * MS_PER_DAY;
302 time += (DOUBLE)(st.wDay-1) * 7 * MS_PER_DAY;
303 if(month_from_time(time) != st.wMonth-1)
304 time -= 7 * MS_PER_DAY;
307 time += st.wDay * MS_PER_DAY;
309 time += st.wHour * MS_PER_HOUR;
310 time += st.wMinute * MS_PER_MINUTE;
315 /* ECMA-262 3rd Edition 15.9.1.9 */
316 static inline DOUBLE daylight_saving_ta(DOUBLE time, DateInstance *date)
318 int year = year_from_time(time);
319 DOUBLE standardTime, daylightTime;
324 standardTime = convert_time(year, date->standardDate);
325 daylightTime = convert_time(year, date->daylightDate);
327 if(isnan(standardTime) || isnan(daylightTime))
329 else if(standardTime > daylightTime) {
330 if(daylightTime <= time && time < standardTime)
331 return date->daylightBias;
333 return date->standardBias;
336 if(standardTime <= time && time < daylightTime)
337 return date->standardBias;
339 return date->daylightBias;
343 /* ECMA-262 3rd Edition 15.9.1.9 */
344 static inline DOUBLE local_time(DOUBLE time, DateInstance *date)
346 return time - (daylight_saving_ta(time, date)+date->bias)*MS_PER_MINUTE;
349 /* ECMA-262 3rd Edition 15.9.1.9 */
350 static inline DOUBLE utc(DOUBLE time, DateInstance *date)
352 time += date->bias * MS_PER_MINUTE;
353 return time + daylight_saving_ta(time, date)*MS_PER_MINUTE;
356 /* ECMA-262 3rd Edition 15.9.1.10 */
357 static inline DOUBLE hour_from_time(DOUBLE time)
364 ret = fmod(floor(time/MS_PER_HOUR), 24);
370 /* ECMA-262 3rd Edition 15.9.1.10 */
371 static inline DOUBLE min_from_time(DOUBLE time)
378 ret = fmod(floor(time/MS_PER_MINUTE), 60);
384 /* ECMA-262 3rd Edition 15.9.1.10 */
385 static inline DOUBLE sec_from_time(DOUBLE time)
392 ret = fmod(floor(time/1000), 60);
398 /* ECMA-262 3rd Edition 15.9.1.10 */
399 static inline DOUBLE ms_from_time(DOUBLE time)
406 ret = fmod(time, 1000);
407 if(ret<0) ret += 1000;
412 /* ECMA-262 3rd Edition 15.9.1.11 */
413 static inline DOUBLE make_time(DOUBLE hour, DOUBLE min, DOUBLE sec, DOUBLE ms)
415 return hour*MS_PER_HOUR + min*MS_PER_MINUTE + sec*1000 + ms;
418 /* ECMA-262 3rd Edition 15.9.1.12 */
419 static inline DOUBLE make_day(DOUBLE year, DOUBLE month, DOUBLE day)
423 year += floor(month/12);
425 month = fmod(month, 12);
426 if(month<0) month += 12;
428 time = time_from_year(year);
430 day += floor(time / MS_PER_DAY);
431 day += day_from_month(month, in_leap_year(time));
436 /* ECMA-262 3rd Edition 15.9.1.13 */
437 static inline DOUBLE make_date(DOUBLE day, DOUBLE time)
439 return day*MS_PER_DAY + time;
442 /* ECMA-262 3rd Edition 15.9.1.14 */
443 static inline DOUBLE time_clip(DOUBLE time)
445 if(8.64e15 < time || time < -8.64e15) {
452 static SYSTEMTIME create_systemtime(DOUBLE time)
456 st.wYear = year_from_time(time);
457 st.wMonth = month_from_time(time) + 1;
458 st.wDayOfWeek = week_day(time);
459 st.wDay = date_from_time(time);
460 st.wHour = hour_from_time(time);
461 st.wMinute = min_from_time(time);
462 st.wSecond = sec_from_time(time);
463 st.wMilliseconds = ms_from_time(time);
468 static inline HRESULT date_to_string(DOUBLE time, BOOL show_offset, int offset, jsval_t *r)
470 static const WCHAR NaNW[] = { 'N','a','N',0 };
471 static const WCHAR formatW[] = { '%','s',' ','%','s',' ','%','d',' ',
472 '%','0','2','d',':','%','0','2','d',':','%','0','2','d',' ',
473 'U','T','C','%','c','%','0','2','d','%','0','2','d',' ','%','d','%','s',0 };
474 static const WCHAR formatUTCW[] = { '%','s',' ','%','s',' ','%','d',' ',
475 '%','0','2','d',':','%','0','2','d',':','%','0','2','d',' ',
476 'U','T','C',' ','%','d','%','s',0 };
477 static const WCHAR formatNoOffsetW[] = { '%','s',' ','%','s',' ',
478 '%','d',' ','%','0','2','d',':','%','0','2','d',':',
479 '%','0','2','d',' ','%','d','%','s',0 };
480 static const WCHAR ADW[] = { 0 };
481 static const WCHAR BCW[] = { ' ','B','.','C','.',0 };
483 static const DWORD week_ids[] = { LOCALE_SABBREVDAYNAME7, LOCALE_SABBREVDAYNAME1,
484 LOCALE_SABBREVDAYNAME2, LOCALE_SABBREVDAYNAME3, LOCALE_SABBREVDAYNAME4,
485 LOCALE_SABBREVDAYNAME5, LOCALE_SABBREVDAYNAME6 };
486 static const DWORD month_ids[] = { LOCALE_SABBREVMONTHNAME1, LOCALE_SABBREVMONTHNAME2,
487 LOCALE_SABBREVMONTHNAME3, LOCALE_SABBREVMONTHNAME4,
488 LOCALE_SABBREVMONTHNAME5, LOCALE_SABBREVMONTHNAME6,
489 LOCALE_SABBREVMONTHNAME7, LOCALE_SABBREVMONTHNAME8,
490 LOCALE_SABBREVMONTHNAME9, LOCALE_SABBREVMONTHNAME10,
491 LOCALE_SABBREVMONTHNAME11, LOCALE_SABBREVMONTHNAME12 };
493 BOOL formatAD = TRUE;
496 int len, size, year, day;
497 DWORD lcid_en, week_id, month_id;
502 BSTR ret = SysAllocString(NaNW);
504 return E_OUTOFMEMORY;
505 *r = jsval_string(ret);
513 lcid_en = MAKELCID(MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),SORT_DEFAULT);
515 week_id = week_ids[(int)week_day(time)];
516 size = GetLocaleInfoW(lcid_en, week_id, NULL, 0);
517 week = SysAllocStringLen(NULL, size);
519 return E_OUTOFMEMORY;
520 GetLocaleInfoW(lcid_en, week_id, week, size);
523 month_id = month_ids[(int)month_from_time(time)];
524 size = GetLocaleInfoW(lcid_en, month_id, NULL, 0);
525 month = SysAllocStringLen(NULL, size);
528 return E_OUTOFMEMORY;
530 GetLocaleInfoW(lcid_en, month_id, month, size);
533 year = year_from_time(time);
541 year = year_from_time(time);
548 day = date_from_time(time);
553 day = date_from_time(time);
555 if(!show_offset) len -= 9;
556 else if(offset == 0) len -= 5;
557 else if(offset < 0) {
562 date_str = SysAllocStringLen(NULL, len);
565 SysFreeString(month);
566 return E_OUTOFMEMORY;
570 sprintfW(date_str, formatNoOffsetW, week, month, day,
571 (int)hour_from_time(time), (int)min_from_time(time),
572 (int)sec_from_time(time), year, formatAD?ADW:BCW);
574 sprintfW(date_str, formatW, week, month, day,
575 (int)hour_from_time(time), (int)min_from_time(time),
576 (int)sec_from_time(time), sign, offset/60, offset%60,
577 year, formatAD?ADW:BCW);
579 sprintfW(date_str, formatUTCW, week, month, day,
580 (int)hour_from_time(time), (int)min_from_time(time),
581 (int)sec_from_time(time), year, formatAD?ADW:BCW);
584 SysFreeString(month);
586 *r = jsval_string(date_str);
591 /* ECMA-262 3rd Edition 15.9.1.2 */
592 static HRESULT dateobj_to_string(DateInstance *date, jsval_t *r)
597 time = local_time(date->time, date);
598 offset = date->bias +
599 daylight_saving_ta(time, date);
601 return date_to_string(time, TRUE, offset, r);
604 static HRESULT Date_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
610 if(!(date = date_this(jsthis)))
611 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
613 return dateobj_to_string(date, r);
616 /* ECMA-262 3rd Edition 15.9.1.5 */
617 static HRESULT Date_toLocaleString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
620 static const WCHAR NaNW[] = { 'N','a','N',0 };
624 int date_len, time_len;
628 if(!(date = date_this(jsthis)))
629 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
631 if(isnan(date->time)) {
633 BSTR ret = SysAllocString(NaNW);
635 return E_OUTOFMEMORY;
636 *r = jsval_string(ret);
641 st = create_systemtime(local_time(date->time, date));
643 if(st.wYear<1601 || st.wYear>9999)
644 return dateobj_to_string(date, r);
647 date_len = GetDateFormatW(ctx->lcid, DATE_LONGDATE, &st, NULL, NULL, 0);
648 time_len = GetTimeFormatW(ctx->lcid, 0, &st, NULL, NULL, 0);
649 date_str = SysAllocStringLen(NULL, date_len+time_len-1);
651 return E_OUTOFMEMORY;
652 GetDateFormatW(ctx->lcid, DATE_LONGDATE, &st, NULL, date_str, date_len);
653 GetTimeFormatW(ctx->lcid, 0, &st, NULL, &date_str[date_len], time_len);
654 date_str[date_len-1] = ' ';
656 *r = jsval_string(date_str);
661 static HRESULT Date_valueOf(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
668 if(!(date = date_this(jsthis)))
669 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
672 *r = jsval_number(date->time);
676 static inline HRESULT create_utc_string(script_ctx_t *ctx, vdisp_t *jsthis, jsval_t *r)
678 static const WCHAR NaNW[] = { 'N','a','N',0 };
679 static const WCHAR formatADW[] = { '%','s',',',' ','%','d',' ','%','s',' ','%','d',' ',
680 '%','0','2','d',':','%','0','2','d',':','%','0','2','d',' ','U','T','C',0 };
681 static const WCHAR formatBCW[] = { '%','s',',',' ','%','d',' ','%','s',' ','%','d',' ','B','.','C','.',' ',
682 '%','0','2','d',':','%','0','2','d',':','%','0','2','d',' ','U','T','C',0 };
684 static const DWORD week_ids[] = { LOCALE_SABBREVDAYNAME7, LOCALE_SABBREVDAYNAME1,
685 LOCALE_SABBREVDAYNAME2, LOCALE_SABBREVDAYNAME3, LOCALE_SABBREVDAYNAME4,
686 LOCALE_SABBREVDAYNAME5, LOCALE_SABBREVDAYNAME6 };
687 static const DWORD month_ids[] = { LOCALE_SABBREVMONTHNAME1, LOCALE_SABBREVMONTHNAME2,
688 LOCALE_SABBREVMONTHNAME3, LOCALE_SABBREVMONTHNAME4,
689 LOCALE_SABBREVMONTHNAME5, LOCALE_SABBREVMONTHNAME6,
690 LOCALE_SABBREVMONTHNAME7, LOCALE_SABBREVMONTHNAME8,
691 LOCALE_SABBREVMONTHNAME9, LOCALE_SABBREVMONTHNAME10,
692 LOCALE_SABBREVMONTHNAME11, LOCALE_SABBREVMONTHNAME12 };
694 BOOL formatAD = TRUE;
698 int len, size, year, day;
699 DWORD lcid_en, week_id, month_id;
701 if(!(date = date_this(jsthis)))
702 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
704 if(isnan(date->time)) {
706 BSTR ret = SysAllocString(NaNW);
708 return E_OUTOFMEMORY;
709 *r = jsval_string(ret);
717 lcid_en = MAKELCID(MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),SORT_DEFAULT);
719 week_id = week_ids[(int)week_day(date->time)];
720 size = GetLocaleInfoW(lcid_en, week_id, NULL, 0);
721 week = SysAllocStringLen(NULL, size);
723 return E_OUTOFMEMORY;
724 GetLocaleInfoW(lcid_en, week_id, week, size);
727 month_id = month_ids[(int)month_from_time(date->time)];
728 size = GetLocaleInfoW(lcid_en, month_id, NULL, 0);
729 month = SysAllocStringLen(NULL, size);
732 return E_OUTOFMEMORY;
734 GetLocaleInfoW(lcid_en, month_id, month, size);
737 year = year_from_time(date->time);
745 year = year_from_time(date->time);
752 day = date_from_time(date->time);
757 day = date_from_time(date->time);
759 date_str = SysAllocStringLen(NULL, len);
762 SysFreeString(month);
763 return E_OUTOFMEMORY;
765 sprintfW(date_str, formatAD?formatADW:formatBCW, week, day, month, year,
766 (int)hour_from_time(date->time), (int)min_from_time(date->time),
767 (int)sec_from_time(date->time));
770 SysFreeString(month);
772 *r = jsval_string(date_str);
777 /* ECMA-262 3rd Edition 15.9.5.42 */
778 static HRESULT Date_toUTCString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
782 return create_utc_string(ctx, jsthis, r);
785 static HRESULT Date_toGMTString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
789 return create_utc_string(ctx, jsthis, r);
792 /* ECMA-262 3rd Edition 15.9.5.3 */
793 static HRESULT dateobj_to_date_string(DateInstance *date, jsval_t *r)
795 static const WCHAR NaNW[] = { 'N','a','N',0 };
796 static const WCHAR formatADW[] = { '%','s',' ','%','s',' ','%','d',' ','%','d',0 };
797 static const WCHAR formatBCW[] = { '%','s',' ','%','s',' ','%','d',' ','%','d',' ','B','.','C','.',0 };
799 static const DWORD week_ids[] = { LOCALE_SABBREVDAYNAME7, LOCALE_SABBREVDAYNAME1,
800 LOCALE_SABBREVDAYNAME2, LOCALE_SABBREVDAYNAME3, LOCALE_SABBREVDAYNAME4,
801 LOCALE_SABBREVDAYNAME5, LOCALE_SABBREVDAYNAME6 };
802 static const DWORD month_ids[] = { LOCALE_SABBREVMONTHNAME1, LOCALE_SABBREVMONTHNAME2,
803 LOCALE_SABBREVMONTHNAME3, LOCALE_SABBREVMONTHNAME4,
804 LOCALE_SABBREVMONTHNAME5, LOCALE_SABBREVMONTHNAME6,
805 LOCALE_SABBREVMONTHNAME7, LOCALE_SABBREVMONTHNAME8,
806 LOCALE_SABBREVMONTHNAME9, LOCALE_SABBREVMONTHNAME10,
807 LOCALE_SABBREVMONTHNAME11, LOCALE_SABBREVMONTHNAME12 };
809 BOOL formatAD = TRUE;
813 int len, size, year, day;
814 DWORD lcid_en, week_id, month_id;
816 if(isnan(date->time)) {
818 BSTR ret = SysAllocString(NaNW);
820 return E_OUTOFMEMORY;
821 *r = jsval_string(ret);
826 time = local_time(date->time, date);
831 lcid_en = MAKELCID(MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),SORT_DEFAULT);
833 week_id = week_ids[(int)week_day(time)];
834 size = GetLocaleInfoW(lcid_en, week_id, NULL, 0);
835 week = SysAllocStringLen(NULL, size);
837 return E_OUTOFMEMORY;
838 GetLocaleInfoW(lcid_en, week_id, week, size);
841 month_id = month_ids[(int)month_from_time(time)];
842 size = GetLocaleInfoW(lcid_en, month_id, NULL, 0);
843 month = SysAllocStringLen(NULL, size);
846 return E_OUTOFMEMORY;
848 GetLocaleInfoW(lcid_en, month_id, month, size);
851 year = year_from_time(time);
859 year = year_from_time(time);
866 day = date_from_time(time);
871 day = date_from_time(time);
873 date_str = SysAllocStringLen(NULL, len);
876 SysFreeString(month);
877 return E_OUTOFMEMORY;
879 sprintfW(date_str, formatAD?formatADW:formatBCW, week, month, day, year);
882 SysFreeString(month);
884 *r = jsval_string(date_str);
889 static HRESULT Date_toDateString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
894 if(!(date = date_this(jsthis)))
895 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
897 return dateobj_to_date_string(date, r);
900 /* ECMA-262 3rd Edition 15.9.5.4 */
901 static HRESULT Date_toTimeString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
904 static const WCHAR NaNW[] = { 'N','a','N',0 };
905 static const WCHAR formatW[] = { '%','0','2','d',':','%','0','2','d',':','%','0','2','d',
906 ' ','U','T','C','%','c','%','0','2','d','%','0','2','d',0 };
907 static const WCHAR formatUTCW[] = { '%','0','2','d',':','%','0','2','d',
908 ':','%','0','2','d',' ','U','T','C',0 };
917 if(!(date = date_this(jsthis)))
918 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
920 if(isnan(date->time)) {
922 BSTR ret = SysAllocString(NaNW);
924 return E_OUTOFMEMORY;
925 *r = jsval_string(ret);
929 time = local_time(date->time, date);
932 date_str = SysAllocStringLen(NULL, 17);
934 return E_OUTOFMEMORY;
936 offset = date->bias +
937 daylight_saving_ta(time, date);
946 sprintfW(date_str, formatW, (int)hour_from_time(time),
947 (int)min_from_time(time), (int)sec_from_time(time),
948 sign, offset/60, offset%60);
950 sprintfW(date_str, formatUTCW, (int)hour_from_time(time),
951 (int)min_from_time(time), (int)sec_from_time(time));
953 *r = jsval_string(date_str);
958 /* ECMA-262 3rd Edition 15.9.5.6 */
959 static HRESULT Date_toLocaleDateString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
962 static const WCHAR NaNW[] = { 'N','a','N',0 };
970 if(!(date = date_this(jsthis)))
971 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
973 if(isnan(date->time)) {
975 BSTR ret = SysAllocString(NaNW);
977 return E_OUTOFMEMORY;
978 *r = jsval_string(ret);
983 st = create_systemtime(local_time(date->time, date));
985 if(st.wYear<1601 || st.wYear>9999)
986 return dateobj_to_date_string(date, r);
989 len = GetDateFormatW(ctx->lcid, DATE_LONGDATE, &st, NULL, NULL, 0);
990 date_str = SysAllocStringLen(NULL, len);
992 return E_OUTOFMEMORY;
993 GetDateFormatW(ctx->lcid, DATE_LONGDATE, &st, NULL, date_str, len);
995 *r = jsval_string(date_str);
1000 /* ECMA-262 3rd Edition 15.9.5.7 */
1001 static HRESULT Date_toLocaleTimeString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1004 static const WCHAR NaNW[] = { 'N','a','N',0 };
1012 if(!(date = date_this(jsthis)))
1013 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
1015 if(isnan(date->time)) {
1017 BSTR ret = SysAllocString(NaNW);
1019 return E_OUTOFMEMORY;
1020 *r = jsval_string(ret);
1025 st = create_systemtime(local_time(date->time, date));
1027 if(st.wYear<1601 || st.wYear>9999)
1028 return Date_toTimeString(ctx, jsthis, flags, argc, argv, r);
1031 len = GetTimeFormatW(ctx->lcid, 0, &st, NULL, NULL, 0);
1032 date_str = SysAllocStringLen(NULL, len);
1034 return E_OUTOFMEMORY;
1035 GetTimeFormatW(ctx->lcid, 0, &st, NULL, date_str, len);
1037 *r = jsval_string(date_str);
1042 /* ECMA-262 3rd Edition 15.9.5.9 */
1043 static HRESULT Date_getTime(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1050 if(!(date = date_this(jsthis)))
1051 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
1054 *r = jsval_number(date->time);
1058 /* ECMA-262 3rd Edition 15.9.5.10 */
1059 static HRESULT Date_getFullYear(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1066 if(!(date = date_this(jsthis)))
1067 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
1070 DOUBLE time = local_time(date->time, date);
1072 *r = jsval_number(year_from_time(time));
1077 /* ECMA-262 3rd Edition 15.9.5.11 */
1078 static HRESULT Date_getUTCFullYear(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1085 if(!(date = date_this(jsthis)))
1086 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
1089 *r = jsval_number(year_from_time(date->time));
1093 /* ECMA-262 3rd Edition 15.9.5.12 */
1094 static HRESULT Date_getMonth(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
1100 if(!(date = date_this(jsthis)))
1101 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
1104 *r = jsval_number(month_from_time(local_time(date->time, date)));
1108 /* ECMA-262 3rd Edition 15.9.5.13 */
1109 static HRESULT Date_getUTCMonth(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1116 if(!(date = date_this(jsthis)))
1117 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
1120 *r = jsval_number(month_from_time(date->time));
1124 /* ECMA-262 3rd Edition 15.9.5.14 */
1125 static HRESULT Date_getDate(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
1131 if(!(date = date_this(jsthis)))
1132 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
1135 *r = jsval_number(date_from_time(local_time(date->time, date)));
1139 /* ECMA-262 3rd Edition 15.9.5.15 */
1140 static HRESULT Date_getUTCDate(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1147 if(!(date = date_this(jsthis)))
1148 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
1151 *r = jsval_number(date_from_time(date->time));
1155 /* ECMA-262 3rd Edition 15.9.5.16 */
1156 static HRESULT Date_getDay(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1163 if(!(date = date_this(jsthis)))
1164 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
1167 *r = jsval_number(week_day(local_time(date->time, date)));
1171 /* ECMA-262 3rd Edition 15.9.5.17 */
1172 static HRESULT Date_getUTCDay(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1179 if(!(date = date_this(jsthis)))
1180 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
1183 *r = jsval_number(week_day(date->time));
1187 /* ECMA-262 3rd Edition 15.9.5.18 */
1188 static HRESULT Date_getHours(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1195 if(!(date = date_this(jsthis)))
1196 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
1199 *r = jsval_number(hour_from_time(local_time(date->time, date)));
1203 /* ECMA-262 3rd Edition 15.9.5.19 */
1204 static HRESULT Date_getUTCHours(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1211 if(!(date = date_this(jsthis)))
1212 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
1215 *r = jsval_number(hour_from_time(date->time));
1219 /* ECMA-262 3rd Edition 15.9.5.20 */
1220 static HRESULT Date_getMinutes(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1227 if(!(date = date_this(jsthis)))
1228 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
1231 *r = jsval_number(min_from_time(local_time(date->time, date)));
1235 /* ECMA-262 3rd Edition 15.9.5.21 */
1236 static HRESULT Date_getUTCMinutes(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1243 if(!(date = date_this(jsthis)))
1244 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
1247 *r = jsval_number(min_from_time(date->time));
1251 /* ECMA-262 3rd Edition 15.9.5.22 */
1252 static HRESULT Date_getSeconds(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
1258 if(!(date = date_this(jsthis)))
1259 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
1262 *r = jsval_number(sec_from_time(local_time(date->time, date)));
1266 /* ECMA-262 3rd Edition 15.9.5.23 */
1267 static HRESULT Date_getUTCSeconds(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1274 if(!(date = date_this(jsthis)))
1275 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
1278 *r = jsval_number(sec_from_time(date->time));
1282 /* ECMA-262 3rd Edition 15.9.5.24 */
1283 static HRESULT Date_getMilliseconds(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1290 if(!(date = date_this(jsthis)))
1291 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
1294 *r = jsval_number(ms_from_time(local_time(date->time, date)));
1298 /* ECMA-262 3rd Edition 15.9.5.25 */
1299 static HRESULT Date_getUTCMilliseconds(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1306 if(!(date = date_this(jsthis)))
1307 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
1310 *r = jsval_number(ms_from_time(date->time));
1314 /* ECMA-262 3rd Edition 15.9.5.26 */
1315 static HRESULT Date_getTimezoneOffset(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1322 if(!(date = date_this(jsthis)))
1323 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
1326 *r = jsval_number(floor((date->time-local_time(date->time, date))/MS_PER_MINUTE));
1330 /* ECMA-262 3rd Edition 15.9.5.27 */
1331 static HRESULT Date_setTime(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1340 if(!(date = date_this(jsthis)))
1341 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
1344 return throw_type_error(ctx, JS_E_MISSING_ARG, NULL);
1346 hres = to_number(ctx, argv[0], &n);
1350 date->time = time_clip(n);
1353 *r = jsval_number(date->time);
1357 /* ECMA-262 3rd Edition 15.9.5.28 */
1358 static HRESULT Date_setMilliseconds(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1367 if(!(date = date_this(jsthis)))
1368 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
1371 return throw_type_error(ctx, JS_E_MISSING_ARG, NULL);
1373 hres = to_number(ctx, argv[0], &n);
1377 t = local_time(date->time, date);
1378 t = make_date(day(t), make_time(hour_from_time(t), min_from_time(t),
1379 sec_from_time(t), n));
1380 date->time = time_clip(utc(t, date));
1383 *r = jsval_number(date->time);
1387 /* ECMA-262 3rd Edition 15.9.5.29 */
1388 static HRESULT Date_setUTCMilliseconds(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1397 if(!(date = date_this(jsthis)))
1398 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
1401 return throw_type_error(ctx, JS_E_MISSING_ARG, NULL);
1403 hres = to_number(ctx, argv[0], &n);
1408 t = make_date(day(t), make_time(hour_from_time(t), min_from_time(t),
1409 sec_from_time(t), n));
1410 date->time = time_clip(t);
1413 *r = jsval_number(date->time);
1417 /* ECMA-262 3rd Edition 15.9.5.30 */
1418 static HRESULT Date_setSeconds(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1427 if(!(date = date_this(jsthis)))
1428 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
1431 return throw_type_error(ctx, JS_E_MISSING_ARG, NULL);
1433 t = local_time(date->time, date);
1435 hres = to_number(ctx, argv[0], &sec);
1440 hres = to_number(ctx, argv[1], &ms);
1444 ms = ms_from_time(t);
1447 t = make_date(day(t), make_time(hour_from_time(t),
1448 min_from_time(t), sec, ms));
1449 date->time = time_clip(utc(t, date));
1452 *r = jsval_number(date->time);
1456 /* ECMA-262 3rd Edition 15.9.5.31 */
1457 static HRESULT Date_setUTCSeconds(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1466 if(!(date = date_this(jsthis)))
1467 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
1470 return throw_type_error(ctx, JS_E_MISSING_ARG, NULL);
1474 hres = to_number(ctx, argv[0], &sec);
1479 hres = to_number(ctx, argv[1], &ms);
1483 ms = ms_from_time(t);
1486 t = make_date(day(t), make_time(hour_from_time(t),
1487 min_from_time(t), sec, ms));
1488 date->time = time_clip(t);
1491 *r = jsval_number(date->time);
1495 /* ECMA-262 3rd Edition 15.9.5.33 */
1496 static HRESULT Date_setMinutes(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1500 double t, min, sec, ms;
1505 if(!(date = date_this(jsthis)))
1506 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
1509 return throw_type_error(ctx, JS_E_MISSING_ARG, NULL);
1511 t = local_time(date->time, date);
1513 hres = to_number(ctx, argv[0], &min);
1518 hres = to_number(ctx, argv[1], &sec);
1522 sec = sec_from_time(t);
1526 hres = to_number(ctx, argv[2], &ms);
1530 ms = ms_from_time(t);
1533 t = make_date(day(t), make_time(hour_from_time(t),
1535 date->time = time_clip(utc(t, date));
1538 *r = jsval_number(date->time);
1542 /* ECMA-262 3rd Edition 15.9.5.34 */
1543 static HRESULT Date_setUTCMinutes(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1547 double t, min, sec, ms;
1552 if(!(date = date_this(jsthis)))
1553 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
1556 return throw_type_error(ctx, JS_E_MISSING_ARG, NULL);
1560 hres = to_number(ctx, argv[0], &min);
1565 hres = to_number(ctx, argv[1], &sec);
1569 sec = sec_from_time(t);
1573 hres = to_number(ctx, argv[2], &ms);
1577 ms = ms_from_time(t);
1580 t = make_date(day(t), make_time(hour_from_time(t),
1582 date->time = time_clip(t);
1585 *r = jsval_number(date->time);
1589 /* ECMA-262 3rd Edition 15.9.5.35 */
1590 static HRESULT Date_setHours(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1594 double t, hour, min, sec, ms;
1599 if(!(date = date_this(jsthis)))
1600 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
1603 return throw_type_error(ctx, JS_E_MISSING_ARG, NULL);
1605 t = local_time(date->time, date);
1607 hres = to_number(ctx, argv[0], &hour);
1612 hres = to_number(ctx, argv[1], &min);
1616 min = min_from_time(t);
1620 hres = to_number(ctx, argv[2], &sec);
1624 sec = sec_from_time(t);
1628 hres = to_number(ctx, argv[3], &ms);
1632 ms = ms_from_time(t);
1635 t = make_date(day(t), make_time(hour, min, sec, ms));
1636 date->time = time_clip(utc(t, date));
1639 *r = jsval_number(date->time);
1643 /* ECMA-262 3rd Edition 15.9.5.36 */
1644 static HRESULT Date_setUTCHours(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1648 double t, hour, min, sec, ms;
1653 if(!(date = date_this(jsthis)))
1654 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
1657 return throw_type_error(ctx, JS_E_MISSING_ARG, NULL);
1661 hres = to_number(ctx, argv[0], &hour);
1666 hres = to_number(ctx, argv[1], &min);
1670 min = min_from_time(t);
1674 hres = to_number(ctx, argv[2], &sec);
1678 sec = sec_from_time(t);
1682 hres = to_number(ctx, argv[3], &ms);
1686 ms = ms_from_time(t);
1689 t = make_date(day(t), make_time(hour, min, sec, ms));
1690 date->time = time_clip(t);
1693 *r = jsval_number(date->time);
1697 /* ECMA-262 3rd Edition 15.9.5.36 */
1698 static HRESULT Date_setDate(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1707 if(!(date = date_this(jsthis)))
1708 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
1711 return throw_type_error(ctx, JS_E_MISSING_ARG, NULL);
1713 hres = to_number(ctx, argv[0], &n);
1717 t = local_time(date->time, date);
1718 t = make_date(make_day(year_from_time(t), month_from_time(t), n), time_within_day(t));
1719 date->time = time_clip(utc(t, date));
1722 *r = jsval_number(date->time);
1726 /* ECMA-262 3rd Edition 15.9.5.37 */
1727 static HRESULT Date_setUTCDate(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1736 if(!(date = date_this(jsthis)))
1737 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
1740 return throw_type_error(ctx, JS_E_MISSING_ARG, NULL);
1742 hres = to_number(ctx, argv[0], &n);
1747 t = make_date(make_day(year_from_time(t), month_from_time(t), n), time_within_day(t));
1748 date->time = time_clip(t);
1751 *r = jsval_number(date->time);
1755 /* ECMA-262 3rd Edition 15.9.5.38 */
1756 static HRESULT Date_setMonth(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1760 DOUBLE t, month, ddate;
1765 if(!(date = date_this(jsthis)))
1766 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
1769 return throw_type_error(ctx, JS_E_MISSING_ARG, NULL);
1771 t = local_time(date->time, date);
1773 hres = to_number(ctx, argv[0], &month);
1778 hres = to_number(ctx, argv[1], &ddate);
1782 ddate = date_from_time(t);
1785 t = make_date(make_day(year_from_time(t), month, ddate),
1786 time_within_day(t));
1787 date->time = time_clip(utc(t, date));
1790 *r = jsval_number(date->time);
1794 /* ECMA-262 3rd Edition 15.9.5.39 */
1795 static HRESULT Date_setUTCMonth(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1799 double t, month, ddate;
1804 if(!(date = date_this(jsthis)))
1805 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
1808 return throw_type_error(ctx, JS_E_MISSING_ARG, NULL);
1812 hres = to_number(ctx, argv[0], &month);
1817 hres = to_number(ctx, argv[1], &ddate);
1821 ddate = date_from_time(t);
1824 t = make_date(make_day(year_from_time(t), month, ddate),
1825 time_within_day(t));
1826 date->time = time_clip(t);
1829 *r = jsval_number(date->time);
1833 /* ECMA-262 3rd Edition 15.9.5.40 */
1834 static HRESULT Date_setFullYear(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1838 double t, year, month, ddate;
1843 if(!(date = date_this(jsthis)))
1844 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
1847 return throw_type_error(ctx, JS_E_MISSING_ARG, NULL);
1849 t = local_time(date->time, date);
1851 hres = to_number(ctx, argv[0], &year);
1856 hres = to_number(ctx, argv[1], &month);
1860 month = month_from_time(t);
1864 hres = to_number(ctx, argv[2], &ddate);
1868 ddate = date_from_time(t);
1871 t = make_date(make_day(year, month, ddate), time_within_day(t));
1872 date->time = time_clip(utc(t, date));
1875 *r = jsval_number(date->time);
1879 /* ECMA-262 3rd Edition 15.9.5.41 */
1880 static HRESULT Date_setUTCFullYear(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1884 double t, year, month, ddate;
1889 if(!(date = date_this(jsthis)))
1890 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
1893 return throw_type_error(ctx, JS_E_MISSING_ARG, NULL);
1897 hres = to_number(ctx, argv[0], &year);
1902 hres = to_number(ctx, argv[1], &month);
1906 month = month_from_time(t);
1910 hres = to_number(ctx, argv[2], &ddate);
1914 ddate = date_from_time(t);
1917 t = make_date(make_day(year, month, ddate), time_within_day(t));
1918 date->time = time_clip(t);
1921 *r = jsval_number(date->time);
1925 /* ECMA-262 3rd Edition B2.4 */
1926 static HRESULT Date_getYear(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1934 if(!(date = date_this(jsthis)))
1935 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
1937 t = local_time(date->time, date);
1940 *r = jsval_number(NAN);
1944 year = year_from_time(t);
1946 *r = jsval_number((1900<=year && year<2000)?year-1900:year);
1950 /* ECMA-262 3rd Edition B2.5 */
1951 static HRESULT Date_setYear(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1960 if(!(date = date_this(jsthis)))
1961 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
1964 return throw_type_error(ctx, JS_E_MISSING_ARG, NULL);
1966 t = local_time(date->time, date);
1968 hres = to_number(ctx, argv[0], &year);
1975 *r = jsval_number(NAN);
1979 year = year >= 0.0 ? floor(year) : -floor(-year);
1980 if(-1.0 < year && year < 100.0)
1983 date->time = time_clip(utc(make_date(make_day(year, month_from_time(t), date_from_time(t)), time_within_day(t)), date));
1986 *r = jsval_number(date->time);
1990 static HRESULT Date_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1997 return throw_type_error(ctx, JS_E_FUNCTION_EXPECTED, NULL);
1999 FIXME("unimplemented flags %x\n", flags);
2006 static const builtin_prop_t Date_props[] = {
2007 {getDateW, Date_getDate, PROPF_METHOD},
2008 {getDayW, Date_getDay, PROPF_METHOD},
2009 {getFullYearW, Date_getFullYear, PROPF_METHOD},
2010 {getHoursW, Date_getHours, PROPF_METHOD},
2011 {getMillisecondsW, Date_getMilliseconds, PROPF_METHOD},
2012 {getMinutesW, Date_getMinutes, PROPF_METHOD},
2013 {getMonthW, Date_getMonth, PROPF_METHOD},
2014 {getSecondsW, Date_getSeconds, PROPF_METHOD},
2015 {getTimeW, Date_getTime, PROPF_METHOD},
2016 {getTimezoneOffsetW, Date_getTimezoneOffset, PROPF_METHOD},
2017 {getUTCDateW, Date_getUTCDate, PROPF_METHOD},
2018 {getUTCDayW, Date_getUTCDay, PROPF_METHOD},
2019 {getUTCFullYearW, Date_getUTCFullYear, PROPF_METHOD},
2020 {getUTCHoursW, Date_getUTCHours, PROPF_METHOD},
2021 {getUTCMillisecondsW, Date_getUTCMilliseconds, PROPF_METHOD},
2022 {getUTCMinutesW, Date_getUTCMinutes, PROPF_METHOD},
2023 {getUTCMonthW, Date_getUTCMonth, PROPF_METHOD},
2024 {getUTCSecondsW, Date_getUTCSeconds, PROPF_METHOD},
2025 {getYearW, Date_getYear, PROPF_METHOD},
2026 {setDateW, Date_setDate, PROPF_METHOD|1},
2027 {setFullYearW, Date_setFullYear, PROPF_METHOD|3},
2028 {setHoursW, Date_setHours, PROPF_METHOD|4},
2029 {setMillisecondsW, Date_setMilliseconds, PROPF_METHOD|1},
2030 {setMinutesW, Date_setMinutes, PROPF_METHOD|3},
2031 {setMonthW, Date_setMonth, PROPF_METHOD|2},
2032 {setSecondsW, Date_setSeconds, PROPF_METHOD|2},
2033 {setTimeW, Date_setTime, PROPF_METHOD|1},
2034 {setUTCDateW, Date_setUTCDate, PROPF_METHOD|1},
2035 {setUTCFullYearW, Date_setUTCFullYear, PROPF_METHOD|3},
2036 {setUTCHoursW, Date_setUTCHours, PROPF_METHOD|4},
2037 {setUTCMillisecondsW, Date_setUTCMilliseconds, PROPF_METHOD|1},
2038 {setUTCMinutesW, Date_setUTCMinutes, PROPF_METHOD|3},
2039 {setUTCMonthW, Date_setUTCMonth, PROPF_METHOD|2},
2040 {setUTCSecondsW, Date_setUTCSeconds, PROPF_METHOD|2},
2041 {setYearW, Date_setYear, PROPF_METHOD|1},
2042 {toDateStringW, Date_toDateString, PROPF_METHOD},
2043 {toGMTStringW, Date_toGMTString, PROPF_METHOD},
2044 {toLocaleDateStringW, Date_toLocaleDateString, PROPF_METHOD},
2045 {toLocaleStringW, Date_toLocaleString, PROPF_METHOD},
2046 {toLocaleTimeStringW, Date_toLocaleTimeString, PROPF_METHOD},
2047 {toStringW, Date_toString, PROPF_METHOD},
2048 {toTimeStringW, Date_toTimeString, PROPF_METHOD},
2049 {toUTCStringW, Date_toUTCString, PROPF_METHOD},
2050 {valueOfW, Date_valueOf, PROPF_METHOD},
2053 static const builtin_info_t Date_info = {
2055 {NULL, Date_value, 0},
2056 sizeof(Date_props)/sizeof(*Date_props),
2062 static const builtin_info_t DateInst_info = {
2064 {NULL, Date_value, 0},
2070 static HRESULT create_date(script_ctx_t *ctx, jsdisp_t *object_prototype, DOUBLE time, jsdisp_t **ret)
2074 TIME_ZONE_INFORMATION tzi;
2076 GetTimeZoneInformation(&tzi);
2078 date = heap_alloc_zero(sizeof(DateInstance));
2080 return E_OUTOFMEMORY;
2082 if(object_prototype)
2083 hres = init_dispex(&date->dispex, ctx, &Date_info, object_prototype);
2085 hres = init_dispex_from_constr(&date->dispex, ctx, &DateInst_info, ctx->date_constr);
2092 date->bias = tzi.Bias;
2093 date->standardDate = tzi.StandardDate;
2094 date->standardBias = tzi.StandardBias;
2095 date->daylightDate = tzi.DaylightDate;
2096 date->daylightBias = tzi.DaylightBias;
2098 *ret = &date->dispex;
2102 static inline HRESULT date_parse(BSTR input, double *ret) {
2103 static const DWORD string_ids[] = { LOCALE_SMONTHNAME12, LOCALE_SMONTHNAME11,
2104 LOCALE_SMONTHNAME10, LOCALE_SMONTHNAME9, LOCALE_SMONTHNAME8,
2105 LOCALE_SMONTHNAME7, LOCALE_SMONTHNAME6, LOCALE_SMONTHNAME5,
2106 LOCALE_SMONTHNAME4, LOCALE_SMONTHNAME3, LOCALE_SMONTHNAME2,
2107 LOCALE_SMONTHNAME1, LOCALE_SDAYNAME7, LOCALE_SDAYNAME1,
2108 LOCALE_SDAYNAME2, LOCALE_SDAYNAME3, LOCALE_SDAYNAME4,
2109 LOCALE_SDAYNAME5, LOCALE_SDAYNAME6 };
2110 BSTR strings[sizeof(string_ids)/sizeof(DWORD)];
2113 int input_len, parse_len = 0, nest_level = 0, i, size;
2114 int year = 0, month = 0, day = 0, hour = 0, min = 0, sec = 0;
2115 int ms = 0, offset = 0, hour_adjust = 0;
2116 BOOL set_year = FALSE, set_month = FALSE, set_day = FALSE, set_hour = FALSE;
2117 BOOL set_offset = FALSE, set_era = FALSE, ad = TRUE, set_am = FALSE, am = TRUE;
2118 BOOL set_hour_adjust = TRUE;
2119 TIME_ZONE_INFORMATION tzi;
2123 input_len = SysStringLen(input);
2124 for(i=0; i<input_len; i++) {
2125 if(input[i] == '(') nest_level++;
2126 else if(input[i] == ')') {
2133 else if(!nest_level) parse_len++;
2136 parse = SysAllocStringLen(NULL, parse_len);
2138 return E_OUTOFMEMORY;
2141 for(i=0; i<input_len; i++) {
2142 if(input[i] == '(') nest_level++;
2143 else if(input[i] == ')') nest_level--;
2144 else if(!nest_level) parse[parse_len++] = toupperW(input[i]);
2147 GetTimeZoneInformation(&tzi);
2149 di.standardDate = tzi.StandardDate;
2150 di.standardBias = tzi.StandardBias;
2151 di.daylightDate = tzi.DaylightDate;
2152 di.daylightBias = tzi.DaylightBias;
2154 lcid_en = MAKELCID(MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),SORT_DEFAULT);
2155 for(i=0; i<sizeof(string_ids)/sizeof(DWORD); i++) {
2156 size = GetLocaleInfoW(lcid_en, string_ids[i], NULL, 0);
2157 strings[i] = SysAllocStringLen(NULL, size);
2161 SysFreeString(strings[i]);
2162 SysFreeString(parse);
2163 return E_OUTOFMEMORY;
2165 GetLocaleInfoW(lcid_en, string_ids[i], strings[i], size);
2168 for(i=0; i<parse_len;) {
2169 while(isspaceW(parse[i])) i++;
2170 if(parse[i] == ',') {
2171 while(parse[i] == ',') i++;
2175 if(parse[i]>='0' && parse[i]<='9') {
2176 int tmp = atoiW(&parse[i]);
2177 while(parse[i]>='0' && parse[i]<='9') i++;
2178 while(isspaceW(parse[i])) i++;
2180 if(parse[i] == ':') {
2187 while(parse[i] == ':') i++;
2188 while(isspaceW(parse[i])) i++;
2189 if(parse[i]>='0' && parse[i]<='9') {
2190 min = atoiW(&parse[i]);
2191 while(parse[i]>='0' && parse[i]<='9') i++;
2194 while(isspaceW(parse[i])) i++;
2195 while(parse[i] == ':') i++;
2196 while(isspaceW(parse[i])) i++;
2197 if(parse[i]>='0' && parse[i]<='9') {
2198 sec = atoiW(&parse[i]);
2199 while(parse[i]>='0' && parse[i]<='9') i++;
2202 else if(parse[i]=='-' || parse[i]=='/') {
2204 if(set_day || set_month || set_year) break;
2211 while(isspaceW(parse[i])) i++;
2212 while(parse[i]=='-' || parse[i]=='/') i++;
2213 while(isspaceW(parse[i])) i++;
2214 if(parse[i]<'0' || parse[i]>'9') break;
2215 day = atoiW(&parse[i]);
2216 while(parse[i]>='0' && parse[i]<='9') i++;
2218 while(parse[i]=='-' || parse[i]=='/') i++;
2219 while(isspaceW(parse[i])) i++;
2220 if(parse[i]<'0' || parse[i]>'9') break;
2221 year = atoiW(&parse[i]);
2222 while(parse[i]>='0' && parse[i]<='9') i++;
2224 else if(tmp<0) break;
2239 if(parse[i]<'A' || parse[i]>'Z') break;
2240 else if(parse[i]=='B' && (parse[i+1]=='C' ||
2241 (parse[i+1]=='.' && parse[i+2]=='C'))) {
2248 if(parse[i] == '.') i++;
2250 if(parse[i] == '.') i++;
2252 else if(parse[i]=='A' && (parse[i+1]=='D' ||
2253 (parse[i+1]=='.' && parse[i+2]=='D'))) {
2259 if(parse[i] == '.') i++;
2261 if(parse[i] == '.') i++;
2263 else if(parse[i+1]<'A' || parse[i+1]>'Z') {
2265 if(set_offset) break;
2268 if(parse[i] <= 'I') hour_adjust = parse[i]-'A'+2;
2269 else if(parse[i] == 'J') break;
2270 else if(parse[i] <= 'M') hour_adjust = parse[i]-'K'+11;
2271 else if(parse[i] <= 'Y') hour_adjust = parse[i]-'N';
2272 else hour_adjust = 1;
2275 if(parse[i] == '.') i++;
2277 else if(parse[i]=='A' && parse[i+1]=='M') {
2284 else if(parse[i]=='P' && parse[i+1]=='M') {
2291 else if((parse[i]=='U' && parse[i+1]=='T' && parse[i+2]=='C')
2292 || (parse[i]=='G' && parse[i+1]=='M' && parse[i+2]=='T')) {
2294 BOOL positive = TRUE;
2296 if(set_offset) break;
2298 set_hour_adjust = FALSE;
2301 while(isspaceW(parse[i])) i++;
2302 if(parse[i] == '-') positive = FALSE;
2303 else if(parse[i] != '+') continue;
2306 while(isspaceW(parse[i])) i++;
2307 if(parse[i]<'0' || parse[i]>'9') break;
2308 offset = atoiW(&parse[i]);
2309 while(parse[i]>='0' && parse[i]<='9') i++;
2311 if(offset<24) offset *= 60;
2312 else offset = (offset/100)*60 + offset%100;
2314 if(positive) offset = -offset;
2317 /* Month or garbage */
2320 for(size=i; parse[size]>='A' && parse[size]<='Z'; size++);
2323 for(j=0; j<sizeof(string_ids)/sizeof(DWORD); j++)
2324 if(!memicmpW(&parse[i], strings[j], size)) break;
2327 if(set_month) break;
2331 else if(j == sizeof(string_ids)/sizeof(DWORD)) break;
2338 if(i == parse_len && set_year && set_month && set_day && (!set_am || hour<13)) {
2340 if(hour == 12) hour = 0;
2344 if(!ad) year = -year+1;
2345 else if(year<100) year += 1900;
2347 *ret = time_clip(make_date(make_day(year, month, day),
2348 make_time(hour+hour_adjust, min, sec, ms)) + offset*MS_PER_MINUTE);
2351 *ret = utc(*ret, &di);
2356 for(i=0; i<sizeof(string_ids)/sizeof(DWORD); i++)
2357 SysFreeString(strings[i]);
2358 SysFreeString(parse);
2363 static HRESULT DateConstr_parse(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
2374 *r = jsval_number(NAN);
2378 hres = to_string(ctx, argv[0], &parse_str);
2382 hres = date_parse(parse_str, &n);
2383 SysFreeString(parse_str);
2387 *r = jsval_number(n);
2391 static HRESULT date_utc(script_ctx_t *ctx, unsigned argc, jsval_t *argv, double *ret)
2393 double year, month, vdate, hours, minutes, seconds, ms;
2399 hres = to_number(ctx, argv[0], &year);
2402 if(0 <= year && year <= 99)
2409 hres = to_number(ctx, argv[1], &month);
2417 hres = to_number(ctx, argv[2], &vdate);
2425 hres = to_number(ctx, argv[3], &hours);
2433 hres = to_number(ctx, argv[4], &minutes);
2441 hres = to_number(ctx, argv[5], &seconds);
2449 hres = to_number(ctx, argv[6], &ms);
2456 *ret = time_clip(make_date(make_day(year, month, vdate),
2457 make_time(hours, minutes,seconds, ms)));
2461 static HRESULT DateConstr_UTC(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
2469 hres = date_utc(ctx, argc, argv, &n);
2470 if(SUCCEEDED(hres) && r)
2471 *r = jsval_number(n);
2475 static HRESULT DateConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
2484 case DISPATCH_CONSTRUCT:
2486 /* ECMA-262 3rd Edition 15.9.3.3 */
2491 GetSystemTimeAsFileTime(&time);
2492 lltime = ((LONGLONG)time.dwHighDateTime<<32)
2493 + time.dwLowDateTime;
2495 hres = create_date(ctx, NULL, lltime/10000-TIME_EPOCH, &date);
2501 /* ECMA-262 3rd Edition 15.9.3.2 */
2506 hres = to_primitive(ctx, argv[0], &prim, NO_HINT);
2511 hres = date_parse(get_string(prim), &n);
2513 hres = to_number(ctx, prim, &n);
2515 jsval_release(prim);
2519 hres = create_date(ctx, NULL, time_clip(n), &date);
2525 /* ECMA-262 3rd Edition 15.9.3.1 */
2530 hres = date_utc(ctx, argc, argv, &ret_date);
2534 hres = create_date(ctx, NULL, ret_date, &date);
2538 di = (DateInstance*)date;
2539 di->time = utc(di->time, di);
2543 *r = jsval_obj(date);
2547 FILETIME system_time, local_time;
2550 GetSystemTimeAsFileTime(&system_time);
2551 FileTimeToLocalFileTime(&system_time, &local_time);
2552 lltime = ((LONGLONG)local_time.dwHighDateTime<<32)
2553 + local_time.dwLowDateTime;
2555 return date_to_string(lltime/10000-TIME_EPOCH, FALSE, 0, r);
2559 FIXME("unimplemented flags %x\n", flags);
2566 static const builtin_prop_t DateConstr_props[] = {
2567 {UTCW, DateConstr_UTC, PROPF_METHOD},
2568 {parseW, DateConstr_parse, PROPF_METHOD}
2571 static const builtin_info_t DateConstr_info = {
2573 {NULL, Function_value, 0},
2574 sizeof(DateConstr_props)/sizeof(*DateConstr_props),
2580 HRESULT create_date_constr(script_ctx_t *ctx, jsdisp_t *object_prototype, jsdisp_t **ret)
2585 static const WCHAR DateW[] = {'D','a','t','e',0};
2587 hres = create_date(ctx, object_prototype, 0.0, &date);
2591 hres = create_builtin_constructor(ctx, DateConstr_value, DateW, &DateConstr_info,
2592 PROPF_CONSTR|7, date, ret);
2594 jsdisp_release(date);