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
, VARIANT
*retv
)
470 static const WCHAR NaNW
[] = { 'N','a','N',0 };
471 static const WCHAR formatW
[] = { '%','s',' ','%','s',' ','%','d',' ',
472 '%','0','2','d',':','%','0','2','d',':','%','0','2','d',' ',
473 'U','T','C','%','c','%','0','2','d','%','0','2','d',' ','%','d','%','s',0 };
474 static const WCHAR formatUTCW
[] = { '%','s',' ','%','s',' ','%','d',' ',
475 '%','0','2','d',':','%','0','2','d',':','%','0','2','d',' ',
476 'U','T','C',' ','%','d','%','s',0 };
477 static const WCHAR formatNoOffsetW
[] = { '%','s',' ','%','s',' ',
478 '%','d',' ','%','0','2','d',':','%','0','2','d',':',
479 '%','0','2','d',' ','%','d','%','s',0 };
480 static const WCHAR ADW
[] = { 0 };
481 static const WCHAR BCW
[] = { ' ','B','.','C','.',0 };
483 static const DWORD week_ids
[] = { LOCALE_SABBREVDAYNAME7
, LOCALE_SABBREVDAYNAME1
,
484 LOCALE_SABBREVDAYNAME2
, LOCALE_SABBREVDAYNAME3
, LOCALE_SABBREVDAYNAME4
,
485 LOCALE_SABBREVDAYNAME5
, LOCALE_SABBREVDAYNAME6
};
486 static const DWORD month_ids
[] = { LOCALE_SABBREVMONTHNAME1
, LOCALE_SABBREVMONTHNAME2
,
487 LOCALE_SABBREVMONTHNAME3
, LOCALE_SABBREVMONTHNAME4
,
488 LOCALE_SABBREVMONTHNAME5
, LOCALE_SABBREVMONTHNAME6
,
489 LOCALE_SABBREVMONTHNAME7
, LOCALE_SABBREVMONTHNAME8
,
490 LOCALE_SABBREVMONTHNAME9
, LOCALE_SABBREVMONTHNAME10
,
491 LOCALE_SABBREVMONTHNAME11
, LOCALE_SABBREVMONTHNAME12
};
493 BOOL formatAD
= TRUE
;
496 int len
, size
, year
, day
;
497 DWORD lcid_en
, week_id
, month_id
;
502 V_VT(retv
) = VT_BSTR
;
503 V_BSTR(retv
) = SysAllocString(NaNW
);
505 return E_OUTOFMEMORY
;
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 V_VT(retv
) = VT_BSTR
;
587 V_BSTR(retv
) = date_str
;
592 /* ECMA-262 3rd Edition 15.9.1.2 */
593 static HRESULT
dateobj_to_string(DateInstance
*date
, VARIANT
*retv
)
598 time
= local_time(date
->time
, date
);
599 offset
= date
->bias
+
600 daylight_saving_ta(time
, date
);
602 return date_to_string(time
, TRUE
, offset
, retv
);
605 static HRESULT
Date_toString(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, VARIANT
*argv
,
606 VARIANT
*retv
, jsexcept_t
*ei
)
612 if(!(date
= date_this(jsthis
)))
613 return throw_type_error(ctx
, ei
, JS_E_DATE_EXPECTED
, NULL
);
615 return dateobj_to_string(date
, retv
);
618 /* ECMA-262 3rd Edition 15.9.1.5 */
619 static HRESULT
Date_toLocaleString(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, VARIANT
*argv
,
620 VARIANT
*retv
, jsexcept_t
*ei
)
622 static const WCHAR NaNW
[] = { 'N','a','N',0 };
626 int date_len
, time_len
;
630 if(!(date
= date_this(jsthis
)))
631 return throw_type_error(ctx
, ei
, JS_E_DATE_EXPECTED
, NULL
);
633 if(isnan(date
->time
)) {
635 V_VT(retv
) = VT_BSTR
;
636 V_BSTR(retv
) = SysAllocString(NaNW
);
638 return E_OUTOFMEMORY
;
643 st
= create_systemtime(local_time(date
->time
, date
));
645 if(st
.wYear
<1601 || st
.wYear
>9999)
646 return dateobj_to_string(date
, retv
);
649 date_len
= GetDateFormatW(ctx
->lcid
, DATE_LONGDATE
, &st
, NULL
, NULL
, 0);
650 time_len
= GetTimeFormatW(ctx
->lcid
, 0, &st
, NULL
, NULL
, 0);
651 date_str
= SysAllocStringLen(NULL
, date_len
+time_len
-1);
653 return E_OUTOFMEMORY
;
654 GetDateFormatW(ctx
->lcid
, DATE_LONGDATE
, &st
, NULL
, date_str
, date_len
);
655 GetTimeFormatW(ctx
->lcid
, 0, &st
, NULL
, &date_str
[date_len
], time_len
);
656 date_str
[date_len
-1] = ' ';
658 V_VT(retv
) = VT_BSTR
;
659 V_BSTR(retv
) = date_str
;
664 static HRESULT
Date_valueOf(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, VARIANT
*argv
,
665 VARIANT
*retv
, jsexcept_t
*ei
)
671 if(!(date
= date_this(jsthis
)))
672 return throw_type_error(ctx
, ei
, JS_E_DATE_EXPECTED
, NULL
);
675 num_set_val(retv
, date
->time
);
679 static inline HRESULT
create_utc_string(script_ctx_t
*ctx
, vdisp_t
*jsthis
,
680 VARIANT
*retv
, jsexcept_t
*ei
)
682 static const WCHAR NaNW
[] = { 'N','a','N',0 };
683 static const WCHAR formatADW
[] = { '%','s',',',' ','%','d',' ','%','s',' ','%','d',' ',
684 '%','0','2','d',':','%','0','2','d',':','%','0','2','d',' ','U','T','C',0 };
685 static const WCHAR formatBCW
[] = { '%','s',',',' ','%','d',' ','%','s',' ','%','d',' ','B','.','C','.',' ',
686 '%','0','2','d',':','%','0','2','d',':','%','0','2','d',' ','U','T','C',0 };
688 static const DWORD week_ids
[] = { LOCALE_SABBREVDAYNAME7
, LOCALE_SABBREVDAYNAME1
,
689 LOCALE_SABBREVDAYNAME2
, LOCALE_SABBREVDAYNAME3
, LOCALE_SABBREVDAYNAME4
,
690 LOCALE_SABBREVDAYNAME5
, LOCALE_SABBREVDAYNAME6
};
691 static const DWORD month_ids
[] = { LOCALE_SABBREVMONTHNAME1
, LOCALE_SABBREVMONTHNAME2
,
692 LOCALE_SABBREVMONTHNAME3
, LOCALE_SABBREVMONTHNAME4
,
693 LOCALE_SABBREVMONTHNAME5
, LOCALE_SABBREVMONTHNAME6
,
694 LOCALE_SABBREVMONTHNAME7
, LOCALE_SABBREVMONTHNAME8
,
695 LOCALE_SABBREVMONTHNAME9
, LOCALE_SABBREVMONTHNAME10
,
696 LOCALE_SABBREVMONTHNAME11
, LOCALE_SABBREVMONTHNAME12
};
698 BOOL formatAD
= TRUE
;
702 int len
, size
, year
, day
;
703 DWORD lcid_en
, week_id
, month_id
;
705 if(!(date
= date_this(jsthis
)))
706 return throw_type_error(ctx
, ei
, JS_E_DATE_EXPECTED
, NULL
);
708 if(isnan(date
->time
)) {
710 V_VT(retv
) = VT_BSTR
;
711 V_BSTR(retv
) = SysAllocString(NaNW
);
713 return E_OUTOFMEMORY
;
721 lcid_en
= MAKELCID(MAKELANGID(LANG_ENGLISH
,SUBLANG_ENGLISH_US
),SORT_DEFAULT
);
723 week_id
= week_ids
[(int)week_day(date
->time
)];
724 size
= GetLocaleInfoW(lcid_en
, week_id
, NULL
, 0);
725 week
= SysAllocStringLen(NULL
, size
);
727 return E_OUTOFMEMORY
;
728 GetLocaleInfoW(lcid_en
, week_id
, week
, size
);
731 month_id
= month_ids
[(int)month_from_time(date
->time
)];
732 size
= GetLocaleInfoW(lcid_en
, month_id
, NULL
, 0);
733 month
= SysAllocStringLen(NULL
, size
);
736 return E_OUTOFMEMORY
;
738 GetLocaleInfoW(lcid_en
, month_id
, month
, size
);
741 year
= year_from_time(date
->time
);
749 year
= year_from_time(date
->time
);
756 day
= date_from_time(date
->time
);
761 day
= date_from_time(date
->time
);
763 date_str
= SysAllocStringLen(NULL
, len
);
766 SysFreeString(month
);
767 return E_OUTOFMEMORY
;
769 sprintfW(date_str
, formatAD
?formatADW
:formatBCW
, week
, day
, month
, year
,
770 (int)hour_from_time(date
->time
), (int)min_from_time(date
->time
),
771 (int)sec_from_time(date
->time
));
774 SysFreeString(month
);
776 V_VT(retv
) = VT_BSTR
;
777 V_BSTR(retv
) = date_str
;
782 /* ECMA-262 3rd Edition 15.9.5.42 */
783 static HRESULT
Date_toUTCString(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, VARIANT
*argv
,
784 VARIANT
*retv
, jsexcept_t
*ei
)
787 return create_utc_string(ctx
, jsthis
, retv
, ei
);
790 static HRESULT
Date_toGMTString(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, VARIANT
*argv
,
791 VARIANT
*retv
, jsexcept_t
*ei
)
794 return create_utc_string(ctx
, jsthis
, retv
, ei
);
797 /* ECMA-262 3rd Edition 15.9.5.3 */
798 static HRESULT
dateobj_to_date_string(DateInstance
*date
, VARIANT
*retv
)
800 static const WCHAR NaNW
[] = { 'N','a','N',0 };
801 static const WCHAR formatADW
[] = { '%','s',' ','%','s',' ','%','d',' ','%','d',0 };
802 static const WCHAR formatBCW
[] = { '%','s',' ','%','s',' ','%','d',' ','%','d',' ','B','.','C','.',0 };
804 static const DWORD week_ids
[] = { LOCALE_SABBREVDAYNAME7
, LOCALE_SABBREVDAYNAME1
,
805 LOCALE_SABBREVDAYNAME2
, LOCALE_SABBREVDAYNAME3
, LOCALE_SABBREVDAYNAME4
,
806 LOCALE_SABBREVDAYNAME5
, LOCALE_SABBREVDAYNAME6
};
807 static const DWORD month_ids
[] = { LOCALE_SABBREVMONTHNAME1
, LOCALE_SABBREVMONTHNAME2
,
808 LOCALE_SABBREVMONTHNAME3
, LOCALE_SABBREVMONTHNAME4
,
809 LOCALE_SABBREVMONTHNAME5
, LOCALE_SABBREVMONTHNAME6
,
810 LOCALE_SABBREVMONTHNAME7
, LOCALE_SABBREVMONTHNAME8
,
811 LOCALE_SABBREVMONTHNAME9
, LOCALE_SABBREVMONTHNAME10
,
812 LOCALE_SABBREVMONTHNAME11
, LOCALE_SABBREVMONTHNAME12
};
814 BOOL formatAD
= TRUE
;
818 int len
, size
, year
, day
;
819 DWORD lcid_en
, week_id
, month_id
;
821 if(isnan(date
->time
)) {
823 V_VT(retv
) = VT_BSTR
;
824 V_BSTR(retv
) = SysAllocString(NaNW
);
826 return E_OUTOFMEMORY
;
831 time
= local_time(date
->time
, date
);
836 lcid_en
= MAKELCID(MAKELANGID(LANG_ENGLISH
,SUBLANG_ENGLISH_US
),SORT_DEFAULT
);
838 week_id
= week_ids
[(int)week_day(time
)];
839 size
= GetLocaleInfoW(lcid_en
, week_id
, NULL
, 0);
840 week
= SysAllocStringLen(NULL
, size
);
842 return E_OUTOFMEMORY
;
843 GetLocaleInfoW(lcid_en
, week_id
, week
, size
);
846 month_id
= month_ids
[(int)month_from_time(time
)];
847 size
= GetLocaleInfoW(lcid_en
, month_id
, NULL
, 0);
848 month
= SysAllocStringLen(NULL
, size
);
851 return E_OUTOFMEMORY
;
853 GetLocaleInfoW(lcid_en
, month_id
, month
, size
);
856 year
= year_from_time(time
);
864 year
= year_from_time(time
);
871 day
= date_from_time(time
);
876 day
= date_from_time(time
);
878 date_str
= SysAllocStringLen(NULL
, len
);
881 SysFreeString(month
);
882 return E_OUTOFMEMORY
;
884 sprintfW(date_str
, formatAD
?formatADW
:formatBCW
, week
, month
, day
, year
);
887 SysFreeString(month
);
889 V_VT(retv
) = VT_BSTR
;
890 V_BSTR(retv
) = date_str
;
895 static HRESULT
Date_toDateString(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, VARIANT
*argv
,
896 VARIANT
*retv
, jsexcept_t
*ei
)
900 if(!(date
= date_this(jsthis
)))
901 return throw_type_error(ctx
, ei
, JS_E_DATE_EXPECTED
, NULL
);
903 return dateobj_to_date_string(date
, retv
);
906 /* ECMA-262 3rd Edition 15.9.5.4 */
907 static HRESULT
Date_toTimeString(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, VARIANT
*argv
,
908 VARIANT
*retv
, jsexcept_t
*ei
)
910 static const WCHAR NaNW
[] = { 'N','a','N',0 };
911 static const WCHAR formatW
[] = { '%','0','2','d',':','%','0','2','d',':','%','0','2','d',
912 ' ','U','T','C','%','c','%','0','2','d','%','0','2','d',0 };
913 static const WCHAR formatUTCW
[] = { '%','0','2','d',':','%','0','2','d',
914 ':','%','0','2','d',' ','U','T','C',0 };
923 if(!(date
= date_this(jsthis
)))
924 return throw_type_error(ctx
, ei
, JS_E_DATE_EXPECTED
, NULL
);
926 if(isnan(date
->time
)) {
928 V_VT(retv
) = VT_BSTR
;
929 V_BSTR(retv
) = SysAllocString(NaNW
);
931 return E_OUTOFMEMORY
;
936 time
= local_time(date
->time
, date
);
939 date_str
= SysAllocStringLen(NULL
, 17);
941 return E_OUTOFMEMORY
;
943 offset
= date
->bias
+
944 daylight_saving_ta(time
, date
);
953 sprintfW(date_str
, formatW
, (int)hour_from_time(time
),
954 (int)min_from_time(time
), (int)sec_from_time(time
),
955 sign
, offset
/60, offset
%60);
957 sprintfW(date_str
, formatUTCW
, (int)hour_from_time(time
),
958 (int)min_from_time(time
), (int)sec_from_time(time
));
960 V_VT(retv
) = VT_BSTR
;
961 V_BSTR(retv
) = date_str
;
966 /* ECMA-262 3rd Edition 15.9.5.6 */
967 static HRESULT
Date_toLocaleDateString(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, VARIANT
*argv
,
968 VARIANT
*retv
, jsexcept_t
*ei
)
970 static const WCHAR NaNW
[] = { 'N','a','N',0 };
978 if(!(date
= date_this(jsthis
)))
979 return throw_type_error(ctx
, ei
, JS_E_DATE_EXPECTED
, NULL
);
981 if(isnan(date
->time
)) {
983 V_VT(retv
) = VT_BSTR
;
984 V_BSTR(retv
) = SysAllocString(NaNW
);
986 return E_OUTOFMEMORY
;
991 st
= create_systemtime(local_time(date
->time
, date
));
993 if(st
.wYear
<1601 || st
.wYear
>9999)
994 return dateobj_to_date_string(date
, retv
);
997 len
= GetDateFormatW(ctx
->lcid
, DATE_LONGDATE
, &st
, NULL
, NULL
, 0);
998 date_str
= SysAllocStringLen(NULL
, len
);
1000 return E_OUTOFMEMORY
;
1001 GetDateFormatW(ctx
->lcid
, DATE_LONGDATE
, &st
, NULL
, date_str
, len
);
1003 V_VT(retv
) = VT_BSTR
;
1004 V_BSTR(retv
) = date_str
;
1009 /* ECMA-262 3rd Edition 15.9.5.7 */
1010 static HRESULT
Date_toLocaleTimeString(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, VARIANT
*argv
,
1011 VARIANT
*retv
, jsexcept_t
*ei
)
1013 static const WCHAR NaNW
[] = { 'N','a','N',0 };
1021 if(!(date
= date_this(jsthis
)))
1022 return throw_type_error(ctx
, ei
, JS_E_DATE_EXPECTED
, NULL
);
1024 if(isnan(date
->time
)) {
1026 V_VT(retv
) = VT_BSTR
;
1027 V_BSTR(retv
) = SysAllocString(NaNW
);
1029 return E_OUTOFMEMORY
;
1034 st
= create_systemtime(local_time(date
->time
, date
));
1036 if(st
.wYear
<1601 || st
.wYear
>9999)
1037 return Date_toTimeString(ctx
, jsthis
, flags
, argc
, argv
, retv
, ei
);
1040 len
= GetTimeFormatW(ctx
->lcid
, 0, &st
, NULL
, NULL
, 0);
1041 date_str
= SysAllocStringLen(NULL
, len
);
1043 return E_OUTOFMEMORY
;
1044 GetTimeFormatW(ctx
->lcid
, 0, &st
, NULL
, date_str
, len
);
1046 V_VT(retv
) = VT_BSTR
;
1047 V_BSTR(retv
) = date_str
;
1052 /* ECMA-262 3rd Edition 15.9.5.9 */
1053 static HRESULT
Date_getTime(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, VARIANT
*argv
,
1054 VARIANT
*retv
, jsexcept_t
*ei
)
1060 if(!(date
= date_this(jsthis
)))
1061 return throw_type_error(ctx
, ei
, JS_E_DATE_EXPECTED
, NULL
);
1064 num_set_val(retv
, date
->time
);
1068 /* ECMA-262 3rd Edition 15.9.5.10 */
1069 static HRESULT
Date_getFullYear(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, VARIANT
*argv
,
1070 VARIANT
*retv
, jsexcept_t
*ei
)
1076 if(!(date
= date_this(jsthis
)))
1077 return throw_type_error(ctx
, ei
, JS_E_DATE_EXPECTED
, NULL
);
1080 DOUBLE time
= local_time(date
->time
, date
);
1082 num_set_val(retv
, year_from_time(time
));
1087 /* ECMA-262 3rd Edition 15.9.5.11 */
1088 static HRESULT
Date_getUTCFullYear(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, VARIANT
*argv
,
1089 VARIANT
*retv
, jsexcept_t
*ei
)
1095 if(!(date
= date_this(jsthis
)))
1096 return throw_type_error(ctx
, ei
, JS_E_DATE_EXPECTED
, NULL
);
1099 num_set_val(retv
, year_from_time(date
->time
));
1103 /* ECMA-262 3rd Edition 15.9.5.12 */
1104 static HRESULT
Date_getMonth(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, VARIANT
*argv
,
1105 VARIANT
*retv
, jsexcept_t
*ei
)
1111 if(!(date
= date_this(jsthis
)))
1112 return throw_type_error(ctx
, ei
, JS_E_DATE_EXPECTED
, NULL
);
1115 DOUBLE time
= local_time(date
->time
, date
);
1117 num_set_val(retv
, month_from_time(time
));
1122 /* ECMA-262 3rd Edition 15.9.5.13 */
1123 static HRESULT
Date_getUTCMonth(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, VARIANT
*argv
,
1124 VARIANT
*retv
, jsexcept_t
*ei
)
1130 if(!(date
= date_this(jsthis
)))
1131 return throw_type_error(ctx
, ei
, JS_E_DATE_EXPECTED
, NULL
);
1134 num_set_val(retv
, month_from_time(date
->time
));
1138 /* ECMA-262 3rd Edition 15.9.5.14 */
1139 static HRESULT
Date_getDate(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, VARIANT
*argv
,
1140 VARIANT
*retv
, jsexcept_t
*ei
)
1146 if(!(date
= date_this(jsthis
)))
1147 return throw_type_error(ctx
, ei
, JS_E_DATE_EXPECTED
, NULL
);
1150 DOUBLE time
= local_time(date
->time
, date
);
1152 num_set_val(retv
, date_from_time(time
));
1157 /* ECMA-262 3rd Edition 15.9.5.15 */
1158 static HRESULT
Date_getUTCDate(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, VARIANT
*argv
,
1159 VARIANT
*retv
, jsexcept_t
*ei
)
1165 if(!(date
= date_this(jsthis
)))
1166 return throw_type_error(ctx
, ei
, JS_E_DATE_EXPECTED
, NULL
);
1169 num_set_val(retv
, date_from_time(date
->time
));
1173 /* ECMA-262 3rd Edition 15.9.5.16 */
1174 static HRESULT
Date_getDay(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, VARIANT
*argv
,
1175 VARIANT
*retv
, jsexcept_t
*ei
)
1181 if(!(date
= date_this(jsthis
)))
1182 return throw_type_error(ctx
, ei
, JS_E_DATE_EXPECTED
, NULL
);
1185 DOUBLE time
= local_time(date
->time
, date
);
1187 num_set_val(retv
, week_day(time
));
1192 /* ECMA-262 3rd Edition 15.9.5.17 */
1193 static HRESULT
Date_getUTCDay(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, VARIANT
*argv
,
1194 VARIANT
*retv
, jsexcept_t
*ei
)
1200 if(!(date
= date_this(jsthis
)))
1201 return throw_type_error(ctx
, ei
, JS_E_DATE_EXPECTED
, NULL
);
1204 num_set_val(retv
, week_day(date
->time
));
1208 /* ECMA-262 3rd Edition 15.9.5.18 */
1209 static HRESULT
Date_getHours(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, VARIANT
*argv
,
1210 VARIANT
*retv
, jsexcept_t
*ei
)
1216 if(!(date
= date_this(jsthis
)))
1217 return throw_type_error(ctx
, ei
, JS_E_DATE_EXPECTED
, NULL
);
1220 DOUBLE time
= local_time(date
->time
, date
);
1222 num_set_val(retv
, hour_from_time(time
));
1227 /* ECMA-262 3rd Edition 15.9.5.19 */
1228 static HRESULT
Date_getUTCHours(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, VARIANT
*argv
,
1229 VARIANT
*retv
, jsexcept_t
*ei
)
1235 if(!(date
= date_this(jsthis
)))
1236 return throw_type_error(ctx
, ei
, JS_E_DATE_EXPECTED
, NULL
);
1239 num_set_val(retv
, hour_from_time(date
->time
));
1243 /* ECMA-262 3rd Edition 15.9.5.20 */
1244 static HRESULT
Date_getMinutes(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, VARIANT
*argv
,
1245 VARIANT
*retv
, jsexcept_t
*ei
)
1251 if(!(date
= date_this(jsthis
)))
1252 return throw_type_error(ctx
, ei
, JS_E_DATE_EXPECTED
, NULL
);
1255 DOUBLE time
= local_time(date
->time
, date
);
1257 num_set_val(retv
, min_from_time(time
));
1262 /* ECMA-262 3rd Edition 15.9.5.21 */
1263 static HRESULT
Date_getUTCMinutes(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, VARIANT
*argv
,
1264 VARIANT
*retv
, jsexcept_t
*ei
)
1270 if(!(date
= date_this(jsthis
)))
1271 return throw_type_error(ctx
, ei
, JS_E_DATE_EXPECTED
, NULL
);
1274 num_set_val(retv
, min_from_time(date
->time
));
1278 /* ECMA-262 3rd Edition 15.9.5.22 */
1279 static HRESULT
Date_getSeconds(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, VARIANT
*argv
,
1280 VARIANT
*retv
, jsexcept_t
*ei
)
1286 if(!(date
= date_this(jsthis
)))
1287 return throw_type_error(ctx
, ei
, JS_E_DATE_EXPECTED
, NULL
);
1290 DOUBLE time
= local_time(date
->time
, date
);
1292 num_set_val(retv
, sec_from_time(time
));
1297 /* ECMA-262 3rd Edition 15.9.5.23 */
1298 static HRESULT
Date_getUTCSeconds(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, VARIANT
*argv
,
1299 VARIANT
*retv
, jsexcept_t
*ei
)
1305 if(!(date
= date_this(jsthis
)))
1306 return throw_type_error(ctx
, ei
, JS_E_DATE_EXPECTED
, NULL
);
1309 num_set_val(retv
, sec_from_time(date
->time
));
1313 /* ECMA-262 3rd Edition 15.9.5.24 */
1314 static HRESULT
Date_getMilliseconds(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, VARIANT
*argv
,
1315 VARIANT
*retv
, jsexcept_t
*ei
)
1321 if(!(date
= date_this(jsthis
)))
1322 return throw_type_error(ctx
, ei
, JS_E_DATE_EXPECTED
, NULL
);
1325 DOUBLE time
= local_time(date
->time
, date
);
1327 num_set_val(retv
, ms_from_time(time
));
1332 /* ECMA-262 3rd Edition 15.9.5.25 */
1333 static HRESULT
Date_getUTCMilliseconds(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, VARIANT
*argv
,
1334 VARIANT
*retv
, jsexcept_t
*ei
)
1340 if(!(date
= date_this(jsthis
)))
1341 return throw_type_error(ctx
, ei
, JS_E_DATE_EXPECTED
, NULL
);
1344 num_set_val(retv
, ms_from_time(date
->time
));
1348 /* ECMA-262 3rd Edition 15.9.5.26 */
1349 static HRESULT
Date_getTimezoneOffset(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, VARIANT
*argv
,
1350 VARIANT
*retv
, jsexcept_t
*ei
)
1356 if(!(date
= date_this(jsthis
)))
1357 return throw_type_error(ctx
, ei
, JS_E_DATE_EXPECTED
, NULL
);
1360 num_set_val(retv
, floor(
1361 (date
->time
-local_time(date
->time
, date
))/MS_PER_MINUTE
));
1365 /* ECMA-262 3rd Edition 15.9.5.27 */
1366 static HRESULT
Date_setTime(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, VARIANT
*argv
,
1367 VARIANT
*retv
, jsexcept_t
*ei
)
1375 if(!(date
= date_this(jsthis
)))
1376 return throw_type_error(ctx
, ei
, JS_E_DATE_EXPECTED
, NULL
);
1379 return throw_type_error(ctx
, ei
, JS_E_MISSING_ARG
, NULL
);
1381 hres
= to_number(ctx
, argv
, ei
, &n
);
1385 date
->time
= time_clip(n
);
1388 num_set_val(retv
, date
->time
);
1393 /* ECMA-262 3rd Edition 15.9.5.28 */
1394 static HRESULT
Date_setMilliseconds(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, VARIANT
*argv
,
1395 VARIANT
*retv
, jsexcept_t
*ei
)
1403 if(!(date
= date_this(jsthis
)))
1404 return throw_type_error(ctx
, ei
, JS_E_DATE_EXPECTED
, NULL
);
1407 return throw_type_error(ctx
, ei
, JS_E_MISSING_ARG
, NULL
);
1409 hres
= to_number(ctx
, argv
, ei
, &n
);
1413 t
= local_time(date
->time
, date
);
1414 t
= make_date(day(t
), make_time(hour_from_time(t
), min_from_time(t
),
1415 sec_from_time(t
), n
));
1416 date
->time
= time_clip(utc(t
, date
));
1419 num_set_val(retv
, date
->time
);
1424 /* ECMA-262 3rd Edition 15.9.5.29 */
1425 static HRESULT
Date_setUTCMilliseconds(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, VARIANT
*argv
,
1426 VARIANT
*retv
, jsexcept_t
*ei
)
1434 if(!(date
= date_this(jsthis
)))
1435 return throw_type_error(ctx
, ei
, JS_E_DATE_EXPECTED
, NULL
);
1438 return throw_type_error(ctx
, ei
, JS_E_MISSING_ARG
, NULL
);
1440 hres
= to_number(ctx
, argv
, ei
, &n
);
1445 t
= make_date(day(t
), make_time(hour_from_time(t
), min_from_time(t
),
1446 sec_from_time(t
), n
));
1447 date
->time
= time_clip(t
);
1450 num_set_val(retv
, date
->time
);
1455 /* ECMA-262 3rd Edition 15.9.5.30 */
1456 static HRESULT
Date_setSeconds(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, VARIANT
*argv
,
1457 VARIANT
*retv
, jsexcept_t
*ei
)
1465 if(!(date
= date_this(jsthis
)))
1466 return throw_type_error(ctx
, ei
, JS_E_DATE_EXPECTED
, NULL
);
1469 return throw_type_error(ctx
, ei
, JS_E_MISSING_ARG
, NULL
);
1471 t
= local_time(date
->time
, date
);
1473 hres
= to_number(ctx
, argv
, ei
, &sec
);
1478 hres
= to_number(ctx
, argv
+1, ei
, &ms
);
1482 ms
= ms_from_time(t
);
1485 t
= make_date(day(t
), make_time(hour_from_time(t
),
1486 min_from_time(t
), sec
, ms
));
1487 date
->time
= time_clip(utc(t
, date
));
1490 num_set_val(retv
, date
->time
);
1495 /* ECMA-262 3rd Edition 15.9.5.31 */
1496 static HRESULT
Date_setUTCSeconds(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, VARIANT
*argv
,
1497 VARIANT
*retv
, jsexcept_t
*ei
)
1505 if(!(date
= date_this(jsthis
)))
1506 return throw_type_error(ctx
, ei
, JS_E_DATE_EXPECTED
, NULL
);
1509 return throw_type_error(ctx
, ei
, JS_E_MISSING_ARG
, NULL
);
1513 hres
= to_number(ctx
, argv
, ei
, &sec
);
1518 hres
= to_number(ctx
, argv
+1, ei
, &ms
);
1522 ms
= ms_from_time(t
);
1525 t
= make_date(day(t
), make_time(hour_from_time(t
),
1526 min_from_time(t
), sec
, ms
));
1527 date
->time
= time_clip(t
);
1530 num_set_val(retv
, date
->time
);
1535 /* ECMA-262 3rd Edition 15.9.5.33 */
1536 static HRESULT
Date_setMinutes(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, VARIANT
*argv
,
1537 VARIANT
*retv
, jsexcept_t
*ei
)
1540 double t
, min
, sec
, ms
;
1545 if(!(date
= date_this(jsthis
)))
1546 return throw_type_error(ctx
, ei
, JS_E_DATE_EXPECTED
, NULL
);
1549 return throw_type_error(ctx
, ei
, JS_E_MISSING_ARG
, NULL
);
1551 t
= local_time(date
->time
, date
);
1553 hres
= to_number(ctx
, argv
, ei
, &min
);
1558 hres
= to_number(ctx
, argv
+1, ei
, &sec
);
1562 sec
= sec_from_time(t
);
1566 hres
= to_number(ctx
, argv
+2, ei
, &ms
);
1570 ms
= ms_from_time(t
);
1573 t
= make_date(day(t
), make_time(hour_from_time(t
),
1575 date
->time
= time_clip(utc(t
, date
));
1578 num_set_val(retv
, date
->time
);
1583 /* ECMA-262 3rd Edition 15.9.5.34 */
1584 static HRESULT
Date_setUTCMinutes(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, VARIANT
*argv
,
1585 VARIANT
*retv
, jsexcept_t
*ei
)
1588 double t
, min
, sec
, ms
;
1593 if(!(date
= date_this(jsthis
)))
1594 return throw_type_error(ctx
, ei
, JS_E_DATE_EXPECTED
, NULL
);
1597 return throw_type_error(ctx
, ei
, JS_E_MISSING_ARG
, NULL
);
1601 hres
= to_number(ctx
, argv
, ei
, &min
);
1606 hres
= to_number(ctx
, argv
+1, ei
, &sec
);
1610 sec
= sec_from_time(t
);
1614 hres
= to_number(ctx
, argv
+2, ei
, &ms
);
1618 ms
= ms_from_time(t
);
1621 t
= make_date(day(t
), make_time(hour_from_time(t
),
1623 date
->time
= time_clip(t
);
1626 num_set_val(retv
, date
->time
);
1631 /* ECMA-262 3rd Edition 15.9.5.35 */
1632 static HRESULT
Date_setHours(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, VARIANT
*argv
,
1633 VARIANT
*retv
, jsexcept_t
*ei
)
1636 double t
, hour
, min
, sec
, ms
;
1641 if(!(date
= date_this(jsthis
)))
1642 return throw_type_error(ctx
, ei
, JS_E_DATE_EXPECTED
, NULL
);
1645 return throw_type_error(ctx
, ei
, JS_E_MISSING_ARG
, NULL
);
1647 t
= local_time(date
->time
, date
);
1649 hres
= to_number(ctx
, argv
, ei
, &hour
);
1654 hres
= to_number(ctx
, argv
+1, ei
, &min
);
1658 min
= min_from_time(t
);
1662 hres
= to_number(ctx
, argv
+2, ei
, &sec
);
1666 sec
= sec_from_time(t
);
1670 hres
= to_number(ctx
, argv
+3, ei
, &ms
);
1674 ms
= ms_from_time(t
);
1677 t
= make_date(day(t
), make_time(hour
, min
, sec
, ms
));
1678 date
->time
= time_clip(utc(t
, date
));
1681 num_set_val(retv
, date
->time
);
1686 /* ECMA-262 3rd Edition 15.9.5.36 */
1687 static HRESULT
Date_setUTCHours(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, VARIANT
*argv
,
1688 VARIANT
*retv
, jsexcept_t
*ei
)
1691 double t
, hour
, min
, sec
, ms
;
1696 if(!(date
= date_this(jsthis
)))
1697 return throw_type_error(ctx
, ei
, JS_E_DATE_EXPECTED
, NULL
);
1700 return throw_type_error(ctx
, ei
, JS_E_MISSING_ARG
, NULL
);
1704 hres
= to_number(ctx
, argv
, ei
, &hour
);
1709 hres
= to_number(ctx
, argv
+1, ei
, &min
);
1713 min
= min_from_time(t
);
1717 hres
= to_number(ctx
, argv
+2, ei
, &sec
);
1721 sec
= sec_from_time(t
);
1725 hres
= to_number(ctx
, argv
+3, ei
, &ms
);
1729 ms
= ms_from_time(t
);
1732 t
= make_date(day(t
), make_time(hour
, min
, sec
, ms
));
1733 date
->time
= time_clip(t
);
1736 num_set_val(retv
, date
->time
);
1741 /* ECMA-262 3rd Edition 15.9.5.36 */
1742 static HRESULT
Date_setDate(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, VARIANT
*argv
,
1743 VARIANT
*retv
, jsexcept_t
*ei
)
1751 if(!(date
= date_this(jsthis
)))
1752 return throw_type_error(ctx
, ei
, JS_E_DATE_EXPECTED
, NULL
);
1755 return throw_type_error(ctx
, ei
, JS_E_MISSING_ARG
, NULL
);
1757 hres
= to_number(ctx
, argv
, ei
, &n
);
1761 t
= local_time(date
->time
, date
);
1762 t
= make_date(make_day(year_from_time(t
), month_from_time(t
), n
), time_within_day(t
));
1763 date
->time
= time_clip(utc(t
, date
));
1766 num_set_val(retv
, date
->time
);
1771 /* ECMA-262 3rd Edition 15.9.5.37 */
1772 static HRESULT
Date_setUTCDate(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, VARIANT
*argv
,
1773 VARIANT
*retv
, jsexcept_t
*ei
)
1781 if(!(date
= date_this(jsthis
)))
1782 return throw_type_error(ctx
, ei
, JS_E_DATE_EXPECTED
, NULL
);
1785 return throw_type_error(ctx
, ei
, JS_E_MISSING_ARG
, NULL
);
1787 hres
= to_number(ctx
, argv
, ei
, &n
);
1792 t
= make_date(make_day(year_from_time(t
), month_from_time(t
), n
), time_within_day(t
));
1793 date
->time
= time_clip(t
);
1796 num_set_val(retv
, date
->time
);
1801 /* ECMA-262 3rd Edition 15.9.5.38 */
1802 static HRESULT
Date_setMonth(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, VARIANT
*argv
,
1803 VARIANT
*retv
, jsexcept_t
*ei
)
1806 DOUBLE t
, month
, ddate
;
1811 if(!(date
= date_this(jsthis
)))
1812 return throw_type_error(ctx
, ei
, JS_E_DATE_EXPECTED
, NULL
);
1815 return throw_type_error(ctx
, ei
, JS_E_MISSING_ARG
, NULL
);
1817 t
= local_time(date
->time
, date
);
1819 hres
= to_number(ctx
, argv
, ei
, &month
);
1824 hres
= to_number(ctx
, argv
+1, ei
, &ddate
);
1828 ddate
= date_from_time(t
);
1831 t
= make_date(make_day(year_from_time(t
), month
, ddate
),
1832 time_within_day(t
));
1833 date
->time
= time_clip(utc(t
, date
));
1836 num_set_val(retv
, date
->time
);
1841 /* ECMA-262 3rd Edition 15.9.5.39 */
1842 static HRESULT
Date_setUTCMonth(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, VARIANT
*argv
,
1843 VARIANT
*retv
, jsexcept_t
*ei
)
1846 double t
, month
, ddate
;
1851 if(!(date
= date_this(jsthis
)))
1852 return throw_type_error(ctx
, ei
, JS_E_DATE_EXPECTED
, NULL
);
1855 return throw_type_error(ctx
, ei
, JS_E_MISSING_ARG
, NULL
);
1859 hres
= to_number(ctx
, argv
, ei
, &month
);
1864 hres
= to_number(ctx
, argv
+1, ei
, &ddate
);
1868 ddate
= date_from_time(t
);
1871 t
= make_date(make_day(year_from_time(t
), month
, ddate
),
1872 time_within_day(t
));
1873 date
->time
= time_clip(t
);
1876 num_set_val(retv
, date
->time
);
1881 /* ECMA-262 3rd Edition 15.9.5.40 */
1882 static HRESULT
Date_setFullYear(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, VARIANT
*argv
,
1883 VARIANT
*retv
, jsexcept_t
*ei
)
1886 double t
, year
, month
, ddate
;
1891 if(!(date
= date_this(jsthis
)))
1892 return throw_type_error(ctx
, ei
, JS_E_DATE_EXPECTED
, NULL
);
1895 return throw_type_error(ctx
, ei
, JS_E_MISSING_ARG
, NULL
);
1897 t
= local_time(date
->time
, date
);
1899 hres
= to_number(ctx
, argv
, ei
, &year
);
1904 hres
= to_number(ctx
, argv
+1, ei
, &month
);
1908 month
= month_from_time(t
);
1912 hres
= to_number(ctx
, argv
+2, ei
, &ddate
);
1916 ddate
= date_from_time(t
);
1919 t
= make_date(make_day(year
, month
, ddate
), time_within_day(t
));
1920 date
->time
= time_clip(utc(t
, date
));
1923 num_set_val(retv
, date
->time
);
1928 /* ECMA-262 3rd Edition 15.9.5.41 */
1929 static HRESULT
Date_setUTCFullYear(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, VARIANT
*argv
,
1930 VARIANT
*retv
, jsexcept_t
*ei
)
1933 double t
, year
, month
, ddate
;
1938 if(!(date
= date_this(jsthis
)))
1939 return throw_type_error(ctx
, ei
, JS_E_DATE_EXPECTED
, NULL
);
1942 return throw_type_error(ctx
, ei
, JS_E_MISSING_ARG
, NULL
);
1946 hres
= to_number(ctx
, argv
, ei
, &year
);
1951 hres
= to_number(ctx
, argv
+1, ei
, &month
);
1955 month
= month_from_time(t
);
1959 hres
= to_number(ctx
, argv
+2, ei
, &ddate
);
1963 ddate
= date_from_time(t
);
1966 t
= make_date(make_day(year
, month
, ddate
), time_within_day(t
));
1967 date
->time
= time_clip(t
);
1970 num_set_val(retv
, date
->time
);
1975 /* ECMA-262 3rd Edition B2.4 */
1976 static HRESULT
Date_getYear(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, VARIANT
*argv
,
1977 VARIANT
*retv
, jsexcept_t
*ei
)
1984 if(!(date
= date_this(jsthis
)))
1985 return throw_type_error(ctx
, ei
, JS_E_DATE_EXPECTED
, NULL
);
1987 t
= local_time(date
->time
, date
);
1990 num_set_val(retv
, NAN
);
1994 year
= year_from_time(t
);
1996 num_set_val(retv
, (1900<=year
&& year
<2000)?year
-1900:year
);
2001 /* ECMA-262 3rd Edition B2.5 */
2002 static HRESULT
Date_setYear(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, VARIANT
*argv
,
2003 VARIANT
*retv
, jsexcept_t
*ei
)
2011 if(!(date
= date_this(jsthis
)))
2012 return throw_type_error(ctx
, ei
, JS_E_DATE_EXPECTED
, NULL
);
2015 return throw_type_error(ctx
, ei
, JS_E_MISSING_ARG
, NULL
);
2017 t
= local_time(date
->time
, date
);
2019 hres
= to_number(ctx
, argv
, ei
, &year
);
2026 num_set_val(retv
, NAN
);
2030 year
= year
>= 0.0 ? floor(year
) : -floor(-year
);
2031 if(-1.0 < year
&& year
< 100.0)
2034 date
->time
= time_clip(utc(make_date(make_day(year
, month_from_time(t
), date_from_time(t
)), time_within_day(t
)), date
));
2037 num_set_val(retv
, date
->time
);
2041 static HRESULT
Date_value(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, VARIANT
*argv
,
2042 VARIANT
*retv
, jsexcept_t
*ei
)
2048 return throw_type_error(ctx
, ei
, JS_E_FUNCTION_EXPECTED
, NULL
);
2050 FIXME("unimplemented flags %x\n", flags
);
2057 static const builtin_prop_t Date_props
[] = {
2058 {getDateW
, Date_getDate
, PROPF_METHOD
},
2059 {getDayW
, Date_getDay
, PROPF_METHOD
},
2060 {getFullYearW
, Date_getFullYear
, PROPF_METHOD
},
2061 {getHoursW
, Date_getHours
, PROPF_METHOD
},
2062 {getMillisecondsW
, Date_getMilliseconds
, PROPF_METHOD
},
2063 {getMinutesW
, Date_getMinutes
, PROPF_METHOD
},
2064 {getMonthW
, Date_getMonth
, PROPF_METHOD
},
2065 {getSecondsW
, Date_getSeconds
, PROPF_METHOD
},
2066 {getTimeW
, Date_getTime
, PROPF_METHOD
},
2067 {getTimezoneOffsetW
, Date_getTimezoneOffset
, PROPF_METHOD
},
2068 {getUTCDateW
, Date_getUTCDate
, PROPF_METHOD
},
2069 {getUTCDayW
, Date_getUTCDay
, PROPF_METHOD
},
2070 {getUTCFullYearW
, Date_getUTCFullYear
, PROPF_METHOD
},
2071 {getUTCHoursW
, Date_getUTCHours
, PROPF_METHOD
},
2072 {getUTCMillisecondsW
, Date_getUTCMilliseconds
, PROPF_METHOD
},
2073 {getUTCMinutesW
, Date_getUTCMinutes
, PROPF_METHOD
},
2074 {getUTCMonthW
, Date_getUTCMonth
, PROPF_METHOD
},
2075 {getUTCSecondsW
, Date_getUTCSeconds
, PROPF_METHOD
},
2076 {getYearW
, Date_getYear
, PROPF_METHOD
},
2077 {setDateW
, Date_setDate
, PROPF_METHOD
|1},
2078 {setFullYearW
, Date_setFullYear
, PROPF_METHOD
|3},
2079 {setHoursW
, Date_setHours
, PROPF_METHOD
|4},
2080 {setMillisecondsW
, Date_setMilliseconds
, PROPF_METHOD
|1},
2081 {setMinutesW
, Date_setMinutes
, PROPF_METHOD
|3},
2082 {setMonthW
, Date_setMonth
, PROPF_METHOD
|2},
2083 {setSecondsW
, Date_setSeconds
, PROPF_METHOD
|2},
2084 {setTimeW
, Date_setTime
, PROPF_METHOD
|1},
2085 {setUTCDateW
, Date_setUTCDate
, PROPF_METHOD
|1},
2086 {setUTCFullYearW
, Date_setUTCFullYear
, PROPF_METHOD
|3},
2087 {setUTCHoursW
, Date_setUTCHours
, PROPF_METHOD
|4},
2088 {setUTCMillisecondsW
, Date_setUTCMilliseconds
, PROPF_METHOD
|1},
2089 {setUTCMinutesW
, Date_setUTCMinutes
, PROPF_METHOD
|3},
2090 {setUTCMonthW
, Date_setUTCMonth
, PROPF_METHOD
|2},
2091 {setUTCSecondsW
, Date_setUTCSeconds
, PROPF_METHOD
|2},
2092 {setYearW
, Date_setYear
, PROPF_METHOD
|1},
2093 {toDateStringW
, Date_toDateString
, PROPF_METHOD
},
2094 {toGMTStringW
, Date_toGMTString
, PROPF_METHOD
},
2095 {toLocaleDateStringW
, Date_toLocaleDateString
, PROPF_METHOD
},
2096 {toLocaleStringW
, Date_toLocaleString
, PROPF_METHOD
},
2097 {toLocaleTimeStringW
, Date_toLocaleTimeString
, PROPF_METHOD
},
2098 {toStringW
, Date_toString
, PROPF_METHOD
},
2099 {toTimeStringW
, Date_toTimeString
, PROPF_METHOD
},
2100 {toUTCStringW
, Date_toUTCString
, PROPF_METHOD
},
2101 {valueOfW
, Date_valueOf
, PROPF_METHOD
},
2104 static const builtin_info_t Date_info
= {
2106 {NULL
, Date_value
, 0},
2107 sizeof(Date_props
)/sizeof(*Date_props
),
2113 static const builtin_info_t DateInst_info
= {
2115 {NULL
, Date_value
, 0},
2121 static HRESULT
create_date(script_ctx_t
*ctx
, jsdisp_t
*object_prototype
, DOUBLE time
, jsdisp_t
**ret
)
2125 TIME_ZONE_INFORMATION tzi
;
2127 GetTimeZoneInformation(&tzi
);
2129 date
= heap_alloc_zero(sizeof(DateInstance
));
2131 return E_OUTOFMEMORY
;
2133 if(object_prototype
)
2134 hres
= init_dispex(&date
->dispex
, ctx
, &Date_info
, object_prototype
);
2136 hres
= init_dispex_from_constr(&date
->dispex
, ctx
, &DateInst_info
, ctx
->date_constr
);
2143 date
->bias
= tzi
.Bias
;
2144 date
->standardDate
= tzi
.StandardDate
;
2145 date
->standardBias
= tzi
.StandardBias
;
2146 date
->daylightDate
= tzi
.DaylightDate
;
2147 date
->daylightBias
= tzi
.DaylightBias
;
2149 *ret
= &date
->dispex
;
2153 static inline HRESULT
date_parse(BSTR input
, double *ret
) {
2154 static const DWORD string_ids
[] = { LOCALE_SMONTHNAME12
, LOCALE_SMONTHNAME11
,
2155 LOCALE_SMONTHNAME10
, LOCALE_SMONTHNAME9
, LOCALE_SMONTHNAME8
,
2156 LOCALE_SMONTHNAME7
, LOCALE_SMONTHNAME6
, LOCALE_SMONTHNAME5
,
2157 LOCALE_SMONTHNAME4
, LOCALE_SMONTHNAME3
, LOCALE_SMONTHNAME2
,
2158 LOCALE_SMONTHNAME1
, LOCALE_SDAYNAME7
, LOCALE_SDAYNAME1
,
2159 LOCALE_SDAYNAME2
, LOCALE_SDAYNAME3
, LOCALE_SDAYNAME4
,
2160 LOCALE_SDAYNAME5
, LOCALE_SDAYNAME6
};
2161 BSTR strings
[sizeof(string_ids
)/sizeof(DWORD
)];
2164 int input_len
, parse_len
= 0, nest_level
= 0, i
, size
;
2165 int year
= 0, month
= 0, day
= 0, hour
= 0, min
= 0, sec
= 0;
2166 int ms
= 0, offset
= 0, hour_adjust
= 0;
2167 BOOL set_year
= FALSE
, set_month
= FALSE
, set_day
= FALSE
, set_hour
= FALSE
;
2168 BOOL set_offset
= FALSE
, set_era
= FALSE
, ad
= TRUE
, set_am
= FALSE
, am
= TRUE
;
2169 BOOL set_hour_adjust
= TRUE
;
2170 TIME_ZONE_INFORMATION tzi
;
2174 input_len
= SysStringLen(input
);
2175 for(i
=0; i
<input_len
; i
++) {
2176 if(input
[i
] == '(') nest_level
++;
2177 else if(input
[i
] == ')') {
2184 else if(!nest_level
) parse_len
++;
2187 parse
= SysAllocStringLen(NULL
, parse_len
);
2189 return E_OUTOFMEMORY
;
2192 for(i
=0; i
<input_len
; i
++) {
2193 if(input
[i
] == '(') nest_level
++;
2194 else if(input
[i
] == ')') nest_level
--;
2195 else if(!nest_level
) parse
[parse_len
++] = toupperW(input
[i
]);
2198 GetTimeZoneInformation(&tzi
);
2200 di
.standardDate
= tzi
.StandardDate
;
2201 di
.standardBias
= tzi
.StandardBias
;
2202 di
.daylightDate
= tzi
.DaylightDate
;
2203 di
.daylightBias
= tzi
.DaylightBias
;
2205 lcid_en
= MAKELCID(MAKELANGID(LANG_ENGLISH
,SUBLANG_ENGLISH_US
),SORT_DEFAULT
);
2206 for(i
=0; i
<sizeof(string_ids
)/sizeof(DWORD
); i
++) {
2207 size
= GetLocaleInfoW(lcid_en
, string_ids
[i
], NULL
, 0);
2208 strings
[i
] = SysAllocStringLen(NULL
, size
);
2212 SysFreeString(strings
[i
]);
2213 SysFreeString(parse
);
2214 return E_OUTOFMEMORY
;
2216 GetLocaleInfoW(lcid_en
, string_ids
[i
], strings
[i
], size
);
2219 for(i
=0; i
<parse_len
;) {
2220 while(isspaceW(parse
[i
])) i
++;
2221 if(parse
[i
] == ',') {
2222 while(parse
[i
] == ',') i
++;
2226 if(parse
[i
]>='0' && parse
[i
]<='9') {
2227 int tmp
= atoiW(&parse
[i
]);
2228 while(parse
[i
]>='0' && parse
[i
]<='9') i
++;
2229 while(isspaceW(parse
[i
])) i
++;
2231 if(parse
[i
] == ':') {
2238 while(parse
[i
] == ':') i
++;
2239 while(isspaceW(parse
[i
])) i
++;
2240 if(parse
[i
]>='0' && parse
[i
]<='9') {
2241 min
= atoiW(&parse
[i
]);
2242 while(parse
[i
]>='0' && parse
[i
]<='9') i
++;
2245 while(isspaceW(parse
[i
])) i
++;
2246 while(parse
[i
] == ':') i
++;
2247 while(isspaceW(parse
[i
])) i
++;
2248 if(parse
[i
]>='0' && parse
[i
]<='9') {
2249 sec
= atoiW(&parse
[i
]);
2250 while(parse
[i
]>='0' && parse
[i
]<='9') i
++;
2253 else if(parse
[i
]=='-' || parse
[i
]=='/') {
2255 if(set_day
|| set_month
|| set_year
) break;
2262 while(isspaceW(parse
[i
])) i
++;
2263 while(parse
[i
]=='-' || parse
[i
]=='/') i
++;
2264 while(isspaceW(parse
[i
])) i
++;
2265 if(parse
[i
]<'0' || parse
[i
]>'9') break;
2266 day
= atoiW(&parse
[i
]);
2267 while(parse
[i
]>='0' && parse
[i
]<='9') i
++;
2269 while(parse
[i
]=='-' || parse
[i
]=='/') i
++;
2270 while(isspaceW(parse
[i
])) i
++;
2271 if(parse
[i
]<'0' || parse
[i
]>'9') break;
2272 year
= atoiW(&parse
[i
]);
2273 while(parse
[i
]>='0' && parse
[i
]<='9') i
++;
2275 else if(tmp
<0) break;
2290 if(parse
[i
]<'A' || parse
[i
]>'Z') break;
2291 else if(parse
[i
]=='B' && (parse
[i
+1]=='C' ||
2292 (parse
[i
+1]=='.' && parse
[i
+2]=='C'))) {
2299 if(parse
[i
] == '.') i
++;
2301 if(parse
[i
] == '.') i
++;
2303 else if(parse
[i
]=='A' && (parse
[i
+1]=='D' ||
2304 (parse
[i
+1]=='.' && parse
[i
+2]=='D'))) {
2310 if(parse
[i
] == '.') i
++;
2312 if(parse
[i
] == '.') i
++;
2314 else if(parse
[i
+1]<'A' || parse
[i
+1]>'Z') {
2316 if(set_offset
) break;
2319 if(parse
[i
] <= 'I') hour_adjust
= parse
[i
]-'A'+2;
2320 else if(parse
[i
] == 'J') break;
2321 else if(parse
[i
] <= 'M') hour_adjust
= parse
[i
]-'K'+11;
2322 else if(parse
[i
] <= 'Y') hour_adjust
= parse
[i
]-'N';
2323 else hour_adjust
= 1;
2326 if(parse
[i
] == '.') i
++;
2328 else if(parse
[i
]=='A' && parse
[i
+1]=='M') {
2335 else if(parse
[i
]=='P' && parse
[i
+1]=='M') {
2342 else if((parse
[i
]=='U' && parse
[i
+1]=='T' && parse
[i
+2]=='C')
2343 || (parse
[i
]=='G' && parse
[i
+1]=='M' && parse
[i
+2]=='T')) {
2345 BOOL positive
= TRUE
;
2347 if(set_offset
) break;
2349 set_hour_adjust
= FALSE
;
2352 while(isspaceW(parse
[i
])) i
++;
2353 if(parse
[i
] == '-') positive
= FALSE
;
2354 else if(parse
[i
] != '+') continue;
2357 while(isspaceW(parse
[i
])) i
++;
2358 if(parse
[i
]<'0' || parse
[i
]>'9') break;
2359 offset
= atoiW(&parse
[i
]);
2360 while(parse
[i
]>='0' && parse
[i
]<='9') i
++;
2362 if(offset
<24) offset
*= 60;
2363 else offset
= (offset
/100)*60 + offset
%100;
2365 if(positive
) offset
= -offset
;
2368 /* Month or garbage */
2371 for(size
=i
; parse
[size
]>='A' && parse
[size
]<='Z'; size
++);
2374 for(j
=0; j
<sizeof(string_ids
)/sizeof(DWORD
); j
++)
2375 if(!memicmpW(&parse
[i
], strings
[j
], size
)) break;
2378 if(set_month
) break;
2382 else if(j
== sizeof(string_ids
)/sizeof(DWORD
)) break;
2389 if(i
== parse_len
&& set_year
&& set_month
&& set_day
&& (!set_am
|| hour
<13)) {
2391 if(hour
== 12) hour
= 0;
2395 if(!ad
) year
= -year
+1;
2396 else if(year
<100) year
+= 1900;
2398 *ret
= time_clip(make_date(make_day(year
, month
, day
),
2399 make_time(hour
+hour_adjust
, min
, sec
, ms
)) + offset
*MS_PER_MINUTE
);
2402 *ret
= utc(*ret
, &di
);
2407 for(i
=0; i
<sizeof(string_ids
)/sizeof(DWORD
); i
++)
2408 SysFreeString(strings
[i
]);
2409 SysFreeString(parse
);
2414 static HRESULT
DateConstr_parse(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, VARIANT
*argv
,
2415 VARIANT
*retv
, jsexcept_t
*ei
)
2425 num_set_val(retv
, NAN
);
2429 hres
= to_string(ctx
, argv
, ei
, &parse_str
);
2433 hres
= date_parse(parse_str
, &n
);
2434 SysFreeString(parse_str
);
2438 num_set_val(retv
, n
);
2442 static HRESULT
date_utc(script_ctx_t
*ctx
, unsigned argc
, VARIANT
*argv
, double *ret
, jsexcept_t
*ei
)
2444 double year
, month
, vdate
, hours
, minutes
, seconds
, ms
;
2450 hres
= to_number(ctx
, argv
, ei
, &year
);
2453 if(0 <= year
&& year
<= 99)
2460 hres
= to_number(ctx
, argv
+1, ei
, &month
);
2468 hres
= to_number(ctx
, argv
+2, ei
, &vdate
);
2476 hres
= to_number(ctx
, argv
+3, ei
, &hours
);
2484 hres
= to_number(ctx
, argv
+4, ei
, &minutes
);
2492 hres
= to_number(ctx
, argv
+5, ei
, &seconds
);
2500 hres
= to_number(ctx
, argv
+6, ei
, &ms
);
2507 *ret
= time_clip(make_date(make_day(year
, month
, vdate
),
2508 make_time(hours
, minutes
,seconds
, ms
)));
2512 static HRESULT
DateConstr_UTC(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, VARIANT
*argv
,
2513 VARIANT
*retv
, jsexcept_t
*ei
)
2520 hres
= date_utc(ctx
, argc
, argv
, &n
, ei
);
2521 if(SUCCEEDED(hres
) && retv
)
2522 num_set_val(retv
, n
);
2526 static HRESULT
DateConstr_value(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, VARIANT
*argv
,
2527 VARIANT
*retv
, jsexcept_t
*ei
)
2535 case DISPATCH_CONSTRUCT
:
2537 /* ECMA-262 3rd Edition 15.9.3.3 */
2542 GetSystemTimeAsFileTime(&time
);
2543 lltime
= ((LONGLONG
)time
.dwHighDateTime
<<32)
2544 + time
.dwLowDateTime
;
2546 hres
= create_date(ctx
, NULL
, lltime
/10000-TIME_EPOCH
, &date
);
2552 /* ECMA-262 3rd Edition 15.9.3.2 */
2557 hres
= to_primitive(ctx
, argv
, ei
, &prim
, NO_HINT
);
2561 if(V_VT(&prim
) == VT_BSTR
)
2562 hres
= date_parse(V_BSTR(&prim
), &n
);
2564 hres
= to_number(ctx
, &prim
, ei
, &n
);
2566 VariantClear(&prim
);
2570 hres
= create_date(ctx
, NULL
, time_clip(n
), &date
);
2576 /* ECMA-262 3rd Edition 15.9.3.1 */
2581 hres
= date_utc(ctx
, argc
, argv
, &ret_date
, ei
);
2585 hres
= create_date(ctx
, NULL
, ret_date
, &date
);
2589 di
= (DateInstance
*)date
;
2590 di
->time
= utc(di
->time
, di
);
2594 var_set_jsdisp(retv
, date
);
2598 FILETIME system_time
, local_time
;
2601 GetSystemTimeAsFileTime(&system_time
);
2602 FileTimeToLocalFileTime(&system_time
, &local_time
);
2603 lltime
= ((LONGLONG
)local_time
.dwHighDateTime
<<32)
2604 + local_time
.dwLowDateTime
;
2606 return date_to_string(lltime
/10000-TIME_EPOCH
, FALSE
, 0, retv
);
2610 FIXME("unimplemented flags %x\n", flags
);
2617 static const builtin_prop_t DateConstr_props
[] = {
2618 {UTCW
, DateConstr_UTC
, PROPF_METHOD
},
2619 {parseW
, DateConstr_parse
, PROPF_METHOD
}
2622 static const builtin_info_t DateConstr_info
= {
2624 {NULL
, Function_value
, 0},
2625 sizeof(DateConstr_props
)/sizeof(*DateConstr_props
),
2631 HRESULT
create_date_constr(script_ctx_t
*ctx
, jsdisp_t
*object_prototype
, jsdisp_t
**ret
)
2636 static const WCHAR DateW
[] = {'D','a','t','e',0};
2638 hres
= create_date(ctx
, object_prototype
, 0.0, &date
);
2642 hres
= create_builtin_constructor(ctx
, DateConstr_value
, DateW
, &DateConstr_info
,
2643 PROPF_CONSTR
|7, date
, ret
);
2645 jsdisp_release(date
);