build/pgo/js-input/date-format-tofte.html

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

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 <!DOCTYPE html>
michael@0 2 <head>
michael@0 3 <!--
michael@0 4 Copyright (C) 2007 Apple Inc. All rights reserved.
michael@0 5
michael@0 6 Redistribution and use in source and binary forms, with or without
michael@0 7 modification, are permitted provided that the following conditions
michael@0 8 are met:
michael@0 9 1. Redistributions of source code must retain the above copyright
michael@0 10 notice, this list of conditions and the following disclaimer.
michael@0 11 2. Redistributions in binary form must reproduce the above copyright
michael@0 12 notice, this list of conditions and the following disclaimer in the
michael@0 13 documentation and/or other materials provided with the distribution.
michael@0 14
michael@0 15 THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
michael@0 16 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
michael@0 17 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
michael@0 18 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
michael@0 19 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
michael@0 20 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
michael@0 21 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
michael@0 22 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
michael@0 23 OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
michael@0 24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
michael@0 25 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
michael@0 26 -->
michael@0 27
michael@0 28 <title>SunSpider date-format-tofte</title>
michael@0 29
michael@0 30 </head>
michael@0 31
michael@0 32 <body>
michael@0 33 <h3>date-format-tofte</h3>
michael@0 34 <div id="console">
michael@0 35 </div>
michael@0 36
michael@0 37 <script>
michael@0 38
michael@0 39 var _sunSpiderStartDate = new Date();
michael@0 40
michael@0 41 function arrayExists(array, x) {
michael@0 42 for (var i = 0; i < array.length; i++) {
michael@0 43 if (array[i] == x) return true;
michael@0 44 }
michael@0 45 return false;
michael@0 46 }
michael@0 47
michael@0 48 Date.prototype.formatDate = function (input,time) {
michael@0 49 // formatDate :
michael@0 50 // a PHP date like function, for formatting date strings
michael@0 51 // See: http://www.php.net/date
michael@0 52 //
michael@0 53 // input : format string
michael@0 54 // time : epoch time (seconds, and optional)
michael@0 55 //
michael@0 56 // if time is not passed, formatting is based on
michael@0 57 // the current "this" date object's set time.
michael@0 58 //
michael@0 59 // supported:
michael@0 60 // a, A, B, d, D, F, g, G, h, H, i, j, l (lowercase L), L,
michael@0 61 // m, M, n, O, r, s, S, t, U, w, W, y, Y, z
michael@0 62 //
michael@0 63 // unsupported:
michael@0 64 // I (capital i), T, Z
michael@0 65
michael@0 66 var switches = ["a", "A", "B", "d", "D", "F", "g", "G", "h", "H",
michael@0 67 "i", "j", "l", "L", "m", "M", "n", "O", "r", "s",
michael@0 68 "S", "t", "U", "w", "W", "y", "Y", "z"];
michael@0 69 var daysLong = ["Sunday", "Monday", "Tuesday", "Wednesday",
michael@0 70 "Thursday", "Friday", "Saturday"];
michael@0 71 var daysShort = ["Sun", "Mon", "Tue", "Wed",
michael@0 72 "Thu", "Fri", "Sat"];
michael@0 73 var monthsShort = ["Jan", "Feb", "Mar", "Apr",
michael@0 74 "May", "Jun", "Jul", "Aug", "Sep",
michael@0 75 "Oct", "Nov", "Dec"];
michael@0 76 var monthsLong = ["January", "February", "March", "April",
michael@0 77 "May", "June", "July", "August", "September",
michael@0 78 "October", "November", "December"];
michael@0 79 var daysSuffix = ["st", "nd", "rd", "th", "th", "th", "th", // 1st - 7th
michael@0 80 "th", "th", "th", "th", "th", "th", "th", // 8th - 14th
michael@0 81 "th", "th", "th", "th", "th", "th", "st", // 15th - 21st
michael@0 82 "nd", "rd", "th", "th", "th", "th", "th", // 22nd - 28th
michael@0 83 "th", "th", "st"]; // 29th - 31st
michael@0 84
michael@0 85 function a() {
michael@0 86 // Lowercase Ante meridiem and Post meridiem
michael@0 87 return self.getHours() > 11? "pm" : "am";
michael@0 88 }
michael@0 89 function A() {
michael@0 90 // Uppercase Ante meridiem and Post meridiem
michael@0 91 return self.getHours() > 11? "PM" : "AM";
michael@0 92 }
michael@0 93
michael@0 94 function B(){
michael@0 95 // Swatch internet time. code simply grabbed from ppk,
michael@0 96 // since I was feeling lazy:
michael@0 97 // http://www.xs4all.nl/~ppk/js/beat.html
michael@0 98 var off = (self.getTimezoneOffset() + 60)*60;
michael@0 99 var theSeconds = (self.getHours() * 3600) +
michael@0 100 (self.getMinutes() * 60) +
michael@0 101 self.getSeconds() + off;
michael@0 102 var beat = Math.floor(theSeconds/86.4);
michael@0 103 if (beat > 1000) beat -= 1000;
michael@0 104 if (beat < 0) beat += 1000;
michael@0 105 if ((""+beat).length == 1) beat = "00"+beat;
michael@0 106 if ((""+beat).length == 2) beat = "0"+beat;
michael@0 107 return beat;
michael@0 108 }
michael@0 109
michael@0 110 function d() {
michael@0 111 // Day of the month, 2 digits with leading zeros
michael@0 112 return new String(self.getDate()).length == 1?
michael@0 113 "0"+self.getDate() : self.getDate();
michael@0 114 }
michael@0 115 function D() {
michael@0 116 // A textual representation of a day, three letters
michael@0 117 return daysShort[self.getDay()];
michael@0 118 }
michael@0 119 function F() {
michael@0 120 // A full textual representation of a month
michael@0 121 return monthsLong[self.getMonth()];
michael@0 122 }
michael@0 123 function g() {
michael@0 124 // 12-hour format of an hour without leading zeros
michael@0 125 return self.getHours() > 12? self.getHours()-12 : self.getHours();
michael@0 126 }
michael@0 127 function G() {
michael@0 128 // 24-hour format of an hour without leading zeros
michael@0 129 return self.getHours();
michael@0 130 }
michael@0 131 function h() {
michael@0 132 // 12-hour format of an hour with leading zeros
michael@0 133 if (self.getHours() > 12) {
michael@0 134 var s = new String(self.getHours()-12);
michael@0 135 return s.length == 1?
michael@0 136 "0"+ (self.getHours()-12) : self.getHours()-12;
michael@0 137 } else {
michael@0 138 var s = new String(self.getHours());
michael@0 139 return s.length == 1?
michael@0 140 "0"+self.getHours() : self.getHours();
michael@0 141 }
michael@0 142 }
michael@0 143 function H() {
michael@0 144 // 24-hour format of an hour with leading zeros
michael@0 145 return new String(self.getHours()).length == 1?
michael@0 146 "0"+self.getHours() : self.getHours();
michael@0 147 }
michael@0 148 function i() {
michael@0 149 // Minutes with leading zeros
michael@0 150 return new String(self.getMinutes()).length == 1?
michael@0 151 "0"+self.getMinutes() : self.getMinutes();
michael@0 152 }
michael@0 153 function j() {
michael@0 154 // Day of the month without leading zeros
michael@0 155 return self.getDate();
michael@0 156 }
michael@0 157 function l() {
michael@0 158 // A full textual representation of the day of the week
michael@0 159 return daysLong[self.getDay()];
michael@0 160 }
michael@0 161 function L() {
michael@0 162 // leap year or not. 1 if leap year, 0 if not.
michael@0 163 // the logic should match iso's 8601 standard.
michael@0 164 var y_ = Y();
michael@0 165 if (
michael@0 166 (y_ % 4 == 0 && y_ % 100 != 0) ||
michael@0 167 (y_ % 4 == 0 && y_ % 100 == 0 && y_ % 400 == 0)
michael@0 168 ) {
michael@0 169 return 1;
michael@0 170 } else {
michael@0 171 return 0;
michael@0 172 }
michael@0 173 }
michael@0 174 function m() {
michael@0 175 // Numeric representation of a month, with leading zeros
michael@0 176 return self.getMonth() < 9?
michael@0 177 "0"+(self.getMonth()+1) :
michael@0 178 self.getMonth()+1;
michael@0 179 }
michael@0 180 function M() {
michael@0 181 // A short textual representation of a month, three letters
michael@0 182 return monthsShort[self.getMonth()];
michael@0 183 }
michael@0 184 function n() {
michael@0 185 // Numeric representation of a month, without leading zeros
michael@0 186 return self.getMonth()+1;
michael@0 187 }
michael@0 188 function O() {
michael@0 189 // Difference to Greenwich time (GMT) in hours
michael@0 190 var os = Math.abs(self.getTimezoneOffset());
michael@0 191 var h = ""+Math.floor(os/60);
michael@0 192 var m = ""+(os%60);
michael@0 193 h.length == 1? h = "0"+h:1;
michael@0 194 m.length == 1? m = "0"+m:1;
michael@0 195 return self.getTimezoneOffset() < 0 ? "+"+h+m : "-"+h+m;
michael@0 196 }
michael@0 197 function r() {
michael@0 198 // RFC 822 formatted date
michael@0 199 var r; // result
michael@0 200 // Thu , 21 Dec 2000
michael@0 201 r = D() + ", " + j() + " " + M() + " " + Y() +
michael@0 202 // 16 : 01 : 07 +0200
michael@0 203 " " + H() + ":" + i() + ":" + s() + " " + O();
michael@0 204 return r;
michael@0 205 }
michael@0 206 function S() {
michael@0 207 // English ordinal suffix for the day of the month, 2 characters
michael@0 208 return daysSuffix[self.getDate()-1];
michael@0 209 }
michael@0 210 function s() {
michael@0 211 // Seconds, with leading zeros
michael@0 212 return new String(self.getSeconds()).length == 1?
michael@0 213 "0"+self.getSeconds() : self.getSeconds();
michael@0 214 }
michael@0 215 function t() {
michael@0 216
michael@0 217 // thanks to Matt Bannon for some much needed code-fixes here!
michael@0 218 var daysinmonths = [null,31,28,31,30,31,30,31,31,30,31,30,31];
michael@0 219 if (L()==1 && n()==2) return 29; // leap day
michael@0 220 return daysinmonths[n()];
michael@0 221 }
michael@0 222 function U() {
michael@0 223 // Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)
michael@0 224 return Math.round(self.getTime()/1000);
michael@0 225 }
michael@0 226 function W() {
michael@0 227 // Weeknumber, as per ISO specification:
michael@0 228 // http://www.cl.cam.ac.uk/~mgk25/iso-time.html
michael@0 229
michael@0 230 // if the day is three days before newyears eve,
michael@0 231 // there's a chance it's "week 1" of next year.
michael@0 232 // here we check for that.
michael@0 233 var beforeNY = 364+L() - z();
michael@0 234 var afterNY = z();
michael@0 235 var weekday = w()!=0?w()-1:6; // makes sunday (0), into 6.
michael@0 236 if (beforeNY <= 2 && weekday <= 2-beforeNY) {
michael@0 237 return 1;
michael@0 238 }
michael@0 239 // similarly, if the day is within threedays of newyears
michael@0 240 // there's a chance it belongs in the old year.
michael@0 241 var ny = new Date("January 1 " + Y() + " 00:00:00");
michael@0 242 var nyDay = ny.getDay()!=0?ny.getDay()-1:6;
michael@0 243 if (
michael@0 244 (afterNY <= 2) &&
michael@0 245 (nyDay >=4) &&
michael@0 246 (afterNY >= (6-nyDay))
michael@0 247 ) {
michael@0 248 // Since I'm not sure we can just always return 53,
michael@0 249 // i call the function here again, using the last day
michael@0 250 // of the previous year, as the date, and then just
michael@0 251 // return that week.
michael@0 252 var prevNY = new Date("December 31 " + (Y()-1) + " 00:00:00");
michael@0 253 return prevNY.formatDate("W");
michael@0 254 }
michael@0 255
michael@0 256 // week 1, is the week that has the first thursday in it.
michael@0 257 // note that this value is not zero index.
michael@0 258 if (nyDay <= 3) {
michael@0 259 // first day of the year fell on a thursday, or earlier.
michael@0 260 return 1 + Math.floor( ( z() + nyDay ) / 7 );
michael@0 261 } else {
michael@0 262 // first day of the year fell on a friday, or later.
michael@0 263 return 1 + Math.floor( ( z() - ( 7 - nyDay ) ) / 7 );
michael@0 264 }
michael@0 265 }
michael@0 266 function w() {
michael@0 267 // Numeric representation of the day of the week
michael@0 268 return self.getDay();
michael@0 269 }
michael@0 270
michael@0 271 function Y() {
michael@0 272 // A full numeric representation of a year, 4 digits
michael@0 273
michael@0 274 // we first check, if getFullYear is supported. if it
michael@0 275 // is, we just use that. ppks code is nice, but wont
michael@0 276 // work with dates outside 1900-2038, or something like that
michael@0 277 if (self.getFullYear) {
michael@0 278 var newDate = new Date("January 1 2001 00:00:00 +0000");
michael@0 279 var x = newDate .getFullYear();
michael@0 280 if (x == 2001) {
michael@0 281 // i trust the method now
michael@0 282 return self.getFullYear();
michael@0 283 }
michael@0 284 }
michael@0 285 // else, do this:
michael@0 286 // codes thanks to ppk:
michael@0 287 // http://www.xs4all.nl/~ppk/js/introdate.html
michael@0 288 var x = self.getYear();
michael@0 289 var y = x % 100;
michael@0 290 y += (y < 38) ? 2000 : 1900;
michael@0 291 return y;
michael@0 292 }
michael@0 293 function y() {
michael@0 294 // A two-digit representation of a year
michael@0 295 var y = Y()+"";
michael@0 296 return y.substring(y.length-2,y.length);
michael@0 297 }
michael@0 298 function z() {
michael@0 299 // The day of the year, zero indexed! 0 through 366
michael@0 300 var t = new Date("January 1 " + Y() + " 00:00:00");
michael@0 301 var diff = self.getTime() - t.getTime();
michael@0 302 return Math.floor(diff/1000/60/60/24);
michael@0 303 }
michael@0 304
michael@0 305 var self = this;
michael@0 306 if (time) {
michael@0 307 // save time
michael@0 308 var prevTime = self.getTime();
michael@0 309 self.setTime(time);
michael@0 310 }
michael@0 311
michael@0 312 var ia = input.split("");
michael@0 313 var ij = 0;
michael@0 314 while (ia[ij]) {
michael@0 315 if (ia[ij] == "\\") {
michael@0 316 // this is our way of allowing users to escape stuff
michael@0 317 ia.splice(ij,1);
michael@0 318 } else {
michael@0 319 if (arrayExists(switches,ia[ij])) {
michael@0 320 ia[ij] = eval(ia[ij] + "()");
michael@0 321 }
michael@0 322 }
michael@0 323 ij++;
michael@0 324 }
michael@0 325 // reset time, back to what it was
michael@0 326 if (prevTime) {
michael@0 327 self.setTime(prevTime);
michael@0 328 }
michael@0 329 return ia.join("");
michael@0 330 }
michael@0 331
michael@0 332 var date = new Date("1/1/2007 1:11:11");
michael@0 333
michael@0 334 for (i = 0; i < 500; ++i) {
michael@0 335 var shortFormat = date.formatDate("Y-m-d");
michael@0 336 var longFormat = date.formatDate("l, F d, Y g:i:s A");
michael@0 337 date.setTime(date.getTime() + 84266956);
michael@0 338 }
michael@0 339
michael@0 340
michael@0 341
michael@0 342 var _sunSpiderInterval = new Date() - _sunSpiderStartDate;
michael@0 343
michael@0 344 document.getElementById("console").innerHTML = _sunSpiderInterval;
michael@0 345 </script>
michael@0 346
michael@0 347
michael@0 348 </body>
michael@0 349 </html>

mercurial