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