js/src/tests/ecma/shell.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial