forked from mozilla/rhino
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNativeDate.java
More file actions
1441 lines (1250 loc) · 46.8 KB
/
Copy pathNativeDate.java
File metadata and controls
1441 lines (1250 loc) · 46.8 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 -*-
*
* The contents of this file are subject to the Netscape Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/NPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express oqr
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is Rhino code, released
* May 6, 1999.
*
* The Initial Developer of the Original Code is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1997-1999 Netscape Communications Corporation. All
* Rights Reserved.
*
* Contributor(s):
* Norris Boyd
* Mike McCabe
*
* Alternatively, the contents of this file may be used under the
* terms of the GNU Public License (the "GPL"), in which case the
* provisions of the GPL are applicable instead of those above.
* If you wish to allow use of your version of this file only
* under the terms of the GPL and not to allow others to use your
* version of this file under the NPL, indicate your decision by
* deleting the provisions above and replace them with the notice
* and other provisions required by the GPL. If you do not delete
* the provisions above, a recipient may use your version of this
* file under either the NPL or the GPL.
*/
package org.mozilla.javascript;
import java.util.Date;
import java.util.TimeZone;
import java.util.Locale;
import java.text.NumberFormat;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.lang.reflect.Method;
/**
* This class implements the Date native object.
* See ECMA 15.9.
* @author Mike McCabe
*/
public class NativeDate extends ScriptableObject {
public static void finishInit(Scriptable scope, FunctionObject ctor,
Scriptable proto)
throws PropertyException
{
// initialization of cached values moved to static initializers.
// alias toUTCString to toGMTString
((ScriptableObject) proto).defineProperty("toGMTString",
proto.get("toUTCString", proto),
ScriptableObject.DONTENUM);
// Set some method length values.
// These functions need to be varargs, because their behavior
// depends on the number of arguments they are called with.
String[] specialLengthNames = { "setSeconds", "setUTCSeconds",
"setMinutes", "setUTCMinutes",
"setHours", "setUTCHours",
"setMonth", "setUTCMonth",
"setFullYear", "setUTCFullYear",
};
short[] specialLengthValues = { 2, 2,
3, 3,
4, 4,
2, 2,
3, 3,
};
for (int i=0; i < specialLengthNames.length; i++) {
Object obj = proto.get(specialLengthNames[i], proto);
((FunctionObject) obj).setLength(specialLengthValues[i]);
}
// Set the value of the prototype Date to NaN ('invalid date');
((NativeDate)proto).date = ScriptRuntime.NaN;
}
public NativeDate() {
}
public String getClassName() {
return "Date";
}
public Object getDefaultValue(Class typeHint) {
if (typeHint == null)
typeHint = ScriptRuntime.StringClass;
return super.getDefaultValue(typeHint);
}
/* ECMA helper functions */
private static boolean isFinite(double d) {
if (d != d ||
d == Double.POSITIVE_INFINITY ||
d == Double.NEGATIVE_INFINITY)
return false;
else
return true;
}
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 int DaysInYear(int y) {
if (y % 4 == 0 && (y % 100 != 0 || y % 400 == 0))
return 366;
else
return 365;
}
/* 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) {
int lo = (int) Math.floor((t / msPerDay) / 366) + 1970;
int hi = (int) Math.floor((t / msPerDay) / 365) + 1970;
int mid;
/* above doesn't work for negative dates... */
if (hi < lo) {
int temp = lo;
lo = hi;
hi = temp;
}
/* Use a simple binary search algorithm to find the right
year. This seems like brute force... but the computation
of hi and lo years above lands within one year of the
correct answer for years within a thousand years of
1970; the loop below only requires six iterations
for year 270000. */
while (hi > lo) {
mid = (hi + lo) / 2;
if (TimeFromYear(mid) > t) {
hi = mid - 1;
} else {
if (TimeFromYear(mid) <= t) {
int temp = mid + 1;
if (TimeFromYear(temp) > t) {
return mid;
}
lo = mid + 1;
}
}
}
return lo;
}
private static boolean InLeapYear(double t) {
return DaysInYear(YearFromTime(t)) == 366;
}
private static int DayWithinYear(double t) {
int year = YearFromTime(t);
return (int) (Day(t) - DayFromYear(year));
}
/*
* The following array contains the day of year for the first day of
* each month, where index 0 is January, and day 0 is January 1.
*/
private static double DayFromMonth(int m, boolean leap) {
double firstDay[] = {0.0, 31.0, 59.0, 90.0, 120.0, 151.0,
181.0, 212.0, 243.0, 273.0, 304.0, 334.0};
double leapFirstDay[] = {0.0, 31.0, 60.0, 91.0, 121.0, 152.0,
182.0, 213.0, 244.0, 274.0, 305.0, 335.0};
if (leap)
return leapFirstDay[m];
else
return firstDay[m];
}
private static int MonthFromTime(double t) {
int d, step;
d = DayWithinYear(t);
if (d < (step = 31))
return 0;
// Originally coded as step += (InLeapYear(t) ? 29 : 28);
// but some jits always returned 28!
if (InLeapYear(t))
step += 29;
else
step += 28;
if (d < step)
return 1;
if (d < (step += 31))
return 2;
if (d < (step += 30))
return 3;
if (d < (step += 31))
return 4;
if (d < (step += 30))
return 5;
if (d < (step += 31))
return 6;
if (d < (step += 31))
return 7;
if (d < (step += 30))
return 8;
if (d < (step += 31))
return 9;
if (d < (step += 30))
return 10;
return 11;
}
private static int DateFromTime(double t) {
int d, step, next;
d = DayWithinYear(t);
if (d <= (next = 30))
return d + 1;
step = next;
// Originally coded as next += (InLeapYear(t) ? 29 : 28);
// but some jits always returned 28!
if (InLeapYear(t))
next += 29;
else
next += 28;
if (d <= next)
return d - step;
step = next;
if (d <= (next += 31))
return d - step;
step = next;
if (d <= (next += 30))
return d - step;
step = next;
if (d <= (next += 31))
return d - step;
step = next;
if (d <= (next += 30))
return d - step;
step = next;
if (d <= (next += 31))
return d - step;
step = next;
if (d <= (next += 31))
return d - step;
step = next;
if (d <= (next += 30))
return d - step;
step = next;
if (d <= (next += 31))
return d - step;
step = next;
if (d <= (next += 30))
return d - step;
step = next;
return d - step;
}
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 (double) System.currentTimeMillis();
}
/* Should be possible to determine the need for this dynamically
* if we go with the workaround... I'm not using it now, because I
* can't think of any clean way to make toLocaleString() and the
* time zone (comment) in toString match the generated string
* values. Currently it's wrong-but-consistent in all but the
* most recent betas of the JRE - seems to work in 1.1.7.
*/
private final static boolean TZO_WORKAROUND = false;
private static double DaylightSavingTA(double t) {
if (!TZO_WORKAROUND) {
Date date = new Date((long) t);
if (thisTimeZone.inDaylightTime(date))
return msPerHour;
else
return 0;
} else {
/* Use getOffset if inDaylightTime() is broken, because it
* seems to work acceptably. We don't switch over to it
* entirely, because it requires (expensive) exploded date arguments,
* and the api makes it impossible to handle dst
* changeovers cleanly.
*/
// Hardcode the assumption that the changeover always
// happens at 2:00 AM:
t += LocalTZA + (HourFromTime(t) <= 2 ? msPerHour : 0);
int year = YearFromTime(t);
double offset = thisTimeZone.getOffset(year > 0 ? 1 : 0,
year,
MonthFromTime(t),
DateFromTime(t),
WeekDay(t),
(int)TimeWithinDay(t));
if ((offset - LocalTZA) != 0)
return msPerHour;
else
return 0;
// return offset - LocalTZA;
}
}
private static double LocalTime(double t) {
return t + LocalTZA + DaylightSavingTA(t);
}
private static double internalUTC(double t) {
return t - LocalTZA - DaylightSavingTA(t - LocalTZA);
}
private static int HourFromTime(double t) {
double result;
result = Math.floor(t / msPerHour) % HoursPerDay;
if (result < 0)
result += HoursPerDay;
return (int) result;
}
private static int MinFromTime(double t) {
double result;
result = Math.floor(t / msPerMinute) % MinutesPerHour;
if (result < 0)
result += MinutesPerHour;
return (int) result;
}
private static int SecFromTime(double t) {
double result;
result = Math.floor(t / msPerSecond) % SecondsPerMinute;
if (result < 0)
result += SecondsPerMinute;
return (int) result;
}
private static int msFromTime(double t) {
double result;
result = t % msPerSecond;
if (result < 0)
result += msPerSecond;
return (int) result;
}
private static double MakeTime(double hour, double min,
double sec, double ms)
{
return ((hour * MinutesPerHour + min) * SecondsPerMinute + sec)
* msPerSecond + ms;
}
private static double MakeDay(double year, double month, double date) {
double result;
boolean leap;
double yearday;
double monthday;
year += Math.floor(month / 12);
month = month % 12;
if (month < 0)
month += 12;
leap = (DaysInYear((int) year) == 366);
yearday = Math.floor(TimeFromYear(year) / msPerDay);
monthday = DayFromMonth((int) month, leap);
result = yearday
+ monthday
+ date - 1;
return result;
}
private static double MakeDate(double day, double time) {
return day * msPerDay + time;
}
private static double TimeClip(double d) {
if (d != d ||
d == Double.POSITIVE_INFINITY ||
d == Double.NEGATIVE_INFINITY ||
Math.abs(d) > HalfTimeDomain)
{
return ScriptRuntime.NaN;
}
if (d > 0.0)
return Math.floor(d + 0.);
else
return Math.ceil(d + 0.);
}
/* end of ECMA helper functions */
/* find UTC time from given date... no 1900 correction! */
private static double date_msecFromDate(double year, double mon,
double mday, double hour,
double min, double sec,
double msec)
{
double day;
double time;
double result;
day = MakeDay(year, mon, mday);
time = MakeTime(hour, min, sec, msec);
result = MakeDate(day, time);
return result;
}
private static final int MAXARGS = 7;
public static double jsStaticFunction_UTC(Context cx, Scriptable thisObj,
Object[] args, Function funObj)
{
double array[] = new double[MAXARGS];
int loop;
double d;
for (loop = 0; loop < MAXARGS; loop++) {
if (loop < args.length) {
d = ScriptRuntime.toNumber(args[loop]);
if (d != d || Double.isInfinite(d)) {
return ScriptRuntime.NaN;
}
array[loop] = ScriptRuntime.toInteger(args[loop]);
} else {
array[loop] = 0;
}
}
/* adjust 2-digit years into the 20th century */
if (array[0] >= 0 && array[0] <= 99)
array[0] += 1900;
/* if we got a 0 for 'date' (which is out of range)
* pretend it's a 1. (So Date.UTC(1972, 5) works) */
if (array[2] < 1)
array[2] = 1;
d = date_msecFromDate(array[0], array[1], array[2],
array[3], array[4], array[5], array[6]);
d = TimeClip(d);
return d;
// return new Double(d);
}
/*
* Use ported code from jsdate.c rather than the locale-specific
* date-parsing code from Java, to keep js and rhino consistent.
* Is this the right strategy?
*/
/* for use by date_parse */
/* replace this with byte arrays? Cheaper? */
private static String wtb[] = {
"am", "pm",
"monday", "tuesday", "wednesday", "thursday", "friday",
"saturday", "sunday",
"january", "february", "march", "april", "may", "june",
"july", "august", "september", "october", "november", "december",
"gmt", "ut", "utc", "est", "edt", "cst", "cdt",
"mst", "mdt", "pst", "pdt"
/* time zone table needs to be expanded */
};
private static int ttb[] = {
-1, -2, 0, 0, 0, 0, 0, 0, 0, /* AM/PM */
2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
10000 + 0, 10000 + 0, 10000 + 0, /* UT/UTC */
10000 + 5 * 60, 10000 + 4 * 60, /* EDT */
10000 + 6 * 60, 10000 + 5 * 60,
10000 + 7 * 60, 10000 + 6 * 60,
10000 + 8 * 60, 10000 + 7 * 60
};
/* helper for date_parse */
private static boolean date_regionMatches(String s1, int s1off,
String s2, int s2off,
int count)
{
boolean result = false;
/* return true if matches, otherwise, false */
int s1len = s1.length();
int s2len = s2.length();
while (count > 0 && s1off < s1len && s2off < s2len) {
if (Character.toLowerCase(s1.charAt(s1off)) !=
Character.toLowerCase(s2.charAt(s2off)))
break;
s1off++;
s2off++;
count--;
}
if (count == 0) {
result = true;
}
return result;
}
private static double date_parseString(String s) {
double msec;
int year = -1;
int mon = -1;
int mday = -1;
int hour = -1;
int min = -1;
int sec = -1;
char c = 0;
char si = 0;
int i = 0;
int n = -1;
double tzoffset = -1;
char prevc = 0;
int limit = 0;
boolean seenplusminus = false;
if (s == null) // ??? Will s be null?
return ScriptRuntime.NaN;
limit = s.length();
while (i < limit) {
c = s.charAt(i);
i++;
if (c <= ' ' || c == ',' || c == '-') {
if (i < limit) {
si = s.charAt(i);
if (c == '-' && '0' <= si && si <= '9') {
prevc = c;
}
}
continue;
}
if (c == '(') { /* comments) */
int depth = 1;
while (i < limit) {
c = s.charAt(i);
i++;
if (c == '(')
depth++;
else if (c == ')')
if (--depth <= 0)
break;
}
continue;
}
if ('0' <= c && c <= '9') {
n = c - '0';
while (i < limit && '0' <= (c = s.charAt(i)) && c <= '9') {
n = n * 10 + c - '0';
i++;
}
/* allow TZA before the year, so
* 'Wed Nov 05 21:49:11 GMT-0800 1997'
* works */
/* uses of seenplusminus allow : in TZA, so Java
* no-timezone style of GMT+4:30 works
*/
if ((prevc == '+' || prevc == '-')/* && year>=0 */) {
/* make ':' case below change tzoffset */
seenplusminus = true;
/* offset */
if (n < 24)
n = n * 60; /* EG. "GMT-3" */
else
n = n % 100 + n / 100 * 60; /* eg "GMT-0430" */
if (prevc == '+') /* plus means east of GMT */
n = -n;
if (tzoffset != 0 && tzoffset != -1)
return ScriptRuntime.NaN;
tzoffset = n;
} else if (n >= 70 ||
(prevc == '/' && mon >= 0 && mday >= 0 && year < 0)) {
if (year >= 0)
return ScriptRuntime.NaN;
else if (c <= ' ' || c == ',' || c == '/' || i >= limit)
year = n < 100 ? n + 1900 : n;
else
return ScriptRuntime.NaN;
} else if (c == ':') {
if (hour < 0)
hour = /*byte*/ n;
else if (min < 0)
min = /*byte*/ n;
else
return ScriptRuntime.NaN;
} else if (c == '/') {
if (mon < 0)
mon = /*byte*/ n-1;
else if (mday < 0)
mday = /*byte*/ n;
else
return ScriptRuntime.NaN;
} else if (i < limit && c != ',' && c > ' ' && c != '-') {
return ScriptRuntime.NaN;
} else if (seenplusminus && n < 60) { /* handle GMT-3:30 */
if (tzoffset < 0)
tzoffset -= n;
else
tzoffset += n;
} else if (hour >= 0 && min < 0) {
min = /*byte*/ n;
} else if (min >= 0 && sec < 0) {
sec = /*byte*/ n;
} else if (mday < 0) {
mday = /*byte*/ n;
} else {
return ScriptRuntime.NaN;
}
prevc = 0;
} else if (c == '/' || c == ':' || c == '+' || c == '-') {
prevc = c;
} else {
int st = i - 1;
int k;
while (i < limit) {
c = s.charAt(i);
if (!(('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z')))
break;
i++;
}
if (i <= st + 1)
return ScriptRuntime.NaN;
for (k = wtb.length; --k >= 0;)
if (date_regionMatches(wtb[k], 0, s, st, i-st)) {
int action = ttb[k];
if (action != 0) {
if (action < 0) {
/*
* AM/PM. Count 12:30 AM as 00:30, 12:30 PM as
* 12:30, instead of blindly adding 12 if PM.
*/
if (hour > 12 || hour < 0) {
return ScriptRuntime.NaN;
} else {
if (action == -1 && hour == 12) { // am
hour = 0;
} else if (action == -2 && hour != 12) {// pm
hour += 12;
}
}
} else if (action <= 13) { /* month! */
if (mon < 0) {
mon = /*byte*/ (action - 2);
} else {
return ScriptRuntime.NaN;
}
} else {
tzoffset = action - 10000;
}
}
break;
}
if (k < 0)
return ScriptRuntime.NaN;
prevc = 0;
}
}
if (year < 0 || mon < 0 || mday < 0)
return ScriptRuntime.NaN;
if (sec < 0)
sec = 0;
if (min < 0)
min = 0;
if (hour < 0)
hour = 0;
if (tzoffset == -1) { /* no time zone specified, have to use local */
double time;
time = date_msecFromDate(year, mon, mday, hour, min, sec, 0);
return internalUTC(time);
}
msec = date_msecFromDate(year, mon, mday, hour, min, sec, 0);
msec += tzoffset * msPerMinute;
return msec;
}
public static double jsStaticFunction_parse(String s) {
return date_parseString(s);
}
private static String date_format(double t) {
StringBuffer result = new StringBuffer(60);
double local = LocalTime(t);
/* offset from GMT in minutes. The offset includes daylight savings,
if it applies. */
int minutes = (int) Math.floor((LocalTZA + DaylightSavingTA(t))
/ msPerMinute);
/* map 510 minutes to 0830 hours */
int offset = (minutes / 60) * 100 + minutes % 60;
String dateStr = Integer.toString(DateFromTime(local));
String hourStr = Integer.toString(HourFromTime(local));
String minStr = Integer.toString(MinFromTime(local));
String secStr = Integer.toString(SecFromTime(local));
String offsetStr = Integer.toString(offset > 0 ? offset : -offset);
int year = YearFromTime(local);
String yearStr = Integer.toString(year > 0 ? year : -year);
result.append(days[WeekDay(local)]);
result.append(" ");
result.append(months[MonthFromTime(local)]);
if (dateStr.length() == 1)
result.append(" 0");
else
result.append(" ");
result.append(dateStr);
if (hourStr.length() == 1)
result.append(" 0");
else
result.append(" ");
result.append(hourStr);
if (minStr.length() == 1)
result.append(":0");
else
result.append(":");
result.append(minStr);
if (secStr.length() == 1)
result.append(":0");
else
result.append(":");
result.append(secStr);
if (offset > 0)
result.append(" GMT+");
else
result.append(" GMT-");
int i;
for (i = offsetStr.length(); i < 4; i++)
result.append("0");
result.append(offsetStr);
// ask Java for a time zone string, if we've been able to find
// a formatter for it.
if (timeZoneFormatter != null) {
result.append(" (");
java.util.Date date = new Date((long) t);
result.append(timeZoneFormatter.format(date));
result.append(") ");
} else {
result.append(" ");
}
if (year < 0)
result.append("-");
for (i = yearStr.length(); i < 4; i++)
result.append("0");
result.append(yearStr);
return result.toString();
}
/* the javascript constructor */
public static Object jsConstructor(Context cx, Object[] args,
Function ctorObj, boolean inNewExpr)
{
// if called as a function, just return a string
// representing the current time.
if (!inNewExpr)
return date_format(Now());
NativeDate obj = new NativeDate();
// if called as a constructor with no args,
// return a new Date with the current time.
if (args.length == 0) {
obj.date = Now();
return obj;
}
// if called with just one arg -
if (args.length == 1) {
double date;
// if it's not a string, use it as a millisecond date
if (!(args[0] instanceof String)) {
date = ScriptRuntime.toNumber(args[0]);
} else {
// it's a string; parse it.
String str = (String) args[0];
date = date_parseString(str);
}
obj.date = TimeClip(date);
return obj;
}
// multiple arguments; year, month, day etc.
double array[] = new double[MAXARGS];
int loop;
double d;
for (loop = 0; loop < MAXARGS; loop++) {
if (loop < args.length) {
d = ScriptRuntime.toNumber(args[loop]);
if (d != d || Double.isInfinite(d)) {
obj.date = ScriptRuntime.NaN;
return obj;
}
array[loop] = ScriptRuntime.toInteger(args[loop]);
} else {
array[loop] = 0;
}
}
/* adjust 2-digit years into the 20th century */
if (array[0] >= 0 && array[0] <= 99)
array[0] += 1900;
/* if we got a 0 for 'date' (which is out of range)
* pretend it's a 1 */
if (array[2] < 1)
array[2] = 1;
double day = MakeDay(array[0], array[1], array[2]);
double time = MakeTime(array[3], array[4], array[5], array[6]);
time = MakeDate(day, time);
time = internalUTC(time);
obj.date = TimeClip(time);
return obj;
}
/* constants for toString, toUTCString */
private static String js_NaN_date_str = "Invalid Date";
private static String[] days = {
"Sun","Mon","Tue","Wed","Thu","Fri","Sat"
};
private static String[] months = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
public String jsFunction_toString() {
/* all for want of printf. All of this garbage seems necessary
* because Java carefully avoids providing non-localized
* string formatting. We need to avoid localization to ensure
* that we generate parseable output.
*/
if (this.date != this.date)
return js_NaN_date_str;
return date_format(this.date);
}
public String jsFunction_toLocaleString() {
if (this.date != this.date)
return js_NaN_date_str;
// the string this returns doesn't have much resemblance
// to what jsdate.c returns... does this matter?
java.util.Date tempdate = new Date((long) this.date);
return localeDateFormatter.format(tempdate);
}
public String jsFunction_toUTCString() {
if (this.date != this.date)
return js_NaN_date_str;
StringBuffer result = new StringBuffer(60);
String dateStr = Integer.toString(DateFromTime(this.date));
String hourStr = Integer.toString(HourFromTime(this.date));
String minStr = Integer.toString(MinFromTime(this.date));
String secStr = Integer.toString(SecFromTime(this.date));
int year = YearFromTime(this.date);
String yearStr = Integer.toString(year > 0 ? year : -year);
result.append(days[WeekDay(this.date)]);
result.append(", ");
if (dateStr.length() == 1)
result.append("0");
result.append(dateStr);
result.append(" ");
result.append(months[MonthFromTime(this.date)]);
if (year < 0)
result.append(" -");
else
result.append(" ");
int i;
for (i = yearStr.length(); i < 4; i++)
result.append("0");
result.append(yearStr);
if (hourStr.length() == 1)
result.append(" 0");
else
result.append(" ");
result.append(hourStr);
if (minStr.length() == 1)
result.append(":0");
else
result.append(":");
result.append(minStr);
if (secStr.length() == 1)
result.append(":0");
else
result.append(":");
result.append(secStr);
result.append(" GMT");
return result.toString();
}
public double jsFunction_valueOf() {
return this.date;
}
public double jsFunction_getTime() {
return this.date;
}
public double jsFunction_getYear() {
if (this.date != this.date)
return this.date;
int result = YearFromTime(LocalTime(this.date));
/*
* During the great date rewrite of 1.3, we tried to track the
* evolving ECMA standard, which then had a definition of
* getYear which always subtracted 1900. Which we
* implemented, not realizing that it was incompatible with
* the old behavior... now, rather than thrash the behavior
* yet again, we've decided to leave it with the - 1900
* behavior and point people to the getFullYear method. But
* we try to protect existing scripts that have specified a
* version...
*/
Context cx = Context.getContext();
int version = cx.getLanguageVersion();
if (version == Context.VERSION_1_0 ||
version == Context.VERSION_1_1 ||
version == Context.VERSION_1_2)
{
if (result >= 1900 && result < 2000)
result -= 1900;
} else {