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 valueOfW
[] = {'v','a','l','u','e','O','f',0};
52 static const WCHAR toISOStringW
[] = {'t','o','I','S','O','S','t','r','i','n','g',0};
53 static const WCHAR toUTCStringW
[] = {'t','o','U','T','C','S','t','r','i','n','g',0};
54 static const WCHAR toGMTStringW
[] = {'t','o','G','M','T','S','t','r','i','n','g',0};
55 static const WCHAR toDateStringW
[] = {'t','o','D','a','t','e','S','t','r','i','n','g',0};
56 static const WCHAR toTimeStringW
[] = {'t','o','T','i','m','e','S','t','r','i','n','g',0};
57 static const WCHAR toLocaleDateStringW
[] = {'t','o','L','o','c','a','l','e','D','a','t','e','S','t','r','i','n','g',0};
58 static const WCHAR toLocaleTimeStringW
[] = {'t','o','L','o','c','a','l','e','T','i','m','e','S','t','r','i','n','g',0};
59 static const WCHAR getTimeW
[] = {'g','e','t','T','i','m','e',0};
60 static const WCHAR getFullYearW
[] = {'g','e','t','F','u','l','l','Y','e','a','r',0};
61 static const WCHAR getUTCFullYearW
[] = {'g','e','t','U','T','C','F','u','l','l','Y','e','a','r',0};
62 static const WCHAR getMonthW
[] = {'g','e','t','M','o','n','t','h',0};
63 static const WCHAR getUTCMonthW
[] = {'g','e','t','U','T','C','M','o','n','t','h',0};
64 static const WCHAR getDateW
[] = {'g','e','t','D','a','t','e',0};
65 static const WCHAR getUTCDateW
[] = {'g','e','t','U','T','C','D','a','t','e',0};
66 static const WCHAR getDayW
[] = {'g','e','t','D','a','y',0};
67 static const WCHAR getUTCDayW
[] = {'g','e','t','U','T','C','D','a','y',0};
68 static const WCHAR getHoursW
[] = {'g','e','t','H','o','u','r','s',0};
69 static const WCHAR getUTCHoursW
[] = {'g','e','t','U','T','C','H','o','u','r','s',0};
70 static const WCHAR getMinutesW
[] = {'g','e','t','M','i','n','u','t','e','s',0};
71 static const WCHAR getUTCMinutesW
[] = {'g','e','t','U','T','C','M','i','n','u','t','e','s',0};
72 static const WCHAR getSecondsW
[] = {'g','e','t','S','e','c','o','n','d','s',0};
73 static const WCHAR getUTCSecondsW
[] = {'g','e','t','U','T','C','S','e','c','o','n','d','s',0};
74 static const WCHAR getMillisecondsW
[] = {'g','e','t','M','i','l','l','i','s','e','c','o','n','d','s',0};
75 static const WCHAR getUTCMillisecondsW
[] = {'g','e','t','U','T','C','M','i','l','l','i','s','e','c','o','n','d','s',0};
76 static const WCHAR getTimezoneOffsetW
[] = {'g','e','t','T','i','m','e','z','o','n','e','O','f','f','s','e','t',0};
77 static const WCHAR setTimeW
[] = {'s','e','t','T','i','m','e',0};
78 static const WCHAR setMillisecondsW
[] = {'s','e','t','M','i','l','l','i','s','e','c','o','n','d','s',0};
79 static const WCHAR setUTCMillisecondsW
[] = {'s','e','t','U','T','C','M','i','l','l','i','s','e','c','o','n','d','s',0};
80 static const WCHAR setSecondsW
[] = {'s','e','t','S','e','c','o','n','d','s',0};
81 static const WCHAR setUTCSecondsW
[] = {'s','e','t','U','T','C','S','e','c','o','n','d','s',0};
82 static const WCHAR setMinutesW
[] = {'s','e','t','M','i','n','u','t','e','s',0};
83 static const WCHAR setUTCMinutesW
[] = {'s','e','t','U','T','C','M','i','n','u','t','e','s',0};
84 static const WCHAR setHoursW
[] = {'s','e','t','H','o','u','r','s',0};
85 static const WCHAR setUTCHoursW
[] = {'s','e','t','U','T','C','H','o','u','r','s',0};
86 static const WCHAR setDateW
[] = {'s','e','t','D','a','t','e',0};
87 static const WCHAR setUTCDateW
[] = {'s','e','t','U','T','C','D','a','t','e',0};
88 static const WCHAR setMonthW
[] = {'s','e','t','M','o','n','t','h',0};
89 static const WCHAR setUTCMonthW
[] = {'s','e','t','U','T','C','M','o','n','t','h',0};
90 static const WCHAR setFullYearW
[] = {'s','e','t','F','u','l','l','Y','e','a','r',0};
91 static const WCHAR setUTCFullYearW
[] = {'s','e','t','U','T','C','F','u','l','l','Y','e','a','r',0};
92 static const WCHAR getYearW
[] = {'g','e','t','Y','e','a','r',0};
93 static const WCHAR setYearW
[] = {'s','e','t','Y','e','a','r',0};
95 static const WCHAR UTCW
[] = {'U','T','C',0};
96 static const WCHAR nowW
[] = {'n','o','w',0};
97 static const WCHAR parseW
[] = {'p','a','r','s','e',0};
99 static inline DateInstance
*date_from_jsdisp(jsdisp_t
*jsdisp
)
101 return CONTAINING_RECORD(jsdisp
, DateInstance
, dispex
);
104 static inline DateInstance
*date_this(vdisp_t
*jsthis
)
106 return is_vclass(jsthis
, JSCLASS_DATE
) ? date_from_jsdisp(jsthis
->u
.jsdisp
) : NULL
;
109 /*ECMA-262 3rd Edition 15.9.1.2 */
110 #define MS_PER_DAY 86400000
111 #define MS_PER_HOUR 3600000
112 #define MS_PER_MINUTE 60000
114 /* ECMA-262 3rd Edition 15.9.1.2 */
115 static inline DOUBLE
day(DOUBLE time
)
117 return floor(time
/ MS_PER_DAY
);
120 /* ECMA-262 3rd Edition 15.9.1.2 */
121 static inline DOUBLE
time_within_day(DOUBLE time
)
125 ret
= fmod(time
, MS_PER_DAY
);
132 /* ECMA-262 3rd Edition 15.9.1.3 */
133 static inline DOUBLE
days_in_year(DOUBLE year
)
137 if(year
!= (int)year
)
141 if(y
%4 != 0) return 365;
142 if(y
%100 != 0) return 366;
143 if(y
%400 != 0) return 365;
147 /* ECMA-262 3rd Edition 15.9.1.3 */
148 static inline DOUBLE
day_from_year(DOUBLE year
)
150 if(year
!= (int)year
)
153 return floor(365.0*(year
-1970) + floor((year
-1969)/4)
154 - floor((year
-1901)/100) + floor((year
-1601)/400));
157 static inline int day_from_month(int month
, int in_leap_year
)
166 return 59+in_leap_year
;
168 return 90+in_leap_year
;
170 return 120+in_leap_year
;
172 return 151+in_leap_year
;
174 return 181+in_leap_year
;
176 return 212+in_leap_year
;
178 return 243+in_leap_year
;
180 return 273+in_leap_year
;
182 return 304+in_leap_year
;
184 return 334+in_leap_year
;
188 /* ECMA-262 3rd Edition 15.9.1.3 */
189 static inline DOUBLE
time_from_year(DOUBLE year
)
191 return MS_PER_DAY
*day_from_year(year
);
194 /* ECMA-262 3rd Edition 15.9.1.3 */
195 static inline DOUBLE
year_from_time(DOUBLE time
)
202 y
= 1970 + time
/365.25/MS_PER_DAY
;
204 if(time_from_year(y
) > time
)
205 while(time_from_year(y
) > time
) y
--;
207 while(time_from_year(y
+1)<=time
) y
++;
212 /* ECMA-262 3rd Edition 15.9.1.3 */
213 static inline int in_leap_year(DOUBLE time
)
215 if(days_in_year(year_from_time(time
))==366)
220 /* ECMA-262 3rd Edition 15.9.1.4 */
221 static inline int day_within_year(DOUBLE time
)
223 return day(time
) - day_from_year(year_from_time(time
));
226 /* ECMA-262 3rd Edition 15.9.1.4 */
227 static inline DOUBLE
month_from_time(DOUBLE time
)
229 int ily
= in_leap_year(time
);
230 int dwy
= day_within_year(time
);
235 if(0<=dwy
&& dwy
<31) return 0;
236 if(dwy
< 59+ily
) return 1;
237 if(dwy
< 90+ily
) return 2;
238 if(dwy
< 120+ily
) return 3;
239 if(dwy
< 151+ily
) return 4;
240 if(dwy
< 181+ily
) return 5;
241 if(dwy
< 212+ily
) return 6;
242 if(dwy
< 243+ily
) return 7;
243 if(dwy
< 273+ily
) return 8;
244 if(dwy
< 304+ily
) return 9;
245 if(dwy
< 334+ily
) return 10;
249 /* ECMA-262 3rd Edition 15.9.1.5 */
250 static inline DOUBLE
date_from_time(DOUBLE time
)
252 int dwy
= day_within_year(time
);
253 int ily
= in_leap_year(time
);
254 int mft
= month_from_time(time
);
259 if(mft
==0) return dwy
+1;
260 if(mft
==1) return dwy
-30;
261 if(mft
==2) return dwy
-58-ily
;
262 if(mft
==3) return dwy
-89-ily
;
263 if(mft
==4) return dwy
-119-ily
;
264 if(mft
==5) return dwy
-150-ily
;
265 if(mft
==6) return dwy
-180-ily
;
266 if(mft
==7) return dwy
-211-ily
;
267 if(mft
==8) return dwy
-242-ily
;
268 if(mft
==9) return dwy
-272-ily
;
269 if(mft
==10) return dwy
-303-ily
;
273 /* ECMA-262 3rd Edition 15.9.1.6 */
274 static inline DOUBLE
week_day(DOUBLE time
)
281 ret
= fmod(day(time
)+4, 7);
287 static inline DOUBLE
convert_time(int year
, SYSTEMTIME st
)
298 time
= time_from_year(year
);
299 time
+= (DOUBLE
)day_from_month(st
.wMonth
-1, in_leap_year(time
)) * MS_PER_DAY
;
302 set_week_day
= st
.wDayOfWeek
-week_day(time
);
305 time
+= set_week_day
* MS_PER_DAY
;
307 time
+= (DOUBLE
)(st
.wDay
-1) * 7 * MS_PER_DAY
;
308 if(month_from_time(time
) != st
.wMonth
-1)
309 time
-= 7 * MS_PER_DAY
;
312 time
+= st
.wDay
* MS_PER_DAY
;
314 time
+= st
.wHour
* MS_PER_HOUR
;
315 time
+= st
.wMinute
* MS_PER_MINUTE
;
320 /* ECMA-262 3rd Edition 15.9.1.9 */
321 static inline DOUBLE
daylight_saving_ta(DOUBLE time
, DateInstance
*date
)
323 int year
= year_from_time(time
);
324 DOUBLE standardTime
, daylightTime
;
329 standardTime
= convert_time(year
, date
->standardDate
);
330 daylightTime
= convert_time(year
, date
->daylightDate
);
332 if(isnan(standardTime
) || isnan(daylightTime
))
334 else if(standardTime
> daylightTime
) {
335 if(daylightTime
<= time
&& time
< standardTime
)
336 return date
->daylightBias
;
338 return date
->standardBias
;
341 if(standardTime
<= time
&& time
< daylightTime
)
342 return date
->standardBias
;
344 return date
->daylightBias
;
348 /* ECMA-262 3rd Edition 15.9.1.9 */
349 static inline DOUBLE
local_time(DOUBLE time
, DateInstance
*date
)
351 return time
- (daylight_saving_ta(time
, date
)+date
->bias
)*MS_PER_MINUTE
;
354 /* ECMA-262 3rd Edition 15.9.1.9 */
355 static inline DOUBLE
utc(DOUBLE time
, DateInstance
*date
)
357 time
+= date
->bias
* MS_PER_MINUTE
;
358 return time
+ daylight_saving_ta(time
, date
)*MS_PER_MINUTE
;
361 /* ECMA-262 3rd Edition 15.9.1.10 */
362 static inline DOUBLE
hour_from_time(DOUBLE time
)
369 ret
= fmod(floor(time
/MS_PER_HOUR
), 24);
375 /* ECMA-262 3rd Edition 15.9.1.10 */
376 static inline DOUBLE
min_from_time(DOUBLE time
)
383 ret
= fmod(floor(time
/MS_PER_MINUTE
), 60);
389 /* ECMA-262 3rd Edition 15.9.1.10 */
390 static inline DOUBLE
sec_from_time(DOUBLE time
)
397 ret
= fmod(floor(time
/1000), 60);
403 /* ECMA-262 3rd Edition 15.9.1.10 */
404 static inline DOUBLE
ms_from_time(DOUBLE time
)
411 ret
= fmod(time
, 1000);
412 if(ret
<0) ret
+= 1000;
417 /* ECMA-262 3rd Edition 15.9.1.11 */
418 static inline DOUBLE
make_time(DOUBLE hour
, DOUBLE min
, DOUBLE sec
, DOUBLE ms
)
420 return hour
*MS_PER_HOUR
+ min
*MS_PER_MINUTE
+ sec
*1000 + ms
;
423 /* ECMA-262 3rd Edition 15.9.1.12 */
424 static inline DOUBLE
make_day(DOUBLE year
, DOUBLE month
, DOUBLE day
)
428 year
+= floor(month
/12);
430 month
= fmod(month
, 12);
431 if(month
<0) month
+= 12;
433 time
= time_from_year(year
);
435 day
+= floor(time
/ MS_PER_DAY
);
436 day
+= day_from_month(month
, in_leap_year(time
));
441 /* ECMA-262 3rd Edition 15.9.1.13 */
442 static inline DOUBLE
make_date(DOUBLE day
, DOUBLE time
)
444 return day
*MS_PER_DAY
+ time
;
447 /* ECMA-262 3rd Edition 15.9.1.14 */
448 static inline DOUBLE
time_clip(DOUBLE time
)
450 if(8.64e15
< time
|| time
< -8.64e15
) {
457 static double date_now(void)
462 GetSystemTimeAsFileTime(&ftime
);
463 time
= ((LONGLONG
)ftime
.dwHighDateTime
<< 32) + ftime
.dwLowDateTime
;
465 return time
/10000 - TIME_EPOCH
;
468 static SYSTEMTIME
create_systemtime(DOUBLE time
)
472 st
.wYear
= year_from_time(time
);
473 st
.wMonth
= month_from_time(time
) + 1;
474 st
.wDayOfWeek
= week_day(time
);
475 st
.wDay
= date_from_time(time
);
476 st
.wHour
= hour_from_time(time
);
477 st
.wMinute
= min_from_time(time
);
478 st
.wSecond
= sec_from_time(time
);
479 st
.wMilliseconds
= ms_from_time(time
);
484 static inline HRESULT
date_to_string(DOUBLE time
, BOOL show_offset
, int offset
, jsval_t
*r
)
486 static const WCHAR formatW
[] = { '%','s',' ','%','s',' ','%','d',' ',
487 '%','0','2','d',':','%','0','2','d',':','%','0','2','d',' ',
488 'U','T','C','%','c','%','0','2','d','%','0','2','d',' ','%','d','%','s',0 };
489 static const WCHAR formatUTCW
[] = { '%','s',' ','%','s',' ','%','d',' ',
490 '%','0','2','d',':','%','0','2','d',':','%','0','2','d',' ',
491 'U','T','C',' ','%','d','%','s',0 };
492 static const WCHAR formatNoOffsetW
[] = { '%','s',' ','%','s',' ',
493 '%','d',' ','%','0','2','d',':','%','0','2','d',':',
494 '%','0','2','d',' ','%','d','%','s',0 };
495 static const WCHAR ADW
[] = { 0 };
496 static const WCHAR BCW
[] = { ' ','B','.','C','.',0 };
498 static const DWORD week_ids
[] = { LOCALE_SABBREVDAYNAME7
, LOCALE_SABBREVDAYNAME1
,
499 LOCALE_SABBREVDAYNAME2
, LOCALE_SABBREVDAYNAME3
, LOCALE_SABBREVDAYNAME4
,
500 LOCALE_SABBREVDAYNAME5
, LOCALE_SABBREVDAYNAME6
};
501 static const DWORD month_ids
[] = { LOCALE_SABBREVMONTHNAME1
, LOCALE_SABBREVMONTHNAME2
,
502 LOCALE_SABBREVMONTHNAME3
, LOCALE_SABBREVMONTHNAME4
,
503 LOCALE_SABBREVMONTHNAME5
, LOCALE_SABBREVMONTHNAME6
,
504 LOCALE_SABBREVMONTHNAME7
, LOCALE_SABBREVMONTHNAME8
,
505 LOCALE_SABBREVMONTHNAME9
, LOCALE_SABBREVMONTHNAME10
,
506 LOCALE_SABBREVMONTHNAME11
, LOCALE_SABBREVMONTHNAME12
};
508 BOOL formatAD
= TRUE
;
509 WCHAR week
[64], month
[64];
518 *r
= jsval_string(jsstr_nan());
523 lcid_en
= MAKELCID(MAKELANGID(LANG_ENGLISH
,SUBLANG_ENGLISH_US
),SORT_DEFAULT
);
526 GetLocaleInfoW(lcid_en
, week_ids
[(int)week_day(time
)], week
, ARRAY_SIZE(week
));
529 GetLocaleInfoW(lcid_en
, month_ids
[(int)month_from_time(time
)], month
, ARRAY_SIZE(month
));
531 year
= year_from_time(time
);
537 day
= date_from_time(time
);
545 sprintfW(buf
, formatNoOffsetW
, week
, month
, day
,
546 (int)hour_from_time(time
), (int)min_from_time(time
),
547 (int)sec_from_time(time
), year
, formatAD
?ADW
:BCW
);
549 sprintfW(buf
, formatW
, week
, month
, day
,
550 (int)hour_from_time(time
), (int)min_from_time(time
),
551 (int)sec_from_time(time
), sign
, offset
/60, offset
%60,
552 year
, formatAD
?ADW
:BCW
);
554 sprintfW(buf
, formatUTCW
, week
, month
, day
,
555 (int)hour_from_time(time
), (int)min_from_time(time
),
556 (int)sec_from_time(time
), year
, formatAD
?ADW
:BCW
);
558 date_jsstr
= jsstr_alloc(buf
);
560 return E_OUTOFMEMORY
;
562 *r
= jsval_string(date_jsstr
);
567 /* ECMA-262 3rd Edition 15.9.1.2 */
568 static HRESULT
dateobj_to_string(DateInstance
*date
, jsval_t
*r
)
573 time
= local_time(date
->time
, date
);
574 offset
= date
->bias
+
575 daylight_saving_ta(time
, date
);
577 return date_to_string(time
, TRUE
, offset
, r
);
580 static HRESULT
Date_toString(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
586 if(!(date
= date_this(jsthis
)))
587 return throw_type_error(ctx
, JS_E_DATE_EXPECTED
, NULL
);
589 return dateobj_to_string(date
, r
);
592 /* ECMA-262 3rd Edition 15.9.1.5 */
593 static HRESULT
Date_toLocaleString(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
599 int date_len
, time_len
;
603 if(!(date
= date_this(jsthis
)))
604 return throw_type_error(ctx
, JS_E_DATE_EXPECTED
, NULL
);
606 if(isnan(date
->time
)) {
608 *r
= jsval_string(jsstr_nan());
612 st
= create_systemtime(local_time(date
->time
, date
));
614 if(st
.wYear
<1601 || st
.wYear
>9999)
615 return dateobj_to_string(date
, r
);
620 date_len
= GetDateFormatW(ctx
->lcid
, DATE_LONGDATE
, &st
, NULL
, NULL
, 0);
621 time_len
= GetTimeFormatW(ctx
->lcid
, 0, &st
, NULL
, NULL
, 0);
623 date_str
= jsstr_alloc_buf(date_len
+time_len
-1, &ptr
);
625 return E_OUTOFMEMORY
;
627 GetDateFormatW(ctx
->lcid
, DATE_LONGDATE
, &st
, NULL
, ptr
, date_len
);
628 GetTimeFormatW(ctx
->lcid
, 0, &st
, NULL
, ptr
+date_len
, time_len
);
629 ptr
[date_len
-1] = ' ';
631 *r
= jsval_string(date_str
);
636 static HRESULT
Date_toISOString(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
640 WCHAR buf
[64], *p
= buf
;
643 static const WCHAR short_year_formatW
[] = {'%','0','4','d',0};
644 static const WCHAR long_year_formatW
[] = {'%','0','6','d',0};
645 static const WCHAR formatW
[] = {'-','%','0','2','d','-','%','0','2','d',
646 'T','%','0','2','d',':','%','0','2','d',':','%','0','2','d','.','%','0','3','d','Z',0};
650 if(!(date
= date_this(jsthis
)))
651 return throw_type_error(ctx
, JS_E_DATE_EXPECTED
, NULL
);
653 year
= year_from_time(date
->time
);
654 if(isnan(year
) || year
> 999999 || year
< -999999) {
655 FIXME("year %lf should throw an exception\n", year
);
661 p
+= sprintfW(p
, long_year_formatW
, -(int)year
);
662 }else if(year
> 9999) {
664 p
+= sprintfW(p
, long_year_formatW
, (int)year
);
666 p
+= sprintfW(p
, short_year_formatW
, (int)year
);
669 sprintfW(p
, formatW
, (int)month_from_time(date
->time
) + 1, (int)date_from_time(date
->time
),
670 (int)hour_from_time(date
->time
), (int)min_from_time(date
->time
),
671 (int)sec_from_time(date
->time
), (int)ms_from_time(date
->time
));
675 if(!(ret
= jsstr_alloc(buf
)))
676 return E_OUTOFMEMORY
;
677 *r
= jsval_string(ret
);
682 static HRESULT
Date_valueOf(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
689 if(!(date
= date_this(jsthis
)))
690 return throw_type_error(ctx
, JS_E_DATE_EXPECTED
, NULL
);
693 *r
= jsval_number(date
->time
);
697 static inline HRESULT
create_utc_string(script_ctx_t
*ctx
, vdisp_t
*jsthis
, jsval_t
*r
)
699 static const WCHAR formatADW
[] = { '%','s',',',' ','%','d',' ','%','s',' ','%','d',' ',
700 '%','0','2','d',':','%','0','2','d',':','%','0','2','d',' ','U','T','C',0 };
701 static const WCHAR formatBCW
[] = { '%','s',',',' ','%','d',' ','%','s',' ','%','d',' ','B','.','C','.',' ',
702 '%','0','2','d',':','%','0','2','d',':','%','0','2','d',' ','U','T','C',0 };
704 static const DWORD week_ids
[] = { LOCALE_SABBREVDAYNAME7
, LOCALE_SABBREVDAYNAME1
,
705 LOCALE_SABBREVDAYNAME2
, LOCALE_SABBREVDAYNAME3
, LOCALE_SABBREVDAYNAME4
,
706 LOCALE_SABBREVDAYNAME5
, LOCALE_SABBREVDAYNAME6
};
707 static const DWORD month_ids
[] = { LOCALE_SABBREVMONTHNAME1
, LOCALE_SABBREVMONTHNAME2
,
708 LOCALE_SABBREVMONTHNAME3
, LOCALE_SABBREVMONTHNAME4
,
709 LOCALE_SABBREVMONTHNAME5
, LOCALE_SABBREVMONTHNAME6
,
710 LOCALE_SABBREVMONTHNAME7
, LOCALE_SABBREVMONTHNAME8
,
711 LOCALE_SABBREVMONTHNAME9
, LOCALE_SABBREVMONTHNAME10
,
712 LOCALE_SABBREVMONTHNAME11
, LOCALE_SABBREVMONTHNAME12
};
714 BOOL formatAD
= TRUE
;
715 WCHAR week
[64], month
[64];
722 if(!(date
= date_this(jsthis
)))
723 return throw_type_error(ctx
, JS_E_DATE_EXPECTED
, NULL
);
725 if(isnan(date
->time
)) {
727 *r
= jsval_string(jsstr_nan());
732 lcid_en
= MAKELCID(MAKELANGID(LANG_ENGLISH
,SUBLANG_ENGLISH_US
),SORT_DEFAULT
);
735 GetLocaleInfoW(lcid_en
, week_ids
[(int)week_day(date
->time
)], week
, ARRAY_SIZE(week
));
738 GetLocaleInfoW(lcid_en
, month_ids
[(int)month_from_time(date
->time
)], month
, ARRAY_SIZE(month
));
740 year
= year_from_time(date
->time
);
746 day
= date_from_time(date
->time
);
748 sprintfW(buf
, formatAD
? formatADW
: formatBCW
, week
, day
, month
, year
,
749 (int)hour_from_time(date
->time
), (int)min_from_time(date
->time
),
750 (int)sec_from_time(date
->time
));
752 date_str
= jsstr_alloc(buf
);
754 return E_OUTOFMEMORY
;
756 *r
= jsval_string(date_str
);
761 /* ECMA-262 3rd Edition 15.9.5.42 */
762 static HRESULT
Date_toUTCString(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
766 return create_utc_string(ctx
, jsthis
, r
);
769 static HRESULT
Date_toGMTString(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
773 return create_utc_string(ctx
, jsthis
, r
);
776 /* ECMA-262 3rd Edition 15.9.5.3 */
777 static HRESULT
dateobj_to_date_string(DateInstance
*date
, jsval_t
*r
)
779 static const WCHAR formatADW
[] = { '%','s',' ','%','s',' ','%','d',' ','%','d',0 };
780 static const WCHAR formatBCW
[] = { '%','s',' ','%','s',' ','%','d',' ','%','d',' ','B','.','C','.',0 };
782 static const DWORD week_ids
[] = { LOCALE_SABBREVDAYNAME7
, LOCALE_SABBREVDAYNAME1
,
783 LOCALE_SABBREVDAYNAME2
, LOCALE_SABBREVDAYNAME3
, LOCALE_SABBREVDAYNAME4
,
784 LOCALE_SABBREVDAYNAME5
, LOCALE_SABBREVDAYNAME6
};
785 static const DWORD month_ids
[] = { LOCALE_SABBREVMONTHNAME1
, LOCALE_SABBREVMONTHNAME2
,
786 LOCALE_SABBREVMONTHNAME3
, LOCALE_SABBREVMONTHNAME4
,
787 LOCALE_SABBREVMONTHNAME5
, LOCALE_SABBREVMONTHNAME6
,
788 LOCALE_SABBREVMONTHNAME7
, LOCALE_SABBREVMONTHNAME8
,
789 LOCALE_SABBREVMONTHNAME9
, LOCALE_SABBREVMONTHNAME10
,
790 LOCALE_SABBREVMONTHNAME11
, LOCALE_SABBREVMONTHNAME12
};
792 BOOL formatAD
= TRUE
;
793 WCHAR week
[64], month
[64];
800 if(isnan(date
->time
)) {
802 *r
= jsval_string(jsstr_nan());
806 time
= local_time(date
->time
, date
);
809 lcid_en
= MAKELCID(MAKELANGID(LANG_ENGLISH
,SUBLANG_ENGLISH_US
),SORT_DEFAULT
);
812 GetLocaleInfoW(lcid_en
, week_ids
[(int)week_day(time
)], week
, ARRAY_SIZE(week
));
815 GetLocaleInfoW(lcid_en
, month_ids
[(int)month_from_time(time
)], month
, ARRAY_SIZE(month
));
817 year
= year_from_time(time
);
823 day
= date_from_time(time
);
825 sprintfW(buf
, formatAD
? formatADW
: formatBCW
, week
, month
, day
, year
);
827 date_str
= jsstr_alloc(buf
);
829 return E_OUTOFMEMORY
;
831 *r
= jsval_string(date_str
);
836 static HRESULT
Date_toDateString(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
841 if(!(date
= date_this(jsthis
)))
842 return throw_type_error(ctx
, JS_E_DATE_EXPECTED
, NULL
);
844 return dateobj_to_date_string(date
, r
);
847 /* ECMA-262 3rd Edition 15.9.5.4 */
848 static HRESULT
Date_toTimeString(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
851 static const WCHAR formatW
[] = { '%','0','2','d',':','%','0','2','d',':','%','0','2','d',
852 ' ','U','T','C','%','c','%','0','2','d','%','0','2','d',0 };
853 static const WCHAR formatUTCW
[] = { '%','0','2','d',':','%','0','2','d',
854 ':','%','0','2','d',' ','U','T','C',0 };
864 if(!(date
= date_this(jsthis
)))
865 return throw_type_error(ctx
, JS_E_DATE_EXPECTED
, NULL
);
867 if(isnan(date
->time
)) {
869 *r
= jsval_string(jsstr_nan());
873 time
= local_time(date
->time
, date
);
876 offset
= date
->bias
+
877 daylight_saving_ta(time
, date
);
886 sprintfW(buf
, formatW
, (int)hour_from_time(time
),
887 (int)min_from_time(time
), (int)sec_from_time(time
),
888 sign
, offset
/60, offset
%60);
890 sprintfW(buf
, formatUTCW
, (int)hour_from_time(time
),
891 (int)min_from_time(time
), (int)sec_from_time(time
));
893 date_str
= jsstr_alloc(buf
);
895 return E_OUTOFMEMORY
;
897 *r
= jsval_string(date_str
);
902 /* ECMA-262 3rd Edition 15.9.5.6 */
903 static HRESULT
Date_toLocaleDateString(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
913 if(!(date
= date_this(jsthis
)))
914 return throw_type_error(ctx
, JS_E_DATE_EXPECTED
, NULL
);
916 if(isnan(date
->time
)) {
918 *r
= jsval_string(jsstr_nan());
922 st
= create_systemtime(local_time(date
->time
, date
));
924 if(st
.wYear
<1601 || st
.wYear
>9999)
925 return dateobj_to_date_string(date
, r
);
930 len
= GetDateFormatW(ctx
->lcid
, DATE_LONGDATE
, &st
, NULL
, NULL
, 0);
931 date_str
= jsstr_alloc_buf(len
-1, &ptr
);
933 return E_OUTOFMEMORY
;
934 GetDateFormatW(ctx
->lcid
, DATE_LONGDATE
, &st
, NULL
, ptr
, len
);
936 *r
= jsval_string(date_str
);
941 /* ECMA-262 3rd Edition 15.9.5.7 */
942 static HRESULT
Date_toLocaleTimeString(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
952 if(!(date
= date_this(jsthis
)))
953 return throw_type_error(ctx
, JS_E_DATE_EXPECTED
, NULL
);
955 if(isnan(date
->time
)) {
957 *r
= jsval_string(jsstr_nan());
961 st
= create_systemtime(local_time(date
->time
, date
));
963 if(st
.wYear
<1601 || st
.wYear
>9999)
964 return Date_toTimeString(ctx
, jsthis
, flags
, argc
, argv
, r
);
969 len
= GetTimeFormatW(ctx
->lcid
, 0, &st
, NULL
, NULL
, 0);
970 date_str
= jsstr_alloc_buf(len
-1, &ptr
);
972 return E_OUTOFMEMORY
;
973 GetTimeFormatW(ctx
->lcid
, 0, &st
, NULL
, ptr
, len
);
975 *r
= jsval_string(date_str
);
980 /* ECMA-262 3rd Edition 15.9.5.9 */
981 static HRESULT
Date_getTime(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
988 if(!(date
= date_this(jsthis
)))
989 return throw_type_error(ctx
, JS_E_DATE_EXPECTED
, NULL
);
992 *r
= jsval_number(date
->time
);
996 /* ECMA-262 3rd Edition 15.9.5.10 */
997 static HRESULT
Date_getFullYear(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1004 if(!(date
= date_this(jsthis
)))
1005 return throw_type_error(ctx
, JS_E_DATE_EXPECTED
, NULL
);
1008 DOUBLE time
= local_time(date
->time
, date
);
1010 *r
= jsval_number(year_from_time(time
));
1015 /* ECMA-262 3rd Edition 15.9.5.11 */
1016 static HRESULT
Date_getUTCFullYear(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1023 if(!(date
= date_this(jsthis
)))
1024 return throw_type_error(ctx
, JS_E_DATE_EXPECTED
, NULL
);
1027 *r
= jsval_number(year_from_time(date
->time
));
1031 /* ECMA-262 3rd Edition 15.9.5.12 */
1032 static HRESULT
Date_getMonth(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
1038 if(!(date
= date_this(jsthis
)))
1039 return throw_type_error(ctx
, JS_E_DATE_EXPECTED
, NULL
);
1042 *r
= jsval_number(month_from_time(local_time(date
->time
, date
)));
1046 /* ECMA-262 3rd Edition 15.9.5.13 */
1047 static HRESULT
Date_getUTCMonth(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1054 if(!(date
= date_this(jsthis
)))
1055 return throw_type_error(ctx
, JS_E_DATE_EXPECTED
, NULL
);
1058 *r
= jsval_number(month_from_time(date
->time
));
1062 /* ECMA-262 3rd Edition 15.9.5.14 */
1063 static HRESULT
Date_getDate(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
1069 if(!(date
= date_this(jsthis
)))
1070 return throw_type_error(ctx
, JS_E_DATE_EXPECTED
, NULL
);
1073 *r
= jsval_number(date_from_time(local_time(date
->time
, date
)));
1077 /* ECMA-262 3rd Edition 15.9.5.15 */
1078 static HRESULT
Date_getUTCDate(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1085 if(!(date
= date_this(jsthis
)))
1086 return throw_type_error(ctx
, JS_E_DATE_EXPECTED
, NULL
);
1089 *r
= jsval_number(date_from_time(date
->time
));
1093 /* ECMA-262 3rd Edition 15.9.5.16 */
1094 static HRESULT
Date_getDay(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1101 if(!(date
= date_this(jsthis
)))
1102 return throw_type_error(ctx
, JS_E_DATE_EXPECTED
, NULL
);
1105 *r
= jsval_number(week_day(local_time(date
->time
, date
)));
1109 /* ECMA-262 3rd Edition 15.9.5.17 */
1110 static HRESULT
Date_getUTCDay(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1117 if(!(date
= date_this(jsthis
)))
1118 return throw_type_error(ctx
, JS_E_DATE_EXPECTED
, NULL
);
1121 *r
= jsval_number(week_day(date
->time
));
1125 /* ECMA-262 3rd Edition 15.9.5.18 */
1126 static HRESULT
Date_getHours(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1133 if(!(date
= date_this(jsthis
)))
1134 return throw_type_error(ctx
, JS_E_DATE_EXPECTED
, NULL
);
1137 *r
= jsval_number(hour_from_time(local_time(date
->time
, date
)));
1141 /* ECMA-262 3rd Edition 15.9.5.19 */
1142 static HRESULT
Date_getUTCHours(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1149 if(!(date
= date_this(jsthis
)))
1150 return throw_type_error(ctx
, JS_E_DATE_EXPECTED
, NULL
);
1153 *r
= jsval_number(hour_from_time(date
->time
));
1157 /* ECMA-262 3rd Edition 15.9.5.20 */
1158 static HRESULT
Date_getMinutes(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1165 if(!(date
= date_this(jsthis
)))
1166 return throw_type_error(ctx
, JS_E_DATE_EXPECTED
, NULL
);
1169 *r
= jsval_number(min_from_time(local_time(date
->time
, date
)));
1173 /* ECMA-262 3rd Edition 15.9.5.21 */
1174 static HRESULT
Date_getUTCMinutes(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1181 if(!(date
= date_this(jsthis
)))
1182 return throw_type_error(ctx
, JS_E_DATE_EXPECTED
, NULL
);
1185 *r
= jsval_number(min_from_time(date
->time
));
1189 /* ECMA-262 3rd Edition 15.9.5.22 */
1190 static HRESULT
Date_getSeconds(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
1196 if(!(date
= date_this(jsthis
)))
1197 return throw_type_error(ctx
, JS_E_DATE_EXPECTED
, NULL
);
1200 *r
= jsval_number(sec_from_time(local_time(date
->time
, date
)));
1204 /* ECMA-262 3rd Edition 15.9.5.23 */
1205 static HRESULT
Date_getUTCSeconds(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1212 if(!(date
= date_this(jsthis
)))
1213 return throw_type_error(ctx
, JS_E_DATE_EXPECTED
, NULL
);
1216 *r
= jsval_number(sec_from_time(date
->time
));
1220 /* ECMA-262 3rd Edition 15.9.5.24 */
1221 static HRESULT
Date_getMilliseconds(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1228 if(!(date
= date_this(jsthis
)))
1229 return throw_type_error(ctx
, JS_E_DATE_EXPECTED
, NULL
);
1232 *r
= jsval_number(ms_from_time(local_time(date
->time
, date
)));
1236 /* ECMA-262 3rd Edition 15.9.5.25 */
1237 static HRESULT
Date_getUTCMilliseconds(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1244 if(!(date
= date_this(jsthis
)))
1245 return throw_type_error(ctx
, JS_E_DATE_EXPECTED
, NULL
);
1248 *r
= jsval_number(ms_from_time(date
->time
));
1252 /* ECMA-262 3rd Edition 15.9.5.26 */
1253 static HRESULT
Date_getTimezoneOffset(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1260 if(!(date
= date_this(jsthis
)))
1261 return throw_type_error(ctx
, JS_E_DATE_EXPECTED
, NULL
);
1264 *r
= jsval_number(floor((date
->time
-local_time(date
->time
, date
))/MS_PER_MINUTE
));
1268 /* ECMA-262 3rd Edition 15.9.5.27 */
1269 static HRESULT
Date_setTime(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1278 if(!(date
= date_this(jsthis
)))
1279 return throw_type_error(ctx
, JS_E_DATE_EXPECTED
, NULL
);
1282 return throw_type_error(ctx
, JS_E_MISSING_ARG
, NULL
);
1284 hres
= to_number(ctx
, argv
[0], &n
);
1288 date
->time
= time_clip(n
);
1291 *r
= jsval_number(date
->time
);
1295 /* ECMA-262 3rd Edition 15.9.5.28 */
1296 static HRESULT
Date_setMilliseconds(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1305 if(!(date
= date_this(jsthis
)))
1306 return throw_type_error(ctx
, JS_E_DATE_EXPECTED
, NULL
);
1309 return throw_type_error(ctx
, JS_E_MISSING_ARG
, NULL
);
1311 hres
= to_number(ctx
, argv
[0], &n
);
1315 t
= local_time(date
->time
, date
);
1316 t
= make_date(day(t
), make_time(hour_from_time(t
), min_from_time(t
),
1317 sec_from_time(t
), n
));
1318 date
->time
= time_clip(utc(t
, date
));
1321 *r
= jsval_number(date
->time
);
1325 /* ECMA-262 3rd Edition 15.9.5.29 */
1326 static HRESULT
Date_setUTCMilliseconds(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1335 if(!(date
= date_this(jsthis
)))
1336 return throw_type_error(ctx
, JS_E_DATE_EXPECTED
, NULL
);
1339 return throw_type_error(ctx
, JS_E_MISSING_ARG
, NULL
);
1341 hres
= to_number(ctx
, argv
[0], &n
);
1346 t
= make_date(day(t
), make_time(hour_from_time(t
), min_from_time(t
),
1347 sec_from_time(t
), n
));
1348 date
->time
= time_clip(t
);
1351 *r
= jsval_number(date
->time
);
1355 /* ECMA-262 3rd Edition 15.9.5.30 */
1356 static HRESULT
Date_setSeconds(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1365 if(!(date
= date_this(jsthis
)))
1366 return throw_type_error(ctx
, JS_E_DATE_EXPECTED
, NULL
);
1369 return throw_type_error(ctx
, JS_E_MISSING_ARG
, NULL
);
1371 t
= local_time(date
->time
, date
);
1373 hres
= to_number(ctx
, argv
[0], &sec
);
1378 hres
= to_number(ctx
, argv
[1], &ms
);
1382 ms
= ms_from_time(t
);
1385 t
= make_date(day(t
), make_time(hour_from_time(t
),
1386 min_from_time(t
), sec
, ms
));
1387 date
->time
= time_clip(utc(t
, date
));
1390 *r
= jsval_number(date
->time
);
1394 /* ECMA-262 3rd Edition 15.9.5.31 */
1395 static HRESULT
Date_setUTCSeconds(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1404 if(!(date
= date_this(jsthis
)))
1405 return throw_type_error(ctx
, JS_E_DATE_EXPECTED
, NULL
);
1408 return throw_type_error(ctx
, JS_E_MISSING_ARG
, NULL
);
1412 hres
= to_number(ctx
, argv
[0], &sec
);
1417 hres
= to_number(ctx
, argv
[1], &ms
);
1421 ms
= ms_from_time(t
);
1424 t
= make_date(day(t
), make_time(hour_from_time(t
),
1425 min_from_time(t
), sec
, ms
));
1426 date
->time
= time_clip(t
);
1429 *r
= jsval_number(date
->time
);
1433 /* ECMA-262 3rd Edition 15.9.5.33 */
1434 static HRESULT
Date_setMinutes(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1438 double t
, min
, sec
, ms
;
1443 if(!(date
= date_this(jsthis
)))
1444 return throw_type_error(ctx
, JS_E_DATE_EXPECTED
, NULL
);
1447 return throw_type_error(ctx
, JS_E_MISSING_ARG
, NULL
);
1449 t
= local_time(date
->time
, date
);
1451 hres
= to_number(ctx
, argv
[0], &min
);
1456 hres
= to_number(ctx
, argv
[1], &sec
);
1460 sec
= sec_from_time(t
);
1464 hres
= to_number(ctx
, argv
[2], &ms
);
1468 ms
= ms_from_time(t
);
1471 t
= make_date(day(t
), make_time(hour_from_time(t
),
1473 date
->time
= time_clip(utc(t
, date
));
1476 *r
= jsval_number(date
->time
);
1480 /* ECMA-262 3rd Edition 15.9.5.34 */
1481 static HRESULT
Date_setUTCMinutes(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1485 double t
, min
, sec
, ms
;
1490 if(!(date
= date_this(jsthis
)))
1491 return throw_type_error(ctx
, JS_E_DATE_EXPECTED
, NULL
);
1494 return throw_type_error(ctx
, JS_E_MISSING_ARG
, NULL
);
1498 hres
= to_number(ctx
, argv
[0], &min
);
1503 hres
= to_number(ctx
, argv
[1], &sec
);
1507 sec
= sec_from_time(t
);
1511 hres
= to_number(ctx
, argv
[2], &ms
);
1515 ms
= ms_from_time(t
);
1518 t
= make_date(day(t
), make_time(hour_from_time(t
),
1520 date
->time
= time_clip(t
);
1523 *r
= jsval_number(date
->time
);
1527 /* ECMA-262 3rd Edition 15.9.5.35 */
1528 static HRESULT
Date_setHours(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1532 double t
, hour
, min
, sec
, ms
;
1537 if(!(date
= date_this(jsthis
)))
1538 return throw_type_error(ctx
, JS_E_DATE_EXPECTED
, NULL
);
1541 return throw_type_error(ctx
, JS_E_MISSING_ARG
, NULL
);
1543 t
= local_time(date
->time
, date
);
1545 hres
= to_number(ctx
, argv
[0], &hour
);
1550 hres
= to_number(ctx
, argv
[1], &min
);
1554 min
= min_from_time(t
);
1558 hres
= to_number(ctx
, argv
[2], &sec
);
1562 sec
= sec_from_time(t
);
1566 hres
= to_number(ctx
, argv
[3], &ms
);
1570 ms
= ms_from_time(t
);
1573 t
= make_date(day(t
), make_time(hour
, min
, sec
, ms
));
1574 date
->time
= time_clip(utc(t
, date
));
1577 *r
= jsval_number(date
->time
);
1581 /* ECMA-262 3rd Edition 15.9.5.36 */
1582 static HRESULT
Date_setUTCHours(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1586 double t
, hour
, min
, sec
, ms
;
1591 if(!(date
= date_this(jsthis
)))
1592 return throw_type_error(ctx
, JS_E_DATE_EXPECTED
, NULL
);
1595 return throw_type_error(ctx
, JS_E_MISSING_ARG
, NULL
);
1599 hres
= to_number(ctx
, argv
[0], &hour
);
1604 hres
= to_number(ctx
, argv
[1], &min
);
1608 min
= min_from_time(t
);
1612 hres
= to_number(ctx
, argv
[2], &sec
);
1616 sec
= sec_from_time(t
);
1620 hres
= to_number(ctx
, argv
[3], &ms
);
1624 ms
= ms_from_time(t
);
1627 t
= make_date(day(t
), make_time(hour
, min
, sec
, ms
));
1628 date
->time
= time_clip(t
);
1631 *r
= jsval_number(date
->time
);
1635 /* ECMA-262 3rd Edition 15.9.5.36 */
1636 static HRESULT
Date_setDate(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1645 if(!(date
= date_this(jsthis
)))
1646 return throw_type_error(ctx
, JS_E_DATE_EXPECTED
, NULL
);
1649 return throw_type_error(ctx
, JS_E_MISSING_ARG
, NULL
);
1651 hres
= to_number(ctx
, argv
[0], &n
);
1655 t
= local_time(date
->time
, date
);
1656 t
= make_date(make_day(year_from_time(t
), month_from_time(t
), n
), time_within_day(t
));
1657 date
->time
= time_clip(utc(t
, date
));
1660 *r
= jsval_number(date
->time
);
1664 /* ECMA-262 3rd Edition 15.9.5.37 */
1665 static HRESULT
Date_setUTCDate(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1674 if(!(date
= date_this(jsthis
)))
1675 return throw_type_error(ctx
, JS_E_DATE_EXPECTED
, NULL
);
1678 return throw_type_error(ctx
, JS_E_MISSING_ARG
, NULL
);
1680 hres
= to_number(ctx
, argv
[0], &n
);
1685 t
= make_date(make_day(year_from_time(t
), month_from_time(t
), n
), time_within_day(t
));
1686 date
->time
= time_clip(t
);
1689 *r
= jsval_number(date
->time
);
1693 /* ECMA-262 3rd Edition 15.9.5.38 */
1694 static HRESULT
Date_setMonth(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1698 DOUBLE t
, month
, ddate
;
1703 if(!(date
= date_this(jsthis
)))
1704 return throw_type_error(ctx
, JS_E_DATE_EXPECTED
, NULL
);
1707 return throw_type_error(ctx
, JS_E_MISSING_ARG
, NULL
);
1709 t
= local_time(date
->time
, date
);
1711 hres
= to_number(ctx
, argv
[0], &month
);
1716 hres
= to_number(ctx
, argv
[1], &ddate
);
1720 ddate
= date_from_time(t
);
1723 t
= make_date(make_day(year_from_time(t
), month
, ddate
),
1724 time_within_day(t
));
1725 date
->time
= time_clip(utc(t
, date
));
1728 *r
= jsval_number(date
->time
);
1732 /* ECMA-262 3rd Edition 15.9.5.39 */
1733 static HRESULT
Date_setUTCMonth(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1737 double t
, month
, ddate
;
1742 if(!(date
= date_this(jsthis
)))
1743 return throw_type_error(ctx
, JS_E_DATE_EXPECTED
, NULL
);
1746 return throw_type_error(ctx
, JS_E_MISSING_ARG
, NULL
);
1750 hres
= to_number(ctx
, argv
[0], &month
);
1755 hres
= to_number(ctx
, argv
[1], &ddate
);
1759 ddate
= date_from_time(t
);
1762 t
= make_date(make_day(year_from_time(t
), month
, ddate
),
1763 time_within_day(t
));
1764 date
->time
= time_clip(t
);
1767 *r
= jsval_number(date
->time
);
1771 /* ECMA-262 3rd Edition 15.9.5.40 */
1772 static HRESULT
Date_setFullYear(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1776 double t
, year
, month
, ddate
;
1781 if(!(date
= date_this(jsthis
)))
1782 return throw_type_error(ctx
, JS_E_DATE_EXPECTED
, NULL
);
1785 return throw_type_error(ctx
, JS_E_MISSING_ARG
, NULL
);
1787 t
= local_time(date
->time
, date
);
1789 hres
= to_number(ctx
, argv
[0], &year
);
1794 hres
= to_number(ctx
, argv
[1], &month
);
1798 month
= month_from_time(t
);
1802 hres
= to_number(ctx
, argv
[2], &ddate
);
1806 ddate
= date_from_time(t
);
1809 t
= make_date(make_day(year
, month
, ddate
), time_within_day(t
));
1810 date
->time
= time_clip(utc(t
, date
));
1813 *r
= jsval_number(date
->time
);
1817 /* ECMA-262 3rd Edition 15.9.5.41 */
1818 static HRESULT
Date_setUTCFullYear(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1822 double t
, year
, month
, ddate
;
1827 if(!(date
= date_this(jsthis
)))
1828 return throw_type_error(ctx
, JS_E_DATE_EXPECTED
, NULL
);
1831 return throw_type_error(ctx
, JS_E_MISSING_ARG
, NULL
);
1835 hres
= to_number(ctx
, argv
[0], &year
);
1840 hres
= to_number(ctx
, argv
[1], &month
);
1844 month
= month_from_time(t
);
1848 hres
= to_number(ctx
, argv
[2], &ddate
);
1852 ddate
= date_from_time(t
);
1855 t
= make_date(make_day(year
, month
, ddate
), time_within_day(t
));
1856 date
->time
= time_clip(t
);
1859 *r
= jsval_number(date
->time
);
1863 /* ECMA-262 3rd Edition B2.4 */
1864 static HRESULT
Date_getYear(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1872 if(!(date
= date_this(jsthis
)))
1873 return throw_type_error(ctx
, JS_E_DATE_EXPECTED
, NULL
);
1875 t
= local_time(date
->time
, date
);
1878 *r
= jsval_number(NAN
);
1882 year
= year_from_time(t
);
1884 *r
= jsval_number((1900<=year
&& year
<2000)?year
-1900:year
);
1888 /* ECMA-262 3rd Edition B2.5 */
1889 static HRESULT
Date_setYear(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1898 if(!(date
= date_this(jsthis
)))
1899 return throw_type_error(ctx
, JS_E_DATE_EXPECTED
, NULL
);
1902 return throw_type_error(ctx
, JS_E_MISSING_ARG
, NULL
);
1904 t
= local_time(date
->time
, date
);
1906 hres
= to_number(ctx
, argv
[0], &year
);
1913 *r
= jsval_number(NAN
);
1917 year
= year
>= 0.0 ? floor(year
) : -floor(-year
);
1918 if(-1.0 < year
&& year
< 100.0)
1921 date
->time
= time_clip(utc(make_date(make_day(year
, month_from_time(t
), date_from_time(t
)), time_within_day(t
)), date
));
1924 *r
= jsval_number(date
->time
);
1928 static HRESULT
Date_get_value(script_ctx_t
*ctx
, jsdisp_t
*jsthis
, jsval_t
*r
)
1932 return dateobj_to_string(date_from_jsdisp(jsthis
), r
);
1935 static const builtin_prop_t Date_props
[] = {
1936 {getDateW
, Date_getDate
, PROPF_METHOD
},
1937 {getDayW
, Date_getDay
, PROPF_METHOD
},
1938 {getFullYearW
, Date_getFullYear
, PROPF_METHOD
},
1939 {getHoursW
, Date_getHours
, PROPF_METHOD
},
1940 {getMillisecondsW
, Date_getMilliseconds
, PROPF_METHOD
},
1941 {getMinutesW
, Date_getMinutes
, PROPF_METHOD
},
1942 {getMonthW
, Date_getMonth
, PROPF_METHOD
},
1943 {getSecondsW
, Date_getSeconds
, PROPF_METHOD
},
1944 {getTimeW
, Date_getTime
, PROPF_METHOD
},
1945 {getTimezoneOffsetW
, Date_getTimezoneOffset
, PROPF_METHOD
},
1946 {getUTCDateW
, Date_getUTCDate
, PROPF_METHOD
},
1947 {getUTCDayW
, Date_getUTCDay
, PROPF_METHOD
},
1948 {getUTCFullYearW
, Date_getUTCFullYear
, PROPF_METHOD
},
1949 {getUTCHoursW
, Date_getUTCHours
, PROPF_METHOD
},
1950 {getUTCMillisecondsW
, Date_getUTCMilliseconds
, PROPF_METHOD
},
1951 {getUTCMinutesW
, Date_getUTCMinutes
, PROPF_METHOD
},
1952 {getUTCMonthW
, Date_getUTCMonth
, PROPF_METHOD
},
1953 {getUTCSecondsW
, Date_getUTCSeconds
, PROPF_METHOD
},
1954 {getYearW
, Date_getYear
, PROPF_METHOD
},
1955 {setDateW
, Date_setDate
, PROPF_METHOD
|1},
1956 {setFullYearW
, Date_setFullYear
, PROPF_METHOD
|3},
1957 {setHoursW
, Date_setHours
, PROPF_METHOD
|4},
1958 {setMillisecondsW
, Date_setMilliseconds
, PROPF_METHOD
|1},
1959 {setMinutesW
, Date_setMinutes
, PROPF_METHOD
|3},
1960 {setMonthW
, Date_setMonth
, PROPF_METHOD
|2},
1961 {setSecondsW
, Date_setSeconds
, PROPF_METHOD
|2},
1962 {setTimeW
, Date_setTime
, PROPF_METHOD
|1},
1963 {setUTCDateW
, Date_setUTCDate
, PROPF_METHOD
|1},
1964 {setUTCFullYearW
, Date_setUTCFullYear
, PROPF_METHOD
|3},
1965 {setUTCHoursW
, Date_setUTCHours
, PROPF_METHOD
|4},
1966 {setUTCMillisecondsW
, Date_setUTCMilliseconds
, PROPF_METHOD
|1},
1967 {setUTCMinutesW
, Date_setUTCMinutes
, PROPF_METHOD
|3},
1968 {setUTCMonthW
, Date_setUTCMonth
, PROPF_METHOD
|2},
1969 {setUTCSecondsW
, Date_setUTCSeconds
, PROPF_METHOD
|2},
1970 {setYearW
, Date_setYear
, PROPF_METHOD
|1},
1971 {toDateStringW
, Date_toDateString
, PROPF_METHOD
},
1972 {toGMTStringW
, Date_toGMTString
, PROPF_METHOD
},
1973 {toISOStringW
, Date_toISOString
, PROPF_METHOD
|PROPF_ES5
},
1974 {toLocaleDateStringW
, Date_toLocaleDateString
, PROPF_METHOD
},
1975 {toLocaleStringW
, Date_toLocaleString
, PROPF_METHOD
},
1976 {toLocaleTimeStringW
, Date_toLocaleTimeString
, PROPF_METHOD
},
1977 {toStringW
, Date_toString
, PROPF_METHOD
},
1978 {toTimeStringW
, Date_toTimeString
, PROPF_METHOD
},
1979 {toUTCStringW
, Date_toUTCString
, PROPF_METHOD
},
1980 {valueOfW
, Date_valueOf
, PROPF_METHOD
},
1983 static const builtin_info_t Date_info
= {
1985 {NULL
, NULL
,0, Date_get_value
},
1986 ARRAY_SIZE(Date_props
),
1992 static const builtin_info_t DateInst_info
= {
1994 {NULL
, NULL
,0, Date_get_value
},
2000 static HRESULT
create_date(script_ctx_t
*ctx
, jsdisp_t
*object_prototype
, DOUBLE time
, jsdisp_t
**ret
)
2004 TIME_ZONE_INFORMATION tzi
;
2006 GetTimeZoneInformation(&tzi
);
2008 date
= heap_alloc_zero(sizeof(DateInstance
));
2010 return E_OUTOFMEMORY
;
2012 if(object_prototype
)
2013 hres
= init_dispex(&date
->dispex
, ctx
, &Date_info
, object_prototype
);
2015 hres
= init_dispex_from_constr(&date
->dispex
, ctx
, &DateInst_info
, ctx
->date_constr
);
2022 date
->bias
= tzi
.Bias
;
2023 date
->standardDate
= tzi
.StandardDate
;
2024 date
->standardBias
= tzi
.StandardBias
;
2025 date
->daylightDate
= tzi
.DaylightDate
;
2026 date
->daylightBias
= tzi
.DaylightBias
;
2028 *ret
= &date
->dispex
;
2032 static inline HRESULT
date_parse(jsstr_t
*input_str
, double *ret
) {
2033 static const DWORD string_ids
[] = { LOCALE_SMONTHNAME12
, LOCALE_SMONTHNAME11
,
2034 LOCALE_SMONTHNAME10
, LOCALE_SMONTHNAME9
, LOCALE_SMONTHNAME8
,
2035 LOCALE_SMONTHNAME7
, LOCALE_SMONTHNAME6
, LOCALE_SMONTHNAME5
,
2036 LOCALE_SMONTHNAME4
, LOCALE_SMONTHNAME3
, LOCALE_SMONTHNAME2
,
2037 LOCALE_SMONTHNAME1
, LOCALE_SDAYNAME7
, LOCALE_SDAYNAME1
,
2038 LOCALE_SDAYNAME2
, LOCALE_SDAYNAME3
, LOCALE_SDAYNAME4
,
2039 LOCALE_SDAYNAME5
, LOCALE_SDAYNAME6
};
2040 WCHAR
*strings
[ARRAY_SIZE(string_ids
)];
2042 int input_len
, parse_len
= 0, nest_level
= 0, i
, size
;
2043 int year
= 0, month
= 0, day
= 0, hour
= 0, min
= 0, sec
= 0;
2044 int ms
= 0, offset
= 0, hour_adjust
= 0;
2045 BOOL set_year
= FALSE
, set_month
= FALSE
, set_day
= FALSE
, set_hour
= FALSE
;
2046 BOOL set_offset
= FALSE
, set_era
= FALSE
, ad
= TRUE
, set_am
= FALSE
, am
= TRUE
;
2047 BOOL set_hour_adjust
= TRUE
;
2048 TIME_ZONE_INFORMATION tzi
;
2053 input_len
= jsstr_length(input_str
);
2054 input
= jsstr_flatten(input_str
);
2056 return E_OUTOFMEMORY
;
2058 for(i
=0; i
<input_len
; i
++) {
2059 if(input
[i
] == '(') nest_level
++;
2060 else if(input
[i
] == ')') {
2067 else if(!nest_level
) parse_len
++;
2070 parse
= heap_alloc((parse_len
+1)*sizeof(WCHAR
));
2072 return E_OUTOFMEMORY
;
2075 for(i
=0; i
<input_len
; i
++) {
2076 if(input
[i
] == '(') nest_level
++;
2077 else if(input
[i
] == ')') nest_level
--;
2078 else if(!nest_level
) parse
[parse_len
++] = toupperW(input
[i
]);
2080 parse
[parse_len
] = 0;
2082 GetTimeZoneInformation(&tzi
);
2084 di
.standardDate
= tzi
.StandardDate
;
2085 di
.standardBias
= tzi
.StandardBias
;
2086 di
.daylightDate
= tzi
.DaylightDate
;
2087 di
.daylightBias
= tzi
.DaylightBias
;
2089 /* FIXME: Cache strings */
2090 lcid_en
= MAKELCID(MAKELANGID(LANG_ENGLISH
,SUBLANG_ENGLISH_US
),SORT_DEFAULT
);
2091 for(i
=0; i
<ARRAY_SIZE(string_ids
); i
++) {
2092 size
= GetLocaleInfoW(lcid_en
, string_ids
[i
], NULL
, 0);
2093 strings
[i
] = heap_alloc((size
+1)*sizeof(WCHAR
));
2097 heap_free(strings
[i
]);
2099 return E_OUTOFMEMORY
;
2101 GetLocaleInfoW(lcid_en
, string_ids
[i
], strings
[i
], size
);
2104 for(i
=0; i
<parse_len
;) {
2105 while(isspaceW(parse
[i
])) i
++;
2106 if(parse
[i
] == ',') {
2107 while(parse
[i
] == ',') i
++;
2111 if(parse
[i
]>='0' && parse
[i
]<='9') {
2112 int tmp
= atoiW(&parse
[i
]);
2113 while(parse
[i
]>='0' && parse
[i
]<='9') i
++;
2114 while(isspaceW(parse
[i
])) i
++;
2116 if(parse
[i
] == ':') {
2123 while(parse
[i
] == ':') i
++;
2124 while(isspaceW(parse
[i
])) i
++;
2125 if(parse
[i
]>='0' && parse
[i
]<='9') {
2126 min
= atoiW(&parse
[i
]);
2127 while(parse
[i
]>='0' && parse
[i
]<='9') i
++;
2130 while(isspaceW(parse
[i
])) i
++;
2131 while(parse
[i
] == ':') i
++;
2132 while(isspaceW(parse
[i
])) i
++;
2133 if(parse
[i
]>='0' && parse
[i
]<='9') {
2134 sec
= atoiW(&parse
[i
]);
2135 while(parse
[i
]>='0' && parse
[i
]<='9') i
++;
2138 else if(parse
[i
]=='-' || parse
[i
]=='/') {
2139 /* Short or long date */
2140 if(set_day
|| set_month
|| set_year
) break;
2147 while(isspaceW(parse
[i
])) i
++;
2148 while(parse
[i
]=='-' || parse
[i
]=='/') i
++;
2149 while(isspaceW(parse
[i
])) i
++;
2150 if(parse
[i
]<'0' || parse
[i
]>'9') break;
2151 day
= atoiW(&parse
[i
]);
2152 while(parse
[i
]>='0' && parse
[i
]<='9') i
++;
2154 while(parse
[i
]=='-' || parse
[i
]=='/') i
++;
2155 while(isspaceW(parse
[i
])) i
++;
2156 if(parse
[i
]<'0' || parse
[i
]>'9') break;
2157 year
= atoiW(&parse
[i
]);
2158 while(parse
[i
]>='0' && parse
[i
]<='9') i
++;
2167 else if(tmp
<0) break;
2181 else if(parse
[i
]=='+' || parse
[i
]=='-') {
2182 /* Timezone offset */
2183 BOOL positive
= TRUE
;
2185 if(set_offset
&& set_hour_adjust
) break;
2187 set_hour_adjust
= FALSE
;
2189 if(parse
[i
] == '-') positive
= FALSE
;
2192 while(isspaceW(parse
[i
])) i
++;
2193 if(parse
[i
]<'0' || parse
[i
]>'9') break;
2194 offset
= atoiW(&parse
[i
]);
2195 while(parse
[i
]>='0' && parse
[i
]<='9') i
++;
2197 if(offset
<24) offset
*= 60;
2198 else offset
= (offset
/100)*60 + offset
%100;
2200 if(positive
) offset
= -offset
;
2204 if(parse
[i
]<'A' || parse
[i
]>'Z') break;
2205 else if(parse
[i
]=='B' && (parse
[i
+1]=='C' ||
2206 (parse
[i
+1]=='.' && parse
[i
+2]=='C'))) {
2213 if(parse
[i
] == '.') i
++;
2215 if(parse
[i
] == '.') i
++;
2217 else if(parse
[i
]=='A' && (parse
[i
+1]=='D' ||
2218 (parse
[i
+1]=='.' && parse
[i
+2]=='D'))) {
2224 if(parse
[i
] == '.') i
++;
2226 if(parse
[i
] == '.') i
++;
2228 else if(parse
[i
+1]<'A' || parse
[i
+1]>'Z') {
2230 if(set_offset
) break;
2233 if(parse
[i
] <= 'I') hour_adjust
= parse
[i
]-'A'+2;
2234 else if(parse
[i
] == 'J') break;
2235 else if(parse
[i
] <= 'M') hour_adjust
= parse
[i
]-'K'+11;
2236 else if(parse
[i
] <= 'Y') hour_adjust
= parse
[i
]-'N';
2237 else hour_adjust
= 1;
2240 if(parse
[i
] == '.') i
++;
2242 else if(parse
[i
]=='A' && parse
[i
+1]=='M') {
2249 else if(parse
[i
]=='P' && parse
[i
+1]=='M') {
2256 else if((parse
[i
]=='U' && parse
[i
+1]=='T' && parse
[i
+2]=='C')
2257 || (parse
[i
]=='G' && parse
[i
+1]=='M' && parse
[i
+2]=='T')) {
2259 if(set_offset
) break;
2261 set_hour_adjust
= FALSE
;
2266 /* Month or garbage */
2269 for(size
=i
; parse
[size
]>='A' && parse
[size
]<='Z'; size
++);
2272 for(j
=0; j
<ARRAY_SIZE(string_ids
); j
++)
2273 if(!strncmpiW(&parse
[i
], strings
[j
], size
)) break;
2276 if(set_month
) break;
2280 else if(j
== ARRAY_SIZE(string_ids
)) break;
2287 if(i
== parse_len
&& set_year
&& set_month
&& set_day
&& (!set_am
|| hour
<13)) {
2289 if(hour
== 12) hour
= 0;
2293 if(!ad
) year
= -year
+1;
2294 else if(year
<100) year
+= 1900;
2296 *ret
= time_clip(make_date(make_day(year
, month
, day
),
2297 make_time(hour
+hour_adjust
, min
, sec
, ms
)) + offset
*MS_PER_MINUTE
);
2300 *ret
= utc(*ret
, &di
);
2305 for(i
=0; i
<ARRAY_SIZE(string_ids
); i
++)
2306 heap_free(strings
[i
]);
2312 static HRESULT
DateConstr_parse(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
2323 *r
= jsval_number(NAN
);
2327 hres
= to_string(ctx
, argv
[0], &parse_str
);
2331 hres
= date_parse(parse_str
, &n
);
2332 jsstr_release(parse_str
);
2336 *r
= jsval_number(n
);
2340 static HRESULT
date_utc(script_ctx_t
*ctx
, unsigned argc
, jsval_t
*argv
, double *ret
)
2342 double year
, month
, vdate
, hours
, minutes
, seconds
, ms
;
2348 hres
= to_number(ctx
, argv
[0], &year
);
2351 if(0 <= year
&& year
<= 99)
2358 hres
= to_number(ctx
, argv
[1], &month
);
2366 hres
= to_number(ctx
, argv
[2], &vdate
);
2374 hres
= to_number(ctx
, argv
[3], &hours
);
2382 hres
= to_number(ctx
, argv
[4], &minutes
);
2390 hres
= to_number(ctx
, argv
[5], &seconds
);
2398 hres
= to_number(ctx
, argv
[6], &ms
);
2405 *ret
= time_clip(make_date(make_day(year
, month
, vdate
),
2406 make_time(hours
, minutes
,seconds
, ms
)));
2410 static HRESULT
DateConstr_UTC(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
2418 hres
= date_utc(ctx
, argc
, argv
, &n
);
2419 if(SUCCEEDED(hres
) && r
)
2420 *r
= jsval_number(n
);
2424 /* ECMA-262 5.1 Edition 15.9.4.4 */
2425 static HRESULT
DateConstr_now(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
2429 if(r
) *r
= jsval_number(date_now());
2433 static HRESULT
DateConstr_value(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
2442 case DISPATCH_CONSTRUCT
:
2444 /* ECMA-262 3rd Edition 15.9.3.3 */
2446 hres
= create_date(ctx
, NULL
, date_now(), &date
);
2451 /* ECMA-262 3rd Edition 15.9.3.2 */
2456 hres
= to_primitive(ctx
, argv
[0], &prim
, NO_HINT
);
2461 hres
= date_parse(get_string(prim
), &n
);
2463 hres
= to_number(ctx
, prim
, &n
);
2465 jsval_release(prim
);
2469 hres
= create_date(ctx
, NULL
, time_clip(n
), &date
);
2475 /* ECMA-262 3rd Edition 15.9.3.1 */
2480 hres
= date_utc(ctx
, argc
, argv
, &ret_date
);
2484 hres
= create_date(ctx
, NULL
, ret_date
, &date
);
2488 di
= date_from_jsdisp(date
);
2489 di
->time
= utc(di
->time
, di
);
2493 *r
= jsval_obj(date
);
2497 FILETIME system_time
, local_time
;
2500 GetSystemTimeAsFileTime(&system_time
);
2501 FileTimeToLocalFileTime(&system_time
, &local_time
);
2502 lltime
= ((LONGLONG
)local_time
.dwHighDateTime
<<32)
2503 + local_time
.dwLowDateTime
;
2505 return date_to_string(lltime
/10000-TIME_EPOCH
, FALSE
, 0, r
);
2509 FIXME("unimplemented flags %x\n", flags
);
2516 static const builtin_prop_t DateConstr_props
[] = {
2517 {UTCW
, DateConstr_UTC
, PROPF_METHOD
},
2518 {nowW
, DateConstr_now
, PROPF_HTML
|PROPF_METHOD
},
2519 {parseW
, DateConstr_parse
, PROPF_METHOD
}
2522 static const builtin_info_t DateConstr_info
= {
2524 DEFAULT_FUNCTION_VALUE
,
2525 ARRAY_SIZE(DateConstr_props
),
2531 HRESULT
create_date_constr(script_ctx_t
*ctx
, jsdisp_t
*object_prototype
, jsdisp_t
**ret
)
2536 static const WCHAR DateW
[] = {'D','a','t','e',0};
2538 hres
= create_date(ctx
, object_prototype
, 0.0, &date
);
2542 hres
= create_builtin_constructor(ctx
, DateConstr_value
, DateW
, &DateConstr_info
,
2543 PROPF_CONSTR
|7, date
, ret
);
2545 jsdisp_release(date
);