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
27 #include "wine/debug.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(jscript
);
31 /* 1601 to 1970 is 369 years plus 89 leap days */
32 #define TIME_EPOCH ((ULONGLONG)(369 * 365 + 89) * 86400 * 1000)
37 /* ECMA-262 3rd Edition 15.9.1.1 */
41 SYSTEMTIME standardDate
;
43 SYSTEMTIME daylightDate
;
47 static inline DateInstance
*date_from_jsdisp(jsdisp_t
*jsdisp
)
49 return CONTAINING_RECORD(jsdisp
, DateInstance
, dispex
);
52 static inline DateInstance
*date_this(vdisp_t
*jsthis
)
54 return is_vclass(jsthis
, JSCLASS_DATE
) ? date_from_jsdisp(jsthis
->u
.jsdisp
) : NULL
;
57 /*ECMA-262 3rd Edition 15.9.1.2 */
58 #define MS_PER_DAY 86400000
59 #define MS_PER_HOUR 3600000
60 #define MS_PER_MINUTE 60000
62 /* ECMA-262 3rd Edition 15.9.1.2 */
63 static inline DOUBLE
day(DOUBLE time
)
65 return floor(time
/ MS_PER_DAY
);
68 /* ECMA-262 3rd Edition 15.9.1.2 */
69 static inline DOUBLE
time_within_day(DOUBLE time
)
73 ret
= fmod(time
, MS_PER_DAY
);
80 /* ECMA-262 3rd Edition 15.9.1.3 */
81 static inline DOUBLE
days_in_year(DOUBLE year
)
89 if(y
%4 != 0) return 365;
90 if(y
%100 != 0) return 366;
91 if(y
%400 != 0) return 365;
95 /* ECMA-262 3rd Edition 15.9.1.3 */
96 static inline DOUBLE
day_from_year(DOUBLE year
)
101 return floor(365.0*(year
-1970) + floor((year
-1969)/4)
102 - floor((year
-1901)/100) + floor((year
-1601)/400));
105 static inline int day_from_month(int month
, int in_leap_year
)
114 return 59+in_leap_year
;
116 return 90+in_leap_year
;
118 return 120+in_leap_year
;
120 return 151+in_leap_year
;
122 return 181+in_leap_year
;
124 return 212+in_leap_year
;
126 return 243+in_leap_year
;
128 return 273+in_leap_year
;
130 return 304+in_leap_year
;
132 return 334+in_leap_year
;
136 /* ECMA-262 3rd Edition 15.9.1.3 */
137 static inline DOUBLE
time_from_year(DOUBLE year
)
139 return MS_PER_DAY
*day_from_year(year
);
142 /* ECMA-262 3rd Edition 15.9.1.3 */
143 static inline DOUBLE
year_from_time(DOUBLE time
)
150 y
= 1970 + time
/365.25/MS_PER_DAY
;
152 if(time_from_year(y
) > time
)
153 while(time_from_year(y
) > time
) y
--;
155 while(time_from_year(y
+1)<=time
) y
++;
160 /* ECMA-262 3rd Edition 15.9.1.3 */
161 static inline int in_leap_year(DOUBLE time
)
163 if(days_in_year(year_from_time(time
))==366)
168 /* ECMA-262 3rd Edition 15.9.1.4 */
169 static inline int day_within_year(DOUBLE time
)
171 return day(time
) - day_from_year(year_from_time(time
));
174 /* ECMA-262 3rd Edition 15.9.1.4 */
175 static inline DOUBLE
month_from_time(DOUBLE time
)
177 int ily
= in_leap_year(time
);
178 int dwy
= day_within_year(time
);
183 if(0<=dwy
&& dwy
<31) return 0;
184 if(dwy
< 59+ily
) return 1;
185 if(dwy
< 90+ily
) return 2;
186 if(dwy
< 120+ily
) return 3;
187 if(dwy
< 151+ily
) return 4;
188 if(dwy
< 181+ily
) return 5;
189 if(dwy
< 212+ily
) return 6;
190 if(dwy
< 243+ily
) return 7;
191 if(dwy
< 273+ily
) return 8;
192 if(dwy
< 304+ily
) return 9;
193 if(dwy
< 334+ily
) return 10;
197 /* ECMA-262 3rd Edition 15.9.1.5 */
198 static inline DOUBLE
date_from_time(DOUBLE time
)
200 int dwy
= day_within_year(time
);
201 int ily
= in_leap_year(time
);
202 int mft
= month_from_time(time
);
207 if(mft
==0) return dwy
+1;
208 if(mft
==1) return dwy
-30;
209 if(mft
==2) return dwy
-58-ily
;
210 if(mft
==3) return dwy
-89-ily
;
211 if(mft
==4) return dwy
-119-ily
;
212 if(mft
==5) return dwy
-150-ily
;
213 if(mft
==6) return dwy
-180-ily
;
214 if(mft
==7) return dwy
-211-ily
;
215 if(mft
==8) return dwy
-242-ily
;
216 if(mft
==9) return dwy
-272-ily
;
217 if(mft
==10) return dwy
-303-ily
;
221 /* ECMA-262 3rd Edition 15.9.1.6 */
222 static inline DOUBLE
week_day(DOUBLE time
)
229 ret
= fmod(day(time
)+4, 7);
235 static inline DOUBLE
convert_time(int year
, SYSTEMTIME st
)
246 time
= time_from_year(year
);
247 time
+= (DOUBLE
)day_from_month(st
.wMonth
-1, in_leap_year(time
)) * MS_PER_DAY
;
250 set_week_day
= st
.wDayOfWeek
-week_day(time
);
253 time
+= set_week_day
* MS_PER_DAY
;
255 time
+= (DOUBLE
)(st
.wDay
-1) * 7 * MS_PER_DAY
;
256 if(month_from_time(time
) != st
.wMonth
-1)
257 time
-= 7 * MS_PER_DAY
;
260 time
+= st
.wDay
* MS_PER_DAY
;
262 time
+= st
.wHour
* MS_PER_HOUR
;
263 time
+= st
.wMinute
* MS_PER_MINUTE
;
268 /* ECMA-262 3rd Edition 15.9.1.9 */
269 static inline DOUBLE
daylight_saving_ta(DOUBLE time
, DateInstance
*date
)
271 int year
= year_from_time(time
);
272 DOUBLE standardTime
, daylightTime
;
277 standardTime
= convert_time(year
, date
->standardDate
);
278 daylightTime
= convert_time(year
, date
->daylightDate
);
280 if(isnan(standardTime
) || isnan(daylightTime
))
282 else if(standardTime
> daylightTime
) {
283 if(daylightTime
<= time
&& time
< standardTime
)
284 return date
->daylightBias
;
286 return date
->standardBias
;
289 if(standardTime
<= time
&& time
< daylightTime
)
290 return date
->standardBias
;
292 return date
->daylightBias
;
296 /* ECMA-262 3rd Edition 15.9.1.9 */
297 static inline DOUBLE
local_time(DOUBLE time
, DateInstance
*date
)
299 return time
- (daylight_saving_ta(time
, date
)+date
->bias
)*MS_PER_MINUTE
;
302 /* ECMA-262 3rd Edition 15.9.1.9 */
303 static inline DOUBLE
utc(DOUBLE time
, DateInstance
*date
)
305 time
+= date
->bias
* MS_PER_MINUTE
;
306 return time
+ daylight_saving_ta(time
, date
)*MS_PER_MINUTE
;
309 /* ECMA-262 3rd Edition 15.9.1.10 */
310 static inline DOUBLE
hour_from_time(DOUBLE time
)
317 ret
= fmod(floor(time
/MS_PER_HOUR
), 24);
323 /* ECMA-262 3rd Edition 15.9.1.10 */
324 static inline DOUBLE
min_from_time(DOUBLE time
)
331 ret
= fmod(floor(time
/MS_PER_MINUTE
), 60);
337 /* ECMA-262 3rd Edition 15.9.1.10 */
338 static inline DOUBLE
sec_from_time(DOUBLE time
)
345 ret
= fmod(floor(time
/1000), 60);
351 /* ECMA-262 3rd Edition 15.9.1.10 */
352 static inline DOUBLE
ms_from_time(DOUBLE time
)
359 ret
= fmod(time
, 1000);
360 if(ret
<0) ret
+= 1000;
365 /* ECMA-262 3rd Edition 15.9.1.11 */
366 static inline DOUBLE
make_time(DOUBLE hour
, DOUBLE min
, DOUBLE sec
, DOUBLE ms
)
368 return hour
*MS_PER_HOUR
+ min
*MS_PER_MINUTE
+ sec
*1000 + ms
;
371 /* ECMA-262 3rd Edition 15.9.1.12 */
372 static inline DOUBLE
make_day(DOUBLE year
, DOUBLE month
, DOUBLE day
)
376 year
+= floor(month
/12);
378 month
= fmod(month
, 12);
379 if(month
<0) month
+= 12;
381 time
= time_from_year(year
);
383 day
+= floor(time
/ MS_PER_DAY
);
384 day
+= day_from_month(month
, in_leap_year(time
));
389 /* ECMA-262 3rd Edition 15.9.1.13 */
390 static inline DOUBLE
make_date(DOUBLE day
, DOUBLE time
)
392 return day
*MS_PER_DAY
+ time
;
395 /* ECMA-262 3rd Edition 15.9.1.14 */
396 static inline DOUBLE
time_clip(DOUBLE time
)
398 if(8.64e15
< time
|| time
< -8.64e15
) {
405 static double date_now(void)
410 GetSystemTimeAsFileTime(&ftime
);
411 time
= ((LONGLONG
)ftime
.dwHighDateTime
<< 32) + ftime
.dwLowDateTime
;
413 return time
/10000 - TIME_EPOCH
;
416 static SYSTEMTIME
create_systemtime(DOUBLE time
)
420 st
.wYear
= year_from_time(time
);
421 st
.wMonth
= month_from_time(time
) + 1;
422 st
.wDayOfWeek
= week_day(time
);
423 st
.wDay
= date_from_time(time
);
424 st
.wHour
= hour_from_time(time
);
425 st
.wMinute
= min_from_time(time
);
426 st
.wSecond
= sec_from_time(time
);
427 st
.wMilliseconds
= ms_from_time(time
);
432 static inline HRESULT
date_to_string(DOUBLE time
, BOOL show_offset
, int offset
, jsval_t
*r
)
434 static const DWORD week_ids
[] = { LOCALE_SABBREVDAYNAME7
, LOCALE_SABBREVDAYNAME1
,
435 LOCALE_SABBREVDAYNAME2
, LOCALE_SABBREVDAYNAME3
, LOCALE_SABBREVDAYNAME4
,
436 LOCALE_SABBREVDAYNAME5
, LOCALE_SABBREVDAYNAME6
};
437 static const DWORD month_ids
[] = { LOCALE_SABBREVMONTHNAME1
, LOCALE_SABBREVMONTHNAME2
,
438 LOCALE_SABBREVMONTHNAME3
, LOCALE_SABBREVMONTHNAME4
,
439 LOCALE_SABBREVMONTHNAME5
, LOCALE_SABBREVMONTHNAME6
,
440 LOCALE_SABBREVMONTHNAME7
, LOCALE_SABBREVMONTHNAME8
,
441 LOCALE_SABBREVMONTHNAME9
, LOCALE_SABBREVMONTHNAME10
,
442 LOCALE_SABBREVMONTHNAME11
, LOCALE_SABBREVMONTHNAME12
};
444 const WCHAR
*formatEra
= L
"";
445 WCHAR week
[64], month
[64];
454 *r
= jsval_string(jsstr_nan());
459 lcid_en
= MAKELCID(MAKELANGID(LANG_ENGLISH
,SUBLANG_ENGLISH_US
),SORT_DEFAULT
);
462 GetLocaleInfoW(lcid_en
, week_ids
[(int)week_day(time
)], week
, ARRAY_SIZE(week
));
465 GetLocaleInfoW(lcid_en
, month_ids
[(int)month_from_time(time
)], month
, ARRAY_SIZE(month
));
467 year
= year_from_time(time
);
469 formatEra
= L
" B.C.";
473 day
= date_from_time(time
);
481 swprintf(buf
, ARRAY_SIZE(buf
), L
"%s %s %d %02d:%02d:%02d %d%s", week
, month
, day
,
482 (int)hour_from_time(time
), (int)min_from_time(time
),
483 (int)sec_from_time(time
), year
, formatEra
);
485 swprintf(buf
, ARRAY_SIZE(buf
), L
"%s %s %d %02d:%02d:%02d UTC%c%02d%02d %d%s", week
, month
, day
,
486 (int)hour_from_time(time
), (int)min_from_time(time
),
487 (int)sec_from_time(time
), sign
, offset
/60, offset
%60,
490 swprintf(buf
, ARRAY_SIZE(buf
), L
"%s %s %d %02d:%02d:%02d UTC %d%s", week
, month
, day
,
491 (int)hour_from_time(time
), (int)min_from_time(time
),
492 (int)sec_from_time(time
), year
, formatEra
);
494 date_jsstr
= jsstr_alloc(buf
);
496 return E_OUTOFMEMORY
;
498 *r
= jsval_string(date_jsstr
);
503 /* ECMA-262 3rd Edition 15.9.1.2 */
504 static HRESULT
dateobj_to_string(DateInstance
*date
, jsval_t
*r
)
509 time
= local_time(date
->time
, date
);
510 offset
= date
->bias
+
511 daylight_saving_ta(time
, date
);
513 return date_to_string(time
, TRUE
, offset
, r
);
516 static HRESULT
Date_toString(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
522 if(!(date
= date_this(jsthis
)))
523 return JS_E_DATE_EXPECTED
;
525 return dateobj_to_string(date
, r
);
528 /* ECMA-262 3rd Edition 15.9.1.5 */
529 static HRESULT
Date_toLocaleString(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
535 int date_len
, time_len
;
539 if(!(date
= date_this(jsthis
)))
540 return JS_E_DATE_EXPECTED
;
542 if(isnan(date
->time
)) {
544 *r
= jsval_string(jsstr_nan());
548 st
= create_systemtime(local_time(date
->time
, date
));
550 if(st
.wYear
<1601 || st
.wYear
>9999)
551 return dateobj_to_string(date
, r
);
556 date_len
= GetDateFormatW(ctx
->lcid
, DATE_LONGDATE
, &st
, NULL
, NULL
, 0);
557 time_len
= GetTimeFormatW(ctx
->lcid
, 0, &st
, NULL
, NULL
, 0);
559 date_str
= jsstr_alloc_buf(date_len
+time_len
-1, &ptr
);
561 return E_OUTOFMEMORY
;
563 GetDateFormatW(ctx
->lcid
, DATE_LONGDATE
, &st
, NULL
, ptr
, date_len
);
564 GetTimeFormatW(ctx
->lcid
, 0, &st
, NULL
, ptr
+date_len
, time_len
);
565 ptr
[date_len
-1] = ' ';
567 *r
= jsval_string(date_str
);
572 static HRESULT
Date_toISOString(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
576 WCHAR buf
[64], *p
= buf
;
581 if(!(date
= date_this(jsthis
)))
582 return JS_E_DATE_EXPECTED
;
584 year
= year_from_time(date
->time
);
585 if(isnan(year
) || year
> 999999 || year
< -999999) {
586 FIXME("year %lf should throw an exception\n", year
);
592 p
+= swprintf(p
, ARRAY_SIZE(buf
) - 1, L
"%06d", -(int)year
);
593 }else if(year
> 9999) {
595 p
+= swprintf(p
, ARRAY_SIZE(buf
) - 1, L
"%06d", (int)year
);
597 p
+= swprintf(p
, ARRAY_SIZE(buf
), L
"%04d", (int)year
);
600 swprintf(p
, ARRAY_SIZE(buf
) - (p
- buf
), L
"-%02d-%02dT%02d:%02d:%02d.%03dZ",
601 (int)month_from_time(date
->time
) + 1, (int)date_from_time(date
->time
),
602 (int)hour_from_time(date
->time
), (int)min_from_time(date
->time
),
603 (int)sec_from_time(date
->time
), (int)ms_from_time(date
->time
));
607 if(!(ret
= jsstr_alloc(buf
)))
608 return E_OUTOFMEMORY
;
609 *r
= jsval_string(ret
);
614 static HRESULT
Date_valueOf(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
621 if(!(date
= date_this(jsthis
)))
622 return JS_E_DATE_EXPECTED
;
625 *r
= jsval_number(date
->time
);
629 static inline HRESULT
create_utc_string(script_ctx_t
*ctx
, vdisp_t
*jsthis
, jsval_t
*r
)
631 static const DWORD week_ids
[] = { LOCALE_SABBREVDAYNAME7
, LOCALE_SABBREVDAYNAME1
,
632 LOCALE_SABBREVDAYNAME2
, LOCALE_SABBREVDAYNAME3
, LOCALE_SABBREVDAYNAME4
,
633 LOCALE_SABBREVDAYNAME5
, LOCALE_SABBREVDAYNAME6
};
634 static const DWORD month_ids
[] = { LOCALE_SABBREVMONTHNAME1
, LOCALE_SABBREVMONTHNAME2
,
635 LOCALE_SABBREVMONTHNAME3
, LOCALE_SABBREVMONTHNAME4
,
636 LOCALE_SABBREVMONTHNAME5
, LOCALE_SABBREVMONTHNAME6
,
637 LOCALE_SABBREVMONTHNAME7
, LOCALE_SABBREVMONTHNAME8
,
638 LOCALE_SABBREVMONTHNAME9
, LOCALE_SABBREVMONTHNAME10
,
639 LOCALE_SABBREVMONTHNAME11
, LOCALE_SABBREVMONTHNAME12
};
641 const WCHAR
*formatEra
= L
"";
642 WCHAR week
[64], month
[64];
649 if(!(date
= date_this(jsthis
)))
650 return JS_E_DATE_EXPECTED
;
652 if(isnan(date
->time
)) {
654 *r
= jsval_string(jsstr_nan());
659 lcid_en
= MAKELCID(MAKELANGID(LANG_ENGLISH
,SUBLANG_ENGLISH_US
),SORT_DEFAULT
);
662 GetLocaleInfoW(lcid_en
, week_ids
[(int)week_day(date
->time
)], week
, ARRAY_SIZE(week
));
665 GetLocaleInfoW(lcid_en
, month_ids
[(int)month_from_time(date
->time
)], month
, ARRAY_SIZE(month
));
667 year
= year_from_time(date
->time
);
669 formatEra
= L
" B.C.";
673 day
= date_from_time(date
->time
);
675 swprintf(buf
, ARRAY_SIZE(buf
),
676 L
"%s, %d %s %d%s %02d:%02d:%02d UTC", week
, day
, month
, year
, formatEra
,
677 (int)hour_from_time(date
->time
), (int)min_from_time(date
->time
),
678 (int)sec_from_time(date
->time
));
680 date_str
= jsstr_alloc(buf
);
682 return E_OUTOFMEMORY
;
684 *r
= jsval_string(date_str
);
689 /* ECMA-262 3rd Edition 15.9.5.42 */
690 static HRESULT
Date_toUTCString(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
694 return create_utc_string(ctx
, jsthis
, r
);
697 static HRESULT
Date_toGMTString(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
701 return create_utc_string(ctx
, jsthis
, r
);
704 /* ECMA-262 3rd Edition 15.9.5.3 */
705 static HRESULT
dateobj_to_date_string(DateInstance
*date
, jsval_t
*r
)
707 static const DWORD week_ids
[] = { LOCALE_SABBREVDAYNAME7
, LOCALE_SABBREVDAYNAME1
,
708 LOCALE_SABBREVDAYNAME2
, LOCALE_SABBREVDAYNAME3
, LOCALE_SABBREVDAYNAME4
,
709 LOCALE_SABBREVDAYNAME5
, LOCALE_SABBREVDAYNAME6
};
710 static const DWORD month_ids
[] = { LOCALE_SABBREVMONTHNAME1
, LOCALE_SABBREVMONTHNAME2
,
711 LOCALE_SABBREVMONTHNAME3
, LOCALE_SABBREVMONTHNAME4
,
712 LOCALE_SABBREVMONTHNAME5
, LOCALE_SABBREVMONTHNAME6
,
713 LOCALE_SABBREVMONTHNAME7
, LOCALE_SABBREVMONTHNAME8
,
714 LOCALE_SABBREVMONTHNAME9
, LOCALE_SABBREVMONTHNAME10
,
715 LOCALE_SABBREVMONTHNAME11
, LOCALE_SABBREVMONTHNAME12
};
717 const WCHAR
*formatEra
= L
"";
718 WCHAR week
[64], month
[64];
725 if(isnan(date
->time
)) {
727 *r
= jsval_string(jsstr_nan());
731 time
= local_time(date
->time
, date
);
734 lcid_en
= MAKELCID(MAKELANGID(LANG_ENGLISH
,SUBLANG_ENGLISH_US
),SORT_DEFAULT
);
737 GetLocaleInfoW(lcid_en
, week_ids
[(int)week_day(time
)], week
, ARRAY_SIZE(week
));
740 GetLocaleInfoW(lcid_en
, month_ids
[(int)month_from_time(time
)], month
, ARRAY_SIZE(month
));
742 year
= year_from_time(time
);
744 formatEra
= L
" B.C.";
748 day
= date_from_time(time
);
750 swprintf(buf
, ARRAY_SIZE(buf
), L
"%s %s %d %d%s", week
, month
, day
, year
, formatEra
);
752 date_str
= jsstr_alloc(buf
);
754 return E_OUTOFMEMORY
;
756 *r
= jsval_string(date_str
);
761 static HRESULT
Date_toDateString(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
766 if(!(date
= date_this(jsthis
)))
767 return JS_E_DATE_EXPECTED
;
769 return dateobj_to_date_string(date
, r
);
772 /* ECMA-262 3rd Edition 15.9.5.4 */
773 static HRESULT
Date_toTimeString(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
785 if(!(date
= date_this(jsthis
)))
786 return JS_E_DATE_EXPECTED
;
788 if(isnan(date
->time
)) {
790 *r
= jsval_string(jsstr_nan());
794 time
= local_time(date
->time
, date
);
797 offset
= date
->bias
+
798 daylight_saving_ta(time
, date
);
807 swprintf(buf
, ARRAY_SIZE(buf
), L
"%02d:%02d:%02d UTC%c%02d%02d", (int)hour_from_time(time
),
808 (int)min_from_time(time
), (int)sec_from_time(time
),
809 sign
, offset
/60, offset
%60);
811 swprintf(buf
, ARRAY_SIZE(buf
), L
"%02d:%02d:%02d UTC", (int)hour_from_time(time
),
812 (int)min_from_time(time
), (int)sec_from_time(time
));
814 date_str
= jsstr_alloc(buf
);
816 return E_OUTOFMEMORY
;
818 *r
= jsval_string(date_str
);
823 /* ECMA-262 3rd Edition 15.9.5.6 */
824 static HRESULT
Date_toLocaleDateString(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
834 if(!(date
= date_this(jsthis
)))
835 return JS_E_DATE_EXPECTED
;
837 if(isnan(date
->time
)) {
839 *r
= jsval_string(jsstr_nan());
843 st
= create_systemtime(local_time(date
->time
, date
));
845 if(st
.wYear
<1601 || st
.wYear
>9999)
846 return dateobj_to_date_string(date
, r
);
851 len
= GetDateFormatW(ctx
->lcid
, DATE_LONGDATE
, &st
, NULL
, NULL
, 0);
852 date_str
= jsstr_alloc_buf(len
-1, &ptr
);
854 return E_OUTOFMEMORY
;
855 GetDateFormatW(ctx
->lcid
, DATE_LONGDATE
, &st
, NULL
, ptr
, len
);
857 *r
= jsval_string(date_str
);
862 /* ECMA-262 3rd Edition 15.9.5.7 */
863 static HRESULT
Date_toLocaleTimeString(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
873 if(!(date
= date_this(jsthis
)))
874 return JS_E_DATE_EXPECTED
;
876 if(isnan(date
->time
)) {
878 *r
= jsval_string(jsstr_nan());
882 st
= create_systemtime(local_time(date
->time
, date
));
884 if(st
.wYear
<1601 || st
.wYear
>9999)
885 return Date_toTimeString(ctx
, jsthis
, flags
, argc
, argv
, r
);
890 len
= GetTimeFormatW(ctx
->lcid
, 0, &st
, NULL
, NULL
, 0);
891 date_str
= jsstr_alloc_buf(len
-1, &ptr
);
893 return E_OUTOFMEMORY
;
894 GetTimeFormatW(ctx
->lcid
, 0, &st
, NULL
, ptr
, len
);
896 *r
= jsval_string(date_str
);
901 /* ECMA-262 3rd Edition 15.9.5.9 */
902 static HRESULT
Date_getTime(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
909 if(!(date
= date_this(jsthis
)))
910 return JS_E_DATE_EXPECTED
;
913 *r
= jsval_number(date
->time
);
917 /* ECMA-262 3rd Edition 15.9.5.10 */
918 static HRESULT
Date_getFullYear(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
925 if(!(date
= date_this(jsthis
)))
926 return JS_E_DATE_EXPECTED
;
929 DOUBLE time
= local_time(date
->time
, date
);
931 *r
= jsval_number(year_from_time(time
));
936 /* ECMA-262 3rd Edition 15.9.5.11 */
937 static HRESULT
Date_getUTCFullYear(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
944 if(!(date
= date_this(jsthis
)))
945 return JS_E_DATE_EXPECTED
;
948 *r
= jsval_number(year_from_time(date
->time
));
952 /* ECMA-262 3rd Edition 15.9.5.12 */
953 static HRESULT
Date_getMonth(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
959 if(!(date
= date_this(jsthis
)))
960 return JS_E_DATE_EXPECTED
;
963 *r
= jsval_number(month_from_time(local_time(date
->time
, date
)));
967 /* ECMA-262 3rd Edition 15.9.5.13 */
968 static HRESULT
Date_getUTCMonth(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
975 if(!(date
= date_this(jsthis
)))
976 return JS_E_DATE_EXPECTED
;
979 *r
= jsval_number(month_from_time(date
->time
));
983 /* ECMA-262 3rd Edition 15.9.5.14 */
984 static HRESULT
Date_getDate(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
990 if(!(date
= date_this(jsthis
)))
991 return JS_E_DATE_EXPECTED
;
994 *r
= jsval_number(date_from_time(local_time(date
->time
, date
)));
998 /* ECMA-262 3rd Edition 15.9.5.15 */
999 static HRESULT
Date_getUTCDate(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1006 if(!(date
= date_this(jsthis
)))
1007 return JS_E_DATE_EXPECTED
;
1010 *r
= jsval_number(date_from_time(date
->time
));
1014 /* ECMA-262 3rd Edition 15.9.5.16 */
1015 static HRESULT
Date_getDay(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1022 if(!(date
= date_this(jsthis
)))
1023 return JS_E_DATE_EXPECTED
;
1026 *r
= jsval_number(week_day(local_time(date
->time
, date
)));
1030 /* ECMA-262 3rd Edition 15.9.5.17 */
1031 static HRESULT
Date_getUTCDay(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1038 if(!(date
= date_this(jsthis
)))
1039 return JS_E_DATE_EXPECTED
;
1042 *r
= jsval_number(week_day(date
->time
));
1046 /* ECMA-262 3rd Edition 15.9.5.18 */
1047 static HRESULT
Date_getHours(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1054 if(!(date
= date_this(jsthis
)))
1055 return JS_E_DATE_EXPECTED
;
1058 *r
= jsval_number(hour_from_time(local_time(date
->time
, date
)));
1062 /* ECMA-262 3rd Edition 15.9.5.19 */
1063 static HRESULT
Date_getUTCHours(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1070 if(!(date
= date_this(jsthis
)))
1071 return JS_E_DATE_EXPECTED
;
1074 *r
= jsval_number(hour_from_time(date
->time
));
1078 /* ECMA-262 3rd Edition 15.9.5.20 */
1079 static HRESULT
Date_getMinutes(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1086 if(!(date
= date_this(jsthis
)))
1087 return JS_E_DATE_EXPECTED
;
1090 *r
= jsval_number(min_from_time(local_time(date
->time
, date
)));
1094 /* ECMA-262 3rd Edition 15.9.5.21 */
1095 static HRESULT
Date_getUTCMinutes(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1102 if(!(date
= date_this(jsthis
)))
1103 return JS_E_DATE_EXPECTED
;
1106 *r
= jsval_number(min_from_time(date
->time
));
1110 /* ECMA-262 3rd Edition 15.9.5.22 */
1111 static HRESULT
Date_getSeconds(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
1117 if(!(date
= date_this(jsthis
)))
1118 return JS_E_DATE_EXPECTED
;
1121 *r
= jsval_number(sec_from_time(local_time(date
->time
, date
)));
1125 /* ECMA-262 3rd Edition 15.9.5.23 */
1126 static HRESULT
Date_getUTCSeconds(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1133 if(!(date
= date_this(jsthis
)))
1134 return JS_E_DATE_EXPECTED
;
1137 *r
= jsval_number(sec_from_time(date
->time
));
1141 /* ECMA-262 3rd Edition 15.9.5.24 */
1142 static HRESULT
Date_getMilliseconds(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1149 if(!(date
= date_this(jsthis
)))
1150 return JS_E_DATE_EXPECTED
;
1153 *r
= jsval_number(ms_from_time(local_time(date
->time
, date
)));
1157 /* ECMA-262 3rd Edition 15.9.5.25 */
1158 static HRESULT
Date_getUTCMilliseconds(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1165 if(!(date
= date_this(jsthis
)))
1166 return JS_E_DATE_EXPECTED
;
1169 *r
= jsval_number(ms_from_time(date
->time
));
1173 /* ECMA-262 3rd Edition 15.9.5.26 */
1174 static HRESULT
Date_getTimezoneOffset(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1181 if(!(date
= date_this(jsthis
)))
1182 return JS_E_DATE_EXPECTED
;
1185 *r
= jsval_number(floor((date
->time
-local_time(date
->time
, date
))/MS_PER_MINUTE
));
1189 /* ECMA-262 3rd Edition 15.9.5.27 */
1190 static HRESULT
Date_setTime(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1199 if(!(date
= date_this(jsthis
)))
1200 return JS_E_DATE_EXPECTED
;
1203 return JS_E_MISSING_ARG
;
1205 hres
= to_number(ctx
, argv
[0], &n
);
1209 date
->time
= time_clip(n
);
1212 *r
= jsval_number(date
->time
);
1216 /* ECMA-262 3rd Edition 15.9.5.28 */
1217 static HRESULT
Date_setMilliseconds(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1226 if(!(date
= date_this(jsthis
)))
1227 return JS_E_DATE_EXPECTED
;
1230 return JS_E_MISSING_ARG
;
1232 hres
= to_number(ctx
, argv
[0], &n
);
1236 t
= local_time(date
->time
, date
);
1237 t
= make_date(day(t
), make_time(hour_from_time(t
), min_from_time(t
),
1238 sec_from_time(t
), n
));
1239 date
->time
= time_clip(utc(t
, date
));
1242 *r
= jsval_number(date
->time
);
1246 /* ECMA-262 3rd Edition 15.9.5.29 */
1247 static HRESULT
Date_setUTCMilliseconds(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1256 if(!(date
= date_this(jsthis
)))
1257 return JS_E_DATE_EXPECTED
;
1260 return JS_E_MISSING_ARG
;
1262 hres
= to_number(ctx
, argv
[0], &n
);
1267 t
= make_date(day(t
), make_time(hour_from_time(t
), min_from_time(t
),
1268 sec_from_time(t
), n
));
1269 date
->time
= time_clip(t
);
1272 *r
= jsval_number(date
->time
);
1276 /* ECMA-262 3rd Edition 15.9.5.30 */
1277 static HRESULT
Date_setSeconds(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1286 if(!(date
= date_this(jsthis
)))
1287 return JS_E_DATE_EXPECTED
;
1290 return JS_E_MISSING_ARG
;
1292 t
= local_time(date
->time
, date
);
1294 hres
= to_number(ctx
, argv
[0], &sec
);
1299 hres
= to_number(ctx
, argv
[1], &ms
);
1303 ms
= ms_from_time(t
);
1306 t
= make_date(day(t
), make_time(hour_from_time(t
),
1307 min_from_time(t
), sec
, ms
));
1308 date
->time
= time_clip(utc(t
, date
));
1311 *r
= jsval_number(date
->time
);
1315 /* ECMA-262 3rd Edition 15.9.5.31 */
1316 static HRESULT
Date_setUTCSeconds(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1325 if(!(date
= date_this(jsthis
)))
1326 return JS_E_DATE_EXPECTED
;
1329 return JS_E_MISSING_ARG
;
1333 hres
= to_number(ctx
, argv
[0], &sec
);
1338 hres
= to_number(ctx
, argv
[1], &ms
);
1342 ms
= ms_from_time(t
);
1345 t
= make_date(day(t
), make_time(hour_from_time(t
),
1346 min_from_time(t
), sec
, ms
));
1347 date
->time
= time_clip(t
);
1350 *r
= jsval_number(date
->time
);
1354 /* ECMA-262 3rd Edition 15.9.5.33 */
1355 static HRESULT
Date_setMinutes(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1359 double t
, min
, sec
, ms
;
1364 if(!(date
= date_this(jsthis
)))
1365 return JS_E_DATE_EXPECTED
;
1368 return JS_E_MISSING_ARG
;
1370 t
= local_time(date
->time
, date
);
1372 hres
= to_number(ctx
, argv
[0], &min
);
1377 hres
= to_number(ctx
, argv
[1], &sec
);
1381 sec
= sec_from_time(t
);
1385 hres
= to_number(ctx
, argv
[2], &ms
);
1389 ms
= ms_from_time(t
);
1392 t
= make_date(day(t
), make_time(hour_from_time(t
),
1394 date
->time
= time_clip(utc(t
, date
));
1397 *r
= jsval_number(date
->time
);
1401 /* ECMA-262 3rd Edition 15.9.5.34 */
1402 static HRESULT
Date_setUTCMinutes(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1406 double t
, min
, sec
, ms
;
1411 if(!(date
= date_this(jsthis
)))
1412 return JS_E_DATE_EXPECTED
;
1415 return JS_E_MISSING_ARG
;
1419 hres
= to_number(ctx
, argv
[0], &min
);
1424 hres
= to_number(ctx
, argv
[1], &sec
);
1428 sec
= sec_from_time(t
);
1432 hres
= to_number(ctx
, argv
[2], &ms
);
1436 ms
= ms_from_time(t
);
1439 t
= make_date(day(t
), make_time(hour_from_time(t
),
1441 date
->time
= time_clip(t
);
1444 *r
= jsval_number(date
->time
);
1448 /* ECMA-262 3rd Edition 15.9.5.35 */
1449 static HRESULT
Date_setHours(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1453 double t
, hour
, min
, sec
, ms
;
1458 if(!(date
= date_this(jsthis
)))
1459 return JS_E_DATE_EXPECTED
;
1462 return JS_E_MISSING_ARG
;
1464 t
= local_time(date
->time
, date
);
1466 hres
= to_number(ctx
, argv
[0], &hour
);
1471 hres
= to_number(ctx
, argv
[1], &min
);
1475 min
= min_from_time(t
);
1479 hres
= to_number(ctx
, argv
[2], &sec
);
1483 sec
= sec_from_time(t
);
1487 hres
= to_number(ctx
, argv
[3], &ms
);
1491 ms
= ms_from_time(t
);
1494 t
= make_date(day(t
), make_time(hour
, min
, sec
, ms
));
1495 date
->time
= time_clip(utc(t
, date
));
1498 *r
= jsval_number(date
->time
);
1502 /* ECMA-262 3rd Edition 15.9.5.36 */
1503 static HRESULT
Date_setUTCHours(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1507 double t
, hour
, min
, sec
, ms
;
1512 if(!(date
= date_this(jsthis
)))
1513 return JS_E_DATE_EXPECTED
;
1516 return JS_E_MISSING_ARG
;
1520 hres
= to_number(ctx
, argv
[0], &hour
);
1525 hres
= to_number(ctx
, argv
[1], &min
);
1529 min
= min_from_time(t
);
1533 hres
= to_number(ctx
, argv
[2], &sec
);
1537 sec
= sec_from_time(t
);
1541 hres
= to_number(ctx
, argv
[3], &ms
);
1545 ms
= ms_from_time(t
);
1548 t
= make_date(day(t
), make_time(hour
, min
, sec
, ms
));
1549 date
->time
= time_clip(t
);
1552 *r
= jsval_number(date
->time
);
1556 /* ECMA-262 3rd Edition 15.9.5.36 */
1557 static HRESULT
Date_setDate(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1566 if(!(date
= date_this(jsthis
)))
1567 return JS_E_DATE_EXPECTED
;
1570 return JS_E_MISSING_ARG
;
1572 hres
= to_number(ctx
, argv
[0], &n
);
1576 t
= local_time(date
->time
, date
);
1577 t
= make_date(make_day(year_from_time(t
), month_from_time(t
), n
), time_within_day(t
));
1578 date
->time
= time_clip(utc(t
, date
));
1581 *r
= jsval_number(date
->time
);
1585 /* ECMA-262 3rd Edition 15.9.5.37 */
1586 static HRESULT
Date_setUTCDate(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1595 if(!(date
= date_this(jsthis
)))
1596 return JS_E_DATE_EXPECTED
;
1599 return JS_E_MISSING_ARG
;
1601 hres
= to_number(ctx
, argv
[0], &n
);
1606 t
= make_date(make_day(year_from_time(t
), month_from_time(t
), n
), time_within_day(t
));
1607 date
->time
= time_clip(t
);
1610 *r
= jsval_number(date
->time
);
1614 /* ECMA-262 3rd Edition 15.9.5.38 */
1615 static HRESULT
Date_setMonth(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1619 DOUBLE t
, month
, ddate
;
1624 if(!(date
= date_this(jsthis
)))
1625 return JS_E_DATE_EXPECTED
;
1628 return JS_E_MISSING_ARG
;
1630 t
= local_time(date
->time
, date
);
1632 hres
= to_number(ctx
, argv
[0], &month
);
1637 hres
= to_number(ctx
, argv
[1], &ddate
);
1641 ddate
= date_from_time(t
);
1644 t
= make_date(make_day(year_from_time(t
), month
, ddate
),
1645 time_within_day(t
));
1646 date
->time
= time_clip(utc(t
, date
));
1649 *r
= jsval_number(date
->time
);
1653 /* ECMA-262 3rd Edition 15.9.5.39 */
1654 static HRESULT
Date_setUTCMonth(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1658 double t
, month
, ddate
;
1663 if(!(date
= date_this(jsthis
)))
1664 return JS_E_DATE_EXPECTED
;
1667 return JS_E_MISSING_ARG
;
1671 hres
= to_number(ctx
, argv
[0], &month
);
1676 hres
= to_number(ctx
, argv
[1], &ddate
);
1680 ddate
= date_from_time(t
);
1683 t
= make_date(make_day(year_from_time(t
), month
, ddate
),
1684 time_within_day(t
));
1685 date
->time
= time_clip(t
);
1688 *r
= jsval_number(date
->time
);
1692 /* ECMA-262 3rd Edition 15.9.5.40 */
1693 static HRESULT
Date_setFullYear(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1697 double t
, year
, month
, ddate
;
1702 if(!(date
= date_this(jsthis
)))
1703 return JS_E_DATE_EXPECTED
;
1706 return JS_E_MISSING_ARG
;
1708 t
= local_time(date
->time
, date
);
1710 hres
= to_number(ctx
, argv
[0], &year
);
1715 hres
= to_number(ctx
, argv
[1], &month
);
1719 month
= month_from_time(t
);
1723 hres
= to_number(ctx
, argv
[2], &ddate
);
1727 ddate
= date_from_time(t
);
1730 t
= make_date(make_day(year
, month
, ddate
), time_within_day(t
));
1731 date
->time
= time_clip(utc(t
, date
));
1734 *r
= jsval_number(date
->time
);
1738 /* ECMA-262 3rd Edition 15.9.5.41 */
1739 static HRESULT
Date_setUTCFullYear(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1743 double t
, year
, month
, ddate
;
1748 if(!(date
= date_this(jsthis
)))
1749 return JS_E_DATE_EXPECTED
;
1752 return JS_E_MISSING_ARG
;
1756 hres
= to_number(ctx
, argv
[0], &year
);
1761 hres
= to_number(ctx
, argv
[1], &month
);
1765 month
= month_from_time(t
);
1769 hres
= to_number(ctx
, argv
[2], &ddate
);
1773 ddate
= date_from_time(t
);
1776 t
= make_date(make_day(year
, month
, ddate
), time_within_day(t
));
1777 date
->time
= time_clip(t
);
1780 *r
= jsval_number(date
->time
);
1784 /* ECMA-262 3rd Edition B2.4 */
1785 static HRESULT
Date_getYear(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1793 if(!(date
= date_this(jsthis
)))
1794 return JS_E_DATE_EXPECTED
;
1796 t
= local_time(date
->time
, date
);
1799 *r
= jsval_number(NAN
);
1803 year
= year_from_time(t
);
1805 *r
= jsval_number((1900<=year
&& year
<2000)?year
-1900:year
);
1809 /* ECMA-262 3rd Edition B2.5 */
1810 static HRESULT
Date_setYear(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1819 if(!(date
= date_this(jsthis
)))
1820 return JS_E_DATE_EXPECTED
;
1823 return JS_E_MISSING_ARG
;
1825 t
= local_time(date
->time
, date
);
1827 hres
= to_number(ctx
, argv
[0], &year
);
1834 *r
= jsval_number(NAN
);
1838 year
= year
>= 0.0 ? floor(year
) : -floor(-year
);
1839 if(-1.0 < year
&& year
< 100.0)
1842 date
->time
= time_clip(utc(make_date(make_day(year
, month_from_time(t
), date_from_time(t
)), time_within_day(t
)), date
));
1845 *r
= jsval_number(date
->time
);
1849 static HRESULT
Date_get_value(script_ctx_t
*ctx
, jsdisp_t
*jsthis
, jsval_t
*r
)
1853 return dateobj_to_string(date_from_jsdisp(jsthis
), r
);
1856 static const builtin_prop_t Date_props
[] = {
1857 {L
"getDate", Date_getDate
, PROPF_METHOD
},
1858 {L
"getDay", Date_getDay
, PROPF_METHOD
},
1859 {L
"getFullYear", Date_getFullYear
, PROPF_METHOD
},
1860 {L
"getHours", Date_getHours
, PROPF_METHOD
},
1861 {L
"getMilliseconds", Date_getMilliseconds
, PROPF_METHOD
},
1862 {L
"getMinutes", Date_getMinutes
, PROPF_METHOD
},
1863 {L
"getMonth", Date_getMonth
, PROPF_METHOD
},
1864 {L
"getSeconds", Date_getSeconds
, PROPF_METHOD
},
1865 {L
"getTime", Date_getTime
, PROPF_METHOD
},
1866 {L
"getTimezoneOffset", Date_getTimezoneOffset
, PROPF_METHOD
},
1867 {L
"getUTCDate", Date_getUTCDate
, PROPF_METHOD
},
1868 {L
"getUTCDay", Date_getUTCDay
, PROPF_METHOD
},
1869 {L
"getUTCFullYear", Date_getUTCFullYear
, PROPF_METHOD
},
1870 {L
"getUTCHours", Date_getUTCHours
, PROPF_METHOD
},
1871 {L
"getUTCMilliseconds", Date_getUTCMilliseconds
, PROPF_METHOD
},
1872 {L
"getUTCMinutes", Date_getUTCMinutes
, PROPF_METHOD
},
1873 {L
"getUTCMonth", Date_getUTCMonth
, PROPF_METHOD
},
1874 {L
"getUTCSeconds", Date_getUTCSeconds
, PROPF_METHOD
},
1875 {L
"getYear", Date_getYear
, PROPF_METHOD
},
1876 {L
"setDate", Date_setDate
, PROPF_METHOD
|1},
1877 {L
"setFullYear", Date_setFullYear
, PROPF_METHOD
|3},
1878 {L
"setHours", Date_setHours
, PROPF_METHOD
|4},
1879 {L
"setMilliseconds", Date_setMilliseconds
, PROPF_METHOD
|1},
1880 {L
"setMinutes", Date_setMinutes
, PROPF_METHOD
|3},
1881 {L
"setMonth", Date_setMonth
, PROPF_METHOD
|2},
1882 {L
"setSeconds", Date_setSeconds
, PROPF_METHOD
|2},
1883 {L
"setTime", Date_setTime
, PROPF_METHOD
|1},
1884 {L
"setUTCDate", Date_setUTCDate
, PROPF_METHOD
|1},
1885 {L
"setUTCFullYear", Date_setUTCFullYear
, PROPF_METHOD
|3},
1886 {L
"setUTCHours", Date_setUTCHours
, PROPF_METHOD
|4},
1887 {L
"setUTCMilliseconds", Date_setUTCMilliseconds
, PROPF_METHOD
|1},
1888 {L
"setUTCMinutes", Date_setUTCMinutes
, PROPF_METHOD
|3},
1889 {L
"setUTCMonth", Date_setUTCMonth
, PROPF_METHOD
|2},
1890 {L
"setUTCSeconds", Date_setUTCSeconds
, PROPF_METHOD
|2},
1891 {L
"setYear", Date_setYear
, PROPF_METHOD
|1},
1892 {L
"toDateString", Date_toDateString
, PROPF_METHOD
},
1893 {L
"toGMTString", Date_toGMTString
, PROPF_METHOD
},
1894 {L
"toISOString", Date_toISOString
, PROPF_METHOD
|PROPF_ES5
},
1895 {L
"toLocaleDateString", Date_toLocaleDateString
, PROPF_METHOD
},
1896 {L
"toLocaleString", Date_toLocaleString
, PROPF_METHOD
},
1897 {L
"toLocaleTimeString", Date_toLocaleTimeString
, PROPF_METHOD
},
1898 {L
"toString", Date_toString
, PROPF_METHOD
},
1899 {L
"toTimeString", Date_toTimeString
, PROPF_METHOD
},
1900 {L
"toUTCString", Date_toUTCString
, PROPF_METHOD
},
1901 {L
"valueOf", Date_valueOf
, PROPF_METHOD
},
1904 static const builtin_info_t Date_info
= {
1906 {NULL
, NULL
,0, Date_get_value
},
1907 ARRAY_SIZE(Date_props
),
1913 static const builtin_info_t DateInst_info
= {
1915 {NULL
, NULL
,0, Date_get_value
},
1921 static HRESULT
create_date(script_ctx_t
*ctx
, jsdisp_t
*object_prototype
, DOUBLE time
, DateInstance
**ret
)
1925 TIME_ZONE_INFORMATION tzi
;
1927 GetTimeZoneInformation(&tzi
);
1929 date
= heap_alloc_zero(sizeof(DateInstance
));
1931 return E_OUTOFMEMORY
;
1933 if(object_prototype
)
1934 hres
= init_dispex(&date
->dispex
, ctx
, &Date_info
, object_prototype
);
1936 hres
= init_dispex_from_constr(&date
->dispex
, ctx
, &DateInst_info
, ctx
->date_constr
);
1943 date
->bias
= tzi
.Bias
;
1944 date
->standardDate
= tzi
.StandardDate
;
1945 date
->standardBias
= tzi
.StandardBias
;
1946 date
->daylightDate
= tzi
.DaylightDate
;
1947 date
->daylightBias
= tzi
.DaylightBias
;
1953 static inline HRESULT
date_parse(jsstr_t
*input_str
, double *ret
) {
1954 static const DWORD string_ids
[] = { LOCALE_SMONTHNAME12
, LOCALE_SMONTHNAME11
,
1955 LOCALE_SMONTHNAME10
, LOCALE_SMONTHNAME9
, LOCALE_SMONTHNAME8
,
1956 LOCALE_SMONTHNAME7
, LOCALE_SMONTHNAME6
, LOCALE_SMONTHNAME5
,
1957 LOCALE_SMONTHNAME4
, LOCALE_SMONTHNAME3
, LOCALE_SMONTHNAME2
,
1958 LOCALE_SMONTHNAME1
, LOCALE_SDAYNAME7
, LOCALE_SDAYNAME1
,
1959 LOCALE_SDAYNAME2
, LOCALE_SDAYNAME3
, LOCALE_SDAYNAME4
,
1960 LOCALE_SDAYNAME5
, LOCALE_SDAYNAME6
};
1961 WCHAR
*strings
[ARRAY_SIZE(string_ids
)];
1963 int input_len
, parse_len
= 0, nest_level
= 0, i
, size
;
1964 int year
= 0, month
= 0, day
= 0, hour
= 0, min
= 0, sec
= 0;
1965 int ms
= 0, offset
= 0, hour_adjust
= 0;
1966 BOOL set_year
= FALSE
, set_month
= FALSE
, set_day
= FALSE
, set_hour
= FALSE
;
1967 BOOL set_offset
= FALSE
, set_era
= FALSE
, ad
= TRUE
, set_am
= FALSE
, am
= TRUE
;
1968 BOOL set_hour_adjust
= TRUE
;
1969 TIME_ZONE_INFORMATION tzi
;
1974 input_len
= jsstr_length(input_str
);
1975 input
= jsstr_flatten(input_str
);
1977 return E_OUTOFMEMORY
;
1979 for(i
=0; i
<input_len
; i
++) {
1980 if(input
[i
] == '(') nest_level
++;
1981 else if(input
[i
] == ')') {
1988 else if(!nest_level
) parse_len
++;
1991 parse
= heap_alloc((parse_len
+1)*sizeof(WCHAR
));
1993 return E_OUTOFMEMORY
;
1996 for(i
=0; i
<input_len
; i
++) {
1997 if(input
[i
] == '(') nest_level
++;
1998 else if(input
[i
] == ')') nest_level
--;
1999 else if(!nest_level
) parse
[parse_len
++] = towupper(input
[i
]);
2001 parse
[parse_len
] = 0;
2003 GetTimeZoneInformation(&tzi
);
2005 di
.standardDate
= tzi
.StandardDate
;
2006 di
.standardBias
= tzi
.StandardBias
;
2007 di
.daylightDate
= tzi
.DaylightDate
;
2008 di
.daylightBias
= tzi
.DaylightBias
;
2010 /* FIXME: Cache strings */
2011 lcid_en
= MAKELCID(MAKELANGID(LANG_ENGLISH
,SUBLANG_ENGLISH_US
),SORT_DEFAULT
);
2012 for(i
=0; i
<ARRAY_SIZE(string_ids
); i
++) {
2013 size
= GetLocaleInfoW(lcid_en
, string_ids
[i
], NULL
, 0);
2014 strings
[i
] = heap_alloc((size
+1)*sizeof(WCHAR
));
2018 heap_free(strings
[i
]);
2020 return E_OUTOFMEMORY
;
2022 GetLocaleInfoW(lcid_en
, string_ids
[i
], strings
[i
], size
);
2025 for(i
=0; i
<parse_len
;) {
2026 while(iswspace(parse
[i
])) i
++;
2027 if(parse
[i
] == ',') {
2028 while(parse
[i
] == ',') i
++;
2032 if(parse
[i
]>='0' && parse
[i
]<='9') {
2033 int tmp
= wcstol(&parse
[i
], NULL
, 10);
2034 while(parse
[i
]>='0' && parse
[i
]<='9') i
++;
2035 while(iswspace(parse
[i
])) i
++;
2037 if(parse
[i
] == ':') {
2044 while(parse
[i
] == ':') i
++;
2045 while(iswspace(parse
[i
])) i
++;
2046 if(parse
[i
]>='0' && parse
[i
]<='9') {
2047 min
= wcstol(&parse
[i
], NULL
, 10);
2048 while(parse
[i
]>='0' && parse
[i
]<='9') i
++;
2051 while(iswspace(parse
[i
])) i
++;
2052 while(parse
[i
] == ':') i
++;
2053 while(iswspace(parse
[i
])) i
++;
2054 if(parse
[i
]>='0' && parse
[i
]<='9') {
2055 sec
= wcstol(&parse
[i
], NULL
, 10);
2056 while(parse
[i
]>='0' && parse
[i
]<='9') i
++;
2059 else if(parse
[i
]=='-' || parse
[i
]=='/') {
2060 /* Short or long date */
2061 if(set_day
|| set_month
|| set_year
) break;
2068 while(iswspace(parse
[i
])) i
++;
2069 while(parse
[i
]=='-' || parse
[i
]=='/') i
++;
2070 while(iswspace(parse
[i
])) i
++;
2071 if(parse
[i
]<'0' || parse
[i
]>'9') break;
2072 day
= wcstol(&parse
[i
], NULL
, 10);
2073 while(parse
[i
]>='0' && parse
[i
]<='9') i
++;
2075 while(parse
[i
]=='-' || parse
[i
]=='/') i
++;
2076 while(iswspace(parse
[i
])) i
++;
2077 if(parse
[i
]<'0' || parse
[i
]>'9') break;
2078 year
= wcstol(&parse
[i
], NULL
, 10);
2079 while(parse
[i
]>='0' && parse
[i
]<='9') i
++;
2088 else if(tmp
<0) break;
2102 else if(parse
[i
]=='+' || parse
[i
]=='-') {
2103 /* Timezone offset */
2104 BOOL positive
= TRUE
;
2106 if(set_offset
&& set_hour_adjust
) break;
2108 set_hour_adjust
= FALSE
;
2110 if(parse
[i
] == '-') positive
= FALSE
;
2113 while(iswspace(parse
[i
])) i
++;
2114 if(parse
[i
]<'0' || parse
[i
]>'9') break;
2115 offset
= wcstol(&parse
[i
], NULL
, 10);
2116 while(parse
[i
]>='0' && parse
[i
]<='9') i
++;
2118 if(offset
<24) offset
*= 60;
2119 else offset
= (offset
/100)*60 + offset
%100;
2121 if(positive
) offset
= -offset
;
2125 if(parse
[i
]<'A' || parse
[i
]>'Z') break;
2126 else if(parse
[i
]=='B' && (parse
[i
+1]=='C' ||
2127 (parse
[i
+1]=='.' && parse
[i
+2]=='C'))) {
2134 if(parse
[i
] == '.') i
++;
2136 if(parse
[i
] == '.') i
++;
2138 else if(parse
[i
]=='A' && (parse
[i
+1]=='D' ||
2139 (parse
[i
+1]=='.' && parse
[i
+2]=='D'))) {
2145 if(parse
[i
] == '.') i
++;
2147 if(parse
[i
] == '.') i
++;
2149 else if(parse
[i
+1]<'A' || parse
[i
+1]>'Z') {
2151 if(set_offset
) break;
2154 if(parse
[i
] <= 'I') hour_adjust
= parse
[i
]-'A'+2;
2155 else if(parse
[i
] == 'J') break;
2156 else if(parse
[i
] <= 'M') hour_adjust
= parse
[i
]-'K'+11;
2157 else if(parse
[i
] <= 'Y') hour_adjust
= parse
[i
]-'N';
2158 else hour_adjust
= 1;
2161 if(parse
[i
] == '.') i
++;
2163 else if(parse
[i
]=='A' && parse
[i
+1]=='M') {
2170 else if(parse
[i
]=='P' && parse
[i
+1]=='M') {
2177 else if((parse
[i
]=='U' && parse
[i
+1]=='T' && parse
[i
+2]=='C')
2178 || (parse
[i
]=='G' && parse
[i
+1]=='M' && parse
[i
+2]=='T')) {
2180 if(set_offset
) break;
2182 set_hour_adjust
= FALSE
;
2187 /* Month or garbage */
2190 for(size
=i
; parse
[size
]>='A' && parse
[size
]<='Z'; size
++);
2193 for(j
=0; j
<ARRAY_SIZE(string_ids
); j
++)
2194 if(!wcsnicmp(&parse
[i
], strings
[j
], size
)) break;
2197 if(set_month
) break;
2201 else if(j
== ARRAY_SIZE(string_ids
)) break;
2208 if(i
== parse_len
&& set_year
&& set_month
&& set_day
&& (!set_am
|| hour
<13)) {
2210 if(hour
== 12) hour
= 0;
2214 if(!ad
) year
= -year
+1;
2215 else if(year
<100) year
+= 1900;
2217 *ret
= time_clip(make_date(make_day(year
, month
, day
),
2218 make_time(hour
+hour_adjust
, min
, sec
, ms
)) + offset
*MS_PER_MINUTE
);
2221 *ret
= utc(*ret
, &di
);
2226 for(i
=0; i
<ARRAY_SIZE(string_ids
); i
++)
2227 heap_free(strings
[i
]);
2233 static HRESULT
DateConstr_parse(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
2244 *r
= jsval_number(NAN
);
2248 hres
= to_string(ctx
, argv
[0], &parse_str
);
2252 hres
= date_parse(parse_str
, &n
);
2253 jsstr_release(parse_str
);
2257 *r
= jsval_number(n
);
2261 static HRESULT
date_utc(script_ctx_t
*ctx
, unsigned argc
, jsval_t
*argv
, double *ret
)
2263 double year
, month
, vdate
, hours
, minutes
, seconds
, ms
;
2269 hres
= to_number(ctx
, argv
[0], &year
);
2272 if(0 <= year
&& year
<= 99)
2279 hres
= to_number(ctx
, argv
[1], &month
);
2287 hres
= to_number(ctx
, argv
[2], &vdate
);
2295 hres
= to_number(ctx
, argv
[3], &hours
);
2303 hres
= to_number(ctx
, argv
[4], &minutes
);
2311 hres
= to_number(ctx
, argv
[5], &seconds
);
2319 hres
= to_number(ctx
, argv
[6], &ms
);
2326 *ret
= time_clip(make_date(make_day(year
, month
, vdate
),
2327 make_time(hours
, minutes
,seconds
, ms
)));
2331 static HRESULT
DateConstr_UTC(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
2339 hres
= date_utc(ctx
, argc
, argv
, &n
);
2340 if(SUCCEEDED(hres
) && r
)
2341 *r
= jsval_number(n
);
2345 /* ECMA-262 5.1 Edition 15.9.4.4 */
2346 static HRESULT
DateConstr_now(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
2350 if(r
) *r
= jsval_number(date_now());
2354 static HRESULT
DateConstr_value(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
2363 case DISPATCH_CONSTRUCT
:
2365 /* ECMA-262 3rd Edition 15.9.3.3 */
2367 hres
= create_date(ctx
, NULL
, date_now(), &date
);
2372 /* ECMA-262 3rd Edition 15.9.3.2 */
2377 hres
= to_primitive(ctx
, argv
[0], &prim
, NO_HINT
);
2382 hres
= date_parse(get_string(prim
), &n
);
2384 hres
= to_number(ctx
, prim
, &n
);
2386 jsval_release(prim
);
2390 hres
= create_date(ctx
, NULL
, time_clip(n
), &date
);
2396 /* ECMA-262 3rd Edition 15.9.3.1 */
2400 hres
= date_utc(ctx
, argc
, argv
, &ret_date
);
2404 hres
= create_date(ctx
, NULL
, ret_date
, &date
);
2408 date
->time
= utc(date
->time
, date
);
2412 *r
= jsval_obj(&date
->dispex
);
2416 FILETIME system_time
, local_time
;
2419 GetSystemTimeAsFileTime(&system_time
);
2420 FileTimeToLocalFileTime(&system_time
, &local_time
);
2421 lltime
= ((LONGLONG
)local_time
.dwHighDateTime
<<32)
2422 + local_time
.dwLowDateTime
;
2424 return date_to_string(lltime
/10000-TIME_EPOCH
, FALSE
, 0, r
);
2428 FIXME("unimplemented flags %x\n", flags
);
2435 static const builtin_prop_t DateConstr_props
[] = {
2436 {L
"UTC", DateConstr_UTC
, PROPF_METHOD
},
2437 {L
"now", DateConstr_now
, PROPF_HTML
|PROPF_METHOD
},
2438 {L
"parse", DateConstr_parse
, PROPF_METHOD
}
2441 static const builtin_info_t DateConstr_info
= {
2443 DEFAULT_FUNCTION_VALUE
,
2444 ARRAY_SIZE(DateConstr_props
),
2450 HRESULT
create_date_constr(script_ctx_t
*ctx
, jsdisp_t
*object_prototype
, jsdisp_t
**ret
)
2455 hres
= create_date(ctx
, object_prototype
, 0.0, &date
);
2459 hres
= create_builtin_constructor(ctx
, DateConstr_value
, L
"Date", &DateConstr_info
,
2460 PROPF_CONSTR
|7, &date
->dispex
, ret
);
2462 jsdisp_release(&date
->dispex
);
2466 HRESULT
variant_date_to_number(double date
, double *ret
)
2472 hres
= VarUdateFromDate(date
, 0, &udate
);
2476 if(!TzSpecificLocalTimeToSystemTime(NULL
, &udate
.st
, &st
))
2479 TRACE("%uy %um %u %ud %uh %um %u.%us\n", st
.wYear
, st
.wMonth
, st
.wDayOfWeek
, st
.wDay
, st
.wHour
, st
.wMinute
,
2480 st
.wSecond
, st
.wMilliseconds
);
2482 *ret
= make_date(make_day(st
.wYear
, st
.wMonth
- 1, st
.wDay
),
2483 make_time(st
.wHour
, st
.wMinute
, st
.wSecond
, st
.wMilliseconds
));
2487 HRESULT
variant_date_to_string(script_ctx_t
*ctx
, double date
, jsstr_t
**r
)
2489 DateInstance
*date_obj
;
2494 hres
= variant_date_to_number(date
, &time
);
2498 hres
= create_date(ctx
, NULL
, time
, &date_obj
);
2502 hres
= dateobj_to_string(date_obj
, &val
);
2503 jsdisp_release(&date_obj
->dispex
);
2507 assert(is_string(val
));
2508 *r
= get_string(val
);