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 BOOL formatAD
= TRUE
;
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
);
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
, formatAD
?L
"":L
" B.C.");
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,
488 year
, formatAD
?L
"":L
" B.C.");
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
, formatAD
?L
"":L
" B.C.");
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 BOOL formatAD
= TRUE
;
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
);
673 day
= date_from_time(date
->time
);
675 swprintf(buf
, ARRAY_SIZE(buf
),
676 formatAD
? L
"%s, %d %s %d %02d:%02d:%02d UTC" : L
"%s, %d %s %d B.C. %02d:%02d:%02d UTC",
677 week
, day
, month
, year
, (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 BOOL formatAD
= TRUE
;
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
);
748 day
= date_from_time(time
);
750 swprintf(buf
, ARRAY_SIZE(buf
), formatAD
? L
"%s %s %d %d" : L
"%s %s %d %d B.C.", week
, month
,
753 date_str
= jsstr_alloc(buf
);
755 return E_OUTOFMEMORY
;
757 *r
= jsval_string(date_str
);
762 static HRESULT
Date_toDateString(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
767 if(!(date
= date_this(jsthis
)))
768 return JS_E_DATE_EXPECTED
;
770 return dateobj_to_date_string(date
, r
);
773 /* ECMA-262 3rd Edition 15.9.5.4 */
774 static HRESULT
Date_toTimeString(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
786 if(!(date
= date_this(jsthis
)))
787 return JS_E_DATE_EXPECTED
;
789 if(isnan(date
->time
)) {
791 *r
= jsval_string(jsstr_nan());
795 time
= local_time(date
->time
, date
);
798 offset
= date
->bias
+
799 daylight_saving_ta(time
, date
);
808 swprintf(buf
, ARRAY_SIZE(buf
), L
"%02d:%02d:%02d UTC%c%02d%02d", (int)hour_from_time(time
),
809 (int)min_from_time(time
), (int)sec_from_time(time
),
810 sign
, offset
/60, offset
%60);
812 swprintf(buf
, ARRAY_SIZE(buf
), L
"%02d:%02d:%02d UTC", (int)hour_from_time(time
),
813 (int)min_from_time(time
), (int)sec_from_time(time
));
815 date_str
= jsstr_alloc(buf
);
817 return E_OUTOFMEMORY
;
819 *r
= jsval_string(date_str
);
824 /* ECMA-262 3rd Edition 15.9.5.6 */
825 static HRESULT
Date_toLocaleDateString(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
835 if(!(date
= date_this(jsthis
)))
836 return JS_E_DATE_EXPECTED
;
838 if(isnan(date
->time
)) {
840 *r
= jsval_string(jsstr_nan());
844 st
= create_systemtime(local_time(date
->time
, date
));
846 if(st
.wYear
<1601 || st
.wYear
>9999)
847 return dateobj_to_date_string(date
, r
);
852 len
= GetDateFormatW(ctx
->lcid
, DATE_LONGDATE
, &st
, NULL
, NULL
, 0);
853 date_str
= jsstr_alloc_buf(len
-1, &ptr
);
855 return E_OUTOFMEMORY
;
856 GetDateFormatW(ctx
->lcid
, DATE_LONGDATE
, &st
, NULL
, ptr
, len
);
858 *r
= jsval_string(date_str
);
863 /* ECMA-262 3rd Edition 15.9.5.7 */
864 static HRESULT
Date_toLocaleTimeString(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
874 if(!(date
= date_this(jsthis
)))
875 return JS_E_DATE_EXPECTED
;
877 if(isnan(date
->time
)) {
879 *r
= jsval_string(jsstr_nan());
883 st
= create_systemtime(local_time(date
->time
, date
));
885 if(st
.wYear
<1601 || st
.wYear
>9999)
886 return Date_toTimeString(ctx
, jsthis
, flags
, argc
, argv
, r
);
891 len
= GetTimeFormatW(ctx
->lcid
, 0, &st
, NULL
, NULL
, 0);
892 date_str
= jsstr_alloc_buf(len
-1, &ptr
);
894 return E_OUTOFMEMORY
;
895 GetTimeFormatW(ctx
->lcid
, 0, &st
, NULL
, ptr
, len
);
897 *r
= jsval_string(date_str
);
902 /* ECMA-262 3rd Edition 15.9.5.9 */
903 static HRESULT
Date_getTime(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
910 if(!(date
= date_this(jsthis
)))
911 return JS_E_DATE_EXPECTED
;
914 *r
= jsval_number(date
->time
);
918 /* ECMA-262 3rd Edition 15.9.5.10 */
919 static HRESULT
Date_getFullYear(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
926 if(!(date
= date_this(jsthis
)))
927 return JS_E_DATE_EXPECTED
;
930 DOUBLE time
= local_time(date
->time
, date
);
932 *r
= jsval_number(year_from_time(time
));
937 /* ECMA-262 3rd Edition 15.9.5.11 */
938 static HRESULT
Date_getUTCFullYear(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
945 if(!(date
= date_this(jsthis
)))
946 return JS_E_DATE_EXPECTED
;
949 *r
= jsval_number(year_from_time(date
->time
));
953 /* ECMA-262 3rd Edition 15.9.5.12 */
954 static HRESULT
Date_getMonth(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
960 if(!(date
= date_this(jsthis
)))
961 return JS_E_DATE_EXPECTED
;
964 *r
= jsval_number(month_from_time(local_time(date
->time
, date
)));
968 /* ECMA-262 3rd Edition 15.9.5.13 */
969 static HRESULT
Date_getUTCMonth(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
976 if(!(date
= date_this(jsthis
)))
977 return JS_E_DATE_EXPECTED
;
980 *r
= jsval_number(month_from_time(date
->time
));
984 /* ECMA-262 3rd Edition 15.9.5.14 */
985 static HRESULT
Date_getDate(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
991 if(!(date
= date_this(jsthis
)))
992 return JS_E_DATE_EXPECTED
;
995 *r
= jsval_number(date_from_time(local_time(date
->time
, date
)));
999 /* ECMA-262 3rd Edition 15.9.5.15 */
1000 static HRESULT
Date_getUTCDate(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1007 if(!(date
= date_this(jsthis
)))
1008 return JS_E_DATE_EXPECTED
;
1011 *r
= jsval_number(date_from_time(date
->time
));
1015 /* ECMA-262 3rd Edition 15.9.5.16 */
1016 static HRESULT
Date_getDay(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1023 if(!(date
= date_this(jsthis
)))
1024 return JS_E_DATE_EXPECTED
;
1027 *r
= jsval_number(week_day(local_time(date
->time
, date
)));
1031 /* ECMA-262 3rd Edition 15.9.5.17 */
1032 static HRESULT
Date_getUTCDay(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1039 if(!(date
= date_this(jsthis
)))
1040 return JS_E_DATE_EXPECTED
;
1043 *r
= jsval_number(week_day(date
->time
));
1047 /* ECMA-262 3rd Edition 15.9.5.18 */
1048 static HRESULT
Date_getHours(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1055 if(!(date
= date_this(jsthis
)))
1056 return JS_E_DATE_EXPECTED
;
1059 *r
= jsval_number(hour_from_time(local_time(date
->time
, date
)));
1063 /* ECMA-262 3rd Edition 15.9.5.19 */
1064 static HRESULT
Date_getUTCHours(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1071 if(!(date
= date_this(jsthis
)))
1072 return JS_E_DATE_EXPECTED
;
1075 *r
= jsval_number(hour_from_time(date
->time
));
1079 /* ECMA-262 3rd Edition 15.9.5.20 */
1080 static HRESULT
Date_getMinutes(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1087 if(!(date
= date_this(jsthis
)))
1088 return JS_E_DATE_EXPECTED
;
1091 *r
= jsval_number(min_from_time(local_time(date
->time
, date
)));
1095 /* ECMA-262 3rd Edition 15.9.5.21 */
1096 static HRESULT
Date_getUTCMinutes(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1103 if(!(date
= date_this(jsthis
)))
1104 return JS_E_DATE_EXPECTED
;
1107 *r
= jsval_number(min_from_time(date
->time
));
1111 /* ECMA-262 3rd Edition 15.9.5.22 */
1112 static HRESULT
Date_getSeconds(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
1118 if(!(date
= date_this(jsthis
)))
1119 return JS_E_DATE_EXPECTED
;
1122 *r
= jsval_number(sec_from_time(local_time(date
->time
, date
)));
1126 /* ECMA-262 3rd Edition 15.9.5.23 */
1127 static HRESULT
Date_getUTCSeconds(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1134 if(!(date
= date_this(jsthis
)))
1135 return JS_E_DATE_EXPECTED
;
1138 *r
= jsval_number(sec_from_time(date
->time
));
1142 /* ECMA-262 3rd Edition 15.9.5.24 */
1143 static HRESULT
Date_getMilliseconds(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1150 if(!(date
= date_this(jsthis
)))
1151 return JS_E_DATE_EXPECTED
;
1154 *r
= jsval_number(ms_from_time(local_time(date
->time
, date
)));
1158 /* ECMA-262 3rd Edition 15.9.5.25 */
1159 static HRESULT
Date_getUTCMilliseconds(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1166 if(!(date
= date_this(jsthis
)))
1167 return JS_E_DATE_EXPECTED
;
1170 *r
= jsval_number(ms_from_time(date
->time
));
1174 /* ECMA-262 3rd Edition 15.9.5.26 */
1175 static HRESULT
Date_getTimezoneOffset(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1182 if(!(date
= date_this(jsthis
)))
1183 return JS_E_DATE_EXPECTED
;
1186 *r
= jsval_number(floor((date
->time
-local_time(date
->time
, date
))/MS_PER_MINUTE
));
1190 /* ECMA-262 3rd Edition 15.9.5.27 */
1191 static HRESULT
Date_setTime(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1200 if(!(date
= date_this(jsthis
)))
1201 return JS_E_DATE_EXPECTED
;
1204 return JS_E_MISSING_ARG
;
1206 hres
= to_number(ctx
, argv
[0], &n
);
1210 date
->time
= time_clip(n
);
1213 *r
= jsval_number(date
->time
);
1217 /* ECMA-262 3rd Edition 15.9.5.28 */
1218 static HRESULT
Date_setMilliseconds(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1227 if(!(date
= date_this(jsthis
)))
1228 return JS_E_DATE_EXPECTED
;
1231 return JS_E_MISSING_ARG
;
1233 hres
= to_number(ctx
, argv
[0], &n
);
1237 t
= local_time(date
->time
, date
);
1238 t
= make_date(day(t
), make_time(hour_from_time(t
), min_from_time(t
),
1239 sec_from_time(t
), n
));
1240 date
->time
= time_clip(utc(t
, date
));
1243 *r
= jsval_number(date
->time
);
1247 /* ECMA-262 3rd Edition 15.9.5.29 */
1248 static HRESULT
Date_setUTCMilliseconds(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1257 if(!(date
= date_this(jsthis
)))
1258 return JS_E_DATE_EXPECTED
;
1261 return JS_E_MISSING_ARG
;
1263 hres
= to_number(ctx
, argv
[0], &n
);
1268 t
= make_date(day(t
), make_time(hour_from_time(t
), min_from_time(t
),
1269 sec_from_time(t
), n
));
1270 date
->time
= time_clip(t
);
1273 *r
= jsval_number(date
->time
);
1277 /* ECMA-262 3rd Edition 15.9.5.30 */
1278 static HRESULT
Date_setSeconds(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1287 if(!(date
= date_this(jsthis
)))
1288 return JS_E_DATE_EXPECTED
;
1291 return JS_E_MISSING_ARG
;
1293 t
= local_time(date
->time
, date
);
1295 hres
= to_number(ctx
, argv
[0], &sec
);
1300 hres
= to_number(ctx
, argv
[1], &ms
);
1304 ms
= ms_from_time(t
);
1307 t
= make_date(day(t
), make_time(hour_from_time(t
),
1308 min_from_time(t
), sec
, ms
));
1309 date
->time
= time_clip(utc(t
, date
));
1312 *r
= jsval_number(date
->time
);
1316 /* ECMA-262 3rd Edition 15.9.5.31 */
1317 static HRESULT
Date_setUTCSeconds(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1326 if(!(date
= date_this(jsthis
)))
1327 return JS_E_DATE_EXPECTED
;
1330 return JS_E_MISSING_ARG
;
1334 hres
= to_number(ctx
, argv
[0], &sec
);
1339 hres
= to_number(ctx
, argv
[1], &ms
);
1343 ms
= ms_from_time(t
);
1346 t
= make_date(day(t
), make_time(hour_from_time(t
),
1347 min_from_time(t
), sec
, ms
));
1348 date
->time
= time_clip(t
);
1351 *r
= jsval_number(date
->time
);
1355 /* ECMA-262 3rd Edition 15.9.5.33 */
1356 static HRESULT
Date_setMinutes(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1360 double t
, min
, sec
, ms
;
1365 if(!(date
= date_this(jsthis
)))
1366 return JS_E_DATE_EXPECTED
;
1369 return JS_E_MISSING_ARG
;
1371 t
= local_time(date
->time
, date
);
1373 hres
= to_number(ctx
, argv
[0], &min
);
1378 hres
= to_number(ctx
, argv
[1], &sec
);
1382 sec
= sec_from_time(t
);
1386 hres
= to_number(ctx
, argv
[2], &ms
);
1390 ms
= ms_from_time(t
);
1393 t
= make_date(day(t
), make_time(hour_from_time(t
),
1395 date
->time
= time_clip(utc(t
, date
));
1398 *r
= jsval_number(date
->time
);
1402 /* ECMA-262 3rd Edition 15.9.5.34 */
1403 static HRESULT
Date_setUTCMinutes(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1407 double t
, min
, sec
, ms
;
1412 if(!(date
= date_this(jsthis
)))
1413 return JS_E_DATE_EXPECTED
;
1416 return JS_E_MISSING_ARG
;
1420 hres
= to_number(ctx
, argv
[0], &min
);
1425 hres
= to_number(ctx
, argv
[1], &sec
);
1429 sec
= sec_from_time(t
);
1433 hres
= to_number(ctx
, argv
[2], &ms
);
1437 ms
= ms_from_time(t
);
1440 t
= make_date(day(t
), make_time(hour_from_time(t
),
1442 date
->time
= time_clip(t
);
1445 *r
= jsval_number(date
->time
);
1449 /* ECMA-262 3rd Edition 15.9.5.35 */
1450 static HRESULT
Date_setHours(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1454 double t
, hour
, min
, sec
, ms
;
1459 if(!(date
= date_this(jsthis
)))
1460 return JS_E_DATE_EXPECTED
;
1463 return JS_E_MISSING_ARG
;
1465 t
= local_time(date
->time
, date
);
1467 hres
= to_number(ctx
, argv
[0], &hour
);
1472 hres
= to_number(ctx
, argv
[1], &min
);
1476 min
= min_from_time(t
);
1480 hres
= to_number(ctx
, argv
[2], &sec
);
1484 sec
= sec_from_time(t
);
1488 hres
= to_number(ctx
, argv
[3], &ms
);
1492 ms
= ms_from_time(t
);
1495 t
= make_date(day(t
), make_time(hour
, min
, sec
, ms
));
1496 date
->time
= time_clip(utc(t
, date
));
1499 *r
= jsval_number(date
->time
);
1503 /* ECMA-262 3rd Edition 15.9.5.36 */
1504 static HRESULT
Date_setUTCHours(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1508 double t
, hour
, min
, sec
, ms
;
1513 if(!(date
= date_this(jsthis
)))
1514 return JS_E_DATE_EXPECTED
;
1517 return JS_E_MISSING_ARG
;
1521 hres
= to_number(ctx
, argv
[0], &hour
);
1526 hres
= to_number(ctx
, argv
[1], &min
);
1530 min
= min_from_time(t
);
1534 hres
= to_number(ctx
, argv
[2], &sec
);
1538 sec
= sec_from_time(t
);
1542 hres
= to_number(ctx
, argv
[3], &ms
);
1546 ms
= ms_from_time(t
);
1549 t
= make_date(day(t
), make_time(hour
, min
, sec
, ms
));
1550 date
->time
= time_clip(t
);
1553 *r
= jsval_number(date
->time
);
1557 /* ECMA-262 3rd Edition 15.9.5.36 */
1558 static HRESULT
Date_setDate(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1567 if(!(date
= date_this(jsthis
)))
1568 return JS_E_DATE_EXPECTED
;
1571 return JS_E_MISSING_ARG
;
1573 hres
= to_number(ctx
, argv
[0], &n
);
1577 t
= local_time(date
->time
, date
);
1578 t
= make_date(make_day(year_from_time(t
), month_from_time(t
), n
), time_within_day(t
));
1579 date
->time
= time_clip(utc(t
, date
));
1582 *r
= jsval_number(date
->time
);
1586 /* ECMA-262 3rd Edition 15.9.5.37 */
1587 static HRESULT
Date_setUTCDate(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1596 if(!(date
= date_this(jsthis
)))
1597 return JS_E_DATE_EXPECTED
;
1600 return JS_E_MISSING_ARG
;
1602 hres
= to_number(ctx
, argv
[0], &n
);
1607 t
= make_date(make_day(year_from_time(t
), month_from_time(t
), n
), time_within_day(t
));
1608 date
->time
= time_clip(t
);
1611 *r
= jsval_number(date
->time
);
1615 /* ECMA-262 3rd Edition 15.9.5.38 */
1616 static HRESULT
Date_setMonth(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1620 DOUBLE t
, month
, ddate
;
1625 if(!(date
= date_this(jsthis
)))
1626 return JS_E_DATE_EXPECTED
;
1629 return JS_E_MISSING_ARG
;
1631 t
= local_time(date
->time
, date
);
1633 hres
= to_number(ctx
, argv
[0], &month
);
1638 hres
= to_number(ctx
, argv
[1], &ddate
);
1642 ddate
= date_from_time(t
);
1645 t
= make_date(make_day(year_from_time(t
), month
, ddate
),
1646 time_within_day(t
));
1647 date
->time
= time_clip(utc(t
, date
));
1650 *r
= jsval_number(date
->time
);
1654 /* ECMA-262 3rd Edition 15.9.5.39 */
1655 static HRESULT
Date_setUTCMonth(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1659 double t
, month
, ddate
;
1664 if(!(date
= date_this(jsthis
)))
1665 return JS_E_DATE_EXPECTED
;
1668 return JS_E_MISSING_ARG
;
1672 hres
= to_number(ctx
, argv
[0], &month
);
1677 hres
= to_number(ctx
, argv
[1], &ddate
);
1681 ddate
= date_from_time(t
);
1684 t
= make_date(make_day(year_from_time(t
), month
, ddate
),
1685 time_within_day(t
));
1686 date
->time
= time_clip(t
);
1689 *r
= jsval_number(date
->time
);
1693 /* ECMA-262 3rd Edition 15.9.5.40 */
1694 static HRESULT
Date_setFullYear(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1698 double t
, year
, month
, ddate
;
1703 if(!(date
= date_this(jsthis
)))
1704 return JS_E_DATE_EXPECTED
;
1707 return JS_E_MISSING_ARG
;
1709 t
= local_time(date
->time
, date
);
1711 hres
= to_number(ctx
, argv
[0], &year
);
1716 hres
= to_number(ctx
, argv
[1], &month
);
1720 month
= month_from_time(t
);
1724 hres
= to_number(ctx
, argv
[2], &ddate
);
1728 ddate
= date_from_time(t
);
1731 t
= make_date(make_day(year
, month
, ddate
), time_within_day(t
));
1732 date
->time
= time_clip(utc(t
, date
));
1735 *r
= jsval_number(date
->time
);
1739 /* ECMA-262 3rd Edition 15.9.5.41 */
1740 static HRESULT
Date_setUTCFullYear(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1744 double t
, year
, month
, ddate
;
1749 if(!(date
= date_this(jsthis
)))
1750 return JS_E_DATE_EXPECTED
;
1753 return JS_E_MISSING_ARG
;
1757 hres
= to_number(ctx
, argv
[0], &year
);
1762 hres
= to_number(ctx
, argv
[1], &month
);
1766 month
= month_from_time(t
);
1770 hres
= to_number(ctx
, argv
[2], &ddate
);
1774 ddate
= date_from_time(t
);
1777 t
= make_date(make_day(year
, month
, ddate
), time_within_day(t
));
1778 date
->time
= time_clip(t
);
1781 *r
= jsval_number(date
->time
);
1785 /* ECMA-262 3rd Edition B2.4 */
1786 static HRESULT
Date_getYear(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1794 if(!(date
= date_this(jsthis
)))
1795 return JS_E_DATE_EXPECTED
;
1797 t
= local_time(date
->time
, date
);
1800 *r
= jsval_number(NAN
);
1804 year
= year_from_time(t
);
1806 *r
= jsval_number((1900<=year
&& year
<2000)?year
-1900:year
);
1810 /* ECMA-262 3rd Edition B2.5 */
1811 static HRESULT
Date_setYear(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1820 if(!(date
= date_this(jsthis
)))
1821 return JS_E_DATE_EXPECTED
;
1824 return JS_E_MISSING_ARG
;
1826 t
= local_time(date
->time
, date
);
1828 hres
= to_number(ctx
, argv
[0], &year
);
1835 *r
= jsval_number(NAN
);
1839 year
= year
>= 0.0 ? floor(year
) : -floor(-year
);
1840 if(-1.0 < year
&& year
< 100.0)
1843 date
->time
= time_clip(utc(make_date(make_day(year
, month_from_time(t
), date_from_time(t
)), time_within_day(t
)), date
));
1846 *r
= jsval_number(date
->time
);
1850 static HRESULT
Date_get_value(script_ctx_t
*ctx
, jsdisp_t
*jsthis
, jsval_t
*r
)
1854 return dateobj_to_string(date_from_jsdisp(jsthis
), r
);
1857 static const builtin_prop_t Date_props
[] = {
1858 {L
"getDate", Date_getDate
, PROPF_METHOD
},
1859 {L
"getDay", Date_getDay
, PROPF_METHOD
},
1860 {L
"getFullYear", Date_getFullYear
, PROPF_METHOD
},
1861 {L
"getHours", Date_getHours
, PROPF_METHOD
},
1862 {L
"getMilliseconds", Date_getMilliseconds
, PROPF_METHOD
},
1863 {L
"getMinutes", Date_getMinutes
, PROPF_METHOD
},
1864 {L
"getMonth", Date_getMonth
, PROPF_METHOD
},
1865 {L
"getSeconds", Date_getSeconds
, PROPF_METHOD
},
1866 {L
"getTime", Date_getTime
, PROPF_METHOD
},
1867 {L
"getTimezoneOffset", Date_getTimezoneOffset
, PROPF_METHOD
},
1868 {L
"getUTCDate", Date_getUTCDate
, PROPF_METHOD
},
1869 {L
"getUTCDay", Date_getUTCDay
, PROPF_METHOD
},
1870 {L
"getUTCFullYear", Date_getUTCFullYear
, PROPF_METHOD
},
1871 {L
"getUTCHours", Date_getUTCHours
, PROPF_METHOD
},
1872 {L
"getUTCMilliseconds", Date_getUTCMilliseconds
, PROPF_METHOD
},
1873 {L
"getUTCMinutes", Date_getUTCMinutes
, PROPF_METHOD
},
1874 {L
"getUTCMonth", Date_getUTCMonth
, PROPF_METHOD
},
1875 {L
"getUTCSeconds", Date_getUTCSeconds
, PROPF_METHOD
},
1876 {L
"getYear", Date_getYear
, PROPF_METHOD
},
1877 {L
"setDate", Date_setDate
, PROPF_METHOD
|1},
1878 {L
"setFullYear", Date_setFullYear
, PROPF_METHOD
|3},
1879 {L
"setHours", Date_setHours
, PROPF_METHOD
|4},
1880 {L
"setMilliseconds", Date_setMilliseconds
, PROPF_METHOD
|1},
1881 {L
"setMinutes", Date_setMinutes
, PROPF_METHOD
|3},
1882 {L
"setMonth", Date_setMonth
, PROPF_METHOD
|2},
1883 {L
"setSeconds", Date_setSeconds
, PROPF_METHOD
|2},
1884 {L
"setTime", Date_setTime
, PROPF_METHOD
|1},
1885 {L
"setUTCDate", Date_setUTCDate
, PROPF_METHOD
|1},
1886 {L
"setUTCFullYear", Date_setUTCFullYear
, PROPF_METHOD
|3},
1887 {L
"setUTCHours", Date_setUTCHours
, PROPF_METHOD
|4},
1888 {L
"setUTCMilliseconds", Date_setUTCMilliseconds
, PROPF_METHOD
|1},
1889 {L
"setUTCMinutes", Date_setUTCMinutes
, PROPF_METHOD
|3},
1890 {L
"setUTCMonth", Date_setUTCMonth
, PROPF_METHOD
|2},
1891 {L
"setUTCSeconds", Date_setUTCSeconds
, PROPF_METHOD
|2},
1892 {L
"setYear", Date_setYear
, PROPF_METHOD
|1},
1893 {L
"toDateString", Date_toDateString
, PROPF_METHOD
},
1894 {L
"toGMTString", Date_toGMTString
, PROPF_METHOD
},
1895 {L
"toISOString", Date_toISOString
, PROPF_METHOD
|PROPF_ES5
},
1896 {L
"toLocaleDateString", Date_toLocaleDateString
, PROPF_METHOD
},
1897 {L
"toLocaleString", Date_toLocaleString
, PROPF_METHOD
},
1898 {L
"toLocaleTimeString", Date_toLocaleTimeString
, PROPF_METHOD
},
1899 {L
"toString", Date_toString
, PROPF_METHOD
},
1900 {L
"toTimeString", Date_toTimeString
, PROPF_METHOD
},
1901 {L
"toUTCString", Date_toUTCString
, PROPF_METHOD
},
1902 {L
"valueOf", Date_valueOf
, PROPF_METHOD
},
1905 static const builtin_info_t Date_info
= {
1907 {NULL
, NULL
,0, Date_get_value
},
1908 ARRAY_SIZE(Date_props
),
1914 static const builtin_info_t DateInst_info
= {
1916 {NULL
, NULL
,0, Date_get_value
},
1922 static HRESULT
create_date(script_ctx_t
*ctx
, jsdisp_t
*object_prototype
, DOUBLE time
, jsdisp_t
**ret
)
1926 TIME_ZONE_INFORMATION tzi
;
1928 GetTimeZoneInformation(&tzi
);
1930 date
= heap_alloc_zero(sizeof(DateInstance
));
1932 return E_OUTOFMEMORY
;
1934 if(object_prototype
)
1935 hres
= init_dispex(&date
->dispex
, ctx
, &Date_info
, object_prototype
);
1937 hres
= init_dispex_from_constr(&date
->dispex
, ctx
, &DateInst_info
, ctx
->date_constr
);
1944 date
->bias
= tzi
.Bias
;
1945 date
->standardDate
= tzi
.StandardDate
;
1946 date
->standardBias
= tzi
.StandardBias
;
1947 date
->daylightDate
= tzi
.DaylightDate
;
1948 date
->daylightBias
= tzi
.DaylightBias
;
1950 *ret
= &date
->dispex
;
1954 static inline HRESULT
date_parse(jsstr_t
*input_str
, double *ret
) {
1955 static const DWORD string_ids
[] = { LOCALE_SMONTHNAME12
, LOCALE_SMONTHNAME11
,
1956 LOCALE_SMONTHNAME10
, LOCALE_SMONTHNAME9
, LOCALE_SMONTHNAME8
,
1957 LOCALE_SMONTHNAME7
, LOCALE_SMONTHNAME6
, LOCALE_SMONTHNAME5
,
1958 LOCALE_SMONTHNAME4
, LOCALE_SMONTHNAME3
, LOCALE_SMONTHNAME2
,
1959 LOCALE_SMONTHNAME1
, LOCALE_SDAYNAME7
, LOCALE_SDAYNAME1
,
1960 LOCALE_SDAYNAME2
, LOCALE_SDAYNAME3
, LOCALE_SDAYNAME4
,
1961 LOCALE_SDAYNAME5
, LOCALE_SDAYNAME6
};
1962 WCHAR
*strings
[ARRAY_SIZE(string_ids
)];
1964 int input_len
, parse_len
= 0, nest_level
= 0, i
, size
;
1965 int year
= 0, month
= 0, day
= 0, hour
= 0, min
= 0, sec
= 0;
1966 int ms
= 0, offset
= 0, hour_adjust
= 0;
1967 BOOL set_year
= FALSE
, set_month
= FALSE
, set_day
= FALSE
, set_hour
= FALSE
;
1968 BOOL set_offset
= FALSE
, set_era
= FALSE
, ad
= TRUE
, set_am
= FALSE
, am
= TRUE
;
1969 BOOL set_hour_adjust
= TRUE
;
1970 TIME_ZONE_INFORMATION tzi
;
1975 input_len
= jsstr_length(input_str
);
1976 input
= jsstr_flatten(input_str
);
1978 return E_OUTOFMEMORY
;
1980 for(i
=0; i
<input_len
; i
++) {
1981 if(input
[i
] == '(') nest_level
++;
1982 else if(input
[i
] == ')') {
1989 else if(!nest_level
) parse_len
++;
1992 parse
= heap_alloc((parse_len
+1)*sizeof(WCHAR
));
1994 return E_OUTOFMEMORY
;
1997 for(i
=0; i
<input_len
; i
++) {
1998 if(input
[i
] == '(') nest_level
++;
1999 else if(input
[i
] == ')') nest_level
--;
2000 else if(!nest_level
) parse
[parse_len
++] = towupper(input
[i
]);
2002 parse
[parse_len
] = 0;
2004 GetTimeZoneInformation(&tzi
);
2006 di
.standardDate
= tzi
.StandardDate
;
2007 di
.standardBias
= tzi
.StandardBias
;
2008 di
.daylightDate
= tzi
.DaylightDate
;
2009 di
.daylightBias
= tzi
.DaylightBias
;
2011 /* FIXME: Cache strings */
2012 lcid_en
= MAKELCID(MAKELANGID(LANG_ENGLISH
,SUBLANG_ENGLISH_US
),SORT_DEFAULT
);
2013 for(i
=0; i
<ARRAY_SIZE(string_ids
); i
++) {
2014 size
= GetLocaleInfoW(lcid_en
, string_ids
[i
], NULL
, 0);
2015 strings
[i
] = heap_alloc((size
+1)*sizeof(WCHAR
));
2019 heap_free(strings
[i
]);
2021 return E_OUTOFMEMORY
;
2023 GetLocaleInfoW(lcid_en
, string_ids
[i
], strings
[i
], size
);
2026 for(i
=0; i
<parse_len
;) {
2027 while(iswspace(parse
[i
])) i
++;
2028 if(parse
[i
] == ',') {
2029 while(parse
[i
] == ',') i
++;
2033 if(parse
[i
]>='0' && parse
[i
]<='9') {
2034 int tmp
= wcstol(&parse
[i
], NULL
, 10);
2035 while(parse
[i
]>='0' && parse
[i
]<='9') i
++;
2036 while(iswspace(parse
[i
])) i
++;
2038 if(parse
[i
] == ':') {
2045 while(parse
[i
] == ':') i
++;
2046 while(iswspace(parse
[i
])) i
++;
2047 if(parse
[i
]>='0' && parse
[i
]<='9') {
2048 min
= wcstol(&parse
[i
], NULL
, 10);
2049 while(parse
[i
]>='0' && parse
[i
]<='9') i
++;
2052 while(iswspace(parse
[i
])) i
++;
2053 while(parse
[i
] == ':') i
++;
2054 while(iswspace(parse
[i
])) i
++;
2055 if(parse
[i
]>='0' && parse
[i
]<='9') {
2056 sec
= wcstol(&parse
[i
], NULL
, 10);
2057 while(parse
[i
]>='0' && parse
[i
]<='9') i
++;
2060 else if(parse
[i
]=='-' || parse
[i
]=='/') {
2061 /* Short or long date */
2062 if(set_day
|| set_month
|| set_year
) break;
2069 while(iswspace(parse
[i
])) i
++;
2070 while(parse
[i
]=='-' || parse
[i
]=='/') i
++;
2071 while(iswspace(parse
[i
])) i
++;
2072 if(parse
[i
]<'0' || parse
[i
]>'9') break;
2073 day
= wcstol(&parse
[i
], NULL
, 10);
2074 while(parse
[i
]>='0' && parse
[i
]<='9') i
++;
2076 while(parse
[i
]=='-' || parse
[i
]=='/') i
++;
2077 while(iswspace(parse
[i
])) i
++;
2078 if(parse
[i
]<'0' || parse
[i
]>'9') break;
2079 year
= wcstol(&parse
[i
], NULL
, 10);
2080 while(parse
[i
]>='0' && parse
[i
]<='9') i
++;
2089 else if(tmp
<0) break;
2103 else if(parse
[i
]=='+' || parse
[i
]=='-') {
2104 /* Timezone offset */
2105 BOOL positive
= TRUE
;
2107 if(set_offset
&& set_hour_adjust
) break;
2109 set_hour_adjust
= FALSE
;
2111 if(parse
[i
] == '-') positive
= FALSE
;
2114 while(iswspace(parse
[i
])) i
++;
2115 if(parse
[i
]<'0' || parse
[i
]>'9') break;
2116 offset
= wcstol(&parse
[i
], NULL
, 10);
2117 while(parse
[i
]>='0' && parse
[i
]<='9') i
++;
2119 if(offset
<24) offset
*= 60;
2120 else offset
= (offset
/100)*60 + offset
%100;
2122 if(positive
) offset
= -offset
;
2126 if(parse
[i
]<'A' || parse
[i
]>'Z') break;
2127 else if(parse
[i
]=='B' && (parse
[i
+1]=='C' ||
2128 (parse
[i
+1]=='.' && parse
[i
+2]=='C'))) {
2135 if(parse
[i
] == '.') i
++;
2137 if(parse
[i
] == '.') i
++;
2139 else if(parse
[i
]=='A' && (parse
[i
+1]=='D' ||
2140 (parse
[i
+1]=='.' && parse
[i
+2]=='D'))) {
2146 if(parse
[i
] == '.') i
++;
2148 if(parse
[i
] == '.') i
++;
2150 else if(parse
[i
+1]<'A' || parse
[i
+1]>'Z') {
2152 if(set_offset
) break;
2155 if(parse
[i
] <= 'I') hour_adjust
= parse
[i
]-'A'+2;
2156 else if(parse
[i
] == 'J') break;
2157 else if(parse
[i
] <= 'M') hour_adjust
= parse
[i
]-'K'+11;
2158 else if(parse
[i
] <= 'Y') hour_adjust
= parse
[i
]-'N';
2159 else hour_adjust
= 1;
2162 if(parse
[i
] == '.') i
++;
2164 else if(parse
[i
]=='A' && parse
[i
+1]=='M') {
2171 else if(parse
[i
]=='P' && parse
[i
+1]=='M') {
2178 else if((parse
[i
]=='U' && parse
[i
+1]=='T' && parse
[i
+2]=='C')
2179 || (parse
[i
]=='G' && parse
[i
+1]=='M' && parse
[i
+2]=='T')) {
2181 if(set_offset
) break;
2183 set_hour_adjust
= FALSE
;
2188 /* Month or garbage */
2191 for(size
=i
; parse
[size
]>='A' && parse
[size
]<='Z'; size
++);
2194 for(j
=0; j
<ARRAY_SIZE(string_ids
); j
++)
2195 if(!wcsnicmp(&parse
[i
], strings
[j
], size
)) break;
2198 if(set_month
) break;
2202 else if(j
== ARRAY_SIZE(string_ids
)) break;
2209 if(i
== parse_len
&& set_year
&& set_month
&& set_day
&& (!set_am
|| hour
<13)) {
2211 if(hour
== 12) hour
= 0;
2215 if(!ad
) year
= -year
+1;
2216 else if(year
<100) year
+= 1900;
2218 *ret
= time_clip(make_date(make_day(year
, month
, day
),
2219 make_time(hour
+hour_adjust
, min
, sec
, ms
)) + offset
*MS_PER_MINUTE
);
2222 *ret
= utc(*ret
, &di
);
2227 for(i
=0; i
<ARRAY_SIZE(string_ids
); i
++)
2228 heap_free(strings
[i
]);
2234 static HRESULT
DateConstr_parse(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
2245 *r
= jsval_number(NAN
);
2249 hres
= to_string(ctx
, argv
[0], &parse_str
);
2253 hres
= date_parse(parse_str
, &n
);
2254 jsstr_release(parse_str
);
2258 *r
= jsval_number(n
);
2262 static HRESULT
date_utc(script_ctx_t
*ctx
, unsigned argc
, jsval_t
*argv
, double *ret
)
2264 double year
, month
, vdate
, hours
, minutes
, seconds
, ms
;
2270 hres
= to_number(ctx
, argv
[0], &year
);
2273 if(0 <= year
&& year
<= 99)
2280 hres
= to_number(ctx
, argv
[1], &month
);
2288 hres
= to_number(ctx
, argv
[2], &vdate
);
2296 hres
= to_number(ctx
, argv
[3], &hours
);
2304 hres
= to_number(ctx
, argv
[4], &minutes
);
2312 hres
= to_number(ctx
, argv
[5], &seconds
);
2320 hres
= to_number(ctx
, argv
[6], &ms
);
2327 *ret
= time_clip(make_date(make_day(year
, month
, vdate
),
2328 make_time(hours
, minutes
,seconds
, ms
)));
2332 static HRESULT
DateConstr_UTC(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
2340 hres
= date_utc(ctx
, argc
, argv
, &n
);
2341 if(SUCCEEDED(hres
) && r
)
2342 *r
= jsval_number(n
);
2346 /* ECMA-262 5.1 Edition 15.9.4.4 */
2347 static HRESULT
DateConstr_now(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
2351 if(r
) *r
= jsval_number(date_now());
2355 static HRESULT
DateConstr_value(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
2364 case DISPATCH_CONSTRUCT
:
2366 /* ECMA-262 3rd Edition 15.9.3.3 */
2368 hres
= create_date(ctx
, NULL
, date_now(), &date
);
2373 /* ECMA-262 3rd Edition 15.9.3.2 */
2378 hres
= to_primitive(ctx
, argv
[0], &prim
, NO_HINT
);
2383 hres
= date_parse(get_string(prim
), &n
);
2385 hres
= to_number(ctx
, prim
, &n
);
2387 jsval_release(prim
);
2391 hres
= create_date(ctx
, NULL
, time_clip(n
), &date
);
2397 /* ECMA-262 3rd Edition 15.9.3.1 */
2402 hres
= date_utc(ctx
, argc
, argv
, &ret_date
);
2406 hres
= create_date(ctx
, NULL
, ret_date
, &date
);
2410 di
= date_from_jsdisp(date
);
2411 di
->time
= utc(di
->time
, di
);
2415 *r
= jsval_obj(date
);
2419 FILETIME system_time
, local_time
;
2422 GetSystemTimeAsFileTime(&system_time
);
2423 FileTimeToLocalFileTime(&system_time
, &local_time
);
2424 lltime
= ((LONGLONG
)local_time
.dwHighDateTime
<<32)
2425 + local_time
.dwLowDateTime
;
2427 return date_to_string(lltime
/10000-TIME_EPOCH
, FALSE
, 0, r
);
2431 FIXME("unimplemented flags %x\n", flags
);
2438 static const builtin_prop_t DateConstr_props
[] = {
2439 {L
"UTC", DateConstr_UTC
, PROPF_METHOD
},
2440 {L
"now", DateConstr_now
, PROPF_HTML
|PROPF_METHOD
},
2441 {L
"parse", DateConstr_parse
, PROPF_METHOD
}
2444 static const builtin_info_t DateConstr_info
= {
2446 DEFAULT_FUNCTION_VALUE
,
2447 ARRAY_SIZE(DateConstr_props
),
2453 HRESULT
create_date_constr(script_ctx_t
*ctx
, jsdisp_t
*object_prototype
, jsdisp_t
**ret
)
2458 hres
= create_date(ctx
, object_prototype
, 0.0, &date
);
2462 hres
= create_builtin_constructor(ctx
, DateConstr_value
, L
"Date", &DateConstr_info
,
2463 PROPF_CONSTR
|7, date
, ret
);
2465 jsdisp_release(date
);