forked from mozilla/rhino
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathNativeDate.java
More file actions
2087 lines (1842 loc) · 73.1 KB
/
NativeDate.java
File metadata and controls
2087 lines (1842 loc) · 73.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.javascript;
import static org.mozilla.javascript.ClassDescriptor.Builder.alias;
import static org.mozilla.javascript.ClassDescriptor.Destination.CTOR;
import static org.mozilla.javascript.ClassDescriptor.Destination.PROTO;
import java.time.Instant;
import java.time.ZoneId;
import java.time.chrono.IsoChronology;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.FormatStyle;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Locale;
/**
* This class implements the Date native object. See ECMA 15.9.
*
* @author Mike McCabe
* @author Lai Quang Duong
* <p>Significant parts of this code are adapted from the venerable jsdate.cpp (also Mozilla):
* <a href="https://dxr.mozilla.org/mozilla-central/source/js/src/jsdate.cpp">jsdate.cpp</a>
*/
@SuppressWarnings("AndroidJdkLibsChecker")
// java.time.format API added in API level 26
final class NativeDate extends ScriptableObject {
private static final long serialVersionUID = -8307438915861678966L;
private static final String CLASS_NAME = "Date";
private static final String js_NaN_date_str = "Invalid Date";
private static final ClassDescriptor DESCRIPTOR;
static {
DESCRIPTOR =
new ClassDescriptor.Builder(
CLASS_NAME,
7,
NativeDate::js_constructorFunc,
NativeDate::js_constructor)
.withMethod(CTOR, "now", 0, NativeDate::js_now)
.withMethod(CTOR, "parse", 1, NativeDate::js_parse)
.withMethod(CTOR, "UTC", 7, NativeDate::js_UTC)
.withMethod(PROTO, "toString", 0, NativeDate::js_toString)
.withMethod(PROTO, "toTimeString", 0, NativeDate::js_toTimeString)
.withMethod(PROTO, "toDateString", 0, NativeDate::js_toDateString)
.withMethod(PROTO, "toLocaleString", 0, NativeDate::js_toLocaleString)
.withMethod(
PROTO, "toLocaleTimeString", 0, NativeDate::js_toLocaleTimeString)
.withMethod(
PROTO, "toLocaleDateString", 0, NativeDate::js_toLocaleDateString)
.withMethod(PROTO, "toUTCString", 0, NativeDate::js_toUTCString)
.withProp(PROTO, "toGMTString", alias("toUTCString", DONTENUM))
.withMethod(PROTO, "toSource", 0, NativeDate::js_toSource)
.withMethod(PROTO, "valueOf", 0, NativeDate::js_valueOf)
.withMethod(PROTO, "getTime", 0, NativeDate::js_getTime)
.withMethod(PROTO, "getYear", 0, NativeDate::js_getYear)
.withMethod(PROTO, "getFullYear", 0, NativeDate::js_getFullYear)
.withMethod(PROTO, "getUTCFullYear", 0, NativeDate::js_getUTCFullYear)
.withMethod(PROTO, "getMonth", 0, NativeDate::js_getMonth)
.withMethod(PROTO, "getUTCMonth", 0, NativeDate::js_getUTCMonth)
.withMethod(PROTO, "getDate", 0, NativeDate::js_getDate)
.withMethod(PROTO, "getUTCDate", 0, NativeDate::js_getUTCDate)
.withMethod(PROTO, "getDay", 0, NativeDate::js_getDay)
.withMethod(PROTO, "getUTCDay", 0, NativeDate::js_getUTCDay)
.withMethod(PROTO, "getHours", 0, NativeDate::js_getHours)
.withMethod(PROTO, "getUTCHours", 0, NativeDate::js_getUTCHours)
.withMethod(PROTO, "getMinutes", 0, NativeDate::js_getMinutes)
.withMethod(PROTO, "getUTCMinutes", 0, NativeDate::js_getUTCMinutes)
.withMethod(PROTO, "getSeconds", 0, NativeDate::js_getSeconds)
.withMethod(PROTO, "getUTCSeconds", 0, NativeDate::js_getUTCSeconds)
.withMethod(PROTO, "getMilliseconds", 0, NativeDate::js_getMilliseconds)
.withMethod(
PROTO, "getUTCMilliseconds", 0, NativeDate::js_getUTCMilliseconds)
.withMethod(PROTO, "getTimezoneOffset", 0, NativeDate::js_getTimezoneOffset)
.withMethod(PROTO, "setTime", 1, NativeDate::js_setTime)
.withMethod(PROTO, "setMilliseconds", 1, NativeDate::js_setMilliseconds)
.withMethod(
PROTO, "setUTCMilliseconds", 1, NativeDate::js_setUTCMilliseconds)
.withMethod(PROTO, "setSeconds", 2, NativeDate::js_setSeconds)
.withMethod(PROTO, "setUTCSeconds", 2, NativeDate::js_setUTCSeconds)
.withMethod(PROTO, "setMinutes", 3, NativeDate::js_setMinutes)
.withMethod(PROTO, "setUTCMinutes", 3, NativeDate::js_setUTCMinutes)
.withMethod(PROTO, "setHours", 4, NativeDate::js_setHours)
.withMethod(PROTO, "setUTCHours", 4, NativeDate::js_setUTCHours)
.withMethod(PROTO, "setDate", 1, NativeDate::js_setDate)
.withMethod(PROTO, "setUTCDate", 1, NativeDate::js_setUTCDate)
.withMethod(PROTO, "setMonth", 2, NativeDate::js_setMonth)
.withMethod(PROTO, "setUTCMonth", 2, NativeDate::js_setUTCMonth)
.withMethod(PROTO, "setFullYear", 3, NativeDate::js_setFullYear)
.withMethod(PROTO, "setUTCFullYear", 3, NativeDate::js_setUTCFullYear)
.withMethod(PROTO, "setYear", 1, NativeDate::js_setYear)
.withMethod(PROTO, "toISOString", 0, NativeDate::js_toISOString)
.withMethod(PROTO, "toJSON", 1, NativeDate::js_toJSON)
.withMethod(PROTO, SymbolKey.TO_PRIMITIVE, 1, NativeDate::js_toPrimitive)
.build();
}
static void init(Context cx, Scriptable scope, boolean sealed) {
DESCRIPTOR.buildConstructor(
cx,
scope,
cx.getLanguageVersion() >= Context.VERSION_ES6
? new NativeObject()
: new NativeDate(Double.NaN),
sealed);
}
private NativeDate() {}
private NativeDate(double date) {
this.date = date;
}
@Override
public String getClassName() {
return "Date";
}
@Override
public Object getDefaultValue(Class<?> typeHint) {
if (typeHint == null) typeHint = ScriptRuntime.StringClass;
return super.getDefaultValue(typeHint);
}
double getJSTimeValue() {
return date;
}
private static Object js_now(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
return ScriptRuntime.wrapNumber(now());
}
private static Object js_parse(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
String dataStr = ScriptRuntime.toString(args, 0);
return ScriptRuntime.wrapNumber(date_parseString(cx, dataStr));
}
private static Object js_UTC(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
return ScriptRuntime.wrapNumber(jsStaticFunction_UTC(args));
}
private static Object js_constructor(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
var res = jsConstructor(cx, args);
ScriptRuntime.setBuiltinProtoAndParent(res, f, nt, s, TopLevel.Builtins.Date);
return res;
}
private static Object js_constructorFunc(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
return date_format(cx, now(), Id_toString);
}
private static Object js_toJSON(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
final String toISOString = "toISOString";
Scriptable o = ScriptRuntime.toObject(cx, s, thisObj);
Object tv = ScriptRuntime.toPrimitive(o, ScriptRuntime.NumberClass);
if (tv instanceof Number) {
double d = ((Number) tv).doubleValue();
if (Double.isNaN(d) || Double.isInfinite(d)) {
return null;
}
}
Object toISO = ScriptableObject.getProperty(o, toISOString);
if (toISO == NOT_FOUND) {
throw ScriptRuntime.typeErrorById(
"msg.function.not.found.in", toISOString, ScriptRuntime.toString(o));
}
if (!(toISO instanceof Callable)) {
throw ScriptRuntime.typeErrorById(
"msg.isnt.function.in",
toISOString,
ScriptRuntime.toString(o),
ScriptRuntime.toString(toISO));
}
Object result = ((Callable) toISO).call(cx, s, o, ScriptRuntime.emptyArgs);
if (!ScriptRuntime.isPrimitive(result)) {
throw ScriptRuntime.typeErrorById(
"msg.toisostring.must.return.primitive", ScriptRuntime.toString(result));
}
return result;
}
private static Object js_toPrimitive(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
Scriptable o = ScriptRuntime.toObject(cx, s, thisObj);
final Object arg0 = args.length > 0 ? args[0] : Undefined.instance;
final String hint = (arg0 instanceof CharSequence) ? arg0.toString() : null;
Class<?> typeHint = null;
if ("string".equals(hint) || "default".equals(hint)) {
typeHint = ScriptRuntime.StringClass;
} else if ("number".equals(hint)) {
typeHint = ScriptRuntime.NumberClass;
}
if (typeHint == null) {
throw ScriptRuntime.typeErrorById(
"msg.invalid.toprimitive.hint", ScriptRuntime.toString(arg0));
}
return ScriptableObject.getDefaultValue(o, typeHint);
}
private static Object js_toString(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
var realThis = realThis(thisObj);
double t = realThis.date;
if (!Double.isNaN(t)) {
return date_format(cx, t, Id_toString);
}
return js_NaN_date_str;
}
private static Object js_toTimeString(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
var realThis = realThis(thisObj);
double t = realThis.date;
if (!Double.isNaN(t)) {
return date_format(cx, t, Id_toTimeString);
}
return js_NaN_date_str;
}
private static Object js_toDateString(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
var realThis = realThis(thisObj);
double t = realThis.date;
if (!Double.isNaN(t)) {
return date_format(cx, t, Id_toDateString);
}
return js_NaN_date_str;
}
private static Object js_toLocaleString(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
var realThis = realThis(thisObj);
double t = realThis.date;
if (!Double.isNaN(t)) {
return toLocale_helper(cx, t, Id_toLocaleString, args);
}
return js_NaN_date_str;
}
private static Object js_toLocaleDateString(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
var realThis = realThis(thisObj);
double t = realThis.date;
if (!Double.isNaN(t)) {
return toLocale_helper(cx, t, Id_toLocaleDateString, args);
}
return js_NaN_date_str;
}
private static Object js_toLocaleTimeString(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
var realThis = realThis(thisObj);
double t = realThis.date;
if (!Double.isNaN(t)) {
return toLocale_helper(cx, t, Id_toLocaleTimeString, args);
}
return js_NaN_date_str;
}
private static Object js_toUTCString(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
var realThis = realThis(thisObj);
double t = realThis.date;
if (!Double.isNaN(t)) {
return js_toUTCString(t);
}
return js_NaN_date_str;
}
private static Object js_toSource(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
var realThis = realThis(thisObj);
double t = realThis.date;
return "(new Date(" + ScriptRuntime.toString(t) + "))";
}
private static Object js_valueOf(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
var realThis = realThis(thisObj);
double t = realThis.date;
return ScriptRuntime.wrapNumber(t);
}
private static Object js_getTime(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
var realThis = realThis(thisObj);
double t = realThis.date;
return ScriptRuntime.wrapNumber(t);
}
private static Object js_getYear(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
var realThis = realThis(thisObj);
double t = realThis.date;
if (!Double.isNaN(t)) {
t = LocalTime(cx, t);
t = YearFromTime(t);
t -= 1900;
}
return ScriptRuntime.wrapNumber(t);
}
private static Object js_getFullYear(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
var realThis = realThis(thisObj);
double t = realThis.date;
if (!Double.isNaN(t)) {
t = LocalTime(cx, t);
t = YearFromTime(t);
}
return ScriptRuntime.wrapNumber(t);
}
private static Object js_getUTCFullYear(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
var realThis = realThis(thisObj);
double t = realThis.date;
if (!Double.isNaN(t)) {
t = YearFromTime(t);
}
return ScriptRuntime.wrapNumber(t);
}
private static Object js_getMonth(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
var realThis = realThis(thisObj);
double t = realThis.date;
if (!Double.isNaN(t)) {
t = LocalTime(cx, t);
t = MonthFromTime(t);
}
return ScriptRuntime.wrapNumber(t);
}
private static Object js_getUTCMonth(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
var realThis = realThis(thisObj);
double t = realThis.date;
if (!Double.isNaN(t)) {
t = MonthFromTime(t);
}
return ScriptRuntime.wrapNumber(t);
}
private static Object js_getDate(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
var realThis = realThis(thisObj);
double t = realThis.date;
if (!Double.isNaN(t)) {
t = LocalTime(cx, t);
t = DateFromTime(t);
}
return ScriptRuntime.wrapNumber(t);
}
private static Object js_getUTCDate(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
var realThis = realThis(thisObj);
double t = realThis.date;
if (!Double.isNaN(t)) {
t = DateFromTime(t);
}
return ScriptRuntime.wrapNumber(t);
}
private static Object js_getDay(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
var realThis = realThis(thisObj);
double t = realThis.date;
if (!Double.isNaN(t)) {
t = LocalTime(cx, t);
t = WeekDay(t);
}
return ScriptRuntime.wrapNumber(t);
}
private static Object js_getUTCDay(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
var realThis = realThis(thisObj);
double t = realThis.date;
if (!Double.isNaN(t)) {
t = WeekDay(t);
}
return ScriptRuntime.wrapNumber(t);
}
private static Object js_getHours(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
var realThis = realThis(thisObj);
double t = realThis.date;
if (!Double.isNaN(t)) {
t = LocalTime(cx, t);
t = HourFromTime(t);
}
return ScriptRuntime.wrapNumber(t);
}
private static Object js_getUTCHours(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
var realThis = realThis(thisObj);
double t = realThis.date;
if (!Double.isNaN(t)) {
t = HourFromTime(t);
}
return ScriptRuntime.wrapNumber(t);
}
private static Object js_getMinutes(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
var realThis = realThis(thisObj);
double t = realThis.date;
if (!Double.isNaN(t)) {
t = LocalTime(cx, t);
t = MinFromTime(t);
}
return ScriptRuntime.wrapNumber(t);
}
private static Object js_getUTCMinutes(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
var realThis = realThis(thisObj);
double t = realThis.date;
if (!Double.isNaN(t)) {
t = MinFromTime(t);
}
return ScriptRuntime.wrapNumber(t);
}
private static Object js_getSeconds(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
var realThis = realThis(thisObj);
double t = realThis.date;
if (!Double.isNaN(t)) {
t = LocalTime(cx, t);
t = SecFromTime(t);
}
return ScriptRuntime.wrapNumber(t);
}
private static Object js_getUTCSeconds(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
var realThis = realThis(thisObj);
double t = realThis.date;
if (!Double.isNaN(t)) {
t = SecFromTime(t);
}
return ScriptRuntime.wrapNumber(t);
}
private static Object js_getMilliseconds(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
var realThis = realThis(thisObj);
double t = realThis.date;
if (!Double.isNaN(t)) {
t = LocalTime(cx, t);
t = msFromTime(t);
}
return ScriptRuntime.wrapNumber(t);
}
private static Object js_getUTCMilliseconds(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
var realThis = realThis(thisObj);
double t = realThis.date;
if (!Double.isNaN(t)) {
t = msFromTime(t);
}
return ScriptRuntime.wrapNumber(t);
}
private static Object js_getTimezoneOffset(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
var realThis = realThis(thisObj);
double t = realThis.date;
if (!Double.isNaN(t)) {
t = (t - LocalTime(cx, t)) / msPerMinute;
}
return ScriptRuntime.wrapNumber(t);
}
private static Object js_setTime(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
var realThis = realThis(thisObj);
double t = realThis.date;
t = TimeClip(ScriptRuntime.toNumber(args, 0));
realThis.date = t;
return ScriptRuntime.wrapNumber(t);
}
private static Object js_setMilliseconds(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
var realThis = realThis(thisObj);
double t = realThis.date;
t = makeTime(cx, t, args, Id_setMilliseconds);
realThis.date = t;
return ScriptRuntime.wrapNumber(t);
}
private static Object js_setUTCMilliseconds(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
var realThis = realThis(thisObj);
double t = realThis.date;
t = makeTime(cx, t, args, Id_setUTCMilliseconds);
realThis.date = t;
return ScriptRuntime.wrapNumber(t);
}
private static Object js_setSeconds(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
var realThis = realThis(thisObj);
double t = realThis.date;
t = makeTime(cx, t, args, Id_setSeconds);
realThis.date = t;
return ScriptRuntime.wrapNumber(t);
}
private static Object js_setUTCSeconds(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
var realThis = realThis(thisObj);
double t = realThis.date;
t = makeTime(cx, t, args, Id_setUTCSeconds);
realThis.date = t;
return ScriptRuntime.wrapNumber(t);
}
private static Object js_setMinutes(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
var realThis = realThis(thisObj);
double t = realThis.date;
t = makeTime(cx, t, args, Id_setMinutes);
realThis.date = t;
return ScriptRuntime.wrapNumber(t);
}
private static Object js_setUTCMinutes(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
var realThis = realThis(thisObj);
double t = realThis.date;
t = makeTime(cx, t, args, Id_setUTCMinutes);
realThis.date = t;
return ScriptRuntime.wrapNumber(t);
}
private static Object js_setHours(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
var realThis = realThis(thisObj);
double t = realThis.date;
t = makeTime(cx, t, args, Id_setHours);
realThis.date = t;
return ScriptRuntime.wrapNumber(t);
}
private static Object js_setUTCHours(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
var realThis = realThis(thisObj);
double t = realThis.date;
t = makeTime(cx, t, args, Id_setUTCHours);
realThis.date = t;
return ScriptRuntime.wrapNumber(t);
}
private static Object js_setDate(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
var realThis = realThis(thisObj);
double t = realThis.date;
t = makeDate(cx, t, args, Id_setDate);
realThis.date = t;
return ScriptRuntime.wrapNumber(t);
}
private static Object js_setUTCDate(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
var realThis = realThis(thisObj);
double t = realThis.date;
t = makeDate(cx, t, args, Id_setUTCDate);
realThis.date = t;
return ScriptRuntime.wrapNumber(t);
}
private static Object js_setMonth(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
var realThis = realThis(thisObj);
double t = realThis.date;
t = makeDate(cx, t, args, Id_setMonth);
realThis.date = t;
return ScriptRuntime.wrapNumber(t);
}
private static Object js_setUTCMonth(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
var realThis = realThis(thisObj);
double t = realThis.date;
t = makeDate(cx, t, args, Id_setUTCMonth);
realThis.date = t;
return ScriptRuntime.wrapNumber(t);
}
private static Object js_setFullYear(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
var realThis = realThis(thisObj);
double t = realThis.date;
t = makeDate(cx, t, args, Id_setFullYear);
realThis.date = t;
return ScriptRuntime.wrapNumber(t);
}
private static Object js_setUTCFullYear(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
var realThis = realThis(thisObj);
double t = realThis.date;
t = makeDate(cx, t, args, Id_setUTCFullYear);
realThis.date = t;
return ScriptRuntime.wrapNumber(t);
}
private static Object js_setYear(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
var realThis = realThis(thisObj);
double t = realThis.date;
double year = ScriptRuntime.toNumber(args, 0);
if (Double.isNaN(year) || Double.isInfinite(year)) {
t = ScriptRuntime.NaN;
} else {
if (Double.isNaN(t)) {
t = 0;
} else {
t = LocalTime(cx, t);
}
if (year >= 0 && year <= 99) year += 1900;
double day = MakeDay(year, MonthFromTime(t), DateFromTime(t));
t = MakeDate(day, TimeWithinDay(t));
t = internalUTC(cx, t);
t = TimeClip(t);
}
realThis.date = t;
return ScriptRuntime.wrapNumber(t);
}
private static Object js_toISOString(
Context cx, JSFunction f, Object nt, Scriptable s, Object thisObj, Object[] args) {
var realThis = realThis(thisObj);
double t = realThis.date;
if (!Double.isNaN(t)) {
return js_toISOString(t);
}
String msg = ScriptRuntime.getMessageById("msg.invalid.date");
throw ScriptRuntime.rangeError(msg);
}
private static NativeDate realThis(Object thisObj) {
return LambdaConstructor.convertThisObject(thisObj, NativeDate.class);
}
/* ECMA helper functions */
private static final double HalfTimeDomain = 8.64e15;
private static final double HoursPerDay = 24.0;
private static final double MinutesPerHour = 60.0;
private static final double SecondsPerMinute = 60.0;
private static final double msPerSecond = 1000.0;
private static final double MinutesPerDay = (HoursPerDay * MinutesPerHour);
private static final double SecondsPerDay = (MinutesPerDay * SecondsPerMinute);
private static final double SecondsPerHour = (MinutesPerHour * SecondsPerMinute);
private static final double msPerDay = (SecondsPerDay * msPerSecond);
private static final double msPerHour = (SecondsPerHour * msPerSecond);
private static final double msPerMinute = (SecondsPerMinute * msPerSecond);
private static double Day(double t) {
return Math.floor(t / msPerDay);
}
private static double TimeWithinDay(double t) {
double result;
result = t % msPerDay;
if (result < 0) result += msPerDay;
return result;
}
private static boolean IsLeapYear(int year) {
return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
}
/* math here has to be f.p, because we need
* floor((1968 - 1969) / 4) == -1
*/
private static double DayFromYear(double y) {
return (365 * (y - 1970))
+ Math.floor((y - 1969) / 4.0)
- Math.floor((y - 1901) / 100.0)
+ Math.floor((y - 1601) / 400.0);
}
private static double TimeFromYear(double y) {
return DayFromYear(y) * msPerDay;
}
private static int YearFromTime(double t) {
if (Double.isInfinite(t) || Double.isNaN(t)) {
return 0;
}
double y = Math.floor(t / (msPerDay * 365.2425)) + 1970;
double t2 = TimeFromYear(y);
/*
* Adjust the year if the approximation was wrong. Since the year was
* computed using the average number of ms per year, it will usually
* be wrong for dates within several hours of a year transition.
*/
if (t2 > t) {
y--;
} else {
if (t2 + msPerDay * DaysInYear(y) <= t) y++;
}
return (int) y;
}
private static double DayFromMonth(int m, int year) {
int day = m * 30;
if (m >= 7) {
day += m / 2 - 1;
} else if (m >= 2) {
day += (m - 1) / 2 - 1;
} else {
day += m;
}
if (m >= 2 && IsLeapYear(year)) {
++day;
}
return day;
}
private static double DaysInYear(double year) {
if (Double.isInfinite(year) || Double.isNaN(year)) {
return ScriptRuntime.NaN;
}
return IsLeapYear((int) year) ? 366.0 : 365.0;
}
private static int DaysInMonth(int year, int month) {
// month is 1-based for DaysInMonth!
if (month == 2) return IsLeapYear(year) ? 29 : 28;
return month >= 8 ? 31 - (month & 1) : 30 + (month & 1);
}
private static int MonthFromTime(double t) {
int year = YearFromTime(t);
int d = (int) (Day(t) - DayFromYear(year));
d -= 31 + 28;
if (d < 0) {
return (d < -28) ? 0 : 1;
}
if (IsLeapYear(year)) {
if (d == 0) return 1; // 29 February
--d;
}
// d: date count from 1 March
int estimate = d / 30; // approx number of month since March
int mstart;
switch (estimate) {
case 0:
return 2;
case 1:
mstart = 31;
break;
case 2:
mstart = 31 + 30;
break;
case 3:
mstart = 31 + 30 + 31;
break;
case 4:
mstart = 31 + 30 + 31 + 30;
break;
case 5:
mstart = 31 + 30 + 31 + 30 + 31;
break;
case 6:
mstart = 31 + 30 + 31 + 30 + 31 + 31;
break;
case 7:
mstart = 31 + 30 + 31 + 30 + 31 + 31 + 30;
break;
case 8:
mstart = 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31;
break;
case 9:
mstart = 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30;
break;
case 10:
return 11; // Late december
default:
throw Kit.codeBug();
}
// if d < mstart then real month since March == estimate - 1
return (d >= mstart) ? estimate + 2 : estimate + 1;
}
private static int DateFromTime(double t) {
int year = YearFromTime(t);
int d = (int) (Day(t) - DayFromYear(year));
d -= 31 + 28;
if (d < 0) {
return (d < -28) ? d + 31 + 28 + 1 : d + 28 + 1;
}
if (IsLeapYear(year)) {
if (d == 0) return 29; // 29 February
--d;
}
// d: date count from 1 March
int mdays, mstart;
switch (d / 30) { // approx number of month since March
case 0:
return d + 1;
case 1:
mdays = 31;
mstart = 31;
break;
case 2:
mdays = 30;
mstart = 31 + 30;
break;
case 3:
mdays = 31;
mstart = 31 + 30 + 31;
break;
case 4:
mdays = 30;
mstart = 31 + 30 + 31 + 30;
break;
case 5:
mdays = 31;
mstart = 31 + 30 + 31 + 30 + 31;
break;
case 6:
mdays = 31;
mstart = 31 + 30 + 31 + 30 + 31 + 31;
break;
case 7:
mdays = 30;
mstart = 31 + 30 + 31 + 30 + 31 + 31 + 30;
break;
case 8:
mdays = 31;
mstart = 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31;
break;
case 9:
mdays = 30;
mstart = 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30;
break;
case 10:
return d - (31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30) + 1; // Late december
default:
throw Kit.codeBug();
}
d -= mstart;
if (d < 0) {
// wrong estimate: sfhift to previous month
d += mdays;
}
return d + 1;
}
private static int WeekDay(double t) {
double result;
result = Day(t) + 4;
result = result % 7;
if (result < 0) result += 7;
return (int) result;
}
private static double now() {
return System.currentTimeMillis();
}
private static double DaylightSavingTA(Context cx, double t) {
// Another workaround! The JRE doesn't seem to know about DST
// before year 1 AD, so we map to equivalent dates for the
// purposes of finding DST. To be safe, we do this for years
// before 1970.
if (t < 0.0) {
int year = EquivalentYear(YearFromTime(t));
double day = MakeDay(year, MonthFromTime(t), DateFromTime(t));
t = MakeDate(day, TimeWithinDay(t));
}
Date date = new Date((long) t);
if (cx.getTimeZone().inDaylightTime(date)) return msPerHour;
return 0;
}
/*
* Find a year for which any given date will fall on the same weekday.
*
* This function should be used with caution when used other than
* for determining DST; it hasn't been proven not to produce an
* incorrect year for times near year boundaries.
*/
private static int EquivalentYear(int year) {
int day = (int) DayFromYear(year) + 4;
day = day % 7;
if (day < 0) day += 7;
// Years and leap years on which Jan 1 is a Sunday, Monday, etc.
if (IsLeapYear(year)) {
switch (day) {
case 0:
return 1984;
case 1:
return 1996;
case 2:
return 1980;
case 3:
return 1992;
case 4:
return 1976;
case 5:
return 1988;
case 6:
return 1972;
}
} else {
switch (day) {
case 0:
return 1978;
case 1:
return 1973;