Debian Codegraph @

rhino/testsrc/tests/ecma_3/Date/shell.js

raw symbols SCIP
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6 gTestsubsuite = 'Date';
7
8 /*
9 * Date functions used by tests in Date suite
10 *
11 */
12 var msPerDay = 86400000;
13 var HoursPerDay = 24;
14 var MinutesPerHour = 60;
15 var SecondsPerMinute = 60;
16 var msPerSecond = 1000;
17 var msPerMinute = 60000; // msPerSecond * SecondsPerMinute
18 var msPerHour = 3600000; // msPerMinute * MinutesPerHour
19 var TZ_DIFF = getTimeZoneDiff(); // offset of tester's timezone from UTC
20 var TZ_ADJUST = TZ_DIFF * msPerHour;
21 var TZ_PST = -8; // offset of Pacific Standard Time from UTC
22 var PST_DIFF = TZ_DIFF - TZ_PST; // offset of tester's timezone from PST
23 var TIME_1970 = 0;
24 var TIME_2000 = 946684800000;
25 var TIME_1900 = -2208988800000;
26 var UTC_29_FEB_2000 = TIME_2000 + 31*msPerDay + 28*msPerDay;
27 var UTC_1_JAN_2005 = TIME_2000 + TimeInYear(2000) + TimeInYear(2001) +
28 TimeInYear(2002) + TimeInYear(2003) + TimeInYear(2004);
29 var now = new Date();
30 var TIME_NOW = now.valueOf(); //valueOf() is to accurate to the millisecond
31 //Date.parse() is accurate only to the second
32
33 /*
34 * Originally, the test suite used a hard-coded value TZ_DIFF = -8.
35 * But that was only valid for testers in the Pacific Standard Time Zone!
36 * We calculate the proper number dynamically for any tester. We just
37 * have to be careful not to use a date subject to Daylight Savings Time...
38 */
39 function getTimeZoneDiff()
40 {
41 return -((new Date(2000, 1, 1)).getTimezoneOffset())/60;
42 }
43
44
45 /*
46 * Date test "ResultArrays" are hard-coded for Pacific Standard Time.
47 * We must adjust them for the tester's own timezone -
48 */
49 function adjustResultArray(ResultArray, msMode)
50 {
51 // If the tester's system clock is in PST, no need to continue -
52 if (!PST_DIFF) {return;}
53
54 /* The date gTestcases instantiate Date objects in two different ways:
55 *
56 * millisecond mode: e.g. dt = new Date(10000000);
57 * year-month-day mode: dt = new Date(2000, 5, 1, ...);
58 *
59 * In the first case, the date is measured from Time 0 in Greenwich (i.e. UTC).
60 * In the second case, it is measured with reference to the tester's local timezone.
61 *
62 * In the first case we must correct those values expected for local measurements,
63 * like dt.getHours() etc. No correction is necessary for dt.getUTCHours() etc.
64 *
65 * In the second case, it is exactly the other way around -
66 */
67 if (msMode)
68 {
69 // The hard-coded UTC milliseconds from Time 0 derives from a UTC date.
70 // Shift to the right by the offset between UTC and the tester.
71 var t = ResultArray[TIME] + TZ_DIFF*msPerHour;
72
73 // Use our date arithmetic functions to determine the local hour, day, etc.
74 ResultArray[HOURS] = HourFromTime(t);
75 ResultArray[DAY] = WeekDay(t);
76 ResultArray[DATE] = DateFromTime(t);
77 ResultArray[MONTH] = MonthFromTime(t);
78 ResultArray[YEAR] = YearFromTime(t);
79 }
80 else
81 {
82 // The hard-coded UTC milliseconds from Time 0 derives from a PST date.
83 // Shift to the left by the offset between PST and the tester.
84 var t = ResultArray[TIME] - PST_DIFF*msPerHour;
85
86 // Use our date arithmetic functions to determine the UTC hour, day, etc.
87 ResultArray[TIME] = t;
88 ResultArray[UTC_HOURS] = HourFromTime(t);
89 ResultArray[UTC_DAY] = WeekDay(t);
90 ResultArray[UTC_DATE] = DateFromTime(t);
91 ResultArray[UTC_MONTH] = MonthFromTime(t);
92 ResultArray[UTC_YEAR] = YearFromTime(t);
93 }
94 }
95
96
97 function Day( t ) {
98 return ( Math.floor(t/msPerDay ) );
99 }
100 function DaysInYear( y ) {
101 if ( y % 4 != 0 ) {
102 return 365;
103 }
104 if ( (y % 4 == 0) && (y % 100 != 0) ) {
105 return 366;
106 }
107 if ( (y % 100 == 0) && (y % 400 != 0) ) {
108 return 365;
109 }
110 if ( (y % 400 == 0) ){
111 return 366;
112 } else {
113 return "ERROR: DaysInYear(" + y + ") case not covered";
114 }
115 }
116 function TimeInYear( y ) {
117 return ( DaysInYear(y) * msPerDay );
118 }
119 function DayNumber( t ) {
120 return ( Math.floor( t / msPerDay ) );
121 }
122 function TimeWithinDay( t ) {
123
124 var r = t % msPerDay;
125
126 if (r < 0)
127 {
128 r += msPerDay;
129 }
130 return r;
131
132 }
133 function YearNumber( t ) {
134 }
135 function TimeFromYear( y ) {
136 return ( msPerDay * DayFromYear(y) );
137 }
138 function DayFromYear( y ) {
139 return ( 365*(y-1970) +
140 Math.floor((y-1969)/4) -
141 Math.floor((y-1901)/100) +
142 Math.floor((y-1601)/400) );
143 }
144 function InLeapYear( t ) {
145 if ( DaysInYear(YearFromTime(t)) == 365 ) {
146 return 0;
147 }
148 if ( DaysInYear(YearFromTime(t)) == 366 ) {
149 return 1;
150 } else {
151 return "ERROR: InLeapYear("+ t + ") case not covered";
152 }
153 }
154 function YearFromTime( t ) {
155 t = Number( t );
156 var sign = ( t < 0 ) ? -1 : 1;
157 var year = ( sign < 0 ) ? 1969 : 1970;
158 for ( var timeToTimeZero = t; ; ) {
159 // subtract the current year's time from the time that's left.
160 timeToTimeZero -= sign * TimeInYear(year)
161
162 // if there's less than the current year's worth of time left, then break.
163 if ( sign < 0 ) {
164 if ( sign * timeToTimeZero <= 0 ) {
165 break;
166 } else {
167 year += sign;
168 }
169 } else {
170 if ( sign * timeToTimeZero < 0 ) {
171 break;
172 } else {
173 year += sign;
174 }
175 }
176 }
177 return ( year );
178 }
179 function MonthFromTime( t ) {
180 // i know i could use switch but i'd rather not until it's part of ECMA
181 var day = DayWithinYear( t );
182 var leap = InLeapYear(t);
183
184 if ( (0 <= day) && (day < 31) ) {
185 return 0;
186 }
187 if ( (31 <= day) && (day < (59+leap)) ) {
188 return 1;
189 }
190 if ( ((59+leap) <= day) && (day < (90+leap)) ) {
191 return 2;
192 }
193 if ( ((90+leap) <= day) && (day < (120+leap)) ) {
194 return 3;
195 }
196 if ( ((120+leap) <= day) && (day < (151+leap)) ) {
197 return 4;
198 }
199 if ( ((151+leap) <= day) && (day < (181+leap)) ) {
200 return 5;
201 }
202 if ( ((181+leap) <= day) && (day < (212+leap)) ) {
203 return 6;
204 }
205 if ( ((212+leap) <= day) && (day < (243+leap)) ) {
206 return 7;
207 }
208 if ( ((243+leap) <= day) && (day < (273+leap)) ) {
209 return 8;
210 }
211 if ( ((273+leap) <= day) && (day < (304+leap)) ) {
212 return 9;
213 }
214 if ( ((304+leap) <= day) && (day < (334+leap)) ) {
215 return 10;
216 }
217 if ( ((334+leap) <= day) && (day < (365+leap)) ) {
218 return 11;
219 } else {
220 return "ERROR: MonthFromTime("+t+") not known";
221 }
222 }
223 function DayWithinYear( t ) {
224 return( Day(t) - DayFromYear(YearFromTime(t)));
225 }
226 function DateFromTime( t ) {
227 var day = DayWithinYear(t);
228 var month = MonthFromTime(t);
229
230 if ( month == 0 ) {
231 return ( day + 1 );
232 }
233 if ( month == 1 ) {
234 return ( day - 30 );
235 }
236 if ( month == 2 ) {
237 return ( day - 58 - InLeapYear(t) );
238 }
239 if ( month == 3 ) {
240 return ( day - 89 - InLeapYear(t));
241 }
242 if ( month == 4 ) {
243 return ( day - 119 - InLeapYear(t));
244 }
245 if ( month == 5 ) {
246 return ( day - 150- InLeapYear(t));
247 }
248 if ( month == 6 ) {
249 return ( day - 180- InLeapYear(t));
250 }
251 if ( month == 7 ) {
252 return ( day - 211- InLeapYear(t));
253 }
254 if ( month == 8 ) {
255 return ( day - 242- InLeapYear(t));
256 }
257 if ( month == 9 ) {
258 return ( day - 272- InLeapYear(t));
259 }
260 if ( month == 10 ) {
261 return ( day - 303- InLeapYear(t));
262 }
263 if ( month == 11 ) {
264 return ( day - 333- InLeapYear(t));
265 }
266
267 return ("ERROR: DateFromTime("+t+") not known" );
268 }
269 function WeekDay( t ) {
270 var weekday = (Day(t)+4) % 7;
271 return( weekday < 0 ? 7 + weekday : weekday );
272 }
273
274 // missing daylight savings time adjustment
275
276 function HourFromTime( t ) {
277 var h = Math.floor( t / msPerHour ) % HoursPerDay;
278 return ( (h<0) ? HoursPerDay + h : h );
279 }
280 function MinFromTime( t ) {
281 var min = Math.floor( t / msPerMinute ) % MinutesPerHour;
282 return( ( min < 0 ) ? MinutesPerHour + min : min );
283 }
284 function SecFromTime( t ) {
285 var sec = Math.floor( t / msPerSecond ) % SecondsPerMinute;
286 return ( (sec < 0 ) ? SecondsPerMinute + sec : sec );
287 }
288 function msFromTime( t ) {
289 var ms = t % msPerSecond;
290 return ( (ms < 0 ) ? msPerSecond + ms : ms );
291 }
292 function LocalTZA() {
293 return ( TZ_DIFF * msPerHour );
294 }
295 function UTC( t ) {
296 return ( t - LocalTZA() - DaylightSavingTA(t - LocalTZA()) );
297 }
298
299 function DaylightSavingTA( t ) {
300 t = t - LocalTZA();
301
302 var dst_start = GetDSTStart(t);
303 var dst_end = GetDSTEnd(t);
304
305 if ( t >= dst_start && t < dst_end )
306 return msPerHour;
307
308 return 0;
309 }
310
311 function GetFirstSundayInMonth( t, m ) {
312 var year = YearFromTime(t);
313 var leap = InLeapYear(t);
314
315 // month m 0..11
316 // april == 3
317 // march == 2
318
319 // set time to first day of month m
320 var time = TimeFromYear(year);
321 for (var i = 0; i < m; ++i)
322 {
323 time += TimeInMonth(i, leap);
324 }
325
326 for ( var first_sunday = time; WeekDay(first_sunday) > 0;
327 first_sunday += msPerDay )
328 {
329 ;
330 }
331
332 return first_sunday;
333 }
334
335 function GetLastSundayInMonth( t, m ) {
336 var year = YearFromTime(t);
337 var leap = InLeapYear(t);
338
339 // month m 0..11
340 // april == 3
341 // march == 2
342
343 // first day of following month
344 var time = TimeFromYear(year);
345 for (var i = 0; i <= m; ++i)
346 {
347 time += TimeInMonth(i, leap);
348 }
349 // prev day == last day of month
350 time -= msPerDay;
351
352 for ( var last_sunday = time; WeekDay(last_sunday) > 0;
353 last_sunday -= msPerDay )
354 {
355 ;
356 }
357 return last_sunday;
358 }
359
360 /*
361 15.9.1.9 Daylight Saving Time Adjustment
362
363 The implementation of ECMAScript should not try to determine whether
364 the exact time was subject to daylight saving time, but just whether
365 daylight saving time would have been in effect if the current
366 daylight saving time algorithm had been used at the time. This avoids
367 complications such as taking into account the years that the locale
368 observed daylight saving time year round.
369 */
370
371 /*
372 US DST algorithm
373
374 Before 2007, DST starts first Sunday in April at 2 AM and ends last
375 Sunday in October at 2 AM
376
377 Starting in 2007, DST starts second Sunday in March at 2 AM and ends
378 first Sunday in November at 2 AM
379
380 Note that different operating systems behave differently.
381
382 Fully patched Windows XP uses the 2007 algorithm for all dates while
383 fully patched Fedora Core 6 and RHEL 4 Linux use the algorithm in
384 effect at the time.
385
386 Since pre-2007 DST is a subset of 2007 DST rules, this only affects
387 tests that occur in the period Mar-Apr and Oct-Nov where the two
388 algorithms do not agree.
389
390 */
391
392 function GetDSTStart( t )
393 {
394 return (GetFirstSundayInMonth(t, 2) + 7*msPerDay + 2*msPerHour - LocalTZA());
395 }
396
397 function GetDSTEnd( t )
398 {
399 return (GetFirstSundayInMonth(t, 10) + 2*msPerHour - LocalTZA());
400 }
401
402 function GetOldDSTStart( t )
403 {
404 return (GetFirstSundayInMonth(t, 3) + 2*msPerHour - LocalTZA());
405 }
406
407 function GetOldDSTEnd( t )
408 {
409 return (GetLastSundayInMonth(t, 9) + 2*msPerHour - LocalTZA());
410 }
411
412 function LocalTime( t ) {
413 return ( t + LocalTZA() + DaylightSavingTA(t) );
414 }
415 function MakeTime( hour, min, sec, ms ) {
416 if ( isNaN( hour ) || isNaN( min ) || isNaN( sec ) || isNaN( ms ) ) {
417 return Number.NaN;
418 }
419
420 hour = ToInteger(hour);
421 min = ToInteger( min);
422 sec = ToInteger( sec);
423 ms = ToInteger( ms );
424
425 return( (hour*msPerHour) + (min*msPerMinute) +
426 (sec*msPerSecond) + ms );
427 }
428 function MakeDay( year, month, date ) {
429 if ( isNaN(year) || isNaN(month) || isNaN(date) ) {
430 return Number.NaN;
431 }
432 year = ToInteger(year);
433 month = ToInteger(month);
434 date = ToInteger(date );
435
436 var sign = ( year < 1970 ) ? -1 : 1;
437 var t = ( year < 1970 ) ? 1 : 0;
438 var y = ( year < 1970 ) ? 1969 : 1970;
439
440 var result5 = year + Math.floor( month/12 );
441 var result6 = month % 12;
442
443 if ( year < 1970 ) {
444 for ( y = 1969; y >= year; y += sign ) {
445 t += sign * TimeInYear(y);
446 }
447 } else {
448 for ( y = 1970 ; y < year; y += sign ) {
449 t += sign * TimeInYear(y);
450 }
451 }
452
453 var leap = InLeapYear( t );
454
455 for ( var m = 0; m < month; m++ ) {
456 t += TimeInMonth( m, leap );
457 }
458
459 if ( YearFromTime(t) != result5 ) {
460 return Number.NaN;
461 }
462 if ( MonthFromTime(t) != result6 ) {
463 return Number.NaN;
464 }
465 if ( DateFromTime(t) != 1 ) {
466 return Number.NaN;
467 }
468
469 return ( (Day(t)) + date - 1 );
470 }
471 function TimeInMonth( month, leap ) {
472 // september april june november
473 // jan 0 feb 1 mar 2 apr 3 may 4 june 5 jul 6
474 // aug 7 sep 8 oct 9 nov 10 dec 11
475
476 if ( month == 3 || month == 5 || month == 8 || month == 10 ) {
477 return ( 30*msPerDay );
478 }
479
480 // all the rest
481 if ( month == 0 || month == 2 || month == 4 || month == 6 ||
482 month == 7 || month == 9 || month == 11 ) {
483 return ( 31*msPerDay );
484 }
485
486 // save february
487 return ( (leap == 0) ? 28*msPerDay : 29*msPerDay );
488 }
489 function MakeDate( day, time ) {
490 if ( day == Number.POSITIVE_INFINITY ||
491 day == Number.NEGATIVE_INFINITY ) {
492 return Number.NaN;
493 }
494 if ( time == Number.POSITIVE_INFINITY ||
495 time == Number.NEGATIVE_INFINITY ) {
496 return Number.NaN;
497 }
498 return ( day * msPerDay ) + time;
499 }
500 function TimeClip( t ) {
501 if ( isNaN( t ) ) {
502 return ( Number.NaN );
503 }
504 if ( Math.abs( t ) > 8.64e15 ) {
505 return ( Number.NaN );
506 }
507
508 return ( ToInteger( t ) );
509 }
510 function ToInteger( t ) {
511 t = Number( t );
512
513 if ( isNaN( t ) ){
514 return ( Number.NaN );
515 }
516 if ( t == 0 || t == -0 ||
517 t == Number.POSITIVE_INFINITY || t == Number.NEGATIVE_INFINITY ) {
518 return 0;
519 }
520
521 var sign = ( t < 0 ) ? -1 : 1;
522
523 return ( sign * Math.floor( Math.abs( t ) ) );
524 }
525 function Enumerate ( o ) {
526 var p;
527 for ( p in o ) {
528 print( p +": " + o[p] );
529 }
530 }
531