js/src/devtools/jint/sunspider/date-format-tofte.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 function arrayExists(array, x) {
michael@0 2 /* BEGIN LOOP */
michael@0 3 for (var i = 0; i < array.length; i++) {
michael@0 4 if (array[i] == x) return true;
michael@0 5 }
michael@0 6 /* END LOOP */
michael@0 7 return false;
michael@0 8 }
michael@0 9
michael@0 10 Date.prototype.formatDate = function (input,time) {
michael@0 11 // formatDate :
michael@0 12 // a PHP date like function, for formatting date strings
michael@0 13 // See: http://www.php.net/date
michael@0 14 //
michael@0 15 // input : format string
michael@0 16 // time : epoch time (seconds, and optional)
michael@0 17 //
michael@0 18 // if time is not passed, formatting is based on
michael@0 19 // the current "this" date object's set time.
michael@0 20 //
michael@0 21 // supported:
michael@0 22 // a, A, B, d, D, F, g, G, h, H, i, j, l (lowercase L), L,
michael@0 23 // m, M, n, O, r, s, S, t, U, w, W, y, Y, z
michael@0 24 //
michael@0 25 // unsupported:
michael@0 26 // I (capital i), T, Z
michael@0 27
michael@0 28 var switches = ["a", "A", "B", "d", "D", "F", "g", "G", "h", "H",
michael@0 29 "i", "j", "l", "L", "m", "M", "n", "O", "r", "s",
michael@0 30 "S", "t", "U", "w", "W", "y", "Y", "z"];
michael@0 31 var daysLong = ["Sunday", "Monday", "Tuesday", "Wednesday",
michael@0 32 "Thursday", "Friday", "Saturday"];
michael@0 33 var daysShort = ["Sun", "Mon", "Tue", "Wed",
michael@0 34 "Thu", "Fri", "Sat"];
michael@0 35 var monthsShort = ["Jan", "Feb", "Mar", "Apr",
michael@0 36 "May", "Jun", "Jul", "Aug", "Sep",
michael@0 37 "Oct", "Nov", "Dec"];
michael@0 38 var monthsLong = ["January", "February", "March", "April",
michael@0 39 "May", "June", "July", "August", "September",
michael@0 40 "October", "November", "December"];
michael@0 41 var daysSuffix = ["st", "nd", "rd", "th", "th", "th", "th", // 1st - 7th
michael@0 42 "th", "th", "th", "th", "th", "th", "th", // 8th - 14th
michael@0 43 "th", "th", "th", "th", "th", "th", "st", // 15th - 21st
michael@0 44 "nd", "rd", "th", "th", "th", "th", "th", // 22nd - 28th
michael@0 45 "th", "th", "st"]; // 29th - 31st
michael@0 46
michael@0 47 function a() {
michael@0 48 // Lowercase Ante meridiem and Post meridiem
michael@0 49 return self.getHours() > 11? "pm" : "am";
michael@0 50 }
michael@0 51 function A() {
michael@0 52 // Uppercase Ante meridiem and Post meridiem
michael@0 53 return self.getHours() > 11? "PM" : "AM";
michael@0 54 }
michael@0 55
michael@0 56 function B(){
michael@0 57 // Swatch internet time. code simply grabbed from ppk,
michael@0 58 // since I was feeling lazy:
michael@0 59 // http://www.xs4all.nl/~ppk/js/beat.html
michael@0 60 var off = (self.getTimezoneOffset() + 60)*60;
michael@0 61 var theSeconds = (self.getHours() * 3600) +
michael@0 62 (self.getMinutes() * 60) +
michael@0 63 self.getSeconds() + off;
michael@0 64 var beat = Math.floor(theSeconds/86.4);
michael@0 65 if (beat > 1000) beat -= 1000;
michael@0 66 if (beat < 0) beat += 1000;
michael@0 67 if ((""+beat).length == 1) beat = "00"+beat;
michael@0 68 if ((""+beat).length == 2) beat = "0"+beat;
michael@0 69 return beat;
michael@0 70 }
michael@0 71
michael@0 72 function d() {
michael@0 73 // Day of the month, 2 digits with leading zeros
michael@0 74 return new String(self.getDate()).length == 1?
michael@0 75 "0"+self.getDate() : self.getDate();
michael@0 76 }
michael@0 77 function D() {
michael@0 78 // A textual representation of a day, three letters
michael@0 79 return daysShort[self.getDay()];
michael@0 80 }
michael@0 81 function F() {
michael@0 82 // A full textual representation of a month
michael@0 83 return monthsLong[self.getMonth()];
michael@0 84 }
michael@0 85 function g() {
michael@0 86 // 12-hour format of an hour without leading zeros
michael@0 87 return self.getHours() > 12? self.getHours()-12 : self.getHours();
michael@0 88 }
michael@0 89 function G() {
michael@0 90 // 24-hour format of an hour without leading zeros
michael@0 91 return self.getHours();
michael@0 92 }
michael@0 93 function h() {
michael@0 94 // 12-hour format of an hour with leading zeros
michael@0 95 if (self.getHours() > 12) {
michael@0 96 var s = new String(self.getHours()-12);
michael@0 97 return s.length == 1?
michael@0 98 "0"+ (self.getHours()-12) : self.getHours()-12;
michael@0 99 } else {
michael@0 100 var s = new String(self.getHours());
michael@0 101 return s.length == 1?
michael@0 102 "0"+self.getHours() : self.getHours();
michael@0 103 }
michael@0 104 }
michael@0 105 function H() {
michael@0 106 // 24-hour format of an hour with leading zeros
michael@0 107 return new String(self.getHours()).length == 1?
michael@0 108 "0"+self.getHours() : self.getHours();
michael@0 109 }
michael@0 110 function i() {
michael@0 111 // Minutes with leading zeros
michael@0 112 return new String(self.getMinutes()).length == 1?
michael@0 113 "0"+self.getMinutes() : self.getMinutes();
michael@0 114 }
michael@0 115 function j() {
michael@0 116 // Day of the month without leading zeros
michael@0 117 return self.getDate();
michael@0 118 }
michael@0 119 function l() {
michael@0 120 // A full textual representation of the day of the week
michael@0 121 return daysLong[self.getDay()];
michael@0 122 }
michael@0 123 function L() {
michael@0 124 // leap year or not. 1 if leap year, 0 if not.
michael@0 125 // the logic should match iso's 8601 standard.
michael@0 126 var y_ = Y();
michael@0 127 if (
michael@0 128 (y_ % 4 == 0 && y_ % 100 != 0) ||
michael@0 129 (y_ % 4 == 0 && y_ % 100 == 0 && y_ % 400 == 0)
michael@0 130 ) {
michael@0 131 return 1;
michael@0 132 } else {
michael@0 133 return 0;
michael@0 134 }
michael@0 135 }
michael@0 136 function m() {
michael@0 137 // Numeric representation of a month, with leading zeros
michael@0 138 return self.getMonth() < 9?
michael@0 139 "0"+(self.getMonth()+1) :
michael@0 140 self.getMonth()+1;
michael@0 141 }
michael@0 142 function M() {
michael@0 143 // A short textual representation of a month, three letters
michael@0 144 return monthsShort[self.getMonth()];
michael@0 145 }
michael@0 146 function n() {
michael@0 147 // Numeric representation of a month, without leading zeros
michael@0 148 return self.getMonth()+1;
michael@0 149 }
michael@0 150 function O() {
michael@0 151 // Difference to Greenwich time (GMT) in hours
michael@0 152 var os = Math.abs(self.getTimezoneOffset());
michael@0 153 var h = ""+Math.floor(os/60);
michael@0 154 var m = ""+(os%60);
michael@0 155 h.length == 1? h = "0"+h:1;
michael@0 156 m.length == 1? m = "0"+m:1;
michael@0 157 return self.getTimezoneOffset() < 0 ? "+"+h+m : "-"+h+m;
michael@0 158 }
michael@0 159 function r() {
michael@0 160 // RFC 822 formatted date
michael@0 161 var r; // result
michael@0 162 // Thu , 21 Dec 2000
michael@0 163 r = D() + ", " + j() + " " + M() + " " + Y() +
michael@0 164 // 16 : 01 : 07 +0200
michael@0 165 " " + H() + ":" + i() + ":" + s() + " " + O();
michael@0 166 return r;
michael@0 167 }
michael@0 168 function S() {
michael@0 169 // English ordinal suffix for the day of the month, 2 characters
michael@0 170 return daysSuffix[self.getDate()-1];
michael@0 171 }
michael@0 172 function s() {
michael@0 173 // Seconds, with leading zeros
michael@0 174 return new String(self.getSeconds()).length == 1?
michael@0 175 "0"+self.getSeconds() : self.getSeconds();
michael@0 176 }
michael@0 177 function t() {
michael@0 178
michael@0 179 // thanks to Matt Bannon for some much needed code-fixes here!
michael@0 180 var daysinmonths = [null,31,28,31,30,31,30,31,31,30,31,30,31];
michael@0 181 if (L()==1 && n()==2) return 29; // leap day
michael@0 182 return daysinmonths[n()];
michael@0 183 }
michael@0 184 function U() {
michael@0 185 // Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)
michael@0 186 return Math.round(self.getTime()/1000);
michael@0 187 }
michael@0 188 function W() {
michael@0 189 // Weeknumber, as per ISO specification:
michael@0 190 // http://www.cl.cam.ac.uk/~mgk25/iso-time.html
michael@0 191
michael@0 192 // if the day is three days before newyears eve,
michael@0 193 // there's a chance it's "week 1" of next year.
michael@0 194 // here we check for that.
michael@0 195 var beforeNY = 364+L() - z();
michael@0 196 var afterNY = z();
michael@0 197 var weekday = w()!=0?w()-1:6; // makes sunday (0), into 6.
michael@0 198 if (beforeNY <= 2 && weekday <= 2-beforeNY) {
michael@0 199 return 1;
michael@0 200 }
michael@0 201 // similarly, if the day is within threedays of newyears
michael@0 202 // there's a chance it belongs in the old year.
michael@0 203 var ny = new Date("January 1 " + Y() + " 00:00:00");
michael@0 204 var nyDay = ny.getDay()!=0?ny.getDay()-1:6;
michael@0 205 if (
michael@0 206 (afterNY <= 2) &&
michael@0 207 (nyDay >=4) &&
michael@0 208 (afterNY >= (6-nyDay))
michael@0 209 ) {
michael@0 210 // Since I'm not sure we can just always return 53,
michael@0 211 // i call the function here again, using the last day
michael@0 212 // of the previous year, as the date, and then just
michael@0 213 // return that week.
michael@0 214 var prevNY = new Date("December 31 " + (Y()-1) + " 00:00:00");
michael@0 215 return prevNY.formatDate("W");
michael@0 216 }
michael@0 217
michael@0 218 // week 1, is the week that has the first thursday in it.
michael@0 219 // note that this value is not zero index.
michael@0 220 if (nyDay <= 3) {
michael@0 221 // first day of the year fell on a thursday, or earlier.
michael@0 222 return 1 + Math.floor( ( z() + nyDay ) / 7 );
michael@0 223 } else {
michael@0 224 // first day of the year fell on a friday, or later.
michael@0 225 return 1 + Math.floor( ( z() - ( 7 - nyDay ) ) / 7 );
michael@0 226 }
michael@0 227 }
michael@0 228 function w() {
michael@0 229 // Numeric representation of the day of the week
michael@0 230 return self.getDay();
michael@0 231 }
michael@0 232
michael@0 233 function Y() {
michael@0 234 // A full numeric representation of a year, 4 digits
michael@0 235
michael@0 236 // we first check, if getFullYear is supported. if it
michael@0 237 // is, we just use that. ppks code is nice, but wont
michael@0 238 // work with dates outside 1900-2038, or something like that
michael@0 239 if (self.getFullYear) {
michael@0 240 var newDate = new Date("January 1 2001 00:00:00 +0000");
michael@0 241 var x = newDate .getFullYear();
michael@0 242 if (x == 2001) {
michael@0 243 // i trust the method now
michael@0 244 return self.getFullYear();
michael@0 245 }
michael@0 246 }
michael@0 247 // else, do this:
michael@0 248 // codes thanks to ppk:
michael@0 249 // http://www.xs4all.nl/~ppk/js/introdate.html
michael@0 250 var x = self.getYear();
michael@0 251 var y = x % 100;
michael@0 252 y += (y < 38) ? 2000 : 1900;
michael@0 253 return y;
michael@0 254 }
michael@0 255 function y() {
michael@0 256 // A two-digit representation of a year
michael@0 257 var y = Y()+"";
michael@0 258 return y.substring(y.length-2,y.length);
michael@0 259 }
michael@0 260 function z() {
michael@0 261 // The day of the year, zero indexed! 0 through 366
michael@0 262 var t = new Date("January 1 " + Y() + " 00:00:00");
michael@0 263 var diff = self.getTime() - t.getTime();
michael@0 264 return Math.floor(diff/1000/60/60/24);
michael@0 265 }
michael@0 266
michael@0 267 var self = this;
michael@0 268 if (time) {
michael@0 269 // save time
michael@0 270 var prevTime = self.getTime();
michael@0 271 self.setTime(time);
michael@0 272 }
michael@0 273
michael@0 274 var ia = input.split("");
michael@0 275 var ij = 0;
michael@0 276 /* BEGIN LOOP */
michael@0 277 while (ia[ij]) {
michael@0 278 if (ia[ij] == "\\") {
michael@0 279 // this is our way of allowing users to escape stuff
michael@0 280 ia.splice(ij,1);
michael@0 281 } else {
michael@0 282 if (arrayExists(switches,ia[ij])) {
michael@0 283 ia[ij] = eval(ia[ij] + "()");
michael@0 284 }
michael@0 285 }
michael@0 286 ij++;
michael@0 287 }
michael@0 288 /* END LOOP */
michael@0 289 // reset time, back to what it was
michael@0 290 if (prevTime) {
michael@0 291 self.setTime(prevTime);
michael@0 292 }
michael@0 293 return ia.join("");
michael@0 294 }
michael@0 295
michael@0 296 var date = new Date("1/1/2007 1:11:11");
michael@0 297
michael@0 298 /* BEGIN LOOP */
michael@0 299 for (i = 0; i < 500; ++i) {
michael@0 300 var shortFormat = date.formatDate("Y-m-d");
michael@0 301 var longFormat = date.formatDate("l, F d, Y g:i:s A");
michael@0 302 date.setTime(date.getTime() + 84266956);
michael@0 303 }
michael@0 304 /* END LOOP */
michael@0 305

mercurial