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 | /// Copyright (c) 2012 Ecma International. All rights reserved. |
michael@0 | 2 | /// Ecma International makes this code available under the terms and conditions set |
michael@0 | 3 | /// forth on http://hg.ecmascript.org/tests/test262/raw-file/tip/LICENSE (the |
michael@0 | 4 | /// "Use Terms"). Any redistribution of this code must retain the above |
michael@0 | 5 | /// copyright and this notice and otherwise comply with the Use Terms. |
michael@0 | 6 | |
michael@0 | 7 | //----------------------------------------------------------------------------- |
michael@0 | 8 | function compareArray(aExpected, aActual) { |
michael@0 | 9 | if (aActual.length != aExpected.length) { |
michael@0 | 10 | return false; |
michael@0 | 11 | } |
michael@0 | 12 | |
michael@0 | 13 | aExpected.sort(); |
michael@0 | 14 | aActual.sort(); |
michael@0 | 15 | |
michael@0 | 16 | var s; |
michael@0 | 17 | for (var i = 0; i < aExpected.length; i++) { |
michael@0 | 18 | if (aActual[i] !== aExpected[i]) { |
michael@0 | 19 | return false; |
michael@0 | 20 | } |
michael@0 | 21 | } |
michael@0 | 22 | return true; |
michael@0 | 23 | } |
michael@0 | 24 | |
michael@0 | 25 | //----------------------------------------------------------------------------- |
michael@0 | 26 | function arrayContains(arr, expected) { |
michael@0 | 27 | var found; |
michael@0 | 28 | for (var i = 0; i < expected.length; i++) { |
michael@0 | 29 | found = false; |
michael@0 | 30 | for (var j = 0; j < arr.length; j++) { |
michael@0 | 31 | if (expected[i] === arr[j]) { |
michael@0 | 32 | found = true; |
michael@0 | 33 | break; |
michael@0 | 34 | } |
michael@0 | 35 | } |
michael@0 | 36 | if (!found) { |
michael@0 | 37 | return false; |
michael@0 | 38 | } |
michael@0 | 39 | } |
michael@0 | 40 | return true; |
michael@0 | 41 | } |
michael@0 | 42 | |
michael@0 | 43 | //----------------------------------------------------------------------------- |
michael@0 | 44 | var supportsArrayIndexGettersOnArrays = undefined; |
michael@0 | 45 | function fnSupportsArrayIndexGettersOnArrays() { |
michael@0 | 46 | if (typeof supportsArrayIndexGettersOnArrays !== "undefined") { |
michael@0 | 47 | return supportsArrayIndexGettersOnArrays; |
michael@0 | 48 | } |
michael@0 | 49 | |
michael@0 | 50 | supportsArrayIndexGettersOnArrays = false; |
michael@0 | 51 | |
michael@0 | 52 | if (fnExists(Object.defineProperty)) { |
michael@0 | 53 | var arr = []; |
michael@0 | 54 | Object.defineProperty(arr, "0", { |
michael@0 | 55 | get: function() { |
michael@0 | 56 | supportsArrayIndexGettersOnArrays = true; |
michael@0 | 57 | return 0; |
michael@0 | 58 | } |
michael@0 | 59 | }); |
michael@0 | 60 | var res = arr[0]; |
michael@0 | 61 | } |
michael@0 | 62 | |
michael@0 | 63 | return supportsArrayIndexGettersOnArrays; |
michael@0 | 64 | } |
michael@0 | 65 | |
michael@0 | 66 | //----------------------------------------------------------------------------- |
michael@0 | 67 | var supportsArrayIndexGettersOnObjects = undefined; |
michael@0 | 68 | function fnSupportsArrayIndexGettersOnObjects() { |
michael@0 | 69 | if (typeof supportsArrayIndexGettersOnObjects !== "undefined") |
michael@0 | 70 | return supportsArrayIndexGettersOnObjects; |
michael@0 | 71 | |
michael@0 | 72 | supportsArrayIndexGettersOnObjects = false; |
michael@0 | 73 | |
michael@0 | 74 | if (fnExists(Object.defineProperty)) { |
michael@0 | 75 | var obj = {}; |
michael@0 | 76 | Object.defineProperty(obj, "0", { |
michael@0 | 77 | get: function() { |
michael@0 | 78 | supportsArrayIndexGettersOnObjects = true; |
michael@0 | 79 | return 0; |
michael@0 | 80 | } |
michael@0 | 81 | }); |
michael@0 | 82 | var res = obj[0]; |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | return supportsArrayIndexGettersOnObjects; |
michael@0 | 86 | } |
michael@0 | 87 | |
michael@0 | 88 | //----------------------------------------------------------------------------- |
michael@0 | 89 | function ConvertToFileUrl(pathStr) { |
michael@0 | 90 | return "file:" + pathStr.replace(/\\/g, "/"); |
michael@0 | 91 | } |
michael@0 | 92 | |
michael@0 | 93 | //----------------------------------------------------------------------------- |
michael@0 | 94 | function fnExists(/*arguments*/) { |
michael@0 | 95 | for (var i = 0; i < arguments.length; i++) { |
michael@0 | 96 | if (typeof (arguments[i]) !== "function") return false; |
michael@0 | 97 | } |
michael@0 | 98 | return true; |
michael@0 | 99 | } |
michael@0 | 100 | |
michael@0 | 101 | //----------------------------------------------------------------------------- |
michael@0 | 102 | var __globalObject = Function("return this;")(); |
michael@0 | 103 | function fnGlobalObject() { |
michael@0 | 104 | return __globalObject; |
michael@0 | 105 | } |
michael@0 | 106 | |
michael@0 | 107 | //----------------------------------------------------------------------------- |
michael@0 | 108 | function fnSupportsStrict() { |
michael@0 | 109 | "use strict"; |
michael@0 | 110 | try { |
michael@0 | 111 | eval('with ({}) {}'); |
michael@0 | 112 | return false; |
michael@0 | 113 | } catch (e) { |
michael@0 | 114 | return true; |
michael@0 | 115 | } |
michael@0 | 116 | } |
michael@0 | 117 | |
michael@0 | 118 | //----------------------------------------------------------------------------- |
michael@0 | 119 | //Verify all attributes specified data property of given object: |
michael@0 | 120 | //value, writable, enumerable, configurable |
michael@0 | 121 | //If all attribute values are expected, return true, otherwise, return false |
michael@0 | 122 | function dataPropertyAttributesAreCorrect(obj, |
michael@0 | 123 | name, |
michael@0 | 124 | value, |
michael@0 | 125 | writable, |
michael@0 | 126 | enumerable, |
michael@0 | 127 | configurable) { |
michael@0 | 128 | var attributesCorrect = true; |
michael@0 | 129 | |
michael@0 | 130 | if (obj[name] !== value) { |
michael@0 | 131 | if (typeof obj[name] === "number" && |
michael@0 | 132 | isNaN(obj[name]) && |
michael@0 | 133 | typeof value === "number" && |
michael@0 | 134 | isNaN(value)) { |
michael@0 | 135 | // keep empty |
michael@0 | 136 | } else { |
michael@0 | 137 | attributesCorrect = false; |
michael@0 | 138 | } |
michael@0 | 139 | } |
michael@0 | 140 | |
michael@0 | 141 | try { |
michael@0 | 142 | if (obj[name] === "oldValue") { |
michael@0 | 143 | obj[name] = "newValue"; |
michael@0 | 144 | } else { |
michael@0 | 145 | obj[name] = "OldValue"; |
michael@0 | 146 | } |
michael@0 | 147 | } catch (we) { |
michael@0 | 148 | } |
michael@0 | 149 | |
michael@0 | 150 | var overwrited = false; |
michael@0 | 151 | if (obj[name] !== value) { |
michael@0 | 152 | if (typeof obj[name] === "number" && |
michael@0 | 153 | isNaN(obj[name]) && |
michael@0 | 154 | typeof value === "number" && |
michael@0 | 155 | isNaN(value)) { |
michael@0 | 156 | // keep empty |
michael@0 | 157 | } else { |
michael@0 | 158 | overwrited = true; |
michael@0 | 159 | } |
michael@0 | 160 | } |
michael@0 | 161 | if (overwrited !== writable) { |
michael@0 | 162 | attributesCorrect = false; |
michael@0 | 163 | } |
michael@0 | 164 | |
michael@0 | 165 | var enumerated = false; |
michael@0 | 166 | for (var prop in obj) { |
michael@0 | 167 | if (obj.hasOwnProperty(prop) && prop === name) { |
michael@0 | 168 | enumerated = true; |
michael@0 | 169 | } |
michael@0 | 170 | } |
michael@0 | 171 | |
michael@0 | 172 | if (enumerated !== enumerable) { |
michael@0 | 173 | attributesCorrect = false; |
michael@0 | 174 | } |
michael@0 | 175 | |
michael@0 | 176 | |
michael@0 | 177 | var deleted = false; |
michael@0 | 178 | |
michael@0 | 179 | try { |
michael@0 | 180 | delete obj[name]; |
michael@0 | 181 | } catch (de) { |
michael@0 | 182 | } |
michael@0 | 183 | if (!obj.hasOwnProperty(name)) { |
michael@0 | 184 | deleted = true; |
michael@0 | 185 | } |
michael@0 | 186 | if (deleted !== configurable) { |
michael@0 | 187 | attributesCorrect = false; |
michael@0 | 188 | } |
michael@0 | 189 | |
michael@0 | 190 | return attributesCorrect; |
michael@0 | 191 | } |
michael@0 | 192 | |
michael@0 | 193 | //----------------------------------------------------------------------------- |
michael@0 | 194 | //Verify all attributes specified accessor property of given object: |
michael@0 | 195 | //get, set, enumerable, configurable |
michael@0 | 196 | //If all attribute values are expected, return true, otherwise, return false |
michael@0 | 197 | function accessorPropertyAttributesAreCorrect(obj, |
michael@0 | 198 | name, |
michael@0 | 199 | get, |
michael@0 | 200 | set, |
michael@0 | 201 | setVerifyHelpProp, |
michael@0 | 202 | enumerable, |
michael@0 | 203 | configurable) { |
michael@0 | 204 | var attributesCorrect = true; |
michael@0 | 205 | |
michael@0 | 206 | if (get !== undefined) { |
michael@0 | 207 | if (obj[name] !== get()) { |
michael@0 | 208 | if (typeof obj[name] === "number" && |
michael@0 | 209 | isNaN(obj[name]) && |
michael@0 | 210 | typeof get() === "number" && |
michael@0 | 211 | isNaN(get())) { |
michael@0 | 212 | // keep empty |
michael@0 | 213 | } else { |
michael@0 | 214 | attributesCorrect = false; |
michael@0 | 215 | } |
michael@0 | 216 | } |
michael@0 | 217 | } else { |
michael@0 | 218 | if (obj[name] !== undefined) { |
michael@0 | 219 | attributesCorrect = false; |
michael@0 | 220 | } |
michael@0 | 221 | } |
michael@0 | 222 | |
michael@0 | 223 | try { |
michael@0 | 224 | var desc = Object.getOwnPropertyDescriptor(obj, name); |
michael@0 | 225 | if (typeof desc.set === "undefined") { |
michael@0 | 226 | if (typeof set !== "undefined") { |
michael@0 | 227 | attributesCorrect = false; |
michael@0 | 228 | } |
michael@0 | 229 | } else { |
michael@0 | 230 | obj[name] = "toBeSetValue"; |
michael@0 | 231 | if (obj[setVerifyHelpProp] !== "toBeSetValue") { |
michael@0 | 232 | attributesCorrect = false; |
michael@0 | 233 | } |
michael@0 | 234 | } |
michael@0 | 235 | } catch (se) { |
michael@0 | 236 | throw se; |
michael@0 | 237 | } |
michael@0 | 238 | |
michael@0 | 239 | |
michael@0 | 240 | var enumerated = false; |
michael@0 | 241 | for (var prop in obj) { |
michael@0 | 242 | if (obj.hasOwnProperty(prop) && prop === name) { |
michael@0 | 243 | enumerated = true; |
michael@0 | 244 | } |
michael@0 | 245 | } |
michael@0 | 246 | |
michael@0 | 247 | if (enumerated !== enumerable) { |
michael@0 | 248 | attributesCorrect = false; |
michael@0 | 249 | } |
michael@0 | 250 | |
michael@0 | 251 | |
michael@0 | 252 | var deleted = false; |
michael@0 | 253 | try { |
michael@0 | 254 | delete obj[name]; |
michael@0 | 255 | } catch (de) { |
michael@0 | 256 | throw de; |
michael@0 | 257 | } |
michael@0 | 258 | if (!obj.hasOwnProperty(name)) { |
michael@0 | 259 | deleted = true; |
michael@0 | 260 | } |
michael@0 | 261 | if (deleted !== configurable) { |
michael@0 | 262 | attributesCorrect = false; |
michael@0 | 263 | } |
michael@0 | 264 | |
michael@0 | 265 | return attributesCorrect; |
michael@0 | 266 | } |
michael@0 | 267 | |
michael@0 | 268 | //----------------------------------------------------------------------------- |
michael@0 | 269 | var NotEarlyErrorString = "NotEarlyError"; |
michael@0 | 270 | var EarlyErrorRePat = "^((?!" + NotEarlyErrorString + ").)*$"; |
michael@0 | 271 | var NotEarlyError = new Error(NotEarlyErrorString); |
michael@0 | 272 | |
michael@0 | 273 | //----------------------------------------------------------------------------- |
michael@0 | 274 | // Copyright 2009 the Sputnik authors. All rights reserved. |
michael@0 | 275 | // This code is governed by the BSD license found in the LICENSE file. |
michael@0 | 276 | |
michael@0 | 277 | function Test262Error(message) { |
michael@0 | 278 | if (message) this.message = message; |
michael@0 | 279 | } |
michael@0 | 280 | |
michael@0 | 281 | Test262Error.prototype.toString = function () { |
michael@0 | 282 | return "Test262 Error: " + this.message; |
michael@0 | 283 | }; |
michael@0 | 284 | |
michael@0 | 285 | function testFailed(message) { |
michael@0 | 286 | throw new Test262Error(message); |
michael@0 | 287 | } |
michael@0 | 288 | |
michael@0 | 289 | |
michael@0 | 290 | function testPrint(message) { |
michael@0 | 291 | |
michael@0 | 292 | } |
michael@0 | 293 | |
michael@0 | 294 | |
michael@0 | 295 | //adaptors for Test262 framework |
michael@0 | 296 | function $PRINT(message) { |
michael@0 | 297 | |
michael@0 | 298 | } |
michael@0 | 299 | |
michael@0 | 300 | function $INCLUDE(message) { } |
michael@0 | 301 | function $ERROR(message) { |
michael@0 | 302 | testFailed(message); |
michael@0 | 303 | } |
michael@0 | 304 | |
michael@0 | 305 | function $FAIL(message) { |
michael@0 | 306 | testFailed(message); |
michael@0 | 307 | } |
michael@0 | 308 | |
michael@0 | 309 | |
michael@0 | 310 | |
michael@0 | 311 | //Sputnik library definitions |
michael@0 | 312 | //Ultimately these should be namespaced some how and only made |
michael@0 | 313 | //available to tests that explicitly include them. |
michael@0 | 314 | //For now, we just define the globally |
michael@0 | 315 | |
michael@0 | 316 | //math_precision.js |
michael@0 | 317 | // Copyright 2009 the Sputnik authors. All rights reserved. |
michael@0 | 318 | // This code is governed by the BSD license found in the LICENSE file. |
michael@0 | 319 | |
michael@0 | 320 | function getPrecision(num) { |
michael@0 | 321 | //TODO: Create a table of prec's, |
michael@0 | 322 | // because using Math for testing Math isn't that correct. |
michael@0 | 323 | |
michael@0 | 324 | var log2num = Math.log(Math.abs(num)) / Math.LN2; |
michael@0 | 325 | var pernum = Math.ceil(log2num); |
michael@0 | 326 | return (2 * Math.pow(2, -52 + pernum)); |
michael@0 | 327 | //return(0); |
michael@0 | 328 | } |
michael@0 | 329 | |
michael@0 | 330 | |
michael@0 | 331 | //math_isequal.js |
michael@0 | 332 | // Copyright 2009 the Sputnik authors. All rights reserved. |
michael@0 | 333 | // This code is governed by the BSD license found in the LICENSE file. |
michael@0 | 334 | |
michael@0 | 335 | var prec; |
michael@0 | 336 | function isEqual(num1, num2) { |
michael@0 | 337 | if ((num1 === Infinity) && (num2 === Infinity)) { |
michael@0 | 338 | return (true); |
michael@0 | 339 | } |
michael@0 | 340 | if ((num1 === -Infinity) && (num2 === -Infinity)) { |
michael@0 | 341 | return (true); |
michael@0 | 342 | } |
michael@0 | 343 | prec = getPrecision(Math.min(Math.abs(num1), Math.abs(num2))); |
michael@0 | 344 | return (Math.abs(num1 - num2) <= prec); |
michael@0 | 345 | //return(num1 === num2); |
michael@0 | 346 | } |
michael@0 | 347 | |
michael@0 | 348 | //numeric_conversion.js |
michael@0 | 349 | // Copyright 2009 the Sputnik authors. All rights reserved. |
michael@0 | 350 | // This code is governed by the BSD license found in the LICENSE file. |
michael@0 | 351 | |
michael@0 | 352 | function ToInteger(p) { |
michael@0 | 353 | var x = Number(p); |
michael@0 | 354 | |
michael@0 | 355 | if (isNaN(x)) { |
michael@0 | 356 | return +0; |
michael@0 | 357 | } |
michael@0 | 358 | |
michael@0 | 359 | if ((x === +0) |
michael@0 | 360 | || (x === -0) |
michael@0 | 361 | || (x === Number.POSITIVE_INFINITY) |
michael@0 | 362 | || (x === Number.NEGATIVE_INFINITY)) { |
michael@0 | 363 | return x; |
michael@0 | 364 | } |
michael@0 | 365 | |
michael@0 | 366 | var sign = (x < 0) ? -1 : 1; |
michael@0 | 367 | |
michael@0 | 368 | return (sign * Math.floor(Math.abs(x))); |
michael@0 | 369 | } |
michael@0 | 370 | |
michael@0 | 371 | //Date_constants.js |
michael@0 | 372 | // Copyright 2009 the Sputnik authors. All rights reserved. |
michael@0 | 373 | // This code is governed by the BSD license found in the LICENSE file. |
michael@0 | 374 | |
michael@0 | 375 | var HoursPerDay = 24; |
michael@0 | 376 | var MinutesPerHour = 60; |
michael@0 | 377 | var SecondsPerMinute = 60; |
michael@0 | 378 | |
michael@0 | 379 | var msPerDay = 86400000; |
michael@0 | 380 | var msPerSecond = 1000; |
michael@0 | 381 | var msPerMinute = 60000; |
michael@0 | 382 | var msPerHour = 3600000; |
michael@0 | 383 | |
michael@0 | 384 | var date_1899_end = -2208988800001; |
michael@0 | 385 | var date_1900_start = -2208988800000; |
michael@0 | 386 | var date_1969_end = -1; |
michael@0 | 387 | var date_1970_start = 0; |
michael@0 | 388 | var date_1999_end = 946684799999; |
michael@0 | 389 | var date_2000_start = 946684800000; |
michael@0 | 390 | var date_2099_end = 4102444799999; |
michael@0 | 391 | var date_2100_start = 4102444800000; |
michael@0 | 392 | |
michael@0 | 393 | // Copyright 2009 the Sputnik authors. All rights reserved. |
michael@0 | 394 | // This code is governed by the BSD license found in the LICENSE file. |
michael@0 | 395 | |
michael@0 | 396 | //the following values are normally generated by the sputnik.py driver |
michael@0 | 397 | var $LocalTZ, |
michael@0 | 398 | $DST_start_month, |
michael@0 | 399 | $DST_start_sunday, |
michael@0 | 400 | $DST_start_hour, |
michael@0 | 401 | $DST_start_minutes, |
michael@0 | 402 | $DST_end_month, |
michael@0 | 403 | $DST_end_sunday, |
michael@0 | 404 | $DST_end_hour, |
michael@0 | 405 | $DST_end_minutes; |
michael@0 | 406 | |
michael@0 | 407 | (function () { |
michael@0 | 408 | /** |
michael@0 | 409 | * Finds the first date, starting from |start|, where |predicate| |
michael@0 | 410 | * holds. |
michael@0 | 411 | */ |
michael@0 | 412 | var findNearestDateBefore = function(start, predicate) { |
michael@0 | 413 | var current = start; |
michael@0 | 414 | var month = 1000 * 60 * 60 * 24 * 30; |
michael@0 | 415 | for (var step = month; step > 0; step = Math.floor(step / 3)) { |
michael@0 | 416 | if (!predicate(current)) { |
michael@0 | 417 | while (!predicate(current)) |
michael@0 | 418 | current = new Date(current.getTime() + step); |
michael@0 | 419 | current = new Date(current.getTime() - step); |
michael@0 | 420 | } |
michael@0 | 421 | } |
michael@0 | 422 | while (!predicate(current)) { |
michael@0 | 423 | current = new Date(current.getTime() + 1); |
michael@0 | 424 | } |
michael@0 | 425 | return current; |
michael@0 | 426 | }; |
michael@0 | 427 | |
michael@0 | 428 | var juneDate = new Date(2000, 5, 20, 0, 0, 0, 0); |
michael@0 | 429 | var decemberDate = new Date(2000, 11, 20, 0, 0, 0, 0); |
michael@0 | 430 | var juneOffset = juneDate.getTimezoneOffset(); |
michael@0 | 431 | var decemberOffset = decemberDate.getTimezoneOffset(); |
michael@0 | 432 | var isSouthernHemisphere = (juneOffset > decemberOffset); |
michael@0 | 433 | var winterTime = isSouthernHemisphere ? juneDate : decemberDate; |
michael@0 | 434 | var summerTime = isSouthernHemisphere ? decemberDate : juneDate; |
michael@0 | 435 | |
michael@0 | 436 | var dstStart = findNearestDateBefore(winterTime, function (date) { |
michael@0 | 437 | return date.getTimezoneOffset() == summerTime.getTimezoneOffset(); |
michael@0 | 438 | }); |
michael@0 | 439 | $DST_start_month = dstStart.getMonth(); |
michael@0 | 440 | $DST_start_sunday = dstStart.getDate() > 15 ? '"last"' : '"first"'; |
michael@0 | 441 | $DST_start_hour = dstStart.getHours(); |
michael@0 | 442 | $DST_start_minutes = dstStart.getMinutes(); |
michael@0 | 443 | |
michael@0 | 444 | var dstEnd = findNearestDateBefore(summerTime, function (date) { |
michael@0 | 445 | return date.getTimezoneOffset() == winterTime.getTimezoneOffset(); |
michael@0 | 446 | }); |
michael@0 | 447 | $DST_end_month = dstEnd.getMonth(); |
michael@0 | 448 | $DST_end_sunday = dstEnd.getDate() > 15 ? '"last"' : '"first"'; |
michael@0 | 449 | $DST_end_hour = dstEnd.getHours(); |
michael@0 | 450 | $DST_end_minutes = dstEnd.getMinutes(); |
michael@0 | 451 | |
michael@0 | 452 | return; |
michael@0 | 453 | })(); |
michael@0 | 454 | |
michael@0 | 455 | |
michael@0 | 456 | //Date.library.js |
michael@0 | 457 | // Copyright 2009 the Sputnik authors. All rights reserved. |
michael@0 | 458 | // This code is governed by the BSD license found in the LICENSE file. |
michael@0 | 459 | |
michael@0 | 460 | //15.9.1.2 Day Number and Time within Day |
michael@0 | 461 | function Day(t) { |
michael@0 | 462 | return Math.floor(t/msPerDay); |
michael@0 | 463 | } |
michael@0 | 464 | |
michael@0 | 465 | function TimeWithinDay(t) { |
michael@0 | 466 | return t%msPerDay; |
michael@0 | 467 | } |
michael@0 | 468 | |
michael@0 | 469 | //15.9.1.3 Year Number |
michael@0 | 470 | function DaysInYear(y){ |
michael@0 | 471 | if(y%4 != 0) return 365; |
michael@0 | 472 | if(y%4 == 0 && y%100 != 0) return 366; |
michael@0 | 473 | if(y%100 == 0 && y%400 != 0) return 365; |
michael@0 | 474 | if(y%400 == 0) return 366; |
michael@0 | 475 | } |
michael@0 | 476 | |
michael@0 | 477 | function DayFromYear(y) { |
michael@0 | 478 | return (365*(y-1970) |
michael@0 | 479 | + Math.floor((y-1969)/4) |
michael@0 | 480 | - Math.floor((y-1901)/100) |
michael@0 | 481 | + Math.floor((y-1601)/400)); |
michael@0 | 482 | } |
michael@0 | 483 | |
michael@0 | 484 | function TimeFromYear(y){ |
michael@0 | 485 | return msPerDay*DayFromYear(y); |
michael@0 | 486 | } |
michael@0 | 487 | |
michael@0 | 488 | function YearFromTime(t) { |
michael@0 | 489 | t = Number(t); |
michael@0 | 490 | var sign = ( t < 0 ) ? -1 : 1; |
michael@0 | 491 | var year = ( sign < 0 ) ? 1969 : 1970; |
michael@0 | 492 | |
michael@0 | 493 | for(var time = 0;;year += sign){ |
michael@0 | 494 | time = TimeFromYear(year); |
michael@0 | 495 | |
michael@0 | 496 | if(sign > 0 && time > t){ |
michael@0 | 497 | year -= sign; |
michael@0 | 498 | break; |
michael@0 | 499 | } |
michael@0 | 500 | else if(sign < 0 && time <= t){ |
michael@0 | 501 | break; |
michael@0 | 502 | } |
michael@0 | 503 | }; |
michael@0 | 504 | return year; |
michael@0 | 505 | } |
michael@0 | 506 | |
michael@0 | 507 | function InLeapYear(t){ |
michael@0 | 508 | if(DaysInYear(YearFromTime(t)) == 365) |
michael@0 | 509 | return 0; |
michael@0 | 510 | |
michael@0 | 511 | if(DaysInYear(YearFromTime(t)) == 366) |
michael@0 | 512 | return 1; |
michael@0 | 513 | } |
michael@0 | 514 | |
michael@0 | 515 | function DayWithinYear(t) { |
michael@0 | 516 | return Day(t)-DayFromYear(YearFromTime(t)); |
michael@0 | 517 | } |
michael@0 | 518 | |
michael@0 | 519 | //15.9.1.4 Month Number |
michael@0 | 520 | function MonthFromTime(t){ |
michael@0 | 521 | var day = DayWithinYear(t); |
michael@0 | 522 | var leap = InLeapYear(t); |
michael@0 | 523 | |
michael@0 | 524 | if((0 <= day) && (day < 31)) return 0; |
michael@0 | 525 | if((31 <= day) && (day < (59+leap))) return 1; |
michael@0 | 526 | if(((59+leap) <= day) && (day < (90+leap))) return 2; |
michael@0 | 527 | if(((90+leap) <= day) && (day < (120+leap))) return 3; |
michael@0 | 528 | if(((120+leap) <= day) && (day < (151+leap))) return 4; |
michael@0 | 529 | if(((151+leap) <= day) && (day < (181+leap))) return 5; |
michael@0 | 530 | if(((181+leap) <= day) && (day < (212+leap))) return 6; |
michael@0 | 531 | if(((212+leap) <= day) && (day < (243+leap))) return 7; |
michael@0 | 532 | if(((243+leap) <= day) && (day < (273+leap))) return 8; |
michael@0 | 533 | if(((273+leap) <= day) && (day < (304+leap))) return 9; |
michael@0 | 534 | if(((304+leap) <= day) && (day < (334+leap))) return 10; |
michael@0 | 535 | if(((334+leap) <= day) && (day < (365+leap))) return 11; |
michael@0 | 536 | } |
michael@0 | 537 | |
michael@0 | 538 | //15.9.1.5 Date Number |
michael@0 | 539 | function DateFromTime(t) { |
michael@0 | 540 | var day = DayWithinYear(t); |
michael@0 | 541 | var month = MonthFromTime(t); |
michael@0 | 542 | var leap = InLeapYear(t); |
michael@0 | 543 | |
michael@0 | 544 | if(month == 0) return day+1; |
michael@0 | 545 | if(month == 1) return day-30; |
michael@0 | 546 | if(month == 2) return day-58-leap; |
michael@0 | 547 | if(month == 3) return day-89-leap; |
michael@0 | 548 | if(month == 4) return day-119-leap; |
michael@0 | 549 | if(month == 5) return day-150-leap; |
michael@0 | 550 | if(month == 6) return day-180-leap; |
michael@0 | 551 | if(month == 7) return day-211-leap; |
michael@0 | 552 | if(month == 8) return day-242-leap; |
michael@0 | 553 | if(month == 9) return day-272-leap; |
michael@0 | 554 | if(month == 10) return day-303-leap; |
michael@0 | 555 | if(month == 11) return day-333-leap; |
michael@0 | 556 | } |
michael@0 | 557 | |
michael@0 | 558 | //15.9.1.6 Week Day |
michael@0 | 559 | function WeekDay(t) { |
michael@0 | 560 | var weekday = (Day(t)+4)%7; |
michael@0 | 561 | return (weekday < 0 ? 7+weekday : weekday); |
michael@0 | 562 | } |
michael@0 | 563 | |
michael@0 | 564 | //15.9.1.9 Daylight Saving Time Adjustment |
michael@0 | 565 | $LocalTZ = (new Date()).getTimezoneOffset() / -60; |
michael@0 | 566 | if (DaylightSavingTA((new Date()).valueOf()) !== 0) { |
michael@0 | 567 | $LocalTZ -= 1; |
michael@0 | 568 | } |
michael@0 | 569 | var LocalTZA = $LocalTZ*msPerHour; |
michael@0 | 570 | |
michael@0 | 571 | function DaysInMonth(m, leap) { |
michael@0 | 572 | m = m%12; |
michael@0 | 573 | |
michael@0 | 574 | //April, June, Sept, Nov |
michael@0 | 575 | if(m == 3 || m == 5 || m == 8 || m == 10 ) { |
michael@0 | 576 | return 30; |
michael@0 | 577 | } |
michael@0 | 578 | |
michael@0 | 579 | //Jan, March, May, July, Aug, Oct, Dec |
michael@0 | 580 | if(m == 0 || m == 2 || m == 4 || m == 6 || m == 7 || m == 9 || m == 11){ |
michael@0 | 581 | return 31; |
michael@0 | 582 | } |
michael@0 | 583 | |
michael@0 | 584 | //Feb |
michael@0 | 585 | return 28+leap; |
michael@0 | 586 | } |
michael@0 | 587 | |
michael@0 | 588 | function GetSundayInMonth(t, m, count){ |
michael@0 | 589 | var year = YearFromTime(t); |
michael@0 | 590 | var tempDate; |
michael@0 | 591 | |
michael@0 | 592 | if (count==='"first"') { |
michael@0 | 593 | for (var d=1; d <= DaysInMonth(m, InLeapYear(t)); d++) { |
michael@0 | 594 | tempDate = new Date(year, m, d); |
michael@0 | 595 | if (tempDate.getDay()===0) { |
michael@0 | 596 | return tempDate.valueOf(); |
michael@0 | 597 | } |
michael@0 | 598 | } |
michael@0 | 599 | } else if(count==='"last"') { |
michael@0 | 600 | for (var d=DaysInMonth(m, InLeapYear(t)); d>0; d--) { |
michael@0 | 601 | tempDate = new Date(year, m, d); |
michael@0 | 602 | if (tempDate.getDay()===0) { |
michael@0 | 603 | return tempDate.valueOf(); |
michael@0 | 604 | } |
michael@0 | 605 | } |
michael@0 | 606 | } |
michael@0 | 607 | throw new Error("Unsupported 'count' arg:" + count); |
michael@0 | 608 | } |
michael@0 | 609 | /* |
michael@0 | 610 | function GetSundayInMonth(t, m, count){ |
michael@0 | 611 | var year = YearFromTime(t); |
michael@0 | 612 | var leap = InLeapYear(t); |
michael@0 | 613 | var day = 0; |
michael@0 | 614 | |
michael@0 | 615 | if(m >= 1) day += DaysInMonth(0, leap); |
michael@0 | 616 | if(m >= 2) day += DaysInMonth(1, leap); |
michael@0 | 617 | if(m >= 3) day += DaysInMonth(2, leap); |
michael@0 | 618 | if(m >= 4) day += DaysInMonth(3, leap); |
michael@0 | 619 | if(m >= 5) day += DaysInMonth(4, leap); |
michael@0 | 620 | if(m >= 6) day += DaysInMonth(5, leap); |
michael@0 | 621 | if(m >= 7) day += DaysInMonth(6, leap); |
michael@0 | 622 | if(m >= 8) day += DaysInMonth(7, leap); |
michael@0 | 623 | if(m >= 9) day += DaysInMonth(8, leap); |
michael@0 | 624 | if(m >= 10) day += DaysInMonth(9, leap); |
michael@0 | 625 | if(m >= 11) day += DaysInMonth(10, leap); |
michael@0 | 626 | |
michael@0 | 627 | var month_start = TimeFromYear(year)+day*msPerDay; |
michael@0 | 628 | var sunday = 0; |
michael@0 | 629 | |
michael@0 | 630 | if(count === "last"){ |
michael@0 | 631 | for(var last_sunday = month_start+DaysInMonth(m, leap)*msPerDay; |
michael@0 | 632 | WeekDay(last_sunday)>0; |
michael@0 | 633 | last_sunday -= msPerDay |
michael@0 | 634 | ){}; |
michael@0 | 635 | sunday = last_sunday; |
michael@0 | 636 | } |
michael@0 | 637 | else { |
michael@0 | 638 | for(var first_sunday = month_start; |
michael@0 | 639 | WeekDay(first_sunday)>0; |
michael@0 | 640 | first_sunday += msPerDay |
michael@0 | 641 | ){}; |
michael@0 | 642 | sunday = first_sunday+7*msPerDay*(count-1); |
michael@0 | 643 | } |
michael@0 | 644 | |
michael@0 | 645 | return sunday; |
michael@0 | 646 | }*/ |
michael@0 | 647 | |
michael@0 | 648 | function DaylightSavingTA(t) { |
michael@0 | 649 | // t = t-LocalTZA; |
michael@0 | 650 | |
michael@0 | 651 | var DST_start = GetSundayInMonth(t, $DST_start_month, $DST_start_sunday) + |
michael@0 | 652 | $DST_start_hour*msPerHour + |
michael@0 | 653 | $DST_start_minutes*msPerMinute; |
michael@0 | 654 | |
michael@0 | 655 | var k = new Date(DST_start); |
michael@0 | 656 | |
michael@0 | 657 | var DST_end = GetSundayInMonth(t, $DST_end_month, $DST_end_sunday) + |
michael@0 | 658 | $DST_end_hour*msPerHour + |
michael@0 | 659 | $DST_end_minutes*msPerMinute; |
michael@0 | 660 | |
michael@0 | 661 | if ( t >= DST_start && t < DST_end ) { |
michael@0 | 662 | return msPerHour; |
michael@0 | 663 | } else { |
michael@0 | 664 | return 0; |
michael@0 | 665 | } |
michael@0 | 666 | } |
michael@0 | 667 | |
michael@0 | 668 | //15.9.1.9 Local Time |
michael@0 | 669 | function LocalTime(t){ |
michael@0 | 670 | return t+LocalTZA+DaylightSavingTA(t); |
michael@0 | 671 | } |
michael@0 | 672 | |
michael@0 | 673 | function UTC(t) { |
michael@0 | 674 | return t-LocalTZA-DaylightSavingTA(t-LocalTZA); |
michael@0 | 675 | } |
michael@0 | 676 | |
michael@0 | 677 | //15.9.1.10 Hours, Minutes, Second, and Milliseconds |
michael@0 | 678 | function HourFromTime(t){ |
michael@0 | 679 | return Math.floor(t/msPerHour)%HoursPerDay; |
michael@0 | 680 | } |
michael@0 | 681 | |
michael@0 | 682 | function MinFromTime(t){ |
michael@0 | 683 | return Math.floor(t/msPerMinute)%MinutesPerHour; |
michael@0 | 684 | } |
michael@0 | 685 | |
michael@0 | 686 | function SecFromTime(t){ |
michael@0 | 687 | return Math.floor(t/msPerSecond)%SecondsPerMinute; |
michael@0 | 688 | } |
michael@0 | 689 | |
michael@0 | 690 | function msFromTime(t){ |
michael@0 | 691 | return t%msPerSecond; |
michael@0 | 692 | } |
michael@0 | 693 | |
michael@0 | 694 | //15.9.1.11 MakeTime (hour, min, sec, ms) |
michael@0 | 695 | function MakeTime(hour, min, sec, ms){ |
michael@0 | 696 | if ( !isFinite(hour) || !isFinite(min) || !isFinite(sec) || !isFinite(ms)) { |
michael@0 | 697 | return Number.NaN; |
michael@0 | 698 | } |
michael@0 | 699 | |
michael@0 | 700 | hour = ToInteger(hour); |
michael@0 | 701 | min = ToInteger(min); |
michael@0 | 702 | sec = ToInteger(sec); |
michael@0 | 703 | ms = ToInteger(ms); |
michael@0 | 704 | |
michael@0 | 705 | return ((hour*msPerHour) + (min*msPerMinute) + (sec*msPerSecond) + ms); |
michael@0 | 706 | } |
michael@0 | 707 | |
michael@0 | 708 | //15.9.1.12 MakeDay (year, month, date) |
michael@0 | 709 | function MakeDay(year, month, date) { |
michael@0 | 710 | if ( !isFinite(year) || !isFinite(month) || !isFinite(date)) { |
michael@0 | 711 | return Number.NaN; |
michael@0 | 712 | } |
michael@0 | 713 | |
michael@0 | 714 | year = ToInteger(year); |
michael@0 | 715 | month = ToInteger(month); |
michael@0 | 716 | date = ToInteger(date ); |
michael@0 | 717 | |
michael@0 | 718 | var result5 = year + Math.floor(month/12); |
michael@0 | 719 | var result6 = month%12; |
michael@0 | 720 | |
michael@0 | 721 | var sign = ( year < 1970 ) ? -1 : 1; |
michael@0 | 722 | var t = ( year < 1970 ) ? 1 : 0; |
michael@0 | 723 | var y = ( year < 1970 ) ? 1969 : 1970; |
michael@0 | 724 | |
michael@0 | 725 | if( sign == -1 ){ |
michael@0 | 726 | for ( y = 1969; y >= year; y += sign ) { |
michael@0 | 727 | t += sign * DaysInYear(y)*msPerDay; |
michael@0 | 728 | } |
michael@0 | 729 | } else { |
michael@0 | 730 | for ( y = 1970 ; y < year; y += sign ) { |
michael@0 | 731 | t += sign * DaysInYear(y)*msPerDay; |
michael@0 | 732 | } |
michael@0 | 733 | } |
michael@0 | 734 | |
michael@0 | 735 | var leap = 0; |
michael@0 | 736 | for ( var m = 0; m < month; m++ ) { |
michael@0 | 737 | //if year is changed, than we need to recalculate leep |
michael@0 | 738 | leap = InLeapYear(t); |
michael@0 | 739 | t += DaysInMonth(m, leap)*msPerDay; |
michael@0 | 740 | } |
michael@0 | 741 | |
michael@0 | 742 | if ( YearFromTime(t) != result5 ) { |
michael@0 | 743 | return Number.NaN; |
michael@0 | 744 | } |
michael@0 | 745 | if ( MonthFromTime(t) != result6 ) { |
michael@0 | 746 | return Number.NaN; |
michael@0 | 747 | } |
michael@0 | 748 | if ( DateFromTime(t) != 1 ) { |
michael@0 | 749 | return Number.NaN; |
michael@0 | 750 | } |
michael@0 | 751 | |
michael@0 | 752 | return Day(t)+date-1; |
michael@0 | 753 | } |
michael@0 | 754 | |
michael@0 | 755 | //15.9.1.13 MakeDate (day, time) |
michael@0 | 756 | function MakeDate( day, time ) { |
michael@0 | 757 | if(!isFinite(day) || !isFinite(time)) { |
michael@0 | 758 | return Number.NaN; |
michael@0 | 759 | } |
michael@0 | 760 | |
michael@0 | 761 | return day*msPerDay+time; |
michael@0 | 762 | } |
michael@0 | 763 | |
michael@0 | 764 | //15.9.1.14 TimeClip (time) |
michael@0 | 765 | function TimeClip(time) { |
michael@0 | 766 | if(!isFinite(time) || Math.abs(time) > 8.64e15){ |
michael@0 | 767 | return Number.NaN; |
michael@0 | 768 | } |
michael@0 | 769 | |
michael@0 | 770 | return ToInteger(time); |
michael@0 | 771 | } |
michael@0 | 772 | |
michael@0 | 773 | //Test Functions |
michael@0 | 774 | //ConstructDate is considered deprecated, and should not be used directly from |
michael@0 | 775 | //test262 tests as it's incredibly sensitive to DST start/end dates that |
michael@0 | 776 | //vary with geographic location. |
michael@0 | 777 | function ConstructDate(year, month, date, hours, minutes, seconds, ms){ |
michael@0 | 778 | /* |
michael@0 | 779 | * 1. Call ToNumber(year) |
michael@0 | 780 | * 2. Call ToNumber(month) |
michael@0 | 781 | * 3. If date is supplied use ToNumber(date); else use 1 |
michael@0 | 782 | * 4. If hours is supplied use ToNumber(hours); else use 0 |
michael@0 | 783 | * 5. If minutes is supplied use ToNumber(minutes); else use 0 |
michael@0 | 784 | * 6. If seconds is supplied use ToNumber(seconds); else use 0 |
michael@0 | 785 | * 7. If ms is supplied use ToNumber(ms); else use 0 |
michael@0 | 786 | * 8. If Result(1) is not NaN and 0 <= ToInteger(Result(1)) <= 99, Result(8) is |
michael@0 | 787 | * 1900+ToInteger(Result(1)); otherwise, Result(8) is Result(1) |
michael@0 | 788 | * 9. Compute MakeDay(Result(8), Result(2), Result(3)) |
michael@0 | 789 | * 10. Compute MakeTime(Result(4), Result(5), Result(6), Result(7)) |
michael@0 | 790 | * 11. Compute MakeDate(Result(9), Result(10)) |
michael@0 | 791 | * 12. Set the [[Value]] property of the newly constructed object to TimeClip(UTC(Result(11))) |
michael@0 | 792 | */ |
michael@0 | 793 | var r1 = Number(year); |
michael@0 | 794 | var r2 = Number(month); |
michael@0 | 795 | var r3 = ((date && arguments.length > 2) ? Number(date) : 1); |
michael@0 | 796 | var r4 = ((hours && arguments.length > 3) ? Number(hours) : 0); |
michael@0 | 797 | var r5 = ((minutes && arguments.length > 4) ? Number(minutes) : 0); |
michael@0 | 798 | var r6 = ((seconds && arguments.length > 5) ? Number(seconds) : 0); |
michael@0 | 799 | var r7 = ((ms && arguments.length > 6) ? Number(ms) : 0); |
michael@0 | 800 | |
michael@0 | 801 | var r8 = r1; |
michael@0 | 802 | |
michael@0 | 803 | if(!isNaN(r1) && (0 <= ToInteger(r1)) && (ToInteger(r1) <= 99)) |
michael@0 | 804 | r8 = 1900+r1; |
michael@0 | 805 | |
michael@0 | 806 | var r9 = MakeDay(r8, r2, r3); |
michael@0 | 807 | var r10 = MakeTime(r4, r5, r6, r7); |
michael@0 | 808 | var r11 = MakeDate(r9, r10); |
michael@0 | 809 | |
michael@0 | 810 | var retVal = TimeClip(UTC(r11)); |
michael@0 | 811 | return retVal; |
michael@0 | 812 | } |
michael@0 | 813 | |
michael@0 | 814 | |
michael@0 | 815 | |
michael@0 | 816 | /**** Python code for initialize the above constants |
michael@0 | 817 | // We may want to replicate the following in JavaScript. |
michael@0 | 818 | // However, using JS date operations to generate parameters that are then used to |
michael@0 | 819 | // test those some date operations seems unsound. However, it isn't clear if there |
michael@0 | 820 | //is a good interoperable alternative. |
michael@0 | 821 | |
michael@0 | 822 | # Copyright 2009 the Sputnik authors. All rights reserved. |
michael@0 | 823 | # This code is governed by the BSD license found in the LICENSE file. |
michael@0 | 824 | |
michael@0 | 825 | def GetDaylightSavingsTimes(): |
michael@0 | 826 | # Is the given floating-point time in DST? |
michael@0 | 827 | def IsDst(t): |
michael@0 | 828 | return time.localtime(t)[-1] |
michael@0 | 829 | # Binary search to find an interval between the two times no greater than |
michael@0 | 830 | # delta where DST switches, returning the midpoint. |
michael@0 | 831 | def FindBetween(start, end, delta): |
michael@0 | 832 | while end - start > delta: |
michael@0 | 833 | middle = (end + start) / 2 |
michael@0 | 834 | if IsDst(middle) == IsDst(start): |
michael@0 | 835 | start = middle |
michael@0 | 836 | else: |
michael@0 | 837 | end = middle |
michael@0 | 838 | return (start + end) / 2 |
michael@0 | 839 | now = time.time() |
michael@0 | 840 | one_month = (30 * 24 * 60 * 60) |
michael@0 | 841 | # First find a date with different daylight savings. To avoid corner cases |
michael@0 | 842 | # we try four months before and after today. |
michael@0 | 843 | after = now + 4 * one_month |
michael@0 | 844 | before = now - 4 * one_month |
michael@0 | 845 | if IsDst(now) == IsDst(before) and IsDst(now) == IsDst(after): |
michael@0 | 846 | logger.warning("Was unable to determine DST info.") |
michael@0 | 847 | return None |
michael@0 | 848 | # Determine when the change occurs between now and the date we just found |
michael@0 | 849 | # in a different DST. |
michael@0 | 850 | if IsDst(now) != IsDst(before): |
michael@0 | 851 | first = FindBetween(before, now, 1) |
michael@0 | 852 | else: |
michael@0 | 853 | first = FindBetween(now, after, 1) |
michael@0 | 854 | # Determine when the change occurs between three and nine months from the |
michael@0 | 855 | # first. |
michael@0 | 856 | second = FindBetween(first + 3 * one_month, first + 9 * one_month, 1) |
michael@0 | 857 | # Find out which switch is into and which if out of DST |
michael@0 | 858 | if IsDst(first - 1) and not IsDst(first + 1): |
michael@0 | 859 | start = second |
michael@0 | 860 | end = first |
michael@0 | 861 | else: |
michael@0 | 862 | start = first |
michael@0 | 863 | end = second |
michael@0 | 864 | return (start, end) |
michael@0 | 865 | |
michael@0 | 866 | |
michael@0 | 867 | def GetDaylightSavingsAttribs(): |
michael@0 | 868 | times = GetDaylightSavingsTimes() |
michael@0 | 869 | if not times: |
michael@0 | 870 | return None |
michael@0 | 871 | (start, end) = times |
michael@0 | 872 | def DstMonth(t): |
michael@0 | 873 | return time.localtime(t)[1] - 1 |
michael@0 | 874 | def DstHour(t): |
michael@0 | 875 | return time.localtime(t - 1)[3] + 1 |
michael@0 | 876 | def DstSunday(t): |
michael@0 | 877 | if time.localtime(t)[2] > 15: |
michael@0 | 878 | return "'last'" |
michael@0 | 879 | else: |
michael@0 | 880 | return "'first'" |
michael@0 | 881 | def DstMinutes(t): |
michael@0 | 882 | return (time.localtime(t - 1)[4] + 1) % 60 |
michael@0 | 883 | attribs = { } |
michael@0 | 884 | attribs['start_month'] = DstMonth(start) |
michael@0 | 885 | attribs['end_month'] = DstMonth(end) |
michael@0 | 886 | attribs['start_sunday'] = DstSunday(start) |
michael@0 | 887 | attribs['end_sunday'] = DstSunday(end) |
michael@0 | 888 | attribs['start_hour'] = DstHour(start) |
michael@0 | 889 | attribs['end_hour'] = DstHour(end) |
michael@0 | 890 | attribs['start_minutes'] = DstMinutes(start) |
michael@0 | 891 | attribs['end_minutes'] = DstMinutes(end) |
michael@0 | 892 | return attribs |
michael@0 | 893 | |
michael@0 | 894 | *********/ |
michael@0 | 895 | |
michael@0 | 896 | //--Test case registration----------------------------------------------------- |
michael@0 | 897 | function runTestCase(testcase) { |
michael@0 | 898 | if (testcase() !== true) { |
michael@0 | 899 | $ERROR("Test case returned non-true value!"); |
michael@0 | 900 | } |
michael@0 | 901 | } |