1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/devtools/jint/sunspider/date-format-tofte.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,305 @@ 1.4 +function arrayExists(array, x) { 1.5 + /* BEGIN LOOP */ 1.6 + for (var i = 0; i < array.length; i++) { 1.7 + if (array[i] == x) return true; 1.8 + } 1.9 + /* END LOOP */ 1.10 + return false; 1.11 +} 1.12 + 1.13 +Date.prototype.formatDate = function (input,time) { 1.14 + // formatDate : 1.15 + // a PHP date like function, for formatting date strings 1.16 + // See: http://www.php.net/date 1.17 + // 1.18 + // input : format string 1.19 + // time : epoch time (seconds, and optional) 1.20 + // 1.21 + // if time is not passed, formatting is based on 1.22 + // the current "this" date object's set time. 1.23 + // 1.24 + // supported: 1.25 + // a, A, B, d, D, F, g, G, h, H, i, j, l (lowercase L), L, 1.26 + // m, M, n, O, r, s, S, t, U, w, W, y, Y, z 1.27 + // 1.28 + // unsupported: 1.29 + // I (capital i), T, Z 1.30 + 1.31 + var switches = ["a", "A", "B", "d", "D", "F", "g", "G", "h", "H", 1.32 + "i", "j", "l", "L", "m", "M", "n", "O", "r", "s", 1.33 + "S", "t", "U", "w", "W", "y", "Y", "z"]; 1.34 + var daysLong = ["Sunday", "Monday", "Tuesday", "Wednesday", 1.35 + "Thursday", "Friday", "Saturday"]; 1.36 + var daysShort = ["Sun", "Mon", "Tue", "Wed", 1.37 + "Thu", "Fri", "Sat"]; 1.38 + var monthsShort = ["Jan", "Feb", "Mar", "Apr", 1.39 + "May", "Jun", "Jul", "Aug", "Sep", 1.40 + "Oct", "Nov", "Dec"]; 1.41 + var monthsLong = ["January", "February", "March", "April", 1.42 + "May", "June", "July", "August", "September", 1.43 + "October", "November", "December"]; 1.44 + var daysSuffix = ["st", "nd", "rd", "th", "th", "th", "th", // 1st - 7th 1.45 + "th", "th", "th", "th", "th", "th", "th", // 8th - 14th 1.46 + "th", "th", "th", "th", "th", "th", "st", // 15th - 21st 1.47 + "nd", "rd", "th", "th", "th", "th", "th", // 22nd - 28th 1.48 + "th", "th", "st"]; // 29th - 31st 1.49 + 1.50 + function a() { 1.51 + // Lowercase Ante meridiem and Post meridiem 1.52 + return self.getHours() > 11? "pm" : "am"; 1.53 + } 1.54 + function A() { 1.55 + // Uppercase Ante meridiem and Post meridiem 1.56 + return self.getHours() > 11? "PM" : "AM"; 1.57 + } 1.58 + 1.59 + function B(){ 1.60 + // Swatch internet time. code simply grabbed from ppk, 1.61 + // since I was feeling lazy: 1.62 + // http://www.xs4all.nl/~ppk/js/beat.html 1.63 + var off = (self.getTimezoneOffset() + 60)*60; 1.64 + var theSeconds = (self.getHours() * 3600) + 1.65 + (self.getMinutes() * 60) + 1.66 + self.getSeconds() + off; 1.67 + var beat = Math.floor(theSeconds/86.4); 1.68 + if (beat > 1000) beat -= 1000; 1.69 + if (beat < 0) beat += 1000; 1.70 + if ((""+beat).length == 1) beat = "00"+beat; 1.71 + if ((""+beat).length == 2) beat = "0"+beat; 1.72 + return beat; 1.73 + } 1.74 + 1.75 + function d() { 1.76 + // Day of the month, 2 digits with leading zeros 1.77 + return new String(self.getDate()).length == 1? 1.78 + "0"+self.getDate() : self.getDate(); 1.79 + } 1.80 + function D() { 1.81 + // A textual representation of a day, three letters 1.82 + return daysShort[self.getDay()]; 1.83 + } 1.84 + function F() { 1.85 + // A full textual representation of a month 1.86 + return monthsLong[self.getMonth()]; 1.87 + } 1.88 + function g() { 1.89 + // 12-hour format of an hour without leading zeros 1.90 + return self.getHours() > 12? self.getHours()-12 : self.getHours(); 1.91 + } 1.92 + function G() { 1.93 + // 24-hour format of an hour without leading zeros 1.94 + return self.getHours(); 1.95 + } 1.96 + function h() { 1.97 + // 12-hour format of an hour with leading zeros 1.98 + if (self.getHours() > 12) { 1.99 + var s = new String(self.getHours()-12); 1.100 + return s.length == 1? 1.101 + "0"+ (self.getHours()-12) : self.getHours()-12; 1.102 + } else { 1.103 + var s = new String(self.getHours()); 1.104 + return s.length == 1? 1.105 + "0"+self.getHours() : self.getHours(); 1.106 + } 1.107 + } 1.108 + function H() { 1.109 + // 24-hour format of an hour with leading zeros 1.110 + return new String(self.getHours()).length == 1? 1.111 + "0"+self.getHours() : self.getHours(); 1.112 + } 1.113 + function i() { 1.114 + // Minutes with leading zeros 1.115 + return new String(self.getMinutes()).length == 1? 1.116 + "0"+self.getMinutes() : self.getMinutes(); 1.117 + } 1.118 + function j() { 1.119 + // Day of the month without leading zeros 1.120 + return self.getDate(); 1.121 + } 1.122 + function l() { 1.123 + // A full textual representation of the day of the week 1.124 + return daysLong[self.getDay()]; 1.125 + } 1.126 + function L() { 1.127 + // leap year or not. 1 if leap year, 0 if not. 1.128 + // the logic should match iso's 8601 standard. 1.129 + var y_ = Y(); 1.130 + if ( 1.131 + (y_ % 4 == 0 && y_ % 100 != 0) || 1.132 + (y_ % 4 == 0 && y_ % 100 == 0 && y_ % 400 == 0) 1.133 + ) { 1.134 + return 1; 1.135 + } else { 1.136 + return 0; 1.137 + } 1.138 + } 1.139 + function m() { 1.140 + // Numeric representation of a month, with leading zeros 1.141 + return self.getMonth() < 9? 1.142 + "0"+(self.getMonth()+1) : 1.143 + self.getMonth()+1; 1.144 + } 1.145 + function M() { 1.146 + // A short textual representation of a month, three letters 1.147 + return monthsShort[self.getMonth()]; 1.148 + } 1.149 + function n() { 1.150 + // Numeric representation of a month, without leading zeros 1.151 + return self.getMonth()+1; 1.152 + } 1.153 + function O() { 1.154 + // Difference to Greenwich time (GMT) in hours 1.155 + var os = Math.abs(self.getTimezoneOffset()); 1.156 + var h = ""+Math.floor(os/60); 1.157 + var m = ""+(os%60); 1.158 + h.length == 1? h = "0"+h:1; 1.159 + m.length == 1? m = "0"+m:1; 1.160 + return self.getTimezoneOffset() < 0 ? "+"+h+m : "-"+h+m; 1.161 + } 1.162 + function r() { 1.163 + // RFC 822 formatted date 1.164 + var r; // result 1.165 + // Thu , 21 Dec 2000 1.166 + r = D() + ", " + j() + " " + M() + " " + Y() + 1.167 + // 16 : 01 : 07 +0200 1.168 + " " + H() + ":" + i() + ":" + s() + " " + O(); 1.169 + return r; 1.170 + } 1.171 + function S() { 1.172 + // English ordinal suffix for the day of the month, 2 characters 1.173 + return daysSuffix[self.getDate()-1]; 1.174 + } 1.175 + function s() { 1.176 + // Seconds, with leading zeros 1.177 + return new String(self.getSeconds()).length == 1? 1.178 + "0"+self.getSeconds() : self.getSeconds(); 1.179 + } 1.180 + function t() { 1.181 + 1.182 + // thanks to Matt Bannon for some much needed code-fixes here! 1.183 + var daysinmonths = [null,31,28,31,30,31,30,31,31,30,31,30,31]; 1.184 + if (L()==1 && n()==2) return 29; // leap day 1.185 + return daysinmonths[n()]; 1.186 + } 1.187 + function U() { 1.188 + // Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT) 1.189 + return Math.round(self.getTime()/1000); 1.190 + } 1.191 + function W() { 1.192 + // Weeknumber, as per ISO specification: 1.193 + // http://www.cl.cam.ac.uk/~mgk25/iso-time.html 1.194 + 1.195 + // if the day is three days before newyears eve, 1.196 + // there's a chance it's "week 1" of next year. 1.197 + // here we check for that. 1.198 + var beforeNY = 364+L() - z(); 1.199 + var afterNY = z(); 1.200 + var weekday = w()!=0?w()-1:6; // makes sunday (0), into 6. 1.201 + if (beforeNY <= 2 && weekday <= 2-beforeNY) { 1.202 + return 1; 1.203 + } 1.204 + // similarly, if the day is within threedays of newyears 1.205 + // there's a chance it belongs in the old year. 1.206 + var ny = new Date("January 1 " + Y() + " 00:00:00"); 1.207 + var nyDay = ny.getDay()!=0?ny.getDay()-1:6; 1.208 + if ( 1.209 + (afterNY <= 2) && 1.210 + (nyDay >=4) && 1.211 + (afterNY >= (6-nyDay)) 1.212 + ) { 1.213 + // Since I'm not sure we can just always return 53, 1.214 + // i call the function here again, using the last day 1.215 + // of the previous year, as the date, and then just 1.216 + // return that week. 1.217 + var prevNY = new Date("December 31 " + (Y()-1) + " 00:00:00"); 1.218 + return prevNY.formatDate("W"); 1.219 + } 1.220 + 1.221 + // week 1, is the week that has the first thursday in it. 1.222 + // note that this value is not zero index. 1.223 + if (nyDay <= 3) { 1.224 + // first day of the year fell on a thursday, or earlier. 1.225 + return 1 + Math.floor( ( z() + nyDay ) / 7 ); 1.226 + } else { 1.227 + // first day of the year fell on a friday, or later. 1.228 + return 1 + Math.floor( ( z() - ( 7 - nyDay ) ) / 7 ); 1.229 + } 1.230 + } 1.231 + function w() { 1.232 + // Numeric representation of the day of the week 1.233 + return self.getDay(); 1.234 + } 1.235 + 1.236 + function Y() { 1.237 + // A full numeric representation of a year, 4 digits 1.238 + 1.239 + // we first check, if getFullYear is supported. if it 1.240 + // is, we just use that. ppks code is nice, but wont 1.241 + // work with dates outside 1900-2038, or something like that 1.242 + if (self.getFullYear) { 1.243 + var newDate = new Date("January 1 2001 00:00:00 +0000"); 1.244 + var x = newDate .getFullYear(); 1.245 + if (x == 2001) { 1.246 + // i trust the method now 1.247 + return self.getFullYear(); 1.248 + } 1.249 + } 1.250 + // else, do this: 1.251 + // codes thanks to ppk: 1.252 + // http://www.xs4all.nl/~ppk/js/introdate.html 1.253 + var x = self.getYear(); 1.254 + var y = x % 100; 1.255 + y += (y < 38) ? 2000 : 1900; 1.256 + return y; 1.257 + } 1.258 + function y() { 1.259 + // A two-digit representation of a year 1.260 + var y = Y()+""; 1.261 + return y.substring(y.length-2,y.length); 1.262 + } 1.263 + function z() { 1.264 + // The day of the year, zero indexed! 0 through 366 1.265 + var t = new Date("January 1 " + Y() + " 00:00:00"); 1.266 + var diff = self.getTime() - t.getTime(); 1.267 + return Math.floor(diff/1000/60/60/24); 1.268 + } 1.269 + 1.270 + var self = this; 1.271 + if (time) { 1.272 + // save time 1.273 + var prevTime = self.getTime(); 1.274 + self.setTime(time); 1.275 + } 1.276 + 1.277 + var ia = input.split(""); 1.278 + var ij = 0; 1.279 + /* BEGIN LOOP */ 1.280 + while (ia[ij]) { 1.281 + if (ia[ij] == "\\") { 1.282 + // this is our way of allowing users to escape stuff 1.283 + ia.splice(ij,1); 1.284 + } else { 1.285 + if (arrayExists(switches,ia[ij])) { 1.286 + ia[ij] = eval(ia[ij] + "()"); 1.287 + } 1.288 + } 1.289 + ij++; 1.290 + } 1.291 + /* END LOOP */ 1.292 + // reset time, back to what it was 1.293 + if (prevTime) { 1.294 + self.setTime(prevTime); 1.295 + } 1.296 + return ia.join(""); 1.297 +} 1.298 + 1.299 +var date = new Date("1/1/2007 1:11:11"); 1.300 + 1.301 + /* BEGIN LOOP */ 1.302 +for (i = 0; i < 500; ++i) { 1.303 + var shortFormat = date.formatDate("Y-m-d"); 1.304 + var longFormat = date.formatDate("l, F d, Y g:i:s A"); 1.305 + date.setTime(date.getTime() + 84266956); 1.306 +} 1.307 + /* END LOOP */ 1.308 +