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"
29 #include "wine/debug.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
33 /* 1601 to 1970 is 369 years plus 89 leap days */
34 #define TIME_EPOCH ((ULONGLONG)(369 * 365 + 89) * 86400 * 1000)
39 /* ECMA-262 3rd Edition 15.9.1.1 */
43 SYSTEMTIME standardDate;
45 SYSTEMTIME daylightDate;
49 static const WCHAR toStringW[] = {'t','o','S','t','r','i','n','g',0};
50 static const WCHAR toLocaleStringW[] = {'t','o','L','o','c','a','l','e','S','t','r','i','n','g',0};
51 static const WCHAR propertyIsEnumerableW[] =
52 {'p','r','o','p','e','r','t','y','I','s','E','n','u','m','e','r','a','b','l','e',0};
53 static const WCHAR isPrototypeOfW[] = {'i','s','P','r','o','t','o','t','y','p','e','O','f',0};
54 static const WCHAR valueOfW[] = {'v','a','l','u','e','O','f',0};
55 static const WCHAR toUTCStringW[] = {'t','o','U','T','C','S','t','r','i','n','g',0};
56 static const WCHAR toGMTStringW[] = {'t','o','G','M','T','S','t','r','i','n','g',0};
57 static const WCHAR toDateStringW[] = {'t','o','D','a','t','e','S','t','r','i','n','g',0};
58 static const WCHAR toTimeStringW[] = {'t','o','T','i','m','e','S','t','r','i','n','g',0};
59 static const WCHAR toLocaleDateStringW[] = {'t','o','L','o','c','a','l','e','D','a','t','e','S','t','r','i','n','g',0};
60 static const WCHAR toLocaleTimeStringW[] = {'t','o','L','o','c','a','l','e','T','i','m','e','S','t','r','i','n','g',0};
61 static const WCHAR getTimeW[] = {'g','e','t','T','i','m','e',0};
62 static const WCHAR getFullYearW[] = {'g','e','t','F','u','l','l','Y','e','a','r',0};
63 static const WCHAR getUTCFullYearW[] = {'g','e','t','U','T','C','F','u','l','l','Y','e','a','r',0};
64 static const WCHAR getMonthW[] = {'g','e','t','M','o','n','t','h',0};
65 static const WCHAR getUTCMonthW[] = {'g','e','t','U','T','C','M','o','n','t','h',0};
66 static const WCHAR getDateW[] = {'g','e','t','D','a','t','e',0};
67 static const WCHAR getUTCDateW[] = {'g','e','t','U','T','C','D','a','t','e',0};
68 static const WCHAR getDayW[] = {'g','e','t','D','a','y',0};
69 static const WCHAR getUTCDayW[] = {'g','e','t','U','T','C','D','a','y',0};
70 static const WCHAR getHoursW[] = {'g','e','t','H','o','u','r','s',0};
71 static const WCHAR getUTCHoursW[] = {'g','e','t','U','T','C','H','o','u','r','s',0};
72 static const WCHAR getMinutesW[] = {'g','e','t','M','i','n','u','t','e','s',0};
73 static const WCHAR getUTCMinutesW[] = {'g','e','t','U','T','C','M','i','n','u','t','e','s',0};
74 static const WCHAR getSecondsW[] = {'g','e','t','S','e','c','o','n','d','s',0};
75 static const WCHAR getUTCSecondsW[] = {'g','e','t','U','T','C','S','e','c','o','n','d','s',0};
76 static const WCHAR getMillisecondsW[] = {'g','e','t','M','i','l','l','i','s','e','c','o','n','d','s',0};
77 static const WCHAR getUTCMillisecondsW[] = {'g','e','t','U','T','C','M','i','l','l','i','s','e','c','o','n','d','s',0};
78 static const WCHAR getTimezoneOffsetW[] = {'g','e','t','T','i','m','e','z','o','n','e','O','f','f','s','e','t',0};
79 static const WCHAR setTimeW[] = {'s','e','t','T','i','m','e',0};
80 static const WCHAR setMillisecondsW[] = {'s','e','t','M','i','l','l','i','s','e','c','o','n','d','s',0};
81 static const WCHAR setUTCMillisecondsW[] = {'s','e','t','U','T','C','M','i','l','l','i','s','e','c','o','n','d','s',0};
82 static const WCHAR setSecondsW[] = {'s','e','t','S','e','c','o','n','d','s',0};
83 static const WCHAR setUTCSecondsW[] = {'s','e','t','U','T','C','S','e','c','o','n','d','s',0};
84 static const WCHAR setMinutesW[] = {'s','e','t','M','i','n','u','t','e','s',0};
85 static const WCHAR setUTCMinutesW[] = {'s','e','t','U','T','C','M','i','n','u','t','e','s',0};
86 static const WCHAR setHoursW[] = {'s','e','t','H','o','u','r','s',0};
87 static const WCHAR setUTCHoursW[] = {'s','e','t','U','T','C','H','o','u','r','s',0};
88 static const WCHAR setDateW[] = {'s','e','t','D','a','t','e',0};
89 static const WCHAR setUTCDateW[] = {'s','e','t','U','T','C','D','a','t','e',0};
90 static const WCHAR setMonthW[] = {'s','e','t','M','o','n','t','h',0};
91 static const WCHAR setUTCMonthW[] = {'s','e','t','U','T','C','M','o','n','t','h',0};
92 static const WCHAR setFullYearW[] = {'s','e','t','F','u','l','l','Y','e','a','r',0};
93 static const WCHAR setUTCFullYearW[] = {'s','e','t','U','T','C','F','u','l','l','Y','e','a','r',0};
94 static const WCHAR getYearW[] = {'g','e','t','Y','e','a','r',0};
95 static const WCHAR setYearW[] = {'s','e','t','Y','e','a','r',0};
97 static const WCHAR UTCW[] = {'U','T','C',0};
98 static const WCHAR parseW[] = {'p','a','r','s','e',0};
100 static inline DateInstance *date_this(vdisp_t *jsthis)
102 return is_vclass(jsthis, JSCLASS_DATE) ? (DateInstance*)jsthis->u.jsdisp : NULL;
105 /*ECMA-262 3rd Edition 15.9.1.2 */
106 #define MS_PER_DAY 86400000
107 #define MS_PER_HOUR 3600000
108 #define MS_PER_MINUTE 60000
110 /* ECMA-262 3rd Edition 15.9.1.2 */
111 static inline DOUBLE day(DOUBLE time)
113 return floor(time / MS_PER_DAY);
116 /* ECMA-262 3rd Edition 15.9.1.2 */
117 static inline DOUBLE time_within_day(DOUBLE time)
121 ret = fmod(time, MS_PER_DAY);
128 /* ECMA-262 3rd Edition 15.9.1.3 */
129 static inline DOUBLE days_in_year(DOUBLE year)
133 if(year != (int)year)
137 if(y%4 != 0) return 365;
138 if(y%100 != 0) return 366;
139 if(y%400 != 0) return 365;
143 /* ECMA-262 3rd Edition 15.9.1.3 */
144 static inline DOUBLE day_from_year(DOUBLE year)
146 if(year != (int)year)
149 return floor(365.0*(year-1970) + floor((year-1969)/4)
150 - floor((year-1901)/100) + floor((year-1601)/400));
153 static inline int day_from_month(int month, int in_leap_year)
162 return 59+in_leap_year;
164 return 90+in_leap_year;
166 return 120+in_leap_year;
168 return 151+in_leap_year;
170 return 181+in_leap_year;
172 return 212+in_leap_year;
174 return 243+in_leap_year;
176 return 273+in_leap_year;
178 return 304+in_leap_year;
180 return 334+in_leap_year;
184 /* ECMA-262 3rd Edition 15.9.1.3 */
185 static inline DOUBLE time_from_year(DOUBLE year)
187 return MS_PER_DAY*day_from_year(year);
190 /* ECMA-262 3rd Edition 15.9.1.3 */
191 static inline DOUBLE year_from_time(DOUBLE time)
198 y = 1970 + time/365.25/MS_PER_DAY;
200 if(time_from_year(y) > time)
201 while(time_from_year(y) > time) y--;
203 while(time_from_year(y+1)<=time) y++;
208 /* ECMA-262 3rd Edition 15.9.1.3 */
209 static inline int in_leap_year(DOUBLE time)
211 if(days_in_year(year_from_time(time))==366)
216 /* ECMA-262 3rd Edition 15.9.1.4 */
217 static inline int day_within_year(DOUBLE time)
219 return day(time) - day_from_year(year_from_time(time));
222 /* ECMA-262 3rd Edition 15.9.1.4 */
223 static inline DOUBLE month_from_time(DOUBLE time)
225 int ily = in_leap_year(time);
226 int dwy = day_within_year(time);
231 if(0<=dwy && dwy<31) return 0;
232 if(dwy < 59+ily) return 1;
233 if(dwy < 90+ily) return 2;
234 if(dwy < 120+ily) return 3;
235 if(dwy < 151+ily) return 4;
236 if(dwy < 181+ily) return 5;
237 if(dwy < 212+ily) return 6;
238 if(dwy < 243+ily) return 7;
239 if(dwy < 273+ily) return 8;
240 if(dwy < 304+ily) return 9;
241 if(dwy < 334+ily) return 10;
245 /* ECMA-262 3rd Edition 15.9.1.5 */
246 static inline DOUBLE date_from_time(DOUBLE time)
248 int dwy = day_within_year(time);
249 int ily = in_leap_year(time);
250 int mft = month_from_time(time);
255 if(mft==0) return dwy+1;
256 if(mft==1) return dwy-30;
257 if(mft==2) return dwy-58-ily;
258 if(mft==3) return dwy-89-ily;
259 if(mft==4) return dwy-119-ily;
260 if(mft==5) return dwy-150-ily;
261 if(mft==6) return dwy-180-ily;
262 if(mft==7) return dwy-211-ily;
263 if(mft==8) return dwy-242-ily;
264 if(mft==9) return dwy-272-ily;
265 if(mft==10) return dwy-303-ily;
269 /* ECMA-262 3rd Edition 15.9.1.6 */
270 static inline DOUBLE week_day(DOUBLE time)
277 ret = fmod(day(time)+4, 7);
283 static inline DOUBLE convert_time(int year, SYSTEMTIME st)
294 time = time_from_year(year);
295 time += (DOUBLE)day_from_month(st.wMonth-1, in_leap_year(time)) * MS_PER_DAY;
298 set_week_day = st.wDayOfWeek-week_day(time);
301 time += set_week_day * MS_PER_DAY;
303 time += (DOUBLE)(st.wDay-1) * 7 * MS_PER_DAY;
304 if(month_from_time(time) != st.wMonth-1)
305 time -= 7 * MS_PER_DAY;
308 time += st.wDay * MS_PER_DAY;
310 time += st.wHour * MS_PER_HOUR;
311 time += st.wMinute * MS_PER_MINUTE;
316 /* ECMA-262 3rd Edition 15.9.1.9 */
317 static inline DOUBLE daylight_saving_ta(DOUBLE time, DateInstance *date)
319 int year = year_from_time(time);
320 DOUBLE standardTime, daylightTime;
325 standardTime = convert_time(year, date->standardDate);
326 daylightTime = convert_time(year, date->daylightDate);
328 if(isnan(standardTime) || isnan(daylightTime))
330 else if(standardTime > daylightTime) {
331 if(daylightTime <= time && time < standardTime)
332 return date->daylightBias;
334 return date->standardBias;
337 if(standardTime <= time && time < daylightTime)
338 return date->standardBias;
340 return date->daylightBias;
344 /* ECMA-262 3rd Edition 15.9.1.9 */
345 static inline DOUBLE local_time(DOUBLE time, DateInstance *date)
347 return time - (daylight_saving_ta(time, date)+date->bias)*MS_PER_MINUTE;
350 /* ECMA-262 3rd Edition 15.9.1.9 */
351 static inline DOUBLE utc(DOUBLE time, DateInstance *date)
353 time += date->bias * MS_PER_MINUTE;
354 return time + daylight_saving_ta(time, date)*MS_PER_MINUTE;
357 /* ECMA-262 3rd Edition 15.9.1.10 */
358 static inline DOUBLE hour_from_time(DOUBLE time)
365 ret = fmod(floor(time/MS_PER_HOUR), 24);
371 /* ECMA-262 3rd Edition 15.9.1.10 */
372 static inline DOUBLE min_from_time(DOUBLE time)
379 ret = fmod(floor(time/MS_PER_MINUTE), 60);
385 /* ECMA-262 3rd Edition 15.9.1.10 */
386 static inline DOUBLE sec_from_time(DOUBLE time)
393 ret = fmod(floor(time/1000), 60);
399 /* ECMA-262 3rd Edition 15.9.1.10 */
400 static inline DOUBLE ms_from_time(DOUBLE time)
407 ret = fmod(time, 1000);
408 if(ret<0) ret += 1000;
413 /* ECMA-262 3rd Edition 15.9.1.11 */
414 static inline DOUBLE make_time(DOUBLE hour, DOUBLE min, DOUBLE sec, DOUBLE ms)
416 return hour*MS_PER_HOUR + min*MS_PER_MINUTE + sec*1000 + ms;
419 /* ECMA-262 3rd Edition 15.9.1.12 */
420 static inline DOUBLE make_day(DOUBLE year, DOUBLE month, DOUBLE day)
424 year += floor(month/12);
426 month = fmod(month, 12);
427 if(month<0) month += 12;
429 time = time_from_year(year);
431 day += floor(time / MS_PER_DAY);
432 day += day_from_month(month, in_leap_year(time));
437 /* ECMA-262 3rd Edition 15.9.1.13 */
438 static inline DOUBLE make_date(DOUBLE day, DOUBLE time)
440 return day*MS_PER_DAY + time;
443 /* ECMA-262 3rd Edition 15.9.1.14 */
444 static inline DOUBLE time_clip(DOUBLE time)
446 if(8.64e15 < time || time < -8.64e15) {
453 static SYSTEMTIME create_systemtime(DOUBLE time)
457 st.wYear = year_from_time(time);
458 st.wMonth = month_from_time(time) + 1;
459 st.wDayOfWeek = week_day(time);
460 st.wDay = date_from_time(time);
461 st.wHour = hour_from_time(time);
462 st.wMinute = min_from_time(time);
463 st.wSecond = sec_from_time(time);
464 st.wMilliseconds = ms_from_time(time);
469 static inline HRESULT date_to_string(DOUBLE time, BOOL show_offset, int offset, jsval_t *r)
471 static const WCHAR formatW[] = { '%','s',' ','%','s',' ','%','d',' ',
472 '%','0','2','d',':','%','0','2','d',':','%','0','2','d',' ',
473 'U','T','C','%','c','%','0','2','d','%','0','2','d',' ','%','d','%','s',0 };
474 static const WCHAR formatUTCW[] = { '%','s',' ','%','s',' ','%','d',' ',
475 '%','0','2','d',':','%','0','2','d',':','%','0','2','d',' ',
476 'U','T','C',' ','%','d','%','s',0 };
477 static const WCHAR formatNoOffsetW[] = { '%','s',' ','%','s',' ',
478 '%','d',' ','%','0','2','d',':','%','0','2','d',':',
479 '%','0','2','d',' ','%','d','%','s',0 };
480 static const WCHAR ADW[] = { 0 };
481 static const WCHAR BCW[] = { ' ','B','.','C','.',0 };
483 static const DWORD week_ids[] = { LOCALE_SABBREVDAYNAME7, LOCALE_SABBREVDAYNAME1,
484 LOCALE_SABBREVDAYNAME2, LOCALE_SABBREVDAYNAME3, LOCALE_SABBREVDAYNAME4,
485 LOCALE_SABBREVDAYNAME5, LOCALE_SABBREVDAYNAME6 };
486 static const DWORD month_ids[] = { LOCALE_SABBREVMONTHNAME1, LOCALE_SABBREVMONTHNAME2,
487 LOCALE_SABBREVMONTHNAME3, LOCALE_SABBREVMONTHNAME4,
488 LOCALE_SABBREVMONTHNAME5, LOCALE_SABBREVMONTHNAME6,
489 LOCALE_SABBREVMONTHNAME7, LOCALE_SABBREVMONTHNAME8,
490 LOCALE_SABBREVMONTHNAME9, LOCALE_SABBREVMONTHNAME10,
491 LOCALE_SABBREVMONTHNAME11, LOCALE_SABBREVMONTHNAME12 };
493 BOOL formatAD = TRUE;
494 WCHAR week[64], month[64];
496 int len, size, year, day;
502 *r = jsval_string(jsstr_nan());
509 lcid_en = MAKELCID(MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),SORT_DEFAULT);
511 size = GetLocaleInfoW(lcid_en, week_ids[(int)week_day(time)], week, sizeof(week)/sizeof(*week));
515 size = GetLocaleInfoW(lcid_en, month_ids[(int)month_from_time(time)], month, sizeof(month)/sizeof(*month));
518 year = year_from_time(time);
526 year = year_from_time(time);
533 day = date_from_time(time);
538 day = date_from_time(time);
540 if(!show_offset) len -= 9;
541 else if(offset == 0) len -= 5;
542 else if(offset < 0) {
547 date_str = jsstr_alloc_buf(len);
549 return E_OUTOFMEMORY;
552 sprintfW(date_str->str, formatNoOffsetW, week, month, day,
553 (int)hour_from_time(time), (int)min_from_time(time),
554 (int)sec_from_time(time), year, formatAD?ADW:BCW);
556 sprintfW(date_str->str, formatW, week, month, day,
557 (int)hour_from_time(time), (int)min_from_time(time),
558 (int)sec_from_time(time), sign, offset/60, offset%60,
559 year, formatAD?ADW:BCW);
561 sprintfW(date_str->str, formatUTCW, week, month, day,
562 (int)hour_from_time(time), (int)min_from_time(time),
563 (int)sec_from_time(time), year, formatAD?ADW:BCW);
565 *r = jsval_string(date_str);
570 /* ECMA-262 3rd Edition 15.9.1.2 */
571 static HRESULT dateobj_to_string(DateInstance *date, jsval_t *r)
576 time = local_time(date->time, date);
577 offset = date->bias +
578 daylight_saving_ta(time, date);
580 return date_to_string(time, TRUE, offset, r);
583 static HRESULT Date_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
589 if(!(date = date_this(jsthis)))
590 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
592 return dateobj_to_string(date, r);
595 /* ECMA-262 3rd Edition 15.9.1.5 */
596 static HRESULT Date_toLocaleString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
602 int date_len, time_len;
606 if(!(date = date_this(jsthis)))
607 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
609 if(isnan(date->time)) {
611 *r = jsval_string(jsstr_nan());
615 st = create_systemtime(local_time(date->time, date));
617 if(st.wYear<1601 || st.wYear>9999)
618 return dateobj_to_string(date, r);
621 date_len = GetDateFormatW(ctx->lcid, DATE_LONGDATE, &st, NULL, NULL, 0);
622 time_len = GetTimeFormatW(ctx->lcid, 0, &st, NULL, NULL, 0);
623 date_str = jsstr_alloc_buf(date_len+time_len-1);
625 return E_OUTOFMEMORY;
626 GetDateFormatW(ctx->lcid, DATE_LONGDATE, &st, NULL, date_str->str, date_len);
627 GetTimeFormatW(ctx->lcid, 0, &st, NULL, date_str->str+date_len, time_len);
628 date_str->str[date_len-1] = ' ';
630 *r = jsval_string(date_str);
635 static HRESULT Date_valueOf(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
642 if(!(date = date_this(jsthis)))
643 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
646 *r = jsval_number(date->time);
650 static inline HRESULT create_utc_string(script_ctx_t *ctx, vdisp_t *jsthis, jsval_t *r)
652 static const WCHAR formatADW[] = { '%','s',',',' ','%','d',' ','%','s',' ','%','d',' ',
653 '%','0','2','d',':','%','0','2','d',':','%','0','2','d',' ','U','T','C',0 };
654 static const WCHAR formatBCW[] = { '%','s',',',' ','%','d',' ','%','s',' ','%','d',' ','B','.','C','.',' ',
655 '%','0','2','d',':','%','0','2','d',':','%','0','2','d',' ','U','T','C',0 };
657 static const DWORD week_ids[] = { LOCALE_SABBREVDAYNAME7, LOCALE_SABBREVDAYNAME1,
658 LOCALE_SABBREVDAYNAME2, LOCALE_SABBREVDAYNAME3, LOCALE_SABBREVDAYNAME4,
659 LOCALE_SABBREVDAYNAME5, LOCALE_SABBREVDAYNAME6 };
660 static const DWORD month_ids[] = { LOCALE_SABBREVMONTHNAME1, LOCALE_SABBREVMONTHNAME2,
661 LOCALE_SABBREVMONTHNAME3, LOCALE_SABBREVMONTHNAME4,
662 LOCALE_SABBREVMONTHNAME5, LOCALE_SABBREVMONTHNAME6,
663 LOCALE_SABBREVMONTHNAME7, LOCALE_SABBREVMONTHNAME8,
664 LOCALE_SABBREVMONTHNAME9, LOCALE_SABBREVMONTHNAME10,
665 LOCALE_SABBREVMONTHNAME11, LOCALE_SABBREVMONTHNAME12 };
667 BOOL formatAD = TRUE;
668 WCHAR week[64], month[64];
671 int len, size, year, day;
674 if(!(date = date_this(jsthis)))
675 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
677 if(isnan(date->time)) {
679 *r = jsval_string(jsstr_nan());
686 lcid_en = MAKELCID(MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),SORT_DEFAULT);
688 size = GetLocaleInfoW(lcid_en, week_ids[(int)week_day(date->time)], week, sizeof(week)/sizeof(*week));
691 size = GetLocaleInfoW(lcid_en, month_ids[(int)month_from_time(date->time)], month, sizeof(month)/sizeof(*month));
694 year = year_from_time(date->time);
702 year = year_from_time(date->time);
709 day = date_from_time(date->time);
714 day = date_from_time(date->time);
716 date_str = jsstr_alloc_buf(len);
718 return E_OUTOFMEMORY;
720 sprintfW(date_str->str, formatAD?formatADW:formatBCW, week, day, month, year,
721 (int)hour_from_time(date->time), (int)min_from_time(date->time),
722 (int)sec_from_time(date->time));
724 *r = jsval_string(date_str);
729 /* ECMA-262 3rd Edition 15.9.5.42 */
730 static HRESULT Date_toUTCString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
734 return create_utc_string(ctx, jsthis, r);
737 static HRESULT Date_toGMTString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
741 return create_utc_string(ctx, jsthis, r);
744 /* ECMA-262 3rd Edition 15.9.5.3 */
745 static HRESULT dateobj_to_date_string(DateInstance *date, jsval_t *r)
747 static const WCHAR formatADW[] = { '%','s',' ','%','s',' ','%','d',' ','%','d',0 };
748 static const WCHAR formatBCW[] = { '%','s',' ','%','s',' ','%','d',' ','%','d',' ','B','.','C','.',0 };
750 static const DWORD week_ids[] = { LOCALE_SABBREVDAYNAME7, LOCALE_SABBREVDAYNAME1,
751 LOCALE_SABBREVDAYNAME2, LOCALE_SABBREVDAYNAME3, LOCALE_SABBREVDAYNAME4,
752 LOCALE_SABBREVDAYNAME5, LOCALE_SABBREVDAYNAME6 };
753 static const DWORD month_ids[] = { LOCALE_SABBREVMONTHNAME1, LOCALE_SABBREVMONTHNAME2,
754 LOCALE_SABBREVMONTHNAME3, LOCALE_SABBREVMONTHNAME4,
755 LOCALE_SABBREVMONTHNAME5, LOCALE_SABBREVMONTHNAME6,
756 LOCALE_SABBREVMONTHNAME7, LOCALE_SABBREVMONTHNAME8,
757 LOCALE_SABBREVMONTHNAME9, LOCALE_SABBREVMONTHNAME10,
758 LOCALE_SABBREVMONTHNAME11, LOCALE_SABBREVMONTHNAME12 };
760 BOOL formatAD = TRUE;
761 WCHAR week[64], month[64];
764 int len, size, year, day;
767 if(isnan(date->time)) {
769 *r = jsval_string(jsstr_nan());
773 time = local_time(date->time, date);
778 lcid_en = MAKELCID(MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),SORT_DEFAULT);
780 size = GetLocaleInfoW(lcid_en, week_ids[(int)week_day(time)], week, sizeof(week)/sizeof(*week));
784 size = GetLocaleInfoW(lcid_en, month_ids[(int)month_from_time(time)], month, sizeof(month)/sizeof(*month));
788 year = year_from_time(time);
796 year = year_from_time(time);
803 day = date_from_time(time);
808 day = date_from_time(time);
810 date_str = jsstr_alloc_buf(len);
812 return E_OUTOFMEMORY;
813 sprintfW(date_str->str, formatAD?formatADW:formatBCW, week, month, day, year);
815 *r = jsval_string(date_str);
820 static HRESULT Date_toDateString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
825 if(!(date = date_this(jsthis)))
826 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
828 return dateobj_to_date_string(date, r);
831 /* ECMA-262 3rd Edition 15.9.5.4 */
832 static HRESULT Date_toTimeString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
835 static const WCHAR formatW[] = { '%','0','2','d',':','%','0','2','d',':','%','0','2','d',
836 ' ','U','T','C','%','c','%','0','2','d','%','0','2','d',0 };
837 static const WCHAR formatUTCW[] = { '%','0','2','d',':','%','0','2','d',
838 ':','%','0','2','d',' ','U','T','C',0 };
847 if(!(date = date_this(jsthis)))
848 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
850 if(isnan(date->time)) {
852 *r = jsval_string(jsstr_nan());
856 time = local_time(date->time, date);
859 date_str = jsstr_alloc_buf(17);
861 return E_OUTOFMEMORY;
863 offset = date->bias +
864 daylight_saving_ta(time, date);
873 sprintfW(date_str->str, formatW, (int)hour_from_time(time),
874 (int)min_from_time(time), (int)sec_from_time(time),
875 sign, offset/60, offset%60);
877 sprintfW(date_str->str, formatUTCW, (int)hour_from_time(time),
878 (int)min_from_time(time), (int)sec_from_time(time));
880 *r = jsval_string(date_str);
885 /* ECMA-262 3rd Edition 15.9.5.6 */
886 static HRESULT Date_toLocaleDateString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
896 if(!(date = date_this(jsthis)))
897 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
899 if(isnan(date->time)) {
901 *r = jsval_string(jsstr_nan());
905 st = create_systemtime(local_time(date->time, date));
907 if(st.wYear<1601 || st.wYear>9999)
908 return dateobj_to_date_string(date, r);
911 len = GetDateFormatW(ctx->lcid, DATE_LONGDATE, &st, NULL, NULL, 0);
912 date_str = jsstr_alloc_buf(len);
914 return E_OUTOFMEMORY;
915 GetDateFormatW(ctx->lcid, DATE_LONGDATE, &st, NULL, date_str->str, len);
917 *r = jsval_string(date_str);
922 /* ECMA-262 3rd Edition 15.9.5.7 */
923 static HRESULT Date_toLocaleTimeString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
933 if(!(date = date_this(jsthis)))
934 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
936 if(isnan(date->time)) {
938 *r = jsval_string(jsstr_nan());
942 st = create_systemtime(local_time(date->time, date));
944 if(st.wYear<1601 || st.wYear>9999)
945 return Date_toTimeString(ctx, jsthis, flags, argc, argv, r);
948 len = GetTimeFormatW(ctx->lcid, 0, &st, NULL, NULL, 0);
949 date_str = jsstr_alloc_buf(len);
951 return E_OUTOFMEMORY;
952 GetTimeFormatW(ctx->lcid, 0, &st, NULL, date_str->str, len);
954 *r = jsval_string(date_str);
959 /* ECMA-262 3rd Edition 15.9.5.9 */
960 static HRESULT Date_getTime(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
967 if(!(date = date_this(jsthis)))
968 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
971 *r = jsval_number(date->time);
975 /* ECMA-262 3rd Edition 15.9.5.10 */
976 static HRESULT Date_getFullYear(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
983 if(!(date = date_this(jsthis)))
984 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
987 DOUBLE time = local_time(date->time, date);
989 *r = jsval_number(year_from_time(time));
994 /* ECMA-262 3rd Edition 15.9.5.11 */
995 static HRESULT Date_getUTCFullYear(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1002 if(!(date = date_this(jsthis)))
1003 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
1006 *r = jsval_number(year_from_time(date->time));
1010 /* ECMA-262 3rd Edition 15.9.5.12 */
1011 static HRESULT Date_getMonth(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
1017 if(!(date = date_this(jsthis)))
1018 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
1021 *r = jsval_number(month_from_time(local_time(date->time, date)));
1025 /* ECMA-262 3rd Edition 15.9.5.13 */
1026 static HRESULT Date_getUTCMonth(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1033 if(!(date = date_this(jsthis)))
1034 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
1037 *r = jsval_number(month_from_time(date->time));
1041 /* ECMA-262 3rd Edition 15.9.5.14 */
1042 static HRESULT Date_getDate(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
1048 if(!(date = date_this(jsthis)))
1049 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
1052 *r = jsval_number(date_from_time(local_time(date->time, date)));
1056 /* ECMA-262 3rd Edition 15.9.5.15 */
1057 static HRESULT Date_getUTCDate(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1064 if(!(date = date_this(jsthis)))
1065 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
1068 *r = jsval_number(date_from_time(date->time));
1072 /* ECMA-262 3rd Edition 15.9.5.16 */
1073 static HRESULT Date_getDay(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1080 if(!(date = date_this(jsthis)))
1081 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
1084 *r = jsval_number(week_day(local_time(date->time, date)));
1088 /* ECMA-262 3rd Edition 15.9.5.17 */
1089 static HRESULT Date_getUTCDay(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1096 if(!(date = date_this(jsthis)))
1097 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
1100 *r = jsval_number(week_day(date->time));
1104 /* ECMA-262 3rd Edition 15.9.5.18 */
1105 static HRESULT Date_getHours(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1112 if(!(date = date_this(jsthis)))
1113 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
1116 *r = jsval_number(hour_from_time(local_time(date->time, date)));
1120 /* ECMA-262 3rd Edition 15.9.5.19 */
1121 static HRESULT Date_getUTCHours(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1128 if(!(date = date_this(jsthis)))
1129 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
1132 *r = jsval_number(hour_from_time(date->time));
1136 /* ECMA-262 3rd Edition 15.9.5.20 */
1137 static HRESULT Date_getMinutes(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1144 if(!(date = date_this(jsthis)))
1145 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
1148 *r = jsval_number(min_from_time(local_time(date->time, date)));
1152 /* ECMA-262 3rd Edition 15.9.5.21 */
1153 static HRESULT Date_getUTCMinutes(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1160 if(!(date = date_this(jsthis)))
1161 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
1164 *r = jsval_number(min_from_time(date->time));
1168 /* ECMA-262 3rd Edition 15.9.5.22 */
1169 static HRESULT Date_getSeconds(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
1175 if(!(date = date_this(jsthis)))
1176 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
1179 *r = jsval_number(sec_from_time(local_time(date->time, date)));
1183 /* ECMA-262 3rd Edition 15.9.5.23 */
1184 static HRESULT Date_getUTCSeconds(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1191 if(!(date = date_this(jsthis)))
1192 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
1195 *r = jsval_number(sec_from_time(date->time));
1199 /* ECMA-262 3rd Edition 15.9.5.24 */
1200 static HRESULT Date_getMilliseconds(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1207 if(!(date = date_this(jsthis)))
1208 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
1211 *r = jsval_number(ms_from_time(local_time(date->time, date)));
1215 /* ECMA-262 3rd Edition 15.9.5.25 */
1216 static HRESULT Date_getUTCMilliseconds(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1223 if(!(date = date_this(jsthis)))
1224 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
1227 *r = jsval_number(ms_from_time(date->time));
1231 /* ECMA-262 3rd Edition 15.9.5.26 */
1232 static HRESULT Date_getTimezoneOffset(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1239 if(!(date = date_this(jsthis)))
1240 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
1243 *r = jsval_number(floor((date->time-local_time(date->time, date))/MS_PER_MINUTE));
1247 /* ECMA-262 3rd Edition 15.9.5.27 */
1248 static HRESULT Date_setTime(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1257 if(!(date = date_this(jsthis)))
1258 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
1261 return throw_type_error(ctx, JS_E_MISSING_ARG, NULL);
1263 hres = to_number(ctx, argv[0], &n);
1267 date->time = time_clip(n);
1270 *r = jsval_number(date->time);
1274 /* ECMA-262 3rd Edition 15.9.5.28 */
1275 static HRESULT Date_setMilliseconds(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1284 if(!(date = date_this(jsthis)))
1285 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
1288 return throw_type_error(ctx, JS_E_MISSING_ARG, NULL);
1290 hres = to_number(ctx, argv[0], &n);
1294 t = local_time(date->time, date);
1295 t = make_date(day(t), make_time(hour_from_time(t), min_from_time(t),
1296 sec_from_time(t), n));
1297 date->time = time_clip(utc(t, date));
1300 *r = jsval_number(date->time);
1304 /* ECMA-262 3rd Edition 15.9.5.29 */
1305 static HRESULT Date_setUTCMilliseconds(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1314 if(!(date = date_this(jsthis)))
1315 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
1318 return throw_type_error(ctx, JS_E_MISSING_ARG, NULL);
1320 hres = to_number(ctx, argv[0], &n);
1325 t = make_date(day(t), make_time(hour_from_time(t), min_from_time(t),
1326 sec_from_time(t), n));
1327 date->time = time_clip(t);
1330 *r = jsval_number(date->time);
1334 /* ECMA-262 3rd Edition 15.9.5.30 */
1335 static HRESULT Date_setSeconds(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1344 if(!(date = date_this(jsthis)))
1345 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
1348 return throw_type_error(ctx, JS_E_MISSING_ARG, NULL);
1350 t = local_time(date->time, date);
1352 hres = to_number(ctx, argv[0], &sec);
1357 hres = to_number(ctx, argv[1], &ms);
1361 ms = ms_from_time(t);
1364 t = make_date(day(t), make_time(hour_from_time(t),
1365 min_from_time(t), sec, ms));
1366 date->time = time_clip(utc(t, date));
1369 *r = jsval_number(date->time);
1373 /* ECMA-262 3rd Edition 15.9.5.31 */
1374 static HRESULT Date_setUTCSeconds(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1383 if(!(date = date_this(jsthis)))
1384 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
1387 return throw_type_error(ctx, JS_E_MISSING_ARG, NULL);
1391 hres = to_number(ctx, argv[0], &sec);
1396 hres = to_number(ctx, argv[1], &ms);
1400 ms = ms_from_time(t);
1403 t = make_date(day(t), make_time(hour_from_time(t),
1404 min_from_time(t), sec, ms));
1405 date->time = time_clip(t);
1408 *r = jsval_number(date->time);
1412 /* ECMA-262 3rd Edition 15.9.5.33 */
1413 static HRESULT Date_setMinutes(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1417 double t, min, sec, ms;
1422 if(!(date = date_this(jsthis)))
1423 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
1426 return throw_type_error(ctx, JS_E_MISSING_ARG, NULL);
1428 t = local_time(date->time, date);
1430 hres = to_number(ctx, argv[0], &min);
1435 hres = to_number(ctx, argv[1], &sec);
1439 sec = sec_from_time(t);
1443 hres = to_number(ctx, argv[2], &ms);
1447 ms = ms_from_time(t);
1450 t = make_date(day(t), make_time(hour_from_time(t),
1452 date->time = time_clip(utc(t, date));
1455 *r = jsval_number(date->time);
1459 /* ECMA-262 3rd Edition 15.9.5.34 */
1460 static HRESULT Date_setUTCMinutes(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1464 double t, min, sec, ms;
1469 if(!(date = date_this(jsthis)))
1470 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
1473 return throw_type_error(ctx, JS_E_MISSING_ARG, NULL);
1477 hres = to_number(ctx, argv[0], &min);
1482 hres = to_number(ctx, argv[1], &sec);
1486 sec = sec_from_time(t);
1490 hres = to_number(ctx, argv[2], &ms);
1494 ms = ms_from_time(t);
1497 t = make_date(day(t), make_time(hour_from_time(t),
1499 date->time = time_clip(t);
1502 *r = jsval_number(date->time);
1506 /* ECMA-262 3rd Edition 15.9.5.35 */
1507 static HRESULT Date_setHours(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1511 double t, hour, min, sec, ms;
1516 if(!(date = date_this(jsthis)))
1517 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
1520 return throw_type_error(ctx, JS_E_MISSING_ARG, NULL);
1522 t = local_time(date->time, date);
1524 hres = to_number(ctx, argv[0], &hour);
1529 hres = to_number(ctx, argv[1], &min);
1533 min = min_from_time(t);
1537 hres = to_number(ctx, argv[2], &sec);
1541 sec = sec_from_time(t);
1545 hres = to_number(ctx, argv[3], &ms);
1549 ms = ms_from_time(t);
1552 t = make_date(day(t), make_time(hour, min, sec, ms));
1553 date->time = time_clip(utc(t, date));
1556 *r = jsval_number(date->time);
1560 /* ECMA-262 3rd Edition 15.9.5.36 */
1561 static HRESULT Date_setUTCHours(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1565 double t, hour, min, sec, ms;
1570 if(!(date = date_this(jsthis)))
1571 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
1574 return throw_type_error(ctx, JS_E_MISSING_ARG, NULL);
1578 hres = to_number(ctx, argv[0], &hour);
1583 hres = to_number(ctx, argv[1], &min);
1587 min = min_from_time(t);
1591 hres = to_number(ctx, argv[2], &sec);
1595 sec = sec_from_time(t);
1599 hres = to_number(ctx, argv[3], &ms);
1603 ms = ms_from_time(t);
1606 t = make_date(day(t), make_time(hour, min, sec, ms));
1607 date->time = time_clip(t);
1610 *r = jsval_number(date->time);
1614 /* ECMA-262 3rd Edition 15.9.5.36 */
1615 static HRESULT Date_setDate(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1624 if(!(date = date_this(jsthis)))
1625 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
1628 return throw_type_error(ctx, JS_E_MISSING_ARG, NULL);
1630 hres = to_number(ctx, argv[0], &n);
1634 t = local_time(date->time, date);
1635 t = make_date(make_day(year_from_time(t), month_from_time(t), n), time_within_day(t));
1636 date->time = time_clip(utc(t, date));
1639 *r = jsval_number(date->time);
1643 /* ECMA-262 3rd Edition 15.9.5.37 */
1644 static HRESULT Date_setUTCDate(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
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);
1659 hres = to_number(ctx, argv[0], &n);
1664 t = make_date(make_day(year_from_time(t), month_from_time(t), n), time_within_day(t));
1665 date->time = time_clip(t);
1668 *r = jsval_number(date->time);
1672 /* ECMA-262 3rd Edition 15.9.5.38 */
1673 static HRESULT Date_setMonth(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1677 DOUBLE t, month, ddate;
1682 if(!(date = date_this(jsthis)))
1683 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
1686 return throw_type_error(ctx, JS_E_MISSING_ARG, NULL);
1688 t = local_time(date->time, date);
1690 hres = to_number(ctx, argv[0], &month);
1695 hres = to_number(ctx, argv[1], &ddate);
1699 ddate = date_from_time(t);
1702 t = make_date(make_day(year_from_time(t), month, ddate),
1703 time_within_day(t));
1704 date->time = time_clip(utc(t, date));
1707 *r = jsval_number(date->time);
1711 /* ECMA-262 3rd Edition 15.9.5.39 */
1712 static HRESULT Date_setUTCMonth(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1716 double t, month, ddate;
1721 if(!(date = date_this(jsthis)))
1722 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
1725 return throw_type_error(ctx, JS_E_MISSING_ARG, NULL);
1729 hres = to_number(ctx, argv[0], &month);
1734 hres = to_number(ctx, argv[1], &ddate);
1738 ddate = date_from_time(t);
1741 t = make_date(make_day(year_from_time(t), month, ddate),
1742 time_within_day(t));
1743 date->time = time_clip(t);
1746 *r = jsval_number(date->time);
1750 /* ECMA-262 3rd Edition 15.9.5.40 */
1751 static HRESULT Date_setFullYear(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1755 double t, year, month, ddate;
1760 if(!(date = date_this(jsthis)))
1761 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
1764 return throw_type_error(ctx, JS_E_MISSING_ARG, NULL);
1766 t = local_time(date->time, date);
1768 hres = to_number(ctx, argv[0], &year);
1773 hres = to_number(ctx, argv[1], &month);
1777 month = month_from_time(t);
1781 hres = to_number(ctx, argv[2], &ddate);
1785 ddate = date_from_time(t);
1788 t = make_date(make_day(year, month, ddate), time_within_day(t));
1789 date->time = time_clip(utc(t, date));
1792 *r = jsval_number(date->time);
1796 /* ECMA-262 3rd Edition 15.9.5.41 */
1797 static HRESULT Date_setUTCFullYear(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1801 double t, year, month, ddate;
1806 if(!(date = date_this(jsthis)))
1807 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
1810 return throw_type_error(ctx, JS_E_MISSING_ARG, NULL);
1814 hres = to_number(ctx, argv[0], &year);
1819 hres = to_number(ctx, argv[1], &month);
1823 month = month_from_time(t);
1827 hres = to_number(ctx, argv[2], &ddate);
1831 ddate = date_from_time(t);
1834 t = make_date(make_day(year, month, ddate), time_within_day(t));
1835 date->time = time_clip(t);
1838 *r = jsval_number(date->time);
1842 /* ECMA-262 3rd Edition B2.4 */
1843 static HRESULT Date_getYear(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1851 if(!(date = date_this(jsthis)))
1852 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
1854 t = local_time(date->time, date);
1857 *r = jsval_number(NAN);
1861 year = year_from_time(t);
1863 *r = jsval_number((1900<=year && year<2000)?year-1900:year);
1867 /* ECMA-262 3rd Edition B2.5 */
1868 static HRESULT Date_setYear(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1877 if(!(date = date_this(jsthis)))
1878 return throw_type_error(ctx, JS_E_DATE_EXPECTED, NULL);
1881 return throw_type_error(ctx, JS_E_MISSING_ARG, NULL);
1883 t = local_time(date->time, date);
1885 hres = to_number(ctx, argv[0], &year);
1892 *r = jsval_number(NAN);
1896 year = year >= 0.0 ? floor(year) : -floor(-year);
1897 if(-1.0 < year && year < 100.0)
1900 date->time = time_clip(utc(make_date(make_day(year, month_from_time(t), date_from_time(t)), time_within_day(t)), date));
1903 *r = jsval_number(date->time);
1907 static HRESULT Date_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1914 return throw_type_error(ctx, JS_E_FUNCTION_EXPECTED, NULL);
1916 FIXME("unimplemented flags %x\n", flags);
1923 static const builtin_prop_t Date_props[] = {
1924 {getDateW, Date_getDate, PROPF_METHOD},
1925 {getDayW, Date_getDay, PROPF_METHOD},
1926 {getFullYearW, Date_getFullYear, PROPF_METHOD},
1927 {getHoursW, Date_getHours, PROPF_METHOD},
1928 {getMillisecondsW, Date_getMilliseconds, PROPF_METHOD},
1929 {getMinutesW, Date_getMinutes, PROPF_METHOD},
1930 {getMonthW, Date_getMonth, PROPF_METHOD},
1931 {getSecondsW, Date_getSeconds, PROPF_METHOD},
1932 {getTimeW, Date_getTime, PROPF_METHOD},
1933 {getTimezoneOffsetW, Date_getTimezoneOffset, PROPF_METHOD},
1934 {getUTCDateW, Date_getUTCDate, PROPF_METHOD},
1935 {getUTCDayW, Date_getUTCDay, PROPF_METHOD},
1936 {getUTCFullYearW, Date_getUTCFullYear, PROPF_METHOD},
1937 {getUTCHoursW, Date_getUTCHours, PROPF_METHOD},
1938 {getUTCMillisecondsW, Date_getUTCMilliseconds, PROPF_METHOD},
1939 {getUTCMinutesW, Date_getUTCMinutes, PROPF_METHOD},
1940 {getUTCMonthW, Date_getUTCMonth, PROPF_METHOD},
1941 {getUTCSecondsW, Date_getUTCSeconds, PROPF_METHOD},
1942 {getYearW, Date_getYear, PROPF_METHOD},
1943 {setDateW, Date_setDate, PROPF_METHOD|1},
1944 {setFullYearW, Date_setFullYear, PROPF_METHOD|3},
1945 {setHoursW, Date_setHours, PROPF_METHOD|4},
1946 {setMillisecondsW, Date_setMilliseconds, PROPF_METHOD|1},
1947 {setMinutesW, Date_setMinutes, PROPF_METHOD|3},
1948 {setMonthW, Date_setMonth, PROPF_METHOD|2},
1949 {setSecondsW, Date_setSeconds, PROPF_METHOD|2},
1950 {setTimeW, Date_setTime, PROPF_METHOD|1},
1951 {setUTCDateW, Date_setUTCDate, PROPF_METHOD|1},
1952 {setUTCFullYearW, Date_setUTCFullYear, PROPF_METHOD|3},
1953 {setUTCHoursW, Date_setUTCHours, PROPF_METHOD|4},
1954 {setUTCMillisecondsW, Date_setUTCMilliseconds, PROPF_METHOD|1},
1955 {setUTCMinutesW, Date_setUTCMinutes, PROPF_METHOD|3},
1956 {setUTCMonthW, Date_setUTCMonth, PROPF_METHOD|2},
1957 {setUTCSecondsW, Date_setUTCSeconds, PROPF_METHOD|2},
1958 {setYearW, Date_setYear, PROPF_METHOD|1},
1959 {toDateStringW, Date_toDateString, PROPF_METHOD},
1960 {toGMTStringW, Date_toGMTString, PROPF_METHOD},
1961 {toLocaleDateStringW, Date_toLocaleDateString, PROPF_METHOD},
1962 {toLocaleStringW, Date_toLocaleString, PROPF_METHOD},
1963 {toLocaleTimeStringW, Date_toLocaleTimeString, PROPF_METHOD},
1964 {toStringW, Date_toString, PROPF_METHOD},
1965 {toTimeStringW, Date_toTimeString, PROPF_METHOD},
1966 {toUTCStringW, Date_toUTCString, PROPF_METHOD},
1967 {valueOfW, Date_valueOf, PROPF_METHOD},
1970 static const builtin_info_t Date_info = {
1972 {NULL, Date_value, 0},
1973 sizeof(Date_props)/sizeof(*Date_props),
1979 static const builtin_info_t DateInst_info = {
1981 {NULL, Date_value, 0},
1987 static HRESULT create_date(script_ctx_t *ctx, jsdisp_t *object_prototype, DOUBLE time, jsdisp_t **ret)
1991 TIME_ZONE_INFORMATION tzi;
1993 GetTimeZoneInformation(&tzi);
1995 date = heap_alloc_zero(sizeof(DateInstance));
1997 return E_OUTOFMEMORY;
1999 if(object_prototype)
2000 hres = init_dispex(&date->dispex, ctx, &Date_info, object_prototype);
2002 hres = init_dispex_from_constr(&date->dispex, ctx, &DateInst_info, ctx->date_constr);
2009 date->bias = tzi.Bias;
2010 date->standardDate = tzi.StandardDate;
2011 date->standardBias = tzi.StandardBias;
2012 date->daylightDate = tzi.DaylightDate;
2013 date->daylightBias = tzi.DaylightBias;
2015 *ret = &date->dispex;
2019 static inline HRESULT date_parse(jsstr_t *input_str, double *ret) {
2020 static const DWORD string_ids[] = { LOCALE_SMONTHNAME12, LOCALE_SMONTHNAME11,
2021 LOCALE_SMONTHNAME10, LOCALE_SMONTHNAME9, LOCALE_SMONTHNAME8,
2022 LOCALE_SMONTHNAME7, LOCALE_SMONTHNAME6, LOCALE_SMONTHNAME5,
2023 LOCALE_SMONTHNAME4, LOCALE_SMONTHNAME3, LOCALE_SMONTHNAME2,
2024 LOCALE_SMONTHNAME1, LOCALE_SDAYNAME7, LOCALE_SDAYNAME1,
2025 LOCALE_SDAYNAME2, LOCALE_SDAYNAME3, LOCALE_SDAYNAME4,
2026 LOCALE_SDAYNAME5, LOCALE_SDAYNAME6 };
2027 WCHAR *strings[sizeof(string_ids)/sizeof(DWORD)];
2029 int input_len, parse_len = 0, nest_level = 0, i, size;
2030 int year = 0, month = 0, day = 0, hour = 0, min = 0, sec = 0;
2031 int ms = 0, offset = 0, hour_adjust = 0;
2032 BOOL set_year = FALSE, set_month = FALSE, set_day = FALSE, set_hour = FALSE;
2033 BOOL set_offset = FALSE, set_era = FALSE, ad = TRUE, set_am = FALSE, am = TRUE;
2034 BOOL set_hour_adjust = TRUE;
2035 TIME_ZONE_INFORMATION tzi;
2040 input_len = jsstr_length(input_str);
2041 input = input_str->str;
2043 for(i=0; i<input_len; i++) {
2044 if(input[i] == '(') nest_level++;
2045 else if(input[i] == ')') {
2052 else if(!nest_level) parse_len++;
2055 parse = heap_alloc((parse_len+1)*sizeof(WCHAR));
2057 return E_OUTOFMEMORY;
2060 for(i=0; i<input_len; i++) {
2061 if(input[i] == '(') nest_level++;
2062 else if(input[i] == ')') nest_level--;
2063 else if(!nest_level) parse[parse_len++] = toupperW(input[i]);
2065 parse[parse_len] = 0;
2067 GetTimeZoneInformation(&tzi);
2069 di.standardDate = tzi.StandardDate;
2070 di.standardBias = tzi.StandardBias;
2071 di.daylightDate = tzi.DaylightDate;
2072 di.daylightBias = tzi.DaylightBias;
2074 /* FIXME: Cache strings */
2075 lcid_en = MAKELCID(MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),SORT_DEFAULT);
2076 for(i=0; i<sizeof(string_ids)/sizeof(DWORD); i++) {
2077 size = GetLocaleInfoW(lcid_en, string_ids[i], NULL, 0);
2078 strings[i] = heap_alloc((size+1)*sizeof(WCHAR));
2082 heap_free(strings[i]);
2084 return E_OUTOFMEMORY;
2086 GetLocaleInfoW(lcid_en, string_ids[i], strings[i], size);
2089 for(i=0; i<parse_len;) {
2090 while(isspaceW(parse[i])) i++;
2091 if(parse[i] == ',') {
2092 while(parse[i] == ',') i++;
2096 if(parse[i]>='0' && parse[i]<='9') {
2097 int tmp = atoiW(&parse[i]);
2098 while(parse[i]>='0' && parse[i]<='9') i++;
2099 while(isspaceW(parse[i])) i++;
2101 if(parse[i] == ':') {
2108 while(parse[i] == ':') i++;
2109 while(isspaceW(parse[i])) i++;
2110 if(parse[i]>='0' && parse[i]<='9') {
2111 min = atoiW(&parse[i]);
2112 while(parse[i]>='0' && parse[i]<='9') i++;
2115 while(isspaceW(parse[i])) i++;
2116 while(parse[i] == ':') i++;
2117 while(isspaceW(parse[i])) i++;
2118 if(parse[i]>='0' && parse[i]<='9') {
2119 sec = atoiW(&parse[i]);
2120 while(parse[i]>='0' && parse[i]<='9') i++;
2123 else if(parse[i]=='-' || parse[i]=='/') {
2125 if(set_day || set_month || set_year) break;
2132 while(isspaceW(parse[i])) i++;
2133 while(parse[i]=='-' || parse[i]=='/') i++;
2134 while(isspaceW(parse[i])) i++;
2135 if(parse[i]<'0' || parse[i]>'9') break;
2136 day = atoiW(&parse[i]);
2137 while(parse[i]>='0' && parse[i]<='9') i++;
2139 while(parse[i]=='-' || parse[i]=='/') i++;
2140 while(isspaceW(parse[i])) i++;
2141 if(parse[i]<'0' || parse[i]>'9') break;
2142 year = atoiW(&parse[i]);
2143 while(parse[i]>='0' && parse[i]<='9') i++;
2145 else if(tmp<0) break;
2160 if(parse[i]<'A' || parse[i]>'Z') break;
2161 else if(parse[i]=='B' && (parse[i+1]=='C' ||
2162 (parse[i+1]=='.' && parse[i+2]=='C'))) {
2169 if(parse[i] == '.') i++;
2171 if(parse[i] == '.') i++;
2173 else if(parse[i]=='A' && (parse[i+1]=='D' ||
2174 (parse[i+1]=='.' && parse[i+2]=='D'))) {
2180 if(parse[i] == '.') i++;
2182 if(parse[i] == '.') i++;
2184 else if(parse[i+1]<'A' || parse[i+1]>'Z') {
2186 if(set_offset) break;
2189 if(parse[i] <= 'I') hour_adjust = parse[i]-'A'+2;
2190 else if(parse[i] == 'J') break;
2191 else if(parse[i] <= 'M') hour_adjust = parse[i]-'K'+11;
2192 else if(parse[i] <= 'Y') hour_adjust = parse[i]-'N';
2193 else hour_adjust = 1;
2196 if(parse[i] == '.') i++;
2198 else if(parse[i]=='A' && parse[i+1]=='M') {
2205 else if(parse[i]=='P' && parse[i+1]=='M') {
2212 else if((parse[i]=='U' && parse[i+1]=='T' && parse[i+2]=='C')
2213 || (parse[i]=='G' && parse[i+1]=='M' && parse[i+2]=='T')) {
2215 BOOL positive = TRUE;
2217 if(set_offset) break;
2219 set_hour_adjust = FALSE;
2222 while(isspaceW(parse[i])) i++;
2223 if(parse[i] == '-') positive = FALSE;
2224 else if(parse[i] != '+') continue;
2227 while(isspaceW(parse[i])) i++;
2228 if(parse[i]<'0' || parse[i]>'9') break;
2229 offset = atoiW(&parse[i]);
2230 while(parse[i]>='0' && parse[i]<='9') i++;
2232 if(offset<24) offset *= 60;
2233 else offset = (offset/100)*60 + offset%100;
2235 if(positive) offset = -offset;
2238 /* Month or garbage */
2241 for(size=i; parse[size]>='A' && parse[size]<='Z'; size++);
2244 for(j=0; j<sizeof(string_ids)/sizeof(DWORD); j++)
2245 if(!strncmpiW(&parse[i], strings[j], size)) break;
2248 if(set_month) break;
2252 else if(j == sizeof(string_ids)/sizeof(DWORD)) break;
2259 if(i == parse_len && set_year && set_month && set_day && (!set_am || hour<13)) {
2261 if(hour == 12) hour = 0;
2265 if(!ad) year = -year+1;
2266 else if(year<100) year += 1900;
2268 *ret = time_clip(make_date(make_day(year, month, day),
2269 make_time(hour+hour_adjust, min, sec, ms)) + offset*MS_PER_MINUTE);
2272 *ret = utc(*ret, &di);
2277 for(i=0; i<sizeof(string_ids)/sizeof(DWORD); i++)
2278 heap_free(strings[i]);
2284 static HRESULT DateConstr_parse(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
2295 *r = jsval_number(NAN);
2299 hres = to_string(ctx, argv[0], &parse_str);
2303 hres = date_parse(parse_str, &n);
2304 jsstr_release(parse_str);
2308 *r = jsval_number(n);
2312 static HRESULT date_utc(script_ctx_t *ctx, unsigned argc, jsval_t *argv, double *ret)
2314 double year, month, vdate, hours, minutes, seconds, ms;
2320 hres = to_number(ctx, argv[0], &year);
2323 if(0 <= year && year <= 99)
2330 hres = to_number(ctx, argv[1], &month);
2338 hres = to_number(ctx, argv[2], &vdate);
2346 hres = to_number(ctx, argv[3], &hours);
2354 hres = to_number(ctx, argv[4], &minutes);
2362 hres = to_number(ctx, argv[5], &seconds);
2370 hres = to_number(ctx, argv[6], &ms);
2377 *ret = time_clip(make_date(make_day(year, month, vdate),
2378 make_time(hours, minutes,seconds, ms)));
2382 static HRESULT DateConstr_UTC(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
2390 hres = date_utc(ctx, argc, argv, &n);
2391 if(SUCCEEDED(hres) && r)
2392 *r = jsval_number(n);
2396 static HRESULT DateConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
2405 case DISPATCH_CONSTRUCT:
2407 /* ECMA-262 3rd Edition 15.9.3.3 */
2412 GetSystemTimeAsFileTime(&time);
2413 lltime = ((LONGLONG)time.dwHighDateTime<<32)
2414 + time.dwLowDateTime;
2416 hres = create_date(ctx, NULL, lltime/10000-TIME_EPOCH, &date);
2422 /* ECMA-262 3rd Edition 15.9.3.2 */
2427 hres = to_primitive(ctx, argv[0], &prim, NO_HINT);
2432 hres = date_parse(get_string(prim), &n);
2434 hres = to_number(ctx, prim, &n);
2436 jsval_release(prim);
2440 hres = create_date(ctx, NULL, time_clip(n), &date);
2446 /* ECMA-262 3rd Edition 15.9.3.1 */
2451 hres = date_utc(ctx, argc, argv, &ret_date);
2455 hres = create_date(ctx, NULL, ret_date, &date);
2459 di = (DateInstance*)date;
2460 di->time = utc(di->time, di);
2464 *r = jsval_obj(date);
2468 FILETIME system_time, local_time;
2471 GetSystemTimeAsFileTime(&system_time);
2472 FileTimeToLocalFileTime(&system_time, &local_time);
2473 lltime = ((LONGLONG)local_time.dwHighDateTime<<32)
2474 + local_time.dwLowDateTime;
2476 return date_to_string(lltime/10000-TIME_EPOCH, FALSE, 0, r);
2480 FIXME("unimplemented flags %x\n", flags);
2487 static const builtin_prop_t DateConstr_props[] = {
2488 {UTCW, DateConstr_UTC, PROPF_METHOD},
2489 {parseW, DateConstr_parse, PROPF_METHOD}
2492 static const builtin_info_t DateConstr_info = {
2494 {NULL, Function_value, 0},
2495 sizeof(DateConstr_props)/sizeof(*DateConstr_props),
2501 HRESULT create_date_constr(script_ctx_t *ctx, jsdisp_t *object_prototype, jsdisp_t **ret)
2506 static const WCHAR DateW[] = {'D','a','t','e',0};
2508 hres = create_date(ctx, object_prototype, 0.0, &date);
2512 hres = create_builtin_constructor(ctx, DateConstr_value, DateW, &DateConstr_info,
2513 PROPF_CONSTR|7, date, ret);
2515 jsdisp_release(date);