Sat, 03 Jan 2015 20:18:00 +0100
Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.
michael@0 | 1 | var completed = false; |
michael@0 | 2 | var testcases; |
michael@0 | 3 | var tc = 0; |
michael@0 | 4 | |
michael@0 | 5 | SECTION = ""; |
michael@0 | 6 | VERSION = ""; |
michael@0 | 7 | BUGNUMBER = ""; |
michael@0 | 8 | EXCLUDE = ""; |
michael@0 | 9 | BUGNUMBER = ""; |
michael@0 | 10 | |
michael@0 | 11 | |
michael@0 | 12 | TZ_DIFF = -8; |
michael@0 | 13 | |
michael@0 | 14 | var TT = ""; |
michael@0 | 15 | var TT_ = ""; |
michael@0 | 16 | var BR = ""; |
michael@0 | 17 | var NBSP = " "; |
michael@0 | 18 | var CR = "\n"; |
michael@0 | 19 | var FONT = ""; |
michael@0 | 20 | var FONT_ = ""; |
michael@0 | 21 | var FONT_RED = ""; |
michael@0 | 22 | var FONT_GREEN = ""; |
michael@0 | 23 | var B = ""; |
michael@0 | 24 | var B_ = "" |
michael@0 | 25 | var H2 = ""; |
michael@0 | 26 | var H2_ = ""; |
michael@0 | 27 | var HR = ""; |
michael@0 | 28 | var DEBUG = false; |
michael@0 | 29 | |
michael@0 | 30 | |
michael@0 | 31 | var PASSED = " PASSED!" |
michael@0 | 32 | var FAILED = " FAILED! expected: "; |
michael@0 | 33 | function test() { |
michael@0 | 34 | for ( tc=0; tc < testcases.length; tc++ ) { |
michael@0 | 35 | testcases[tc].passed = writeTestCaseResult( |
michael@0 | 36 | testcases[tc].expect, |
michael@0 | 37 | testcases[tc].actual, |
michael@0 | 38 | testcases[tc].description +" = "+ |
michael@0 | 39 | testcases[tc].actual ); |
michael@0 | 40 | |
michael@0 | 41 | testcases[tc].reason += ( testcases[tc].passed ) ? "" : "wrong value "; |
michael@0 | 42 | } |
michael@0 | 43 | stopTest(); |
michael@0 | 44 | return ( testcases ); |
michael@0 | 45 | } |
michael@0 | 46 | |
michael@0 | 47 | function TestCase( n, d, e, a ) { |
michael@0 | 48 | this.name = n; |
michael@0 | 49 | this.description = d; |
michael@0 | 50 | this.expect = e; |
michael@0 | 51 | this.actual = a; |
michael@0 | 52 | this.passed = true; |
michael@0 | 53 | this.reason = ""; |
michael@0 | 54 | this.bugnumber = BUGNUMBER; |
michael@0 | 55 | |
michael@0 | 56 | this.passed = getTestCaseResult( this.expect, this.actual ); |
michael@0 | 57 | if ( DEBUG ) { |
michael@0 | 58 | print( "added " + this.description ); |
michael@0 | 59 | } |
michael@0 | 60 | } |
michael@0 | 61 | function startTest() { |
michael@0 | 62 | // JavaScript 1.3 is supposed to be compliant ecma version 1.0 |
michael@0 | 63 | if ( VERSION == "ECMA_1" ) { |
michael@0 | 64 | version ( "130" ); |
michael@0 | 65 | } |
michael@0 | 66 | if ( VERSION == "JS_13" ) { |
michael@0 | 67 | version ( "130" ); |
michael@0 | 68 | } |
michael@0 | 69 | if ( VERSION == "JS_12" ) { |
michael@0 | 70 | version ( "120" ); |
michael@0 | 71 | } |
michael@0 | 72 | if ( VERSION == "JS_11" ) { |
michael@0 | 73 | version ( "110" ); |
michael@0 | 74 | } |
michael@0 | 75 | // for ecma version 2.0, we will leave the javascript version to |
michael@0 | 76 | // the default ( for now ). |
michael@0 | 77 | writeHeaderToLog( SECTION + " "+ TITLE); |
michael@0 | 78 | testcases = new Array(); |
michael@0 | 79 | tc = 0; |
michael@0 | 80 | |
michael@0 | 81 | } |
michael@0 | 82 | function getTestCaseResult( expect, actual ) { |
michael@0 | 83 | // because ( NaN == NaN ) always returns false, need to do |
michael@0 | 84 | // a special compare to see if we got the right result. |
michael@0 | 85 | if ( actual != actual ) { |
michael@0 | 86 | if ( typeof actual == "object" ) { |
michael@0 | 87 | actual = "NaN object"; |
michael@0 | 88 | } else { |
michael@0 | 89 | actual = "NaN number"; |
michael@0 | 90 | } |
michael@0 | 91 | } |
michael@0 | 92 | if ( expect != expect ) { |
michael@0 | 93 | if ( typeof expect == "object" ) { |
michael@0 | 94 | expect = "NaN object"; |
michael@0 | 95 | } else { |
michael@0 | 96 | expect = "NaN number"; |
michael@0 | 97 | } |
michael@0 | 98 | } |
michael@0 | 99 | |
michael@0 | 100 | var passed = ( expect == actual ) ? true : false; |
michael@0 | 101 | |
michael@0 | 102 | // if both objects are numbers |
michael@0 | 103 | // need to replace w/ IEEE standard for rounding |
michael@0 | 104 | if ( !passed |
michael@0 | 105 | && typeof(actual) == "number" |
michael@0 | 106 | && typeof(expect) == "number" |
michael@0 | 107 | ) { |
michael@0 | 108 | if ( Math.abs(actual-expect) < 0.0000001 ) { |
michael@0 | 109 | passed = true; |
michael@0 | 110 | } |
michael@0 | 111 | } |
michael@0 | 112 | |
michael@0 | 113 | // verify type is the same |
michael@0 | 114 | if ( typeof(expect) != typeof(actual) ) { |
michael@0 | 115 | passed = false; |
michael@0 | 116 | } |
michael@0 | 117 | |
michael@0 | 118 | return passed; |
michael@0 | 119 | } |
michael@0 | 120 | function writeTestCaseResult( expect, actual, string ) { |
michael@0 | 121 | var passed = getTestCaseResult( expect, actual ); |
michael@0 | 122 | writeFormattedResult( expect, actual, string, passed ); |
michael@0 | 123 | return passed; |
michael@0 | 124 | } |
michael@0 | 125 | function writeFormattedResult( expect, actual, string, passed ) { |
michael@0 | 126 | var s = TT + string ; |
michael@0 | 127 | |
michael@0 | 128 | for ( k = 0; |
michael@0 | 129 | k < (60 - string.length >= 0 ? 60 - string.length : 5) ; |
michael@0 | 130 | k++ ) { |
michael@0 | 131 | } |
michael@0 | 132 | |
michael@0 | 133 | s += B ; |
michael@0 | 134 | s += ( passed ) ? FONT_GREEN + NBSP + PASSED : FONT_RED + NBSP + FAILED + expect + TT_ ; |
michael@0 | 135 | |
michael@0 | 136 | print( s + FONT_ + B_ + TT_ ); |
michael@0 | 137 | |
michael@0 | 138 | return passed; |
michael@0 | 139 | } |
michael@0 | 140 | |
michael@0 | 141 | function writeHeaderToLog( string ) { |
michael@0 | 142 | print( H2 + string + H2_ ); |
michael@0 | 143 | } |
michael@0 | 144 | function stopTest() |
michael@0 | 145 | { |
michael@0 | 146 | var sizeTag = "<#TEST CASES SIZE>"; |
michael@0 | 147 | var doneTag = "<#TEST CASES DONE>"; |
michael@0 | 148 | var beginTag = "<#TEST CASE "; |
michael@0 | 149 | var endTag = ">"; |
michael@0 | 150 | |
michael@0 | 151 | print(sizeTag); |
michael@0 | 152 | print(testcases.length); |
michael@0 | 153 | for (tc = 0; tc < testcases.length; tc++) |
michael@0 | 154 | { |
michael@0 | 155 | print(beginTag + 'PASSED' + endTag); |
michael@0 | 156 | print(testcases[tc].passed); |
michael@0 | 157 | print(beginTag + 'NAME' + endTag); |
michael@0 | 158 | print(testcases[tc].name); |
michael@0 | 159 | print(beginTag + 'EXPECTED' + endTag); |
michael@0 | 160 | print(testcases[tc].expect); |
michael@0 | 161 | print(beginTag + 'ACTUAL' + endTag); |
michael@0 | 162 | print(testcases[tc].actual); |
michael@0 | 163 | print(beginTag + 'DESCRIPTION' + endTag); |
michael@0 | 164 | print(testcases[tc].description); |
michael@0 | 165 | print(beginTag + 'REASON' + endTag); |
michael@0 | 166 | print(( testcases[tc].passed ) ? "" : "wrong value "); |
michael@0 | 167 | print(beginTag + 'BUGNUMBER' + endTag); |
michael@0 | 168 | print( BUGNUMBER ); |
michael@0 | 169 | } |
michael@0 | 170 | print(doneTag); |
michael@0 | 171 | print( HR ); |
michael@0 | 172 | gc(); |
michael@0 | 173 | } |
michael@0 | 174 | function getFailedCases() { |
michael@0 | 175 | for ( var i = 0; i < testcases.length; i++ ) { |
michael@0 | 176 | if ( ! testcases[i].passed ) { |
michael@0 | 177 | print( testcases[i].description +" = " +testcases[i].actual +" expected: "+ testcases[i].expect ); |
michael@0 | 178 | } |
michael@0 | 179 | } |
michael@0 | 180 | } |
michael@0 | 181 | function err( msg, page, line ) { |
michael@0 | 182 | testcases[tc].actual = "error"; |
michael@0 | 183 | testcases[tc].reason = msg; |
michael@0 | 184 | writeTestCaseResult( testcases[tc].expect, |
michael@0 | 185 | testcases[tc].actual, |
michael@0 | 186 | testcases[tc].description +" = "+ testcases[tc].actual + |
michael@0 | 187 | ": " + testcases[tc].reason ); |
michael@0 | 188 | stopTest(); |
michael@0 | 189 | return true; |
michael@0 | 190 | } |
michael@0 | 191 | |
michael@0 | 192 | /** |
michael@0 | 193 | * Type Conversion functions used by Type Conversion |
michael@0 | 194 | * |
michael@0 | 195 | */ |
michael@0 | 196 | |
michael@0 | 197 | |
michael@0 | 198 | |
michael@0 | 199 | /* |
michael@0 | 200 | * Date functions used by tests in Date suite |
michael@0 | 201 | * |
michael@0 | 202 | */ |
michael@0 | 203 | var msPerDay = 86400000; |
michael@0 | 204 | var HoursPerDay = 24; |
michael@0 | 205 | var MinutesPerHour = 60; |
michael@0 | 206 | var SecondsPerMinute = 60; |
michael@0 | 207 | var msPerSecond = 1000; |
michael@0 | 208 | var msPerMinute = 60000; // msPerSecond * SecondsPerMinute |
michael@0 | 209 | var msPerHour = 3600000; // msPerMinute * MinutesPerHour |
michael@0 | 210 | |
michael@0 | 211 | var TIME_1970 = 0; |
michael@0 | 212 | var TIME_2000 = 946684800000; |
michael@0 | 213 | var TIME_1900 = -2208988800000; |
michael@0 | 214 | |
michael@0 | 215 | function Day( t ) { |
michael@0 | 216 | return ( Math.floor(t/msPerDay ) ); |
michael@0 | 217 | } |
michael@0 | 218 | function DaysInYear( y ) { |
michael@0 | 219 | if ( y % 4 != 0 ) { |
michael@0 | 220 | return 365; |
michael@0 | 221 | } |
michael@0 | 222 | if ( (y % 4 == 0) && (y % 100 != 0) ) { |
michael@0 | 223 | return 366; |
michael@0 | 224 | } |
michael@0 | 225 | if ( (y % 100 == 0) && (y % 400 != 0) ) { |
michael@0 | 226 | return 365; |
michael@0 | 227 | } |
michael@0 | 228 | if ( (y % 400 == 0) ){ |
michael@0 | 229 | return 366; |
michael@0 | 230 | } else { |
michael@0 | 231 | return "ERROR: DaysInYear(" + y + ") case not covered"; |
michael@0 | 232 | } |
michael@0 | 233 | } |
michael@0 | 234 | function TimeInYear( y ) { |
michael@0 | 235 | return ( DaysInYear(y) * msPerDay ); |
michael@0 | 236 | } |
michael@0 | 237 | function DayNumber( t ) { |
michael@0 | 238 | return ( Math.floor( t / msPerDay ) ); |
michael@0 | 239 | } |
michael@0 | 240 | function TimeWithinDay( t ) { |
michael@0 | 241 | if ( t < 0 ) { |
michael@0 | 242 | return ( (t % msPerDay) + msPerDay ); |
michael@0 | 243 | } else { |
michael@0 | 244 | return ( t % msPerDay ); |
michael@0 | 245 | } |
michael@0 | 246 | } |
michael@0 | 247 | function YearNumber( t ) { |
michael@0 | 248 | } |
michael@0 | 249 | function TimeFromYear( y ) { |
michael@0 | 250 | return ( msPerDay * DayFromYear(y) ); |
michael@0 | 251 | } |
michael@0 | 252 | function DayFromYear( y ) { |
michael@0 | 253 | return ( 365*(y-1970) + |
michael@0 | 254 | Math.floor((y-1969)/4) - |
michael@0 | 255 | Math.floor((y-1901)/100) + |
michael@0 | 256 | Math.floor((y-1601)/400) ); |
michael@0 | 257 | } |
michael@0 | 258 | function InLeapYear( t ) { |
michael@0 | 259 | if ( DaysInYear(YearFromTime(t)) == 365 ) { |
michael@0 | 260 | return 0; |
michael@0 | 261 | } |
michael@0 | 262 | if ( DaysInYear(YearFromTime(t)) == 366 ) { |
michael@0 | 263 | return 1; |
michael@0 | 264 | } else { |
michael@0 | 265 | return "ERROR: InLeapYear("+t+") case not covered"; |
michael@0 | 266 | } |
michael@0 | 267 | } |
michael@0 | 268 | function YearFromTime( t ) { |
michael@0 | 269 | t = Number( t ); |
michael@0 | 270 | var sign = ( t < 0 ) ? -1 : 1; |
michael@0 | 271 | var year = ( sign < 0 ) ? 1969 : 1970; |
michael@0 | 272 | for ( var timeToTimeZero = t; ; ) { |
michael@0 | 273 | // subtract the current year's time from the time that's left. |
michael@0 | 274 | timeToTimeZero -= sign * TimeInYear(year) |
michael@0 | 275 | |
michael@0 | 276 | // if there's less than the current year's worth of time left, then break. |
michael@0 | 277 | if ( sign < 0 ) { |
michael@0 | 278 | if ( sign * timeToTimeZero <= 0 ) { |
michael@0 | 279 | break; |
michael@0 | 280 | } else { |
michael@0 | 281 | year += sign; |
michael@0 | 282 | } |
michael@0 | 283 | } else { |
michael@0 | 284 | if ( sign * timeToTimeZero < 0 ) { |
michael@0 | 285 | break; |
michael@0 | 286 | } else { |
michael@0 | 287 | year += sign; |
michael@0 | 288 | } |
michael@0 | 289 | } |
michael@0 | 290 | } |
michael@0 | 291 | return ( year ); |
michael@0 | 292 | } |
michael@0 | 293 | function MonthFromTime( t ) { |
michael@0 | 294 | // i know i could use switch but i'd rather not until it's part of ECMA |
michael@0 | 295 | var day = DayWithinYear( t ); |
michael@0 | 296 | var leap = InLeapYear(t); |
michael@0 | 297 | |
michael@0 | 298 | if ( (0 <= day) && (day < 31) ) { |
michael@0 | 299 | return 0; |
michael@0 | 300 | } |
michael@0 | 301 | if ( (31 <= day) && (day < (59+leap)) ) { |
michael@0 | 302 | return 1; |
michael@0 | 303 | } |
michael@0 | 304 | if ( ((59+leap) <= day) && (day < (90+leap)) ) { |
michael@0 | 305 | return 2; |
michael@0 | 306 | } |
michael@0 | 307 | if ( ((90+leap) <= day) && (day < (120+leap)) ) { |
michael@0 | 308 | return 3; |
michael@0 | 309 | } |
michael@0 | 310 | if ( ((120+leap) <= day) && (day < (151+leap)) ) { |
michael@0 | 311 | return 4; |
michael@0 | 312 | } |
michael@0 | 313 | if ( ((151+leap) <= day) && (day < (181+leap)) ) { |
michael@0 | 314 | return 5; |
michael@0 | 315 | } |
michael@0 | 316 | if ( ((181+leap) <= day) && (day < (212+leap)) ) { |
michael@0 | 317 | return 6; |
michael@0 | 318 | } |
michael@0 | 319 | if ( ((212+leap) <= day) && (day < (243+leap)) ) { |
michael@0 | 320 | return 7; |
michael@0 | 321 | } |
michael@0 | 322 | if ( ((243+leap) <= day) && (day < (273+leap)) ) { |
michael@0 | 323 | return 8; |
michael@0 | 324 | } |
michael@0 | 325 | if ( ((273+leap) <= day) && (day < (304+leap)) ) { |
michael@0 | 326 | return 9; |
michael@0 | 327 | } |
michael@0 | 328 | if ( ((304+leap) <= day) && (day < (334+leap)) ) { |
michael@0 | 329 | return 10; |
michael@0 | 330 | } |
michael@0 | 331 | if ( ((334+leap) <= day) && (day < (365+leap)) ) { |
michael@0 | 332 | return 11; |
michael@0 | 333 | } else { |
michael@0 | 334 | return "ERROR: MonthFromTime("+t+") not known"; |
michael@0 | 335 | } |
michael@0 | 336 | } |
michael@0 | 337 | function DayWithinYear( t ) { |
michael@0 | 338 | return( Day(t) - DayFromYear(YearFromTime(t))); |
michael@0 | 339 | } |
michael@0 | 340 | function DateFromTime( t ) { |
michael@0 | 341 | var day = DayWithinYear(t); |
michael@0 | 342 | var month = MonthFromTime(t); |
michael@0 | 343 | |
michael@0 | 344 | if ( month == 0 ) { |
michael@0 | 345 | return ( day + 1 ); |
michael@0 | 346 | } |
michael@0 | 347 | if ( month == 1 ) { |
michael@0 | 348 | return ( day - 30 ); |
michael@0 | 349 | } |
michael@0 | 350 | if ( month == 2 ) { |
michael@0 | 351 | return ( day - 58 - InLeapYear(t) ); |
michael@0 | 352 | } |
michael@0 | 353 | if ( month == 3 ) { |
michael@0 | 354 | return ( day - 89 - InLeapYear(t)); |
michael@0 | 355 | } |
michael@0 | 356 | if ( month == 4 ) { |
michael@0 | 357 | return ( day - 119 - InLeapYear(t)); |
michael@0 | 358 | } |
michael@0 | 359 | if ( month == 5 ) { |
michael@0 | 360 | return ( day - 150- InLeapYear(t)); |
michael@0 | 361 | } |
michael@0 | 362 | if ( month == 6 ) { |
michael@0 | 363 | return ( day - 180- InLeapYear(t)); |
michael@0 | 364 | } |
michael@0 | 365 | if ( month == 7 ) { |
michael@0 | 366 | return ( day - 211- InLeapYear(t)); |
michael@0 | 367 | } |
michael@0 | 368 | if ( month == 8 ) { |
michael@0 | 369 | return ( day - 242- InLeapYear(t)); |
michael@0 | 370 | } |
michael@0 | 371 | if ( month == 9 ) { |
michael@0 | 372 | return ( day - 272- InLeapYear(t)); |
michael@0 | 373 | } |
michael@0 | 374 | if ( month == 10 ) { |
michael@0 | 375 | return ( day - 303- InLeapYear(t)); |
michael@0 | 376 | } |
michael@0 | 377 | if ( month == 11 ) { |
michael@0 | 378 | return ( day - 333- InLeapYear(t)); |
michael@0 | 379 | } |
michael@0 | 380 | |
michael@0 | 381 | return ("ERROR: DateFromTime("+t+") not known" ); |
michael@0 | 382 | } |
michael@0 | 383 | function WeekDay( t ) { |
michael@0 | 384 | var weekday = (Day(t)+4) % 7; |
michael@0 | 385 | return( weekday < 0 ? 7 + weekday : weekday ); |
michael@0 | 386 | } |
michael@0 | 387 | |
michael@0 | 388 | // missing daylight savins time adjustment |
michael@0 | 389 | |
michael@0 | 390 | function HourFromTime( t ) { |
michael@0 | 391 | var h = Math.floor( t / msPerHour ) % HoursPerDay; |
michael@0 | 392 | return ( (h<0) ? HoursPerDay + h : h ); |
michael@0 | 393 | } |
michael@0 | 394 | function MinFromTime( t ) { |
michael@0 | 395 | var min = Math.floor( t / msPerMinute ) % MinutesPerHour; |
michael@0 | 396 | return( ( min < 0 ) ? MinutesPerHour + min : min ); |
michael@0 | 397 | } |
michael@0 | 398 | function SecFromTime( t ) { |
michael@0 | 399 | var sec = Math.floor( t / msPerSecond ) % SecondsPerMinute; |
michael@0 | 400 | return ( (sec < 0 ) ? SecondsPerMinute + sec : sec ); |
michael@0 | 401 | } |
michael@0 | 402 | function msFromTime( t ) { |
michael@0 | 403 | var ms = t % msPerSecond; |
michael@0 | 404 | return ( (ms < 0 ) ? msPerSecond + ms : ms ); |
michael@0 | 405 | } |
michael@0 | 406 | function LocalTZA() { |
michael@0 | 407 | return ( TZ_DIFF * msPerHour ); |
michael@0 | 408 | } |
michael@0 | 409 | function UTC( t ) { |
michael@0 | 410 | return ( t - LocalTZA() - DaylightSavingTA(t - LocalTZA()) ); |
michael@0 | 411 | } |
michael@0 | 412 | function DaylightSavingTA( t ) { |
michael@0 | 413 | t = t - LocalTZA(); |
michael@0 | 414 | |
michael@0 | 415 | var dst_start = GetFirstSundayInApril(t) + 2*msPerHour; |
michael@0 | 416 | var dst_end = GetLastSundayInOctober(t)+ 2*msPerHour; |
michael@0 | 417 | |
michael@0 | 418 | if ( t >= dst_start && t < dst_end ) { |
michael@0 | 419 | return msPerHour; |
michael@0 | 420 | } else { |
michael@0 | 421 | return 0; |
michael@0 | 422 | } |
michael@0 | 423 | |
michael@0 | 424 | // Daylight Savings Time starts on the first Sunday in April at 2:00AM in |
michael@0 | 425 | // PST. Other time zones will need to override this function. |
michael@0 | 426 | |
michael@0 | 427 | print( new Date( UTC(dst_start + LocalTZA())) ); |
michael@0 | 428 | |
michael@0 | 429 | return UTC(dst_start + LocalTZA()); |
michael@0 | 430 | } |
michael@0 | 431 | function GetFirstSundayInApril( t ) { |
michael@0 | 432 | var year = YearFromTime(t); |
michael@0 | 433 | var leap = InLeapYear(t); |
michael@0 | 434 | |
michael@0 | 435 | var april = TimeFromYear(year) + TimeInMonth(0, leap) + TimeInMonth(1,leap) + |
michael@0 | 436 | TimeInMonth(2,leap); |
michael@0 | 437 | |
michael@0 | 438 | for ( var first_sunday = april; WeekDay(first_sunday) > 0; |
michael@0 | 439 | first_sunday += msPerDay ) |
michael@0 | 440 | { |
michael@0 | 441 | ; |
michael@0 | 442 | } |
michael@0 | 443 | |
michael@0 | 444 | return first_sunday; |
michael@0 | 445 | } |
michael@0 | 446 | function GetLastSundayInOctober( t ) { |
michael@0 | 447 | var year = YearFromTime(t); |
michael@0 | 448 | var leap = InLeapYear(t); |
michael@0 | 449 | |
michael@0 | 450 | for ( var oct = TimeFromYear(year), m = 0; m < 9; m++ ) { |
michael@0 | 451 | oct += TimeInMonth(m, leap); |
michael@0 | 452 | } |
michael@0 | 453 | for ( var last_sunday = oct + 30*msPerDay; WeekDay(last_sunday) > 0; |
michael@0 | 454 | last_sunday -= msPerDay ) |
michael@0 | 455 | { |
michael@0 | 456 | ; |
michael@0 | 457 | } |
michael@0 | 458 | return last_sunday; |
michael@0 | 459 | } |
michael@0 | 460 | function LocalTime( t ) { |
michael@0 | 461 | return ( t + LocalTZA() + DaylightSavingTA(t) ); |
michael@0 | 462 | } |
michael@0 | 463 | function MakeTime( hour, min, sec, ms ) { |
michael@0 | 464 | if ( isNaN( hour ) || isNaN( min ) || isNaN( sec ) || isNaN( ms ) ) { |
michael@0 | 465 | return Number.NaN; |
michael@0 | 466 | } |
michael@0 | 467 | |
michael@0 | 468 | hour = ToInteger(hour); |
michael@0 | 469 | min = ToInteger( min); |
michael@0 | 470 | sec = ToInteger( sec); |
michael@0 | 471 | ms = ToInteger( ms ); |
michael@0 | 472 | |
michael@0 | 473 | return( (hour*msPerHour) + (min*msPerMinute) + |
michael@0 | 474 | (sec*msPerSecond) + ms ); |
michael@0 | 475 | } |
michael@0 | 476 | function MakeDay( year, month, date ) { |
michael@0 | 477 | if ( isNaN(year) || isNaN(month) || isNaN(date) ) { |
michael@0 | 478 | return Number.NaN; |
michael@0 | 479 | } |
michael@0 | 480 | year = ToInteger(year); |
michael@0 | 481 | month = ToInteger(month); |
michael@0 | 482 | date = ToInteger(date ); |
michael@0 | 483 | |
michael@0 | 484 | var sign = ( year < 1970 ) ? -1 : 1; |
michael@0 | 485 | var t = ( year < 1970 ) ? 1 : 0; |
michael@0 | 486 | var y = ( year < 1970 ) ? 1969 : 1970; |
michael@0 | 487 | |
michael@0 | 488 | var result5 = year + Math.floor( month/12 ); |
michael@0 | 489 | var result6 = month % 12; |
michael@0 | 490 | |
michael@0 | 491 | if ( year < 1970 ) { |
michael@0 | 492 | for ( y = 1969; y >= year; y += sign ) { |
michael@0 | 493 | t += sign * TimeInYear(y); |
michael@0 | 494 | } |
michael@0 | 495 | } else { |
michael@0 | 496 | for ( y = 1970 ; y < year; y += sign ) { |
michael@0 | 497 | t += sign * TimeInYear(y); |
michael@0 | 498 | } |
michael@0 | 499 | } |
michael@0 | 500 | |
michael@0 | 501 | var leap = InLeapYear( t ); |
michael@0 | 502 | |
michael@0 | 503 | for ( var m = 0; m < month; m++ ) { |
michael@0 | 504 | t += TimeInMonth( m, leap ); |
michael@0 | 505 | } |
michael@0 | 506 | |
michael@0 | 507 | if ( YearFromTime(t) != result5 ) { |
michael@0 | 508 | return Number.NaN; |
michael@0 | 509 | } |
michael@0 | 510 | if ( MonthFromTime(t) != result6 ) { |
michael@0 | 511 | return Number.NaN; |
michael@0 | 512 | } |
michael@0 | 513 | if ( DateFromTime(t) != 1 ) { |
michael@0 | 514 | return Number.NaN; |
michael@0 | 515 | } |
michael@0 | 516 | |
michael@0 | 517 | return ( (Day(t)) + date - 1 ); |
michael@0 | 518 | } |
michael@0 | 519 | function TimeInMonth( month, leap ) { |
michael@0 | 520 | // september april june november |
michael@0 | 521 | // jan 0 feb 1 mar 2 apr 3 may 4 june 5 jul 6 |
michael@0 | 522 | // aug 7 sep 8 oct 9 nov 10 dec 11 |
michael@0 | 523 | |
michael@0 | 524 | if ( month == 3 || month == 5 || month == 8 || month == 10 ) { |
michael@0 | 525 | return ( 30*msPerDay ); |
michael@0 | 526 | } |
michael@0 | 527 | |
michael@0 | 528 | // all the rest |
michael@0 | 529 | if ( month == 0 || month == 2 || month == 4 || month == 6 || |
michael@0 | 530 | month == 7 || month == 9 || month == 11 ) { |
michael@0 | 531 | return ( 31*msPerDay ); |
michael@0 | 532 | } |
michael@0 | 533 | |
michael@0 | 534 | // save february |
michael@0 | 535 | return ( (leap == 0) ? 28*msPerDay : 29*msPerDay ); |
michael@0 | 536 | } |
michael@0 | 537 | function MakeDate( day, time ) { |
michael@0 | 538 | if ( day == Number.POSITIVE_INFINITY || |
michael@0 | 539 | day == Number.NEGATIVE_INFINITY || |
michael@0 | 540 | day == Number.NaN ) { |
michael@0 | 541 | return Number.NaN; |
michael@0 | 542 | } |
michael@0 | 543 | if ( time == Number.POSITIVE_INFINITY || |
michael@0 | 544 | time == Number.POSITIVE_INFINITY || |
michael@0 | 545 | day == Number.NaN) { |
michael@0 | 546 | return Number.NaN; |
michael@0 | 547 | } |
michael@0 | 548 | return ( day * msPerDay ) + time; |
michael@0 | 549 | } |
michael@0 | 550 | function TimeClip( t ) { |
michael@0 | 551 | if ( isNaN( t ) ) { |
michael@0 | 552 | return ( Number.NaN ); |
michael@0 | 553 | } |
michael@0 | 554 | if ( Math.abs( t ) > 8.64e15 ) { |
michael@0 | 555 | return ( Number.NaN ); |
michael@0 | 556 | } |
michael@0 | 557 | |
michael@0 | 558 | return ( ToInteger( t ) ); |
michael@0 | 559 | } |
michael@0 | 560 | function ToInteger( t ) { |
michael@0 | 561 | t = Number( t ); |
michael@0 | 562 | |
michael@0 | 563 | if ( isNaN( t ) ){ |
michael@0 | 564 | return ( Number.NaN ); |
michael@0 | 565 | } |
michael@0 | 566 | if ( t == 0 || t == -0 || |
michael@0 | 567 | t == Number.POSITIVE_INFINITY || t == Number.NEGATIVE_INFINITY ) { |
michael@0 | 568 | return 0; |
michael@0 | 569 | } |
michael@0 | 570 | |
michael@0 | 571 | var sign = ( t < 0 ) ? -1 : 1; |
michael@0 | 572 | |
michael@0 | 573 | return ( sign * Math.floor( Math.abs( t ) ) ); |
michael@0 | 574 | } |
michael@0 | 575 | function Enumerate ( o ) { |
michael@0 | 576 | var properties = new Array(); |
michael@0 | 577 | for ( p in o ) { |
michael@0 | 578 | properties[ properties.length ] = new Array( p, o[p] ); |
michael@0 | 579 | } |
michael@0 | 580 | return properties; |
michael@0 | 581 | } |
michael@0 | 582 | function AddTestCase( description, expect, actual ) { |
michael@0 | 583 | testcases[tc++] = new TestCase( SECTION, description, expect, actual ); |
michael@0 | 584 | } |
michael@0 | 585 | function getFailedCases() { |
michael@0 | 586 | for ( var i = 0; i < testcases.length; i++ ) { |
michael@0 | 587 | if ( ! testcases[i].passed ) { |
michael@0 | 588 | print( testcases[i].description +" = " +testcases[i].actual +" expected: "+ testcases[i].expect ); |
michael@0 | 589 | } |
michael@0 | 590 | } |
michael@0 | 591 | } |