1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/devtools/jint/sunspider/date-format-xparb.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,427 @@ 1.4 +/* 1.5 + * Copyright (C) 2004 Baron Schwartz <baron at sequent dot org> 1.6 + * 1.7 + * This program is free software; you can redistribute it and/or modify it 1.8 + * under the terms of the GNU Lesser General Public License as published by the 1.9 + * Free Software Foundation, version 2.1. 1.10 + * 1.11 + * This program is distributed in the hope that it will be useful, but WITHOUT 1.12 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 1.13 + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 1.14 + * details. 1.15 + */ 1.16 + 1.17 +Date.parseFunctions = {count:0}; 1.18 +Date.parseRegexes = []; 1.19 +Date.formatFunctions = {count:0}; 1.20 + 1.21 +Date.prototype.dateFormat = function(format) { 1.22 + if (Date.formatFunctions[format] == null) { 1.23 + Date.createNewFormat(format); 1.24 + } 1.25 + var func = Date.formatFunctions[format]; 1.26 + return this[func](); 1.27 +} 1.28 + 1.29 +Date.createNewFormat = function(format) { 1.30 + var funcName = "format" + Date.formatFunctions.count++; 1.31 + Date.formatFunctions[format] = funcName; 1.32 + var code = "Date.prototype." + funcName + " = function(){return "; 1.33 + var special = false; 1.34 + var ch = ''; 1.35 + /* BEGIN LOOP */ 1.36 + for (var i = 0; i < format.length; ++i) { 1.37 + ch = format.charAt(i); 1.38 + if (!special && ch == "\\") { 1.39 + special = true; 1.40 + } 1.41 + else if (special) { 1.42 + special = false; 1.43 + code += "'" + String.escape(ch) + "' + "; 1.44 + } 1.45 + else { 1.46 + code += Date.getFormatCode(ch); 1.47 + } 1.48 + } 1.49 + /* END LOOP */ 1.50 + eval(code.substring(0, code.length - 3) + ";}"); 1.51 +} 1.52 + 1.53 +Date.getFormatCode = function(character) { 1.54 + switch (character) { 1.55 + case "d": 1.56 + return "String.leftPad(this.getDate(), 2, '0') + "; 1.57 + case "D": 1.58 + return "Date.dayNames[this.getDay()].substring(0, 3) + "; 1.59 + case "j": 1.60 + return "this.getDate() + "; 1.61 + case "l": 1.62 + return "Date.dayNames[this.getDay()] + "; 1.63 + case "S": 1.64 + return "this.getSuffix() + "; 1.65 + case "w": 1.66 + return "this.getDay() + "; 1.67 + case "z": 1.68 + return "this.getDayOfYear() + "; 1.69 + case "W": 1.70 + return "this.getWeekOfYear() + "; 1.71 + case "F": 1.72 + return "Date.monthNames[this.getMonth()] + "; 1.73 + case "m": 1.74 + return "String.leftPad(this.getMonth() + 1, 2, '0') + "; 1.75 + case "M": 1.76 + return "Date.monthNames[this.getMonth()].substring(0, 3) + "; 1.77 + case "n": 1.78 + return "(this.getMonth() + 1) + "; 1.79 + case "t": 1.80 + return "this.getDaysInMonth() + "; 1.81 + case "L": 1.82 + return "(this.isLeapYear() ? 1 : 0) + "; 1.83 + case "Y": 1.84 + return "this.getFullYear() + "; 1.85 + case "y": 1.86 + return "('' + this.getFullYear()).substring(2, 4) + "; 1.87 + case "a": 1.88 + return "(this.getHours() < 12 ? 'am' : 'pm') + "; 1.89 + case "A": 1.90 + return "(this.getHours() < 12 ? 'AM' : 'PM') + "; 1.91 + case "g": 1.92 + return "((this.getHours() %12) ? this.getHours() % 12 : 12) + "; 1.93 + case "G": 1.94 + return "this.getHours() + "; 1.95 + case "h": 1.96 + return "String.leftPad((this.getHours() %12) ? this.getHours() % 12 : 12, 2, '0') + "; 1.97 + case "H": 1.98 + return "String.leftPad(this.getHours(), 2, '0') + "; 1.99 + case "i": 1.100 + return "String.leftPad(this.getMinutes(), 2, '0') + "; 1.101 + case "s": 1.102 + return "String.leftPad(this.getSeconds(), 2, '0') + "; 1.103 + case "O": 1.104 + return "this.getGMTOffset() + "; 1.105 + case "T": 1.106 + return "this.getTimezone() + "; 1.107 + case "Z": 1.108 + return "(this.getTimezoneOffset() * -60) + "; 1.109 + default: 1.110 + return "'" + String.escape(character) + "' + "; 1.111 + } 1.112 +} 1.113 + 1.114 +Date.parseDate = function(input, format) { 1.115 + if (Date.parseFunctions[format] == null) { 1.116 + Date.createParser(format); 1.117 + } 1.118 + var func = Date.parseFunctions[format]; 1.119 + return Date[func](input); 1.120 +} 1.121 + 1.122 +Date.createParser = function(format) { 1.123 + var funcName = "parse" + Date.parseFunctions.count++; 1.124 + var regexNum = Date.parseRegexes.length; 1.125 + var currentGroup = 1; 1.126 + Date.parseFunctions[format] = funcName; 1.127 + 1.128 + var code = "Date." + funcName + " = function(input){\n" 1.129 + + "var y = -1, m = -1, d = -1, h = -1, i = -1, s = -1;\n" 1.130 + + "var d = new Date();\n" 1.131 + + "y = d.getFullYear();\n" 1.132 + + "m = d.getMonth();\n" 1.133 + + "d = d.getDate();\n" 1.134 + + "var results = input.match(Date.parseRegexes[" + regexNum + "]);\n" 1.135 + + "if (results && results.length > 0) {" 1.136 + var regex = ""; 1.137 + 1.138 + var special = false; 1.139 + var ch = ''; 1.140 + /* BEGIN LOOP */ 1.141 + for (var i = 0; i < format.length; ++i) { 1.142 + ch = format.charAt(i); 1.143 + if (!special && ch == "\\") { 1.144 + special = true; 1.145 + } 1.146 + else if (special) { 1.147 + special = false; 1.148 + regex += String.escape(ch); 1.149 + } 1.150 + else { 1.151 + obj = Date.formatCodeToRegex(ch, currentGroup); 1.152 + currentGroup += obj.g; 1.153 + regex += obj.s; 1.154 + if (obj.g && obj.c) { 1.155 + code += obj.c; 1.156 + } 1.157 + } 1.158 + } 1.159 + /* END LOOP */ 1.160 + 1.161 + code += "if (y > 0 && m >= 0 && d > 0 && h >= 0 && i >= 0 && s >= 0)\n" 1.162 + + "{return new Date(y, m, d, h, i, s);}\n" 1.163 + + "else if (y > 0 && m >= 0 && d > 0 && h >= 0 && i >= 0)\n" 1.164 + + "{return new Date(y, m, d, h, i);}\n" 1.165 + + "else if (y > 0 && m >= 0 && d > 0 && h >= 0)\n" 1.166 + + "{return new Date(y, m, d, h);}\n" 1.167 + + "else if (y > 0 && m >= 0 && d > 0)\n" 1.168 + + "{return new Date(y, m, d);}\n" 1.169 + + "else if (y > 0 && m >= 0)\n" 1.170 + + "{return new Date(y, m);}\n" 1.171 + + "else if (y > 0)\n" 1.172 + + "{return new Date(y);}\n" 1.173 + + "}return null;}"; 1.174 + 1.175 + Date.parseRegexes[regexNum] = new RegExp("^" + regex + "$"); 1.176 + eval(code); 1.177 +} 1.178 + 1.179 +Date.formatCodeToRegex = function(character, currentGroup) { 1.180 + switch (character) { 1.181 + case "D": 1.182 + return {g:0, 1.183 + c:null, 1.184 + s:"(?:Sun|Mon|Tue|Wed|Thu|Fri|Sat)"}; 1.185 + case "j": 1.186 + case "d": 1.187 + return {g:1, 1.188 + c:"d = parseInt(results[" + currentGroup + "], 10);\n", 1.189 + s:"(\\d{1,2})"}; 1.190 + case "l": 1.191 + return {g:0, 1.192 + c:null, 1.193 + s:"(?:" + Date.dayNames.join("|") + ")"}; 1.194 + case "S": 1.195 + return {g:0, 1.196 + c:null, 1.197 + s:"(?:st|nd|rd|th)"}; 1.198 + case "w": 1.199 + return {g:0, 1.200 + c:null, 1.201 + s:"\\d"}; 1.202 + case "z": 1.203 + return {g:0, 1.204 + c:null, 1.205 + s:"(?:\\d{1,3})"}; 1.206 + case "W": 1.207 + return {g:0, 1.208 + c:null, 1.209 + s:"(?:\\d{2})"}; 1.210 + case "F": 1.211 + return {g:1, 1.212 + c:"m = parseInt(Date.monthNumbers[results[" + currentGroup + "].substring(0, 3)], 10);\n", 1.213 + s:"(" + Date.monthNames.join("|") + ")"}; 1.214 + case "M": 1.215 + return {g:1, 1.216 + c:"m = parseInt(Date.monthNumbers[results[" + currentGroup + "]], 10);\n", 1.217 + s:"(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)"}; 1.218 + case "n": 1.219 + case "m": 1.220 + return {g:1, 1.221 + c:"m = parseInt(results[" + currentGroup + "], 10) - 1;\n", 1.222 + s:"(\\d{1,2})"}; 1.223 + case "t": 1.224 + return {g:0, 1.225 + c:null, 1.226 + s:"\\d{1,2}"}; 1.227 + case "L": 1.228 + return {g:0, 1.229 + c:null, 1.230 + s:"(?:1|0)"}; 1.231 + case "Y": 1.232 + return {g:1, 1.233 + c:"y = parseInt(results[" + currentGroup + "], 10);\n", 1.234 + s:"(\\d{4})"}; 1.235 + case "y": 1.236 + return {g:1, 1.237 + c:"var ty = parseInt(results[" + currentGroup + "], 10);\n" 1.238 + + "y = ty > Date.y2kYear ? 1900 + ty : 2000 + ty;\n", 1.239 + s:"(\\d{1,2})"}; 1.240 + case "a": 1.241 + return {g:1, 1.242 + c:"if (results[" + currentGroup + "] == 'am') {\n" 1.243 + + "if (h == 12) { h = 0; }\n" 1.244 + + "} else { if (h < 12) { h += 12; }}", 1.245 + s:"(am|pm)"}; 1.246 + case "A": 1.247 + return {g:1, 1.248 + c:"if (results[" + currentGroup + "] == 'AM') {\n" 1.249 + + "if (h == 12) { h = 0; }\n" 1.250 + + "} else { if (h < 12) { h += 12; }}", 1.251 + s:"(AM|PM)"}; 1.252 + case "g": 1.253 + case "G": 1.254 + case "h": 1.255 + case "H": 1.256 + return {g:1, 1.257 + c:"h = parseInt(results[" + currentGroup + "], 10);\n", 1.258 + s:"(\\d{1,2})"}; 1.259 + case "i": 1.260 + return {g:1, 1.261 + c:"i = parseInt(results[" + currentGroup + "], 10);\n", 1.262 + s:"(\\d{2})"}; 1.263 + case "s": 1.264 + return {g:1, 1.265 + c:"s = parseInt(results[" + currentGroup + "], 10);\n", 1.266 + s:"(\\d{2})"}; 1.267 + case "O": 1.268 + return {g:0, 1.269 + c:null, 1.270 + s:"[+-]\\d{4}"}; 1.271 + case "T": 1.272 + return {g:0, 1.273 + c:null, 1.274 + s:"[A-Z]{3}"}; 1.275 + case "Z": 1.276 + return {g:0, 1.277 + c:null, 1.278 + s:"[+-]\\d{1,5}"}; 1.279 + default: 1.280 + return {g:0, 1.281 + c:null, 1.282 + s:String.escape(character)}; 1.283 + } 1.284 +} 1.285 + 1.286 +Date.prototype.getTimezone = function() { 1.287 + return this.toString().replace( 1.288 + /^.*? ([A-Z]{3}) [0-9]{4}.*$/, "$1").replace( 1.289 + /^.*?\(([A-Z])[a-z]+ ([A-Z])[a-z]+ ([A-Z])[a-z]+\)$/, "$1$2$3"); 1.290 +} 1.291 + 1.292 +Date.prototype.getGMTOffset = function() { 1.293 + return (this.getTimezoneOffset() > 0 ? "-" : "+") 1.294 + + String.leftPad(Math.floor(this.getTimezoneOffset() / 60), 2, "0") 1.295 + + String.leftPad(this.getTimezoneOffset() % 60, 2, "0"); 1.296 +} 1.297 + 1.298 +Date.prototype.getDayOfYear = function() { 1.299 + var num = 0; 1.300 + Date.daysInMonth[1] = this.isLeapYear() ? 29 : 28; 1.301 + /* BEGIN LOOP */ 1.302 + for (var i = 0; i < this.getMonth(); ++i) { 1.303 + num += Date.daysInMonth[i]; 1.304 + } 1.305 + /* END LOOP */ 1.306 + return num + this.getDate() - 1; 1.307 +} 1.308 + 1.309 +Date.prototype.getWeekOfYear = function() { 1.310 + // Skip to Thursday of this week 1.311 + var now = this.getDayOfYear() + (4 - this.getDay()); 1.312 + // Find the first Thursday of the year 1.313 + var jan1 = new Date(this.getFullYear(), 0, 1); 1.314 + var then = (7 - jan1.getDay() + 4); 1.315 + document.write(then); 1.316 + return String.leftPad(((now - then) / 7) + 1, 2, "0"); 1.317 +} 1.318 + 1.319 +Date.prototype.isLeapYear = function() { 1.320 + var year = this.getFullYear(); 1.321 + return ((year & 3) == 0 && (year % 100 || (year % 400 == 0 && year))); 1.322 +} 1.323 + 1.324 +Date.prototype.getFirstDayOfMonth = function() { 1.325 + var day = (this.getDay() - (this.getDate() - 1)) % 7; 1.326 + return (day < 0) ? (day + 7) : day; 1.327 +} 1.328 + 1.329 +Date.prototype.getLastDayOfMonth = function() { 1.330 + var day = (this.getDay() + (Date.daysInMonth[this.getMonth()] - this.getDate())) % 7; 1.331 + return (day < 0) ? (day + 7) : day; 1.332 +} 1.333 + 1.334 +Date.prototype.getDaysInMonth = function() { 1.335 + Date.daysInMonth[1] = this.isLeapYear() ? 29 : 28; 1.336 + return Date.daysInMonth[this.getMonth()]; 1.337 +} 1.338 + 1.339 +Date.prototype.getSuffix = function() { 1.340 + switch (this.getDate()) { 1.341 + case 1: 1.342 + case 21: 1.343 + case 31: 1.344 + return "st"; 1.345 + case 2: 1.346 + case 22: 1.347 + return "nd"; 1.348 + case 3: 1.349 + case 23: 1.350 + return "rd"; 1.351 + default: 1.352 + return "th"; 1.353 + } 1.354 +} 1.355 + 1.356 +String.escape = function(string) { 1.357 + return string.replace(/('|\\)/g, "\\$1"); 1.358 +} 1.359 + 1.360 +String.leftPad = function (val, size, ch) { 1.361 + var result = new String(val); 1.362 + if (ch == null) { 1.363 + ch = " "; 1.364 + } 1.365 + /* BEGIN LOOP */ 1.366 + while (result.length < size) { 1.367 + result = ch + result; 1.368 + } 1.369 + /* END LOOP */ 1.370 + return result; 1.371 +} 1.372 + 1.373 +Date.daysInMonth = [31,28,31,30,31,30,31,31,30,31,30,31]; 1.374 +Date.monthNames = 1.375 + ["January", 1.376 + "February", 1.377 + "March", 1.378 + "April", 1.379 + "May", 1.380 + "June", 1.381 + "July", 1.382 + "August", 1.383 + "September", 1.384 + "October", 1.385 + "November", 1.386 + "December"]; 1.387 +Date.dayNames = 1.388 + ["Sunday", 1.389 + "Monday", 1.390 + "Tuesday", 1.391 + "Wednesday", 1.392 + "Thursday", 1.393 + "Friday", 1.394 + "Saturday"]; 1.395 +Date.y2kYear = 50; 1.396 +Date.monthNumbers = { 1.397 + Jan:0, 1.398 + Feb:1, 1.399 + Mar:2, 1.400 + Apr:3, 1.401 + May:4, 1.402 + Jun:5, 1.403 + Jul:6, 1.404 + Aug:7, 1.405 + Sep:8, 1.406 + Oct:9, 1.407 + Nov:10, 1.408 + Dec:11}; 1.409 +Date.patterns = { 1.410 + ISO8601LongPattern:"Y-m-d H:i:s", 1.411 + ISO8601ShortPattern:"Y-m-d", 1.412 + ShortDatePattern: "n/j/Y", 1.413 + LongDatePattern: "l, F d, Y", 1.414 + FullDateTimePattern: "l, F d, Y g:i:s A", 1.415 + MonthDayPattern: "F d", 1.416 + ShortTimePattern: "g:i A", 1.417 + LongTimePattern: "g:i:s A", 1.418 + SortableDateTimePattern: "Y-m-d\\TH:i:s", 1.419 + UniversalSortableDateTimePattern: "Y-m-d H:i:sO", 1.420 + YearMonthPattern: "F, Y"}; 1.421 + 1.422 +var date = new Date("1/1/2007 1:11:11"); 1.423 + 1.424 + /* BEGIN LOOP */ 1.425 +for (i = 0; i < 4000; ++i) { 1.426 + var shortFormat = date.dateFormat("Y-m-d"); 1.427 + var longFormat = date.dateFormat("l, F d, Y g:i:s A"); 1.428 + date.setTime(date.getTime() + 84266956); 1.429 +} 1.430 + /* END LOOP */