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};
95 static const WCHAR UTCW
[] = {'U','T','C',0};
96 static const WCHAR parseW
[] = {'p','a','r','s','e',0};
98 static inline DateInstance
*date_this(vdisp_t
*jsthis
)
100 return is_vclass(jsthis
, JSCLASS_DATE
) ? (DateInstance
*)jsthis
->u
.jsdisp
: NULL
;
103 /*ECMA-262 3rd Edition 15.9.1.2 */
104 #define MS_PER_DAY 86400000
105 #define MS_PER_HOUR 3600000
106 #define MS_PER_MINUTE 60000
108 /* ECMA-262 3rd Edition 15.9.1.2 */
109 static inline DOUBLE
day(DOUBLE time
)
111 return floor(time
/ MS_PER_DAY
);
114 /* ECMA-262 3rd Edition 15.9.1.2 */
115 static inline DOUBLE
time_within_day(DOUBLE time
)
119 ret
= fmod(time
, MS_PER_DAY
);
126 /* ECMA-262 3rd Edition 15.9.1.3 */
127 static inline DOUBLE
days_in_year(DOUBLE year
)
131 if(year
!= (int)year
)
135 if(y
%4 != 0) return 365;
136 if(y
%100 != 0) return 366;
137 if(y
%400 != 0) return 365;
141 /* ECMA-262 3rd Edition 15.9.1.3 */
142 static inline DOUBLE
day_from_year(DOUBLE year
)
144 if(year
!= (int)year
)
147 return floor(365.0*(year
-1970) + floor((year
-1969)/4)
148 - floor((year
-1901)/100) + floor((year
-1601)/400));
151 static inline int day_from_month(int month
, int in_leap_year
)
160 return 59+in_leap_year
;
162 return 90+in_leap_year
;
164 return 120+in_leap_year
;
166 return 151+in_leap_year
;
168 return 181+in_leap_year
;
170 return 212+in_leap_year
;
172 return 243+in_leap_year
;
174 return 273+in_leap_year
;
176 return 304+in_leap_year
;
178 return 334+in_leap_year
;
182 /* ECMA-262 3rd Edition 15.9.1.3 */
183 static inline DOUBLE
time_from_year(DOUBLE year
)
185 return MS_PER_DAY
*day_from_year(year
);
188 /* ECMA-262 3rd Edition 15.9.1.3 */
189 static inline DOUBLE
year_from_time(DOUBLE time
)
196 y
= 1970 + time
/365.25/MS_PER_DAY
;
198 if(time_from_year(y
) > time
)
199 while(time_from_year(y
) > time
) y
--;
201 while(time_from_year(y
+1)<=time
) y
++;
206 /* ECMA-262 3rd Edition 15.9.1.3 */
207 static inline int in_leap_year(DOUBLE time
)
209 if(days_in_year(year_from_time(time
))==366)
214 /* ECMA-262 3rd Edition 15.9.1.4 */
215 static inline int day_within_year(DOUBLE time
)
217 return day(time
) - day_from_year(year_from_time(time
));
220 /* ECMA-262 3rd Edition 15.9.1.4 */
221 static inline DOUBLE
month_from_time(DOUBLE time
)
223 int ily
= in_leap_year(time
);
224 int dwy
= day_within_year(time
);
229 if(0<=dwy
&& dwy
<31) return 0;
230 if(dwy
< 59+ily
) return 1;
231 if(dwy
< 90+ily
) return 2;
232 if(dwy
< 120+ily
) return 3;
233 if(dwy
< 151+ily
) return 4;
234 if(dwy
< 181+ily
) return 5;
235 if(dwy
< 212+ily
) return 6;
236 if(dwy
< 243+ily
) return 7;
237 if(dwy
< 273+ily
) return 8;
238 if(dwy
< 304+ily
) return 9;
239 if(dwy
< 334+ily
) return 10;
243 /* ECMA-262 3rd Edition 15.9.1.5 */
244 static inline DOUBLE
date_from_time(DOUBLE time
)
246 int dwy
= day_within_year(time
);
247 int ily
= in_leap_year(time
);
248 int mft
= month_from_time(time
);
253 if(mft
==0) return dwy
+1;
254 if(mft
==1) return dwy
-30;
255 if(mft
==2) return dwy
-58-ily
;
256 if(mft
==3) return dwy
-89-ily
;
257 if(mft
==4) return dwy
-119-ily
;
258 if(mft
==5) return dwy
-150-ily
;
259 if(mft
==6) return dwy
-180-ily
;
260 if(mft
==7) return dwy
-211-ily
;
261 if(mft
==8) return dwy
-242-ily
;
262 if(mft
==9) return dwy
-272-ily
;
263 if(mft
==10) return dwy
-303-ily
;
267 /* ECMA-262 3rd Edition 15.9.1.6 */
268 static inline DOUBLE
week_day(DOUBLE time
)
275 ret
= fmod(day(time
)+4, 7);
281 static inline DOUBLE
convert_time(int year
, SYSTEMTIME st
)
292 time
= time_from_year(year
);
293 time
+= (DOUBLE
)day_from_month(st
.wMonth
-1, in_leap_year(time
)) * MS_PER_DAY
;
296 set_week_day
= st
.wDayOfWeek
-week_day(time
);
299 time
+= set_week_day
* MS_PER_DAY
;
301 time
+= (DOUBLE
)(st
.wDay
-1) * 7 * MS_PER_DAY
;
302 if(month_from_time(time
) != st
.wMonth
-1)
303 time
-= 7 * MS_PER_DAY
;
306 time
+= st
.wDay
* MS_PER_DAY
;
308 time
+= st
.wHour
* MS_PER_HOUR
;
309 time
+= st
.wMinute
* MS_PER_MINUTE
;
314 /* ECMA-262 3rd Edition 15.9.1.9 */
315 static inline DOUBLE
daylight_saving_ta(DOUBLE time
, DateInstance
*date
)
317 int year
= year_from_time(time
);
318 DOUBLE standardTime
, daylightTime
;
323 standardTime
= convert_time(year
, date
->standardDate
);
324 daylightTime
= convert_time(year
, date
->daylightDate
);
326 if(isnan(standardTime
) || isnan(daylightTime
))
328 else if(standardTime
> daylightTime
) {
329 if(daylightTime
<= time
&& time
< standardTime
)
330 return date
->daylightBias
;
332 return date
->standardBias
;
335 if(standardTime
<= time
&& time
< daylightTime
)
336 return date
->standardBias
;
338 return date
->daylightBias
;
342 /* ECMA-262 3rd Edition 15.9.1.9 */
343 static inline DOUBLE
local_time(DOUBLE time
, DateInstance
*date
)
345 return time
- (daylight_saving_ta(time
, date
)+date
->bias
)*MS_PER_MINUTE
;
348 /* ECMA-262 3rd Edition 15.9.1.9 */
349 static inline DOUBLE
utc(DOUBLE time
, DateInstance
*date
)
351 time
+= date
->bias
* MS_PER_MINUTE
;
352 return time
+ daylight_saving_ta(time
, date
)*MS_PER_MINUTE
;
355 /* ECMA-262 3rd Edition 15.9.1.10 */
356 static inline DOUBLE
hour_from_time(DOUBLE time
)
363 ret
= fmod(floor(time
/MS_PER_HOUR
), 24);
369 /* ECMA-262 3rd Edition 15.9.1.10 */
370 static inline DOUBLE
min_from_time(DOUBLE time
)
377 ret
= fmod(floor(time
/MS_PER_MINUTE
), 60);
383 /* ECMA-262 3rd Edition 15.9.1.10 */
384 static inline DOUBLE
sec_from_time(DOUBLE time
)
391 ret
= fmod(floor(time
/1000), 60);
397 /* ECMA-262 3rd Edition 15.9.1.10 */
398 static inline DOUBLE
ms_from_time(DOUBLE time
)
405 ret
= fmod(time
, 1000);
406 if(ret
<0) ret
+= 1000;
411 /* ECMA-262 3rd Edition 15.9.1.11 */
412 static inline DOUBLE
make_time(DOUBLE hour
, DOUBLE min
, DOUBLE sec
, DOUBLE ms
)
414 return hour
*MS_PER_HOUR
+ min
*MS_PER_MINUTE
+ sec
*1000 + ms
;
417 /* ECMA-262 3rd Edition 15.9.1.12 */
418 static inline DOUBLE
make_day(DOUBLE year
, DOUBLE month
, DOUBLE day
)
422 year
+= floor(month
/12);
424 month
= fmod(month
, 12);
425 if(month
<0) month
+= 12;
427 time
= time_from_year(year
);
429 day
+= floor(time
/ MS_PER_DAY
);
430 day
+= day_from_month(month
, in_leap_year(time
));
435 /* ECMA-262 3rd Edition 15.9.1.13 */
436 static inline DOUBLE
make_date(DOUBLE day
, DOUBLE time
)
438 return day
*MS_PER_DAY
+ time
;
441 /* ECMA-262 3rd Edition 15.9.1.14 */
442 static inline DOUBLE
time_clip(DOUBLE time
)
444 if(8.64e15
< time
|| time
< -8.64e15
) {
451 static SYSTEMTIME
create_systemtime(DOUBLE time
)
455 st
.wYear
= year_from_time(time
);
456 st
.wMonth
= month_from_time(time
) + 1;
457 st
.wDayOfWeek
= week_day(time
);
458 st
.wDay
= date_from_time(time
);
459 st
.wHour
= hour_from_time(time
);
460 st
.wMinute
= min_from_time(time
);
461 st
.wSecond
= sec_from_time(time
);
462 st
.wMilliseconds
= ms_from_time(time
);
467 static inline HRESULT
date_to_string(DOUBLE time
, BOOL show_offset
, int offset
, VARIANT
*retv
)
469 static const WCHAR NaNW
[] = { 'N','a','N',0 };
470 static const WCHAR formatW
[] = { '%','s',' ','%','s',' ','%','d',' ',
471 '%','0','2','d',':','%','0','2','d',':','%','0','2','d',' ',
472 'U','T','C','%','c','%','0','2','d','%','0','2','d',' ','%','d','%','s',0 };
473 static const WCHAR formatUTCW
[] = { '%','s',' ','%','s',' ','%','d',' ',
474 '%','0','2','d',':','%','0','2','d',':','%','0','2','d',' ',
475 'U','T','C',' ','%','d','%','s',0 };
476 static const WCHAR formatNoOffsetW
[] = { '%','s',' ','%','s',' ',
477 '%','d',' ','%','0','2','d',':','%','0','2','d',':',
478 '%','0','2','d',' ','%','d','%','s',0 };
479 static const WCHAR ADW
[] = { 0 };
480 static const WCHAR BCW
[] = { ' ','B','.','C','.',0 };
482 static const DWORD week_ids
[] = { LOCALE_SABBREVDAYNAME7
, LOCALE_SABBREVDAYNAME1
,
483 LOCALE_SABBREVDAYNAME2
, LOCALE_SABBREVDAYNAME3
, LOCALE_SABBREVDAYNAME4
,
484 LOCALE_SABBREVDAYNAME5
, LOCALE_SABBREVDAYNAME6
};
485 static const DWORD month_ids
[] = { LOCALE_SABBREVMONTHNAME1
, LOCALE_SABBREVMONTHNAME2
,
486 LOCALE_SABBREVMONTHNAME3
, LOCALE_SABBREVMONTHNAME4
,
487 LOCALE_SABBREVMONTHNAME5
, LOCALE_SABBREVMONTHNAME6
,
488 LOCALE_SABBREVMONTHNAME7
, LOCALE_SABBREVMONTHNAME8
,
489 LOCALE_SABBREVMONTHNAME9
, LOCALE_SABBREVMONTHNAME10
,
490 LOCALE_SABBREVMONTHNAME11
, LOCALE_SABBREVMONTHNAME12
};
492 BOOL formatAD
= TRUE
;
495 int len
, size
, year
, day
;
496 DWORD lcid_en
, week_id
, month_id
;
501 V_VT(retv
) = VT_BSTR
;
502 V_BSTR(retv
) = SysAllocString(NaNW
);
504 return E_OUTOFMEMORY
;
512 lcid_en
= MAKELCID(MAKELANGID(LANG_ENGLISH
,SUBLANG_ENGLISH_US
),SORT_DEFAULT
);
514 week_id
= week_ids
[(int)week_day(time
)];
515 size
= GetLocaleInfoW(lcid_en
, week_id
, NULL
, 0);
516 week
= SysAllocStringLen(NULL
, size
);
518 return E_OUTOFMEMORY
;
519 GetLocaleInfoW(lcid_en
, week_id
, week
, size
);
522 month_id
= month_ids
[(int)month_from_time(time
)];
523 size
= GetLocaleInfoW(lcid_en
, month_id
, NULL
, 0);
524 month
= SysAllocStringLen(NULL
, size
);
527 return E_OUTOFMEMORY
;
529 GetLocaleInfoW(lcid_en
, month_id
, month
, size
);
532 year
= year_from_time(time
);
540 year
= year_from_time(time
);
547 day
= date_from_time(time
);
552 day
= date_from_time(time
);
554 if(!show_offset
) len
-= 9;
555 else if(offset
== 0) len
-= 5;
556 else if(offset
< 0) {
561 date_str
= SysAllocStringLen(NULL
, len
);
564 SysFreeString(month
);
565 return E_OUTOFMEMORY
;
569 sprintfW(date_str
, formatNoOffsetW
, week
, month
, day
,
570 (int)hour_from_time(time
), (int)min_from_time(time
),
571 (int)sec_from_time(time
), year
, formatAD
?ADW
:BCW
);
573 sprintfW(date_str
, formatW
, week
, month
, day
,
574 (int)hour_from_time(time
), (int)min_from_time(time
),
575 (int)sec_from_time(time
), sign
, offset
/60, offset
%60,
576 year
, formatAD
?ADW
:BCW
);
578 sprintfW(date_str
, formatUTCW
, week
, month
, day
,
579 (int)hour_from_time(time
), (int)min_from_time(time
),
580 (int)sec_from_time(time
), year
, formatAD
?ADW
:BCW
);
583 SysFreeString(month
);
585 V_VT(retv
) = VT_BSTR
;
586 V_BSTR(retv
) = date_str
;
591 /* ECMA-262 3rd Edition 15.9.1.2 */
592 static HRESULT
dateobj_to_string(DateInstance
*date
, VARIANT
*retv
)
597 time
= local_time(date
->time
, date
);
598 offset
= date
->bias
+
599 daylight_saving_ta(time
, date
);
601 return date_to_string(time
, TRUE
, offset
, retv
);
604 static HRESULT
Date_toString(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
605 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
611 if(!(date
= date_this(jsthis
)))
612 return throw_type_error(ctx
, ei
, IDS_NOT_DATE
, NULL
);
614 return dateobj_to_string(date
, retv
);
617 /* ECMA-262 3rd Edition 15.9.1.5 */
618 static HRESULT
Date_toLocaleString(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
619 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
621 static const WCHAR NaNW
[] = { 'N','a','N',0 };
625 int date_len
, time_len
;
629 if(!(date
= date_this(jsthis
)))
630 return throw_type_error(ctx
, ei
, IDS_NOT_DATE
, NULL
);
632 if(isnan(date
->time
)) {
634 V_VT(retv
) = VT_BSTR
;
635 V_BSTR(retv
) = SysAllocString(NaNW
);
637 return E_OUTOFMEMORY
;
642 st
= create_systemtime(local_time(date
->time
, date
));
644 if(st
.wYear
<1601 || st
.wYear
>9999)
645 return dateobj_to_string(date
, retv
);
648 date_len
= GetDateFormatW(ctx
->lcid
, DATE_LONGDATE
, &st
, NULL
, NULL
, 0);
649 time_len
= GetTimeFormatW(ctx
->lcid
, 0, &st
, NULL
, NULL
, 0);
650 date_str
= SysAllocStringLen(NULL
, date_len
+time_len
-1);
652 return E_OUTOFMEMORY
;
653 GetDateFormatW(ctx
->lcid
, DATE_LONGDATE
, &st
, NULL
, date_str
, date_len
);
654 GetTimeFormatW(ctx
->lcid
, 0, &st
, NULL
, &date_str
[date_len
], time_len
);
655 date_str
[date_len
-1] = ' ';
657 V_VT(retv
) = VT_BSTR
;
658 V_BSTR(retv
) = date_str
;
663 static HRESULT
Date_valueOf(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
664 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
670 if(!(date
= date_this(jsthis
)))
671 return throw_type_error(ctx
, ei
, IDS_NOT_DATE
, NULL
);
674 num_set_val(retv
, date
->time
);
678 static inline HRESULT
create_utc_string(script_ctx_t
*ctx
, vdisp_t
*jsthis
,
679 VARIANT
*retv
, jsexcept_t
*ei
)
681 static const WCHAR NaNW
[] = { 'N','a','N',0 };
682 static const WCHAR formatADW
[] = { '%','s',',',' ','%','d',' ','%','s',' ','%','d',' ',
683 '%','0','2','d',':','%','0','2','d',':','%','0','2','d',' ','U','T','C',0 };
684 static const WCHAR formatBCW
[] = { '%','s',',',' ','%','d',' ','%','s',' ','%','d',' ','B','.','C','.',' ',
685 '%','0','2','d',':','%','0','2','d',':','%','0','2','d',' ','U','T','C',0 };
687 static const DWORD week_ids
[] = { LOCALE_SABBREVDAYNAME7
, LOCALE_SABBREVDAYNAME1
,
688 LOCALE_SABBREVDAYNAME2
, LOCALE_SABBREVDAYNAME3
, LOCALE_SABBREVDAYNAME4
,
689 LOCALE_SABBREVDAYNAME5
, LOCALE_SABBREVDAYNAME6
};
690 static const DWORD month_ids
[] = { LOCALE_SABBREVMONTHNAME1
, LOCALE_SABBREVMONTHNAME2
,
691 LOCALE_SABBREVMONTHNAME3
, LOCALE_SABBREVMONTHNAME4
,
692 LOCALE_SABBREVMONTHNAME5
, LOCALE_SABBREVMONTHNAME6
,
693 LOCALE_SABBREVMONTHNAME7
, LOCALE_SABBREVMONTHNAME8
,
694 LOCALE_SABBREVMONTHNAME9
, LOCALE_SABBREVMONTHNAME10
,
695 LOCALE_SABBREVMONTHNAME11
, LOCALE_SABBREVMONTHNAME12
};
697 BOOL formatAD
= TRUE
;
701 int len
, size
, year
, day
;
702 DWORD lcid_en
, week_id
, month_id
;
704 if(!(date
= date_this(jsthis
)))
705 return throw_type_error(ctx
, ei
, IDS_NOT_DATE
, NULL
);
707 if(isnan(date
->time
)) {
709 V_VT(retv
) = VT_BSTR
;
710 V_BSTR(retv
) = SysAllocString(NaNW
);
712 return E_OUTOFMEMORY
;
720 lcid_en
= MAKELCID(MAKELANGID(LANG_ENGLISH
,SUBLANG_ENGLISH_US
),SORT_DEFAULT
);
722 week_id
= week_ids
[(int)week_day(date
->time
)];
723 size
= GetLocaleInfoW(lcid_en
, week_id
, NULL
, 0);
724 week
= SysAllocStringLen(NULL
, size
);
726 return E_OUTOFMEMORY
;
727 GetLocaleInfoW(lcid_en
, week_id
, week
, size
);
730 month_id
= month_ids
[(int)month_from_time(date
->time
)];
731 size
= GetLocaleInfoW(lcid_en
, month_id
, NULL
, 0);
732 month
= SysAllocStringLen(NULL
, size
);
735 return E_OUTOFMEMORY
;
737 GetLocaleInfoW(lcid_en
, month_id
, month
, size
);
740 year
= year_from_time(date
->time
);
748 year
= year_from_time(date
->time
);
755 day
= date_from_time(date
->time
);
760 day
= date_from_time(date
->time
);
762 date_str
= SysAllocStringLen(NULL
, len
);
765 SysFreeString(month
);
766 return E_OUTOFMEMORY
;
768 sprintfW(date_str
, formatAD
?formatADW
:formatBCW
, week
, day
, month
, year
,
769 (int)hour_from_time(date
->time
), (int)min_from_time(date
->time
),
770 (int)sec_from_time(date
->time
));
773 SysFreeString(month
);
775 V_VT(retv
) = VT_BSTR
;
776 V_BSTR(retv
) = date_str
;
781 /* ECMA-262 3rd Edition 15.9.5.42 */
782 static HRESULT
Date_toUTCString(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
783 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
786 return create_utc_string(ctx
, jsthis
, retv
, ei
);
789 static HRESULT
Date_toGMTString(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
790 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
793 return create_utc_string(ctx
, jsthis
, retv
, ei
);
796 /* ECMA-262 3rd Edition 15.9.5.3 */
797 static HRESULT
dateobj_to_date_string(DateInstance
*date
, VARIANT
*retv
)
799 static const WCHAR NaNW
[] = { 'N','a','N',0 };
800 static const WCHAR formatADW
[] = { '%','s',' ','%','s',' ','%','d',' ','%','d',0 };
801 static const WCHAR formatBCW
[] = { '%','s',' ','%','s',' ','%','d',' ','%','d',' ','B','.','C','.',0 };
803 static const DWORD week_ids
[] = { LOCALE_SABBREVDAYNAME7
, LOCALE_SABBREVDAYNAME1
,
804 LOCALE_SABBREVDAYNAME2
, LOCALE_SABBREVDAYNAME3
, LOCALE_SABBREVDAYNAME4
,
805 LOCALE_SABBREVDAYNAME5
, LOCALE_SABBREVDAYNAME6
};
806 static const DWORD month_ids
[] = { LOCALE_SABBREVMONTHNAME1
, LOCALE_SABBREVMONTHNAME2
,
807 LOCALE_SABBREVMONTHNAME3
, LOCALE_SABBREVMONTHNAME4
,
808 LOCALE_SABBREVMONTHNAME5
, LOCALE_SABBREVMONTHNAME6
,
809 LOCALE_SABBREVMONTHNAME7
, LOCALE_SABBREVMONTHNAME8
,
810 LOCALE_SABBREVMONTHNAME9
, LOCALE_SABBREVMONTHNAME10
,
811 LOCALE_SABBREVMONTHNAME11
, LOCALE_SABBREVMONTHNAME12
};
813 BOOL formatAD
= TRUE
;
817 int len
, size
, year
, day
;
818 DWORD lcid_en
, week_id
, month_id
;
820 if(isnan(date
->time
)) {
822 V_VT(retv
) = VT_BSTR
;
823 V_BSTR(retv
) = SysAllocString(NaNW
);
825 return E_OUTOFMEMORY
;
830 time
= local_time(date
->time
, date
);
835 lcid_en
= MAKELCID(MAKELANGID(LANG_ENGLISH
,SUBLANG_ENGLISH_US
),SORT_DEFAULT
);
837 week_id
= week_ids
[(int)week_day(time
)];
838 size
= GetLocaleInfoW(lcid_en
, week_id
, NULL
, 0);
839 week
= SysAllocStringLen(NULL
, size
);
841 return E_OUTOFMEMORY
;
842 GetLocaleInfoW(lcid_en
, week_id
, week
, size
);
845 month_id
= month_ids
[(int)month_from_time(time
)];
846 size
= GetLocaleInfoW(lcid_en
, month_id
, NULL
, 0);
847 month
= SysAllocStringLen(NULL
, size
);
850 return E_OUTOFMEMORY
;
852 GetLocaleInfoW(lcid_en
, month_id
, month
, size
);
855 year
= year_from_time(time
);
863 year
= year_from_time(time
);
870 day
= date_from_time(time
);
875 day
= date_from_time(time
);
877 date_str
= SysAllocStringLen(NULL
, len
);
880 SysFreeString(month
);
881 return E_OUTOFMEMORY
;
883 sprintfW(date_str
, formatAD
?formatADW
:formatBCW
, week
, month
, day
, year
);
886 SysFreeString(month
);
888 V_VT(retv
) = VT_BSTR
;
889 V_BSTR(retv
) = date_str
;
894 static HRESULT
Date_toDateString(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
895 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
899 if(!(date
= date_this(jsthis
)))
900 return throw_type_error(ctx
, ei
, IDS_NOT_DATE
, NULL
);
902 return dateobj_to_date_string(date
, retv
);
905 /* ECMA-262 3rd Edition 15.9.5.4 */
906 static HRESULT
Date_toTimeString(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
907 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
909 static const WCHAR NaNW
[] = { 'N','a','N',0 };
910 static const WCHAR formatW
[] = { '%','0','2','d',':','%','0','2','d',':','%','0','2','d',
911 ' ','U','T','C','%','c','%','0','2','d','%','0','2','d',0 };
912 static const WCHAR formatUTCW
[] = { '%','0','2','d',':','%','0','2','d',
913 ':','%','0','2','d',' ','U','T','C',0 };
922 if(!(date
= date_this(jsthis
)))
923 return throw_type_error(ctx
, ei
, IDS_NOT_DATE
, NULL
);
925 if(isnan(date
->time
)) {
927 V_VT(retv
) = VT_BSTR
;
928 V_BSTR(retv
) = SysAllocString(NaNW
);
930 return E_OUTOFMEMORY
;
935 time
= local_time(date
->time
, date
);
938 date_str
= SysAllocStringLen(NULL
, 17);
940 return E_OUTOFMEMORY
;
942 offset
= date
->bias
+
943 daylight_saving_ta(time
, date
);
952 sprintfW(date_str
, formatW
, (int)hour_from_time(time
),
953 (int)min_from_time(time
), (int)sec_from_time(time
),
954 sign
, offset
/60, offset
%60);
956 sprintfW(date_str
, formatUTCW
, (int)hour_from_time(time
),
957 (int)min_from_time(time
), (int)sec_from_time(time
));
959 V_VT(retv
) = VT_BSTR
;
960 V_BSTR(retv
) = date_str
;
965 /* ECMA-262 3rd Edition 15.9.5.6 */
966 static HRESULT
Date_toLocaleDateString(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
967 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
969 static const WCHAR NaNW
[] = { 'N','a','N',0 };
977 if(!(date
= date_this(jsthis
)))
978 return throw_type_error(ctx
, ei
, IDS_NOT_DATE
, NULL
);
980 if(isnan(date
->time
)) {
982 V_VT(retv
) = VT_BSTR
;
983 V_BSTR(retv
) = SysAllocString(NaNW
);
985 return E_OUTOFMEMORY
;
990 st
= create_systemtime(local_time(date
->time
, date
));
992 if(st
.wYear
<1601 || st
.wYear
>9999)
993 return dateobj_to_date_string(date
, retv
);
996 len
= GetDateFormatW(ctx
->lcid
, DATE_LONGDATE
, &st
, NULL
, NULL
, 0);
997 date_str
= SysAllocStringLen(NULL
, len
);
999 return E_OUTOFMEMORY
;
1000 GetDateFormatW(ctx
->lcid
, DATE_LONGDATE
, &st
, NULL
, date_str
, len
);
1002 V_VT(retv
) = VT_BSTR
;
1003 V_BSTR(retv
) = date_str
;
1008 /* ECMA-262 3rd Edition 15.9.5.7 */
1009 static HRESULT
Date_toLocaleTimeString(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
1010 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
1012 static const WCHAR NaNW
[] = { 'N','a','N',0 };
1020 if(!(date
= date_this(jsthis
)))
1021 return throw_type_error(ctx
, ei
, IDS_NOT_DATE
, NULL
);
1023 if(isnan(date
->time
)) {
1025 V_VT(retv
) = VT_BSTR
;
1026 V_BSTR(retv
) = SysAllocString(NaNW
);
1028 return E_OUTOFMEMORY
;
1033 st
= create_systemtime(local_time(date
->time
, date
));
1035 if(st
.wYear
<1601 || st
.wYear
>9999)
1036 return Date_toTimeString(ctx
, jsthis
, flags
, dp
, retv
, ei
, caller
);
1039 len
= GetTimeFormatW(ctx
->lcid
, 0, &st
, NULL
, NULL
, 0);
1040 date_str
= SysAllocStringLen(NULL
, len
);
1042 return E_OUTOFMEMORY
;
1043 GetTimeFormatW(ctx
->lcid
, 0, &st
, NULL
, date_str
, len
);
1045 V_VT(retv
) = VT_BSTR
;
1046 V_BSTR(retv
) = date_str
;
1051 /* ECMA-262 3rd Edition 15.9.5.9 */
1052 static HRESULT
Date_getTime(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
1053 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
1059 if(!(date
= date_this(jsthis
)))
1060 return throw_type_error(ctx
, ei
, IDS_NOT_DATE
, NULL
);
1063 num_set_val(retv
, date
->time
);
1067 /* ECMA-262 3rd Edition 15.9.5.10 */
1068 static HRESULT
Date_getFullYear(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
1069 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
1075 if(!(date
= date_this(jsthis
)))
1076 return throw_type_error(ctx
, ei
, IDS_NOT_DATE
, NULL
);
1079 DOUBLE time
= local_time(date
->time
, date
);
1081 num_set_val(retv
, year_from_time(time
));
1086 /* ECMA-262 3rd Edition 15.9.5.11 */
1087 static HRESULT
Date_getUTCFullYear(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
1088 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
1094 if(!(date
= date_this(jsthis
)))
1095 return throw_type_error(ctx
, ei
, IDS_NOT_DATE
, NULL
);
1098 num_set_val(retv
, year_from_time(date
->time
));
1102 /* ECMA-262 3rd Edition 15.9.5.12 */
1103 static HRESULT
Date_getMonth(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
1104 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
1110 if(!(date
= date_this(jsthis
)))
1111 return throw_type_error(ctx
, ei
, IDS_NOT_DATE
, NULL
);
1114 DOUBLE time
= local_time(date
->time
, date
);
1116 num_set_val(retv
, month_from_time(time
));
1121 /* ECMA-262 3rd Edition 15.9.5.13 */
1122 static HRESULT
Date_getUTCMonth(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
1123 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
1129 if(!(date
= date_this(jsthis
)))
1130 return throw_type_error(ctx
, ei
, IDS_NOT_DATE
, NULL
);
1133 num_set_val(retv
, month_from_time(date
->time
));
1137 /* ECMA-262 3rd Edition 15.9.5.14 */
1138 static HRESULT
Date_getDate(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
1139 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
1145 if(!(date
= date_this(jsthis
)))
1146 return throw_type_error(ctx
, ei
, IDS_NOT_DATE
, NULL
);
1149 DOUBLE time
= local_time(date
->time
, date
);
1151 num_set_val(retv
, date_from_time(time
));
1156 /* ECMA-262 3rd Edition 15.9.5.15 */
1157 static HRESULT
Date_getUTCDate(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
1158 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
1164 if(!(date
= date_this(jsthis
)))
1165 return throw_type_error(ctx
, ei
, IDS_NOT_DATE
, NULL
);
1168 num_set_val(retv
, date_from_time(date
->time
));
1172 /* ECMA-262 3rd Edition 15.9.5.16 */
1173 static HRESULT
Date_getDay(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
1174 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
1180 if(!(date
= date_this(jsthis
)))
1181 return throw_type_error(ctx
, ei
, IDS_NOT_DATE
, NULL
);
1184 DOUBLE time
= local_time(date
->time
, date
);
1186 num_set_val(retv
, week_day(time
));
1191 /* ECMA-262 3rd Edition 15.9.5.17 */
1192 static HRESULT
Date_getUTCDay(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
1193 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
1199 if(!(date
= date_this(jsthis
)))
1200 return throw_type_error(ctx
, ei
, IDS_NOT_DATE
, NULL
);
1203 num_set_val(retv
, week_day(date
->time
));
1207 /* ECMA-262 3rd Edition 15.9.5.18 */
1208 static HRESULT
Date_getHours(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
1209 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
1215 if(!(date
= date_this(jsthis
)))
1216 return throw_type_error(ctx
, ei
, IDS_NOT_DATE
, NULL
);
1219 DOUBLE time
= local_time(date
->time
, date
);
1221 num_set_val(retv
, hour_from_time(time
));
1226 /* ECMA-262 3rd Edition 15.9.5.19 */
1227 static HRESULT
Date_getUTCHours(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
1228 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
1234 if(!(date
= date_this(jsthis
)))
1235 return throw_type_error(ctx
, ei
, IDS_NOT_DATE
, NULL
);
1238 num_set_val(retv
, hour_from_time(date
->time
));
1242 /* ECMA-262 3rd Edition 15.9.5.20 */
1243 static HRESULT
Date_getMinutes(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
1244 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
1250 if(!(date
= date_this(jsthis
)))
1251 return throw_type_error(ctx
, ei
, IDS_NOT_DATE
, NULL
);
1254 DOUBLE time
= local_time(date
->time
, date
);
1256 num_set_val(retv
, min_from_time(time
));
1261 /* ECMA-262 3rd Edition 15.9.5.21 */
1262 static HRESULT
Date_getUTCMinutes(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
1263 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
1269 if(!(date
= date_this(jsthis
)))
1270 return throw_type_error(ctx
, ei
, IDS_NOT_DATE
, NULL
);
1273 num_set_val(retv
, min_from_time(date
->time
));
1277 /* ECMA-262 3rd Edition 15.9.5.22 */
1278 static HRESULT
Date_getSeconds(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
1279 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
1285 if(!(date
= date_this(jsthis
)))
1286 return throw_type_error(ctx
, ei
, IDS_NOT_DATE
, NULL
);
1289 DOUBLE time
= local_time(date
->time
, date
);
1291 num_set_val(retv
, sec_from_time(time
));
1296 /* ECMA-262 3rd Edition 15.9.5.23 */
1297 static HRESULT
Date_getUTCSeconds(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
1298 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
1304 if(!(date
= date_this(jsthis
)))
1305 return throw_type_error(ctx
, ei
, IDS_NOT_DATE
, NULL
);
1308 num_set_val(retv
, sec_from_time(date
->time
));
1312 /* ECMA-262 3rd Edition 15.9.5.24 */
1313 static HRESULT
Date_getMilliseconds(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
1314 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
1320 if(!(date
= date_this(jsthis
)))
1321 return throw_type_error(ctx
, ei
, IDS_NOT_DATE
, NULL
);
1324 DOUBLE time
= local_time(date
->time
, date
);
1326 num_set_val(retv
, ms_from_time(time
));
1331 /* ECMA-262 3rd Edition 15.9.5.25 */
1332 static HRESULT
Date_getUTCMilliseconds(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
1333 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
1339 if(!(date
= date_this(jsthis
)))
1340 return throw_type_error(ctx
, ei
, IDS_NOT_DATE
, NULL
);
1343 num_set_val(retv
, ms_from_time(date
->time
));
1347 /* ECMA-262 3rd Edition 15.9.5.26 */
1348 static HRESULT
Date_getTimezoneOffset(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
1349 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
1355 if(!(date
= date_this(jsthis
)))
1356 return throw_type_error(ctx
, ei
, IDS_NOT_DATE
, NULL
);
1359 num_set_val(retv
, floor(
1360 (date
->time
-local_time(date
->time
, date
))/MS_PER_MINUTE
));
1364 /* ECMA-262 3rd Edition 15.9.5.27 */
1365 static HRESULT
Date_setTime(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
1366 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
1374 if(!(date
= date_this(jsthis
)))
1375 return throw_type_error(ctx
, ei
, IDS_NOT_DATE
, NULL
);
1378 return throw_type_error(ctx
, ei
, IDS_ARG_NOT_OPT
, NULL
);
1380 hres
= to_number(ctx
, get_arg(dp
, 0), ei
, &v
);
1384 date
->time
= time_clip(num_val(&v
));
1387 num_set_val(retv
, date
->time
);
1392 /* ECMA-262 3rd Edition 15.9.5.28 */
1393 static HRESULT
Date_setMilliseconds(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
1394 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
1403 if(!(date
= date_this(jsthis
)))
1404 return throw_type_error(ctx
, ei
, IDS_NOT_DATE
, NULL
);
1407 return throw_type_error(ctx
, ei
, IDS_ARG_NOT_OPT
, NULL
);
1409 hres
= to_number(ctx
, get_arg(dp
, 0), ei
, &v
);
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
), num_val(&v
)));
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
, DISPPARAMS
*dp
,
1426 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
1435 if(!(date
= date_this(jsthis
)))
1436 return throw_type_error(ctx
, ei
, IDS_NOT_DATE
, NULL
);
1439 return throw_type_error(ctx
, ei
, IDS_ARG_NOT_OPT
, NULL
);
1441 hres
= to_number(ctx
, get_arg(dp
, 0), ei
, &v
);
1446 t
= make_date(day(t
), make_time(hour_from_time(t
), min_from_time(t
),
1447 sec_from_time(t
), num_val(&v
)));
1448 date
->time
= time_clip(t
);
1451 num_set_val(retv
, date
->time
);
1456 /* ECMA-262 3rd Edition 15.9.5.30 */
1457 static HRESULT
Date_setSeconds(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
1458 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
1467 if(!(date
= date_this(jsthis
)))
1468 return throw_type_error(ctx
, ei
, IDS_NOT_DATE
, NULL
);
1471 return throw_type_error(ctx
, ei
, IDS_ARG_NOT_OPT
, NULL
);
1473 t
= local_time(date
->time
, date
);
1475 hres
= to_number(ctx
, get_arg(dp
, 0), ei
, &v
);
1480 if(arg_cnt(dp
) > 1) {
1481 hres
= to_number(ctx
, get_arg(dp
, 1), ei
, &v
);
1486 else ms
= ms_from_time(t
);
1488 t
= make_date(day(t
), make_time(hour_from_time(t
),
1489 min_from_time(t
), sec
, ms
));
1490 date
->time
= time_clip(utc(t
, date
));
1493 num_set_val(retv
, date
->time
);
1498 /* ECMA-262 3rd Edition 15.9.5.31 */
1499 static HRESULT
Date_setUTCSeconds(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
1500 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
1509 if(!(date
= date_this(jsthis
)))
1510 return throw_type_error(ctx
, ei
, IDS_NOT_DATE
, NULL
);
1513 return throw_type_error(ctx
, ei
, IDS_ARG_NOT_OPT
, NULL
);
1517 hres
= to_number(ctx
, get_arg(dp
, 0), ei
, &v
);
1522 if(arg_cnt(dp
) > 1) {
1523 hres
= to_number(ctx
, get_arg(dp
, 1), ei
, &v
);
1528 else ms
= ms_from_time(t
);
1530 t
= make_date(day(t
), make_time(hour_from_time(t
),
1531 min_from_time(t
), sec
, ms
));
1532 date
->time
= time_clip(t
);
1535 num_set_val(retv
, date
->time
);
1540 /* ECMA-262 3rd Edition 15.9.5.33 */
1541 static HRESULT
Date_setMinutes(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
1542 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
1547 DOUBLE t
, min
, sec
, ms
;
1551 if(!(date
= date_this(jsthis
)))
1552 return throw_type_error(ctx
, ei
, IDS_NOT_DATE
, NULL
);
1555 return throw_type_error(ctx
, ei
, IDS_ARG_NOT_OPT
, NULL
);
1557 t
= local_time(date
->time
, date
);
1559 hres
= to_number(ctx
, get_arg(dp
, 0), ei
, &v
);
1564 if(arg_cnt(dp
) > 1) {
1565 hres
= to_number(ctx
, get_arg(dp
, 1), ei
, &v
);
1570 else sec
= sec_from_time(t
);
1572 if(arg_cnt(dp
) > 2) {
1573 hres
= to_number(ctx
, get_arg(dp
, 2), ei
, &v
);
1578 else ms
= ms_from_time(t
);
1580 t
= make_date(day(t
), make_time(hour_from_time(t
),
1582 date
->time
= time_clip(utc(t
, date
));
1585 num_set_val(retv
, date
->time
);
1590 /* ECMA-262 3rd Edition 15.9.5.34 */
1591 static HRESULT
Date_setUTCMinutes(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
1592 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
1597 DOUBLE t
, min
, sec
, ms
;
1601 if(!(date
= date_this(jsthis
)))
1602 return throw_type_error(ctx
, ei
, IDS_NOT_DATE
, NULL
);
1605 return throw_type_error(ctx
, ei
, IDS_ARG_NOT_OPT
, NULL
);
1609 hres
= to_number(ctx
, get_arg(dp
, 0), ei
, &v
);
1614 if(arg_cnt(dp
) > 1) {
1615 hres
= to_number(ctx
, get_arg(dp
, 1), ei
, &v
);
1620 else sec
= sec_from_time(t
);
1622 if(arg_cnt(dp
) > 2) {
1623 hres
= to_number(ctx
, get_arg(dp
, 2), ei
, &v
);
1628 else ms
= ms_from_time(t
);
1630 t
= make_date(day(t
), make_time(hour_from_time(t
),
1632 date
->time
= time_clip(t
);
1635 num_set_val(retv
, date
->time
);
1640 /* ECMA-262 3rd Edition 15.9.5.35 */
1641 static HRESULT
Date_setHours(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
1642 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
1647 DOUBLE t
, hour
, min
, sec
, ms
;
1651 if(!(date
= date_this(jsthis
)))
1652 return throw_type_error(ctx
, ei
, IDS_NOT_DATE
, NULL
);
1655 return throw_type_error(ctx
, ei
, IDS_ARG_NOT_OPT
, NULL
);
1657 t
= local_time(date
->time
, date
);
1659 hres
= to_number(ctx
, get_arg(dp
, 0), ei
, &v
);
1664 if(arg_cnt(dp
) > 1) {
1665 hres
= to_number(ctx
, get_arg(dp
, 1), ei
, &v
);
1670 else min
= min_from_time(t
);
1672 if(arg_cnt(dp
) > 2) {
1673 hres
= to_number(ctx
, get_arg(dp
, 2), ei
, &v
);
1678 else sec
= sec_from_time(t
);
1680 if(arg_cnt(dp
) > 3) {
1681 hres
= to_number(ctx
, get_arg(dp
, 3), ei
, &v
);
1686 else ms
= ms_from_time(t
);
1688 t
= make_date(day(t
), make_time(hour
, min
, sec
, ms
));
1689 date
->time
= time_clip(utc(t
, date
));
1692 num_set_val(retv
, date
->time
);
1697 /* ECMA-262 3rd Edition 15.9.5.36 */
1698 static HRESULT
Date_setUTCHours(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
1699 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
1704 DOUBLE t
, hour
, min
, sec
, ms
;
1708 if(!(date
= date_this(jsthis
)))
1709 return throw_type_error(ctx
, ei
, IDS_NOT_DATE
, NULL
);
1712 return throw_type_error(ctx
, ei
, IDS_ARG_NOT_OPT
, NULL
);
1716 hres
= to_number(ctx
, get_arg(dp
, 0), ei
, &v
);
1721 if(arg_cnt(dp
) > 1) {
1722 hres
= to_number(ctx
, get_arg(dp
, 1), ei
, &v
);
1727 else min
= min_from_time(t
);
1729 if(arg_cnt(dp
) > 2) {
1730 hres
= to_number(ctx
, get_arg(dp
, 2), ei
, &v
);
1735 else sec
= sec_from_time(t
);
1737 if(arg_cnt(dp
) > 3) {
1738 hres
= to_number(ctx
, get_arg(dp
, 3), ei
, &v
);
1743 else ms
= ms_from_time(t
);
1745 t
= make_date(day(t
), make_time(hour
, min
, sec
, ms
));
1746 date
->time
= time_clip(t
);
1749 num_set_val(retv
, date
->time
);
1754 /* ECMA-262 3rd Edition 15.9.5.36 */
1755 static HRESULT
Date_setDate(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
1756 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
1765 if(!(date
= date_this(jsthis
)))
1766 return throw_type_error(ctx
, ei
, IDS_NOT_DATE
, NULL
);
1769 return throw_type_error(ctx
, ei
, IDS_ARG_NOT_OPT
, NULL
);
1771 hres
= to_number(ctx
, get_arg(dp
, 0), ei
, &v
);
1775 t
= local_time(date
->time
, date
);
1776 t
= make_date(make_day(year_from_time(t
), month_from_time(t
),
1777 num_val(&v
)), time_within_day(t
));
1778 date
->time
= time_clip(utc(t
, date
));
1781 num_set_val(retv
, date
->time
);
1786 /* ECMA-262 3rd Edition 15.9.5.37 */
1787 static HRESULT
Date_setUTCDate(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
1788 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
1797 if(!(date
= date_this(jsthis
)))
1798 return throw_type_error(ctx
, ei
, IDS_NOT_DATE
, NULL
);
1801 return throw_type_error(ctx
, ei
, IDS_ARG_NOT_OPT
, NULL
);
1803 hres
= to_number(ctx
, get_arg(dp
, 0), ei
, &v
);
1808 t
= make_date(make_day(year_from_time(t
), month_from_time(t
),
1809 num_val(&v
)), time_within_day(t
));
1810 date
->time
= time_clip(t
);
1813 num_set_val(retv
, date
->time
);
1818 /* ECMA-262 3rd Edition 15.9.5.38 */
1819 static HRESULT
Date_setMonth(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
1820 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
1825 DOUBLE t
, month
, ddate
;
1829 if(!(date
= date_this(jsthis
)))
1830 return throw_type_error(ctx
, ei
, IDS_NOT_DATE
, NULL
);
1833 return throw_type_error(ctx
, ei
, IDS_ARG_NOT_OPT
, NULL
);
1835 t
= local_time(date
->time
, date
);
1837 hres
= to_number(ctx
, get_arg(dp
, 0), ei
, &v
);
1840 month
= num_val(&v
);
1842 if(arg_cnt(dp
) > 1) {
1843 hres
= to_number(ctx
, get_arg(dp
, 1), ei
, &v
);
1846 ddate
= num_val(&v
);
1848 else ddate
= date_from_time(t
);
1850 t
= make_date(make_day(year_from_time(t
), month
, ddate
),
1851 time_within_day(t
));
1852 date
->time
= time_clip(utc(t
, date
));
1855 num_set_val(retv
, date
->time
);
1860 /* ECMA-262 3rd Edition 15.9.5.39 */
1861 static HRESULT
Date_setUTCMonth(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
1862 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
1867 DOUBLE t
, month
, ddate
;
1871 if(!(date
= date_this(jsthis
)))
1872 return throw_type_error(ctx
, ei
, IDS_NOT_DATE
, NULL
);
1875 return throw_type_error(ctx
, ei
, IDS_ARG_NOT_OPT
, NULL
);
1879 hres
= to_number(ctx
, get_arg(dp
, 0), ei
, &v
);
1882 month
= num_val(&v
);
1884 if(arg_cnt(dp
) > 1) {
1885 hres
= to_number(ctx
, get_arg(dp
, 1), ei
, &v
);
1888 ddate
= num_val(&v
);
1890 else ddate
= date_from_time(t
);
1892 t
= make_date(make_day(year_from_time(t
), month
, ddate
),
1893 time_within_day(t
));
1894 date
->time
= time_clip(t
);
1897 num_set_val(retv
, date
->time
);
1902 /* ECMA-262 3rd Edition 15.9.5.40 */
1903 static HRESULT
Date_setFullYear(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
1904 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
1909 DOUBLE t
, year
, month
, ddate
;
1913 if(!(date
= date_this(jsthis
)))
1914 return throw_type_error(ctx
, ei
, IDS_NOT_DATE
, NULL
);
1917 return throw_type_error(ctx
, ei
, IDS_ARG_NOT_OPT
, NULL
);
1919 t
= local_time(date
->time
, date
);
1921 hres
= to_number(ctx
, get_arg(dp
, 0), ei
, &v
);
1926 if(arg_cnt(dp
) > 1) {
1927 hres
= to_number(ctx
, get_arg(dp
, 1), ei
, &v
);
1930 month
= num_val(&v
);
1932 else month
= month_from_time(t
);
1934 if(arg_cnt(dp
) > 2) {
1935 hres
= to_number(ctx
, get_arg(dp
, 2), ei
, &v
);
1938 ddate
= num_val(&v
);
1940 else ddate
= date_from_time(t
);
1942 t
= make_date(make_day(year
, month
, ddate
), time_within_day(t
));
1943 date
->time
= time_clip(utc(t
, date
));
1946 num_set_val(retv
, date
->time
);
1951 /* ECMA-262 3rd Edition 15.9.5.41 */
1952 static HRESULT
Date_setUTCFullYear(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
1953 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
1958 DOUBLE t
, year
, month
, ddate
;
1962 if(!(date
= date_this(jsthis
)))
1963 return throw_type_error(ctx
, ei
, IDS_NOT_DATE
, NULL
);
1966 return throw_type_error(ctx
, ei
, IDS_ARG_NOT_OPT
, NULL
);
1970 hres
= to_number(ctx
, get_arg(dp
, 0), ei
, &v
);
1975 if(arg_cnt(dp
) > 1) {
1976 hres
= to_number(ctx
, get_arg(dp
, 1), ei
, &v
);
1979 month
= num_val(&v
);
1981 else month
= month_from_time(t
);
1983 if(arg_cnt(dp
) > 2) {
1984 hres
= to_number(ctx
, get_arg(dp
, 2), ei
, &v
);
1987 ddate
= num_val(&v
);
1989 else ddate
= date_from_time(t
);
1991 t
= make_date(make_day(year
, month
, ddate
), time_within_day(t
));
1992 date
->time
= time_clip(t
);
1995 num_set_val(retv
, date
->time
);
2000 /* ECMA-262 3rd Edition B2.4 */
2001 static HRESULT
Date_getYear(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
2002 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
2009 if(!(date
= date_this(jsthis
)))
2010 return throw_type_error(ctx
, ei
, IDS_NOT_DATE
, NULL
);
2012 t
= local_time(date
->time
, date
);
2019 year
= year_from_time(t
);
2021 num_set_val(retv
, (1900<=year
&& year
<2000)?year
-1900:year
);
2026 static HRESULT
Date_value(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
2027 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
2033 return throw_type_error(ctx
, ei
, IDS_NOT_FUNC
, NULL
);
2035 FIXME("unimplemented flags %x\n", flags
);
2042 static const builtin_prop_t Date_props
[] = {
2043 {getDateW
, Date_getDate
, PROPF_METHOD
},
2044 {getDayW
, Date_getDay
, PROPF_METHOD
},
2045 {getFullYearW
, Date_getFullYear
, PROPF_METHOD
},
2046 {getHoursW
, Date_getHours
, PROPF_METHOD
},
2047 {getMillisecondsW
, Date_getMilliseconds
, PROPF_METHOD
},
2048 {getMinutesW
, Date_getMinutes
, PROPF_METHOD
},
2049 {getMonthW
, Date_getMonth
, PROPF_METHOD
},
2050 {getSecondsW
, Date_getSeconds
, PROPF_METHOD
},
2051 {getTimeW
, Date_getTime
, PROPF_METHOD
},
2052 {getTimezoneOffsetW
, Date_getTimezoneOffset
, PROPF_METHOD
},
2053 {getUTCDateW
, Date_getUTCDate
, PROPF_METHOD
},
2054 {getUTCDayW
, Date_getUTCDay
, PROPF_METHOD
},
2055 {getUTCFullYearW
, Date_getUTCFullYear
, PROPF_METHOD
},
2056 {getUTCHoursW
, Date_getUTCHours
, PROPF_METHOD
},
2057 {getUTCMillisecondsW
, Date_getUTCMilliseconds
, PROPF_METHOD
},
2058 {getUTCMinutesW
, Date_getUTCMinutes
, PROPF_METHOD
},
2059 {getUTCMonthW
, Date_getUTCMonth
, PROPF_METHOD
},
2060 {getUTCSecondsW
, Date_getUTCSeconds
, PROPF_METHOD
},
2061 {getYearW
, Date_getYear
, PROPF_METHOD
},
2062 {setDateW
, Date_setDate
, PROPF_METHOD
|1},
2063 {setFullYearW
, Date_setFullYear
, PROPF_METHOD
|3},
2064 {setHoursW
, Date_setHours
, PROPF_METHOD
|4},
2065 {setMillisecondsW
, Date_setMilliseconds
, PROPF_METHOD
|1},
2066 {setMinutesW
, Date_setMinutes
, PROPF_METHOD
|3},
2067 {setMonthW
, Date_setMonth
, PROPF_METHOD
|2},
2068 {setSecondsW
, Date_setSeconds
, PROPF_METHOD
|2},
2069 {setTimeW
, Date_setTime
, PROPF_METHOD
|1},
2070 {setUTCDateW
, Date_setUTCDate
, PROPF_METHOD
|1},
2071 {setUTCFullYearW
, Date_setUTCFullYear
, PROPF_METHOD
|3},
2072 {setUTCHoursW
, Date_setUTCHours
, PROPF_METHOD
|4},
2073 {setUTCMillisecondsW
, Date_setUTCMilliseconds
, PROPF_METHOD
|1},
2074 {setUTCMinutesW
, Date_setUTCMinutes
, PROPF_METHOD
|3},
2075 {setUTCMonthW
, Date_setUTCMonth
, PROPF_METHOD
|2},
2076 {setUTCSecondsW
, Date_setUTCSeconds
, PROPF_METHOD
|2},
2077 {toDateStringW
, Date_toDateString
, PROPF_METHOD
},
2078 {toGMTStringW
, Date_toGMTString
, PROPF_METHOD
},
2079 {toLocaleDateStringW
, Date_toLocaleDateString
, PROPF_METHOD
},
2080 {toLocaleStringW
, Date_toLocaleString
, PROPF_METHOD
},
2081 {toLocaleTimeStringW
, Date_toLocaleTimeString
, PROPF_METHOD
},
2082 {toStringW
, Date_toString
, PROPF_METHOD
},
2083 {toTimeStringW
, Date_toTimeString
, PROPF_METHOD
},
2084 {toUTCStringW
, Date_toUTCString
, PROPF_METHOD
},
2085 {valueOfW
, Date_valueOf
, PROPF_METHOD
},
2088 static const builtin_info_t Date_info
= {
2090 {NULL
, Date_value
, 0},
2091 sizeof(Date_props
)/sizeof(*Date_props
),
2097 static HRESULT
create_date(script_ctx_t
*ctx
, DispatchEx
*object_prototype
, DOUBLE time
, DispatchEx
**ret
)
2101 TIME_ZONE_INFORMATION tzi
;
2103 GetTimeZoneInformation(&tzi
);
2105 date
= heap_alloc_zero(sizeof(DateInstance
));
2107 return E_OUTOFMEMORY
;
2109 if(object_prototype
)
2110 hres
= init_dispex(&date
->dispex
, ctx
, &Date_info
, object_prototype
);
2112 hres
= init_dispex_from_constr(&date
->dispex
, ctx
, &Date_info
, ctx
->date_constr
);
2119 date
->bias
= tzi
.Bias
;
2120 date
->standardDate
= tzi
.StandardDate
;
2121 date
->standardBias
= tzi
.StandardBias
;
2122 date
->daylightDate
= tzi
.DaylightDate
;
2123 date
->daylightBias
= tzi
.DaylightBias
;
2125 *ret
= &date
->dispex
;
2129 static inline HRESULT
date_parse(BSTR input
, VARIANT
*retv
) {
2130 static const DWORD string_ids
[] = { LOCALE_SMONTHNAME12
, LOCALE_SMONTHNAME11
,
2131 LOCALE_SMONTHNAME10
, LOCALE_SMONTHNAME9
, LOCALE_SMONTHNAME8
,
2132 LOCALE_SMONTHNAME7
, LOCALE_SMONTHNAME6
, LOCALE_SMONTHNAME5
,
2133 LOCALE_SMONTHNAME4
, LOCALE_SMONTHNAME3
, LOCALE_SMONTHNAME2
,
2134 LOCALE_SMONTHNAME1
, LOCALE_SDAYNAME7
, LOCALE_SDAYNAME1
,
2135 LOCALE_SDAYNAME2
, LOCALE_SDAYNAME3
, LOCALE_SDAYNAME4
,
2136 LOCALE_SDAYNAME5
, LOCALE_SDAYNAME6
};
2137 BSTR strings
[sizeof(string_ids
)/sizeof(DWORD
)];
2140 int input_len
, parse_len
= 0, nest_level
= 0, i
, size
;
2141 int year
= 0, month
= 0, day
= 0, hour
= 0, min
= 0, sec
= 0;
2142 int ms
= 0, offset
= 0, hour_adjust
= 0;
2143 BOOL set_year
= FALSE
, set_month
= FALSE
, set_day
= FALSE
, set_hour
= FALSE
;
2144 BOOL set_offset
= FALSE
, set_era
= FALSE
, ad
= TRUE
, set_am
= FALSE
, am
= TRUE
;
2145 BOOL set_hour_adjust
= TRUE
;
2146 TIME_ZONE_INFORMATION tzi
;
2150 if(retv
) num_set_nan(retv
);
2152 input_len
= SysStringLen(input
);
2153 for(i
=0; i
<input_len
; i
++) {
2154 if(input
[i
] == '(') nest_level
++;
2155 else if(input
[i
] == ')') {
2160 else if(!nest_level
) parse_len
++;
2163 parse
= SysAllocStringLen(NULL
, parse_len
);
2165 return E_OUTOFMEMORY
;
2168 for(i
=0; i
<input_len
; i
++) {
2169 if(input
[i
] == '(') nest_level
++;
2170 else if(input
[i
] == ')') nest_level
--;
2171 else if(!nest_level
) parse
[parse_len
++] = toupperW(input
[i
]);
2174 GetTimeZoneInformation(&tzi
);
2176 di
.standardDate
= tzi
.StandardDate
;
2177 di
.standardBias
= tzi
.StandardBias
;
2178 di
.daylightDate
= tzi
.DaylightDate
;
2179 di
.daylightBias
= tzi
.DaylightBias
;
2181 lcid_en
= MAKELCID(MAKELANGID(LANG_ENGLISH
,SUBLANG_ENGLISH_US
),SORT_DEFAULT
);
2182 for(i
=0; i
<sizeof(string_ids
)/sizeof(DWORD
); i
++) {
2183 size
= GetLocaleInfoW(lcid_en
, string_ids
[i
], NULL
, 0);
2184 strings
[i
] = SysAllocStringLen(NULL
, size
);
2188 SysFreeString(strings
[i
]);
2189 SysFreeString(parse
);
2190 return E_OUTOFMEMORY
;
2192 GetLocaleInfoW(lcid_en
, string_ids
[i
], strings
[i
], size
);
2195 for(i
=0; i
<parse_len
;) {
2196 while(isspaceW(parse
[i
])) i
++;
2197 if(parse
[i
] == ',') {
2198 while(parse
[i
] == ',') i
++;
2202 if(parse
[i
]>='0' && parse
[i
]<='9') {
2203 int tmp
= atoiW(&parse
[i
]);
2204 while(parse
[i
]>='0' && parse
[i
]<='9') i
++;
2205 while(isspaceW(parse
[i
])) i
++;
2207 if(parse
[i
] == ':') {
2214 while(parse
[i
] == ':') i
++;
2215 while(isspaceW(parse
[i
])) i
++;
2216 if(parse
[i
]>='0' && parse
[i
]<='9') {
2217 min
= atoiW(&parse
[i
]);
2218 while(parse
[i
]>='0' && parse
[i
]<='9') i
++;
2221 while(isspaceW(parse
[i
])) i
++;
2222 while(parse
[i
] == ':') i
++;
2223 while(isspaceW(parse
[i
])) i
++;
2224 if(parse
[i
]>='0' && parse
[i
]<='9') {
2225 sec
= atoiW(&parse
[i
]);
2226 while(parse
[i
]>='0' && parse
[i
]<='9') i
++;
2229 else if(parse
[i
]=='-' || parse
[i
]=='/') {
2231 if(set_day
|| set_month
|| set_year
) break;
2238 while(isspaceW(parse
[i
])) i
++;
2239 while(parse
[i
]=='-' || parse
[i
]=='/') i
++;
2240 while(isspaceW(parse
[i
])) i
++;
2241 if(parse
[i
]<'0' || parse
[i
]>'9') break;
2242 day
= atoiW(&parse
[i
]);
2243 while(parse
[i
]>='0' && parse
[i
]<='9') i
++;
2245 while(parse
[i
]=='-' || parse
[i
]=='/') i
++;
2246 while(isspaceW(parse
[i
])) i
++;
2247 if(parse
[i
]<'0' || parse
[i
]>'9') break;
2248 year
= atoiW(&parse
[i
]);
2249 while(parse
[i
]>='0' && parse
[i
]<='9') i
++;
2251 else if(tmp
<0) break;
2266 if(parse
[i
]<'A' || parse
[i
]>'Z') break;
2267 else if(parse
[i
]=='B' && (parse
[i
+1]=='C' ||
2268 (parse
[i
+1]=='.' && parse
[i
+2]=='C'))) {
2275 if(parse
[i
] == '.') i
++;
2277 if(parse
[i
] == '.') i
++;
2279 else if(parse
[i
]=='A' && (parse
[i
+1]=='D' ||
2280 (parse
[i
+1]=='.' && parse
[i
+2]=='D'))) {
2286 if(parse
[i
] == '.') i
++;
2288 if(parse
[i
] == '.') i
++;
2290 else if(parse
[i
+1]<'A' || parse
[i
+1]>'Z') {
2292 if(set_offset
) break;
2295 if(parse
[i
] <= 'I') hour_adjust
= parse
[i
]-'A'+2;
2296 else if(parse
[i
] == 'J') break;
2297 else if(parse
[i
] <= 'M') hour_adjust
= parse
[i
]-'K'+11;
2298 else if(parse
[i
] <= 'Y') hour_adjust
= parse
[i
]-'N';
2299 else hour_adjust
= 1;
2302 if(parse
[i
] == '.') i
++;
2304 else if(parse
[i
]=='A' && parse
[i
+1]=='M') {
2311 else if(parse
[i
]=='P' && parse
[i
+1]=='M') {
2318 else if((parse
[i
]=='U' && parse
[i
+1]=='T' && parse
[i
+2]=='C')
2319 || (parse
[i
]=='G' && parse
[i
+1]=='M' && parse
[i
+2]=='T')) {
2321 BOOL positive
= TRUE
;
2323 if(set_offset
) break;
2325 set_hour_adjust
= FALSE
;
2328 while(isspaceW(parse
[i
])) i
++;
2329 if(parse
[i
] == '-') positive
= FALSE
;
2330 else if(parse
[i
] != '+') continue;
2333 while(isspaceW(parse
[i
])) i
++;
2334 if(parse
[i
]<'0' || parse
[i
]>'9') break;
2335 offset
= atoiW(&parse
[i
]);
2336 while(parse
[i
]>='0' && parse
[i
]<='9') i
++;
2338 if(offset
<24) offset
*= 60;
2339 else offset
= (offset
/100)*60 + offset
%100;
2341 if(positive
) offset
= -offset
;
2344 /* Month or garbage */
2347 for(size
=i
; parse
[size
]>='A' && parse
[size
]<='Z'; size
++);
2350 for(j
=0; j
<sizeof(string_ids
)/sizeof(DWORD
); j
++)
2351 if(!memicmpW(&parse
[i
], strings
[j
], size
)) break;
2354 if(set_month
) break;
2358 else if(j
== sizeof(string_ids
)/sizeof(DWORD
)) break;
2365 if(retv
&& i
==parse_len
&& set_year
&& set_month
2366 && set_day
&& (!set_am
|| hour
<13)) {
2368 if(hour
== 12) hour
= 0;
2372 if(!ad
) year
= -year
+1;
2373 else if(year
<100) year
+= 1900;
2376 V_R8(retv
) = time_clip(make_date(make_day(year
, month
, day
),
2377 make_time(hour
+hour_adjust
, min
, sec
, ms
)) + offset
*MS_PER_MINUTE
);
2379 if(set_hour_adjust
) V_R8(retv
) = utc(V_R8(retv
), &di
);
2382 for(i
=0; i
<sizeof(string_ids
)/sizeof(DWORD
); i
++)
2383 SysFreeString(strings
[i
]);
2384 SysFreeString(parse
);
2389 static HRESULT
DateConstr_parse(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
2390 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
2403 hres
= to_string(ctx
, get_arg(dp
,0), ei
, &parse_str
);
2407 hres
= date_parse(parse_str
, retv
);
2409 SysFreeString(parse_str
);
2413 static HRESULT
date_utc(script_ctx_t
*ctx
, DISPPARAMS
*dp
, VARIANT
*retv
, jsexcept_t
*ei
)
2415 VARIANT year
, month
, vdate
, hours
, minutes
, seconds
, ms
;
2417 int arg_no
= arg_cnt(dp
);
2423 hres
= to_number(ctx
, get_arg(dp
, 0), ei
, &year
);
2433 hres
= to_number(ctx
, get_arg(dp
, 1), ei
, &month
);
2438 V_VT(&month
) = VT_R8
;
2443 hres
= to_number(ctx
, get_arg(dp
, 2), ei
, &vdate
);
2448 V_VT(&vdate
) = VT_R8
;
2453 hres
= to_number(ctx
, get_arg(dp
, 3), ei
, &hours
);
2458 V_VT(&hours
) = VT_R8
;
2463 hres
= to_number(ctx
, get_arg(dp
, 4), ei
, &minutes
);
2468 V_VT(&minutes
) = VT_R8
;
2473 hres
= to_number(ctx
, get_arg(dp
, 5), ei
, &seconds
);
2478 V_VT(&seconds
) = VT_R8
;
2483 hres
= to_number(ctx
, get_arg(dp
, 6), ei
, &ms
);
2494 V_R8(retv
) = time_clip(make_date(
2495 make_day(y
, num_val(&month
), num_val(&vdate
)),
2496 make_time(num_val(&hours
), num_val(&minutes
),
2497 num_val(&seconds
), num_val(&ms
))));
2503 static HRESULT
DateConstr_UTC(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
2504 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
2508 return date_utc(ctx
, dp
, retv
, ei
);
2511 static HRESULT
DateConstr_value(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
2512 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
2520 case DISPATCH_CONSTRUCT
:
2521 switch(arg_cnt(dp
)) {
2522 /* ECMA-262 3rd Edition 15.9.3.3 */
2527 GetSystemTimeAsFileTime(&time
);
2528 lltime
= ((LONGLONG
)time
.dwHighDateTime
<<32)
2529 + time
.dwLowDateTime
;
2531 hres
= create_date(ctx
, NULL
, lltime
/10000-TIME_EPOCH
, &date
);
2537 /* ECMA-262 3rd Edition 15.9.3.2 */
2541 hres
= to_primitive(ctx
, get_arg(dp
,0), ei
, &prim
, NO_HINT
);
2545 if(V_VT(&prim
) == VT_BSTR
)
2546 hres
= date_parse(V_BSTR(&prim
), &num
);
2548 hres
= to_number(ctx
, &prim
, ei
, &num
);
2550 VariantClear(&prim
);
2554 hres
= create_date(ctx
, NULL
, time_clip(num_val(&num
)), &date
);
2560 /* ECMA-262 3rd Edition 15.9.3.1 */
2565 hres
= date_utc(ctx
, dp
, &ret_date
, ei
);
2569 hres
= create_date(ctx
, NULL
, num_val(&ret_date
), &date
);
2573 di
= (DateInstance
*)date
;
2574 di
->time
= utc(di
->time
, di
);
2578 V_VT(retv
) = VT_DISPATCH
;
2579 V_DISPATCH(retv
) = (IDispatch
*)_IDispatchEx_(date
);
2583 FILETIME system_time
, local_time
;
2586 GetSystemTimeAsFileTime(&system_time
);
2587 FileTimeToLocalFileTime(&system_time
, &local_time
);
2588 lltime
= ((LONGLONG
)local_time
.dwHighDateTime
<<32)
2589 + local_time
.dwLowDateTime
;
2591 return date_to_string(lltime
/10000-TIME_EPOCH
, FALSE
, 0, retv
);
2595 FIXME("unimplemented flags %x\n", flags
);
2602 static const builtin_prop_t DateConstr_props
[] = {
2603 {UTCW
, DateConstr_UTC
, PROPF_METHOD
},
2604 {parseW
, DateConstr_parse
, PROPF_METHOD
}
2607 static const builtin_info_t DateConstr_info
= {
2609 {NULL
, Function_value
, 0},
2610 sizeof(DateConstr_props
)/sizeof(*DateConstr_props
),
2616 HRESULT
create_date_constr(script_ctx_t
*ctx
, DispatchEx
*object_prototype
, DispatchEx
**ret
)
2621 static const WCHAR DateW
[] = {'D','a','t','e',0};
2623 hres
= create_date(ctx
, object_prototype
, 0.0, &date
);
2627 hres
= create_builtin_function(ctx
, DateConstr_value
, DateW
, &DateConstr_info
,
2628 PROPF_CONSTR
|7, date
, ret
);
2630 jsdisp_release(date
);