js/src/jit-test/tests/sunspider/check-date-format-xparb.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 /*
michael@0 2 * Copyright (C) 2004 Baron Schwartz <baron at sequent dot org>
michael@0 3 *
michael@0 4 * This program is free software; you can redistribute it and/or modify it
michael@0 5 * under the terms of the GNU Lesser General Public License as published by the
michael@0 6 * Free Software Foundation, version 2.1.
michael@0 7 *
michael@0 8 * This program is distributed in the hope that it will be useful, but WITHOUT
michael@0 9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
michael@0 10 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
michael@0 11 * details.
michael@0 12 */
michael@0 13
michael@0 14 Date.parseFunctions = {count:0};
michael@0 15 Date.parseRegexes = [];
michael@0 16 Date.formatFunctions = {count:0};
michael@0 17
michael@0 18 Date.prototype.dateFormat = function(format) {
michael@0 19 if (Date.formatFunctions[format] == null) {
michael@0 20 Date.createNewFormat(format);
michael@0 21 }
michael@0 22 var func = Date.formatFunctions[format];
michael@0 23 return this[func]();
michael@0 24 }
michael@0 25
michael@0 26 Date.createNewFormat = function(format) {
michael@0 27 var funcName = "format" + Date.formatFunctions.count++;
michael@0 28 Date.formatFunctions[format] = funcName;
michael@0 29 var code = "Date.prototype." + funcName + " = function(){return ";
michael@0 30 var special = false;
michael@0 31 var ch = '';
michael@0 32 for (var i = 0; i < format.length; ++i) {
michael@0 33 ch = format.charAt(i);
michael@0 34 if (!special && ch == "\\") {
michael@0 35 special = true;
michael@0 36 }
michael@0 37 else if (special) {
michael@0 38 special = false;
michael@0 39 code += "'" + String.escape(ch) + "' + ";
michael@0 40 }
michael@0 41 else {
michael@0 42 code += Date.getFormatCode(ch);
michael@0 43 }
michael@0 44 }
michael@0 45 eval(code.substring(0, code.length - 3) + ";}");
michael@0 46 }
michael@0 47
michael@0 48 Date.getFormatCode = function(character) {
michael@0 49 switch (character) {
michael@0 50 case "d":
michael@0 51 return "String.leftPad(this.getDate(), 2, '0') + ";
michael@0 52 case "D":
michael@0 53 return "Date.dayNames[this.getDay()].substring(0, 3) + ";
michael@0 54 case "j":
michael@0 55 return "this.getDate() + ";
michael@0 56 case "l":
michael@0 57 return "Date.dayNames[this.getDay()] + ";
michael@0 58 case "S":
michael@0 59 return "this.getSuffix() + ";
michael@0 60 case "w":
michael@0 61 return "this.getDay() + ";
michael@0 62 case "z":
michael@0 63 return "this.getDayOfYear() + ";
michael@0 64 case "W":
michael@0 65 return "this.getWeekOfYear() + ";
michael@0 66 case "F":
michael@0 67 return "Date.monthNames[this.getMonth()] + ";
michael@0 68 case "m":
michael@0 69 return "String.leftPad(this.getMonth() + 1, 2, '0') + ";
michael@0 70 case "M":
michael@0 71 return "Date.monthNames[this.getMonth()].substring(0, 3) + ";
michael@0 72 case "n":
michael@0 73 return "(this.getMonth() + 1) + ";
michael@0 74 case "t":
michael@0 75 return "this.getDaysInMonth() + ";
michael@0 76 case "L":
michael@0 77 return "(this.isLeapYear() ? 1 : 0) + ";
michael@0 78 case "Y":
michael@0 79 return "this.getFullYear() + ";
michael@0 80 case "y":
michael@0 81 return "('' + this.getFullYear()).substring(2, 4) + ";
michael@0 82 case "a":
michael@0 83 return "(this.getHours() < 12 ? 'am' : 'pm') + ";
michael@0 84 case "A":
michael@0 85 return "(this.getHours() < 12 ? 'AM' : 'PM') + ";
michael@0 86 case "g":
michael@0 87 return "((this.getHours() %12) ? this.getHours() % 12 : 12) + ";
michael@0 88 case "G":
michael@0 89 return "this.getHours() + ";
michael@0 90 case "h":
michael@0 91 return "String.leftPad((this.getHours() %12) ? this.getHours() % 12 : 12, 2, '0') + ";
michael@0 92 case "H":
michael@0 93 return "String.leftPad(this.getHours(), 2, '0') + ";
michael@0 94 case "i":
michael@0 95 return "String.leftPad(this.getMinutes(), 2, '0') + ";
michael@0 96 case "s":
michael@0 97 return "String.leftPad(this.getSeconds(), 2, '0') + ";
michael@0 98 case "O":
michael@0 99 return "this.getGMTOffset() + ";
michael@0 100 case "T":
michael@0 101 return "this.getTimezone() + ";
michael@0 102 case "Z":
michael@0 103 return "(this.getTimezoneOffset() * -60) + ";
michael@0 104 default:
michael@0 105 return "'" + String.escape(character) + "' + ";
michael@0 106 }
michael@0 107 }
michael@0 108
michael@0 109 Date.parseDate = function(input, format) {
michael@0 110 if (Date.parseFunctions[format] == null) {
michael@0 111 Date.createParser(format);
michael@0 112 }
michael@0 113 var func = Date.parseFunctions[format];
michael@0 114 return Date[func](input);
michael@0 115 }
michael@0 116
michael@0 117 Date.createParser = function(format) {
michael@0 118 var funcName = "parse" + Date.parseFunctions.count++;
michael@0 119 var regexNum = Date.parseRegexes.length;
michael@0 120 var currentGroup = 1;
michael@0 121 Date.parseFunctions[format] = funcName;
michael@0 122
michael@0 123 var code = "Date." + funcName + " = function(input){\n"
michael@0 124 + "var y = -1, m = -1, d = -1, h = -1, i = -1, s = -1;\n"
michael@0 125 + "var d = new Date();\n"
michael@0 126 + "y = d.getFullYear();\n"
michael@0 127 + "m = d.getMonth();\n"
michael@0 128 + "d = d.getDate();\n"
michael@0 129 + "var results = input.match(Date.parseRegexes[" + regexNum + "]);\n"
michael@0 130 + "if (results && results.length > 0) {"
michael@0 131 var regex = "";
michael@0 132
michael@0 133 var special = false;
michael@0 134 var ch = '';
michael@0 135 for (var i = 0; i < format.length; ++i) {
michael@0 136 ch = format.charAt(i);
michael@0 137 if (!special && ch == "\\") {
michael@0 138 special = true;
michael@0 139 }
michael@0 140 else if (special) {
michael@0 141 special = false;
michael@0 142 regex += String.escape(ch);
michael@0 143 }
michael@0 144 else {
michael@0 145 obj = Date.formatCodeToRegex(ch, currentGroup);
michael@0 146 currentGroup += obj.g;
michael@0 147 regex += obj.s;
michael@0 148 if (obj.g && obj.c) {
michael@0 149 code += obj.c;
michael@0 150 }
michael@0 151 }
michael@0 152 }
michael@0 153
michael@0 154 code += "if (y > 0 && m >= 0 && d > 0 && h >= 0 && i >= 0 && s >= 0)\n"
michael@0 155 + "{return new Date(y, m, d, h, i, s);}\n"
michael@0 156 + "else if (y > 0 && m >= 0 && d > 0 && h >= 0 && i >= 0)\n"
michael@0 157 + "{return new Date(y, m, d, h, i);}\n"
michael@0 158 + "else if (y > 0 && m >= 0 && d > 0 && h >= 0)\n"
michael@0 159 + "{return new Date(y, m, d, h);}\n"
michael@0 160 + "else if (y > 0 && m >= 0 && d > 0)\n"
michael@0 161 + "{return new Date(y, m, d);}\n"
michael@0 162 + "else if (y > 0 && m >= 0)\n"
michael@0 163 + "{return new Date(y, m);}\n"
michael@0 164 + "else if (y > 0)\n"
michael@0 165 + "{return new Date(y);}\n"
michael@0 166 + "}return null;}";
michael@0 167
michael@0 168 Date.parseRegexes[regexNum] = new RegExp("^" + regex + "$");
michael@0 169 eval(code);
michael@0 170 }
michael@0 171
michael@0 172 Date.formatCodeToRegex = function(character, currentGroup) {
michael@0 173 switch (character) {
michael@0 174 case "D":
michael@0 175 return {g:0,
michael@0 176 c:null,
michael@0 177 s:"(?:Sun|Mon|Tue|Wed|Thu|Fri|Sat)"};
michael@0 178 case "j":
michael@0 179 case "d":
michael@0 180 return {g:1,
michael@0 181 c:"d = parseInt(results[" + currentGroup + "], 10);\n",
michael@0 182 s:"(\\d{1,2})"};
michael@0 183 case "l":
michael@0 184 return {g:0,
michael@0 185 c:null,
michael@0 186 s:"(?:" + Date.dayNames.join("|") + ")"};
michael@0 187 case "S":
michael@0 188 return {g:0,
michael@0 189 c:null,
michael@0 190 s:"(?:st|nd|rd|th)"};
michael@0 191 case "w":
michael@0 192 return {g:0,
michael@0 193 c:null,
michael@0 194 s:"\\d"};
michael@0 195 case "z":
michael@0 196 return {g:0,
michael@0 197 c:null,
michael@0 198 s:"(?:\\d{1,3})"};
michael@0 199 case "W":
michael@0 200 return {g:0,
michael@0 201 c:null,
michael@0 202 s:"(?:\\d{2})"};
michael@0 203 case "F":
michael@0 204 return {g:1,
michael@0 205 c:"m = parseInt(Date.monthNumbers[results[" + currentGroup + "].substring(0, 3)], 10);\n",
michael@0 206 s:"(" + Date.monthNames.join("|") + ")"};
michael@0 207 case "M":
michael@0 208 return {g:1,
michael@0 209 c:"m = parseInt(Date.monthNumbers[results[" + currentGroup + "]], 10);\n",
michael@0 210 s:"(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)"};
michael@0 211 case "n":
michael@0 212 case "m":
michael@0 213 return {g:1,
michael@0 214 c:"m = parseInt(results[" + currentGroup + "], 10) - 1;\n",
michael@0 215 s:"(\\d{1,2})"};
michael@0 216 case "t":
michael@0 217 return {g:0,
michael@0 218 c:null,
michael@0 219 s:"\\d{1,2}"};
michael@0 220 case "L":
michael@0 221 return {g:0,
michael@0 222 c:null,
michael@0 223 s:"(?:1|0)"};
michael@0 224 case "Y":
michael@0 225 return {g:1,
michael@0 226 c:"y = parseInt(results[" + currentGroup + "], 10);\n",
michael@0 227 s:"(\\d{4})"};
michael@0 228 case "y":
michael@0 229 return {g:1,
michael@0 230 c:"var ty = parseInt(results[" + currentGroup + "], 10);\n"
michael@0 231 + "y = ty > Date.y2kYear ? 1900 + ty : 2000 + ty;\n",
michael@0 232 s:"(\\d{1,2})"};
michael@0 233 case "a":
michael@0 234 return {g:1,
michael@0 235 c:"if (results[" + currentGroup + "] == 'am') {\n"
michael@0 236 + "if (h == 12) { h = 0; }\n"
michael@0 237 + "} else { if (h < 12) { h += 12; }}",
michael@0 238 s:"(am|pm)"};
michael@0 239 case "A":
michael@0 240 return {g:1,
michael@0 241 c:"if (results[" + currentGroup + "] == 'AM') {\n"
michael@0 242 + "if (h == 12) { h = 0; }\n"
michael@0 243 + "} else { if (h < 12) { h += 12; }}",
michael@0 244 s:"(AM|PM)"};
michael@0 245 case "g":
michael@0 246 case "G":
michael@0 247 case "h":
michael@0 248 case "H":
michael@0 249 return {g:1,
michael@0 250 c:"h = parseInt(results[" + currentGroup + "], 10);\n",
michael@0 251 s:"(\\d{1,2})"};
michael@0 252 case "i":
michael@0 253 return {g:1,
michael@0 254 c:"i = parseInt(results[" + currentGroup + "], 10);\n",
michael@0 255 s:"(\\d{2})"};
michael@0 256 case "s":
michael@0 257 return {g:1,
michael@0 258 c:"s = parseInt(results[" + currentGroup + "], 10);\n",
michael@0 259 s:"(\\d{2})"};
michael@0 260 case "O":
michael@0 261 return {g:0,
michael@0 262 c:null,
michael@0 263 s:"[+-]\\d{4}"};
michael@0 264 case "T":
michael@0 265 return {g:0,
michael@0 266 c:null,
michael@0 267 s:"[A-Z]{3}"};
michael@0 268 case "Z":
michael@0 269 return {g:0,
michael@0 270 c:null,
michael@0 271 s:"[+-]\\d{1,5}"};
michael@0 272 default:
michael@0 273 return {g:0,
michael@0 274 c:null,
michael@0 275 s:String.escape(character)};
michael@0 276 }
michael@0 277 }
michael@0 278
michael@0 279 Date.prototype.getTimezone = function() {
michael@0 280 return this.toString().replace(
michael@0 281 /^.*? ([A-Z]{3}) [0-9]{4}.*$/, "$1").replace(
michael@0 282 /^.*?\(([A-Z])[a-z]+ ([A-Z])[a-z]+ ([A-Z])[a-z]+\)$/, "$1$2$3");
michael@0 283 }
michael@0 284
michael@0 285 Date.prototype.getGMTOffset = function() {
michael@0 286 return (this.getTimezoneOffset() > 0 ? "-" : "+")
michael@0 287 + String.leftPad(Math.floor(this.getTimezoneOffset() / 60), 2, "0")
michael@0 288 + String.leftPad(this.getTimezoneOffset() % 60, 2, "0");
michael@0 289 }
michael@0 290
michael@0 291 Date.prototype.getDayOfYear = function() {
michael@0 292 var num = 0;
michael@0 293 Date.daysInMonth[1] = this.isLeapYear() ? 29 : 28;
michael@0 294 for (var i = 0; i < this.getMonth(); ++i) {
michael@0 295 num += Date.daysInMonth[i];
michael@0 296 }
michael@0 297 return num + this.getDate() - 1;
michael@0 298 }
michael@0 299
michael@0 300 Date.prototype.getWeekOfYear = function() {
michael@0 301 // Skip to Thursday of this week
michael@0 302 var now = this.getDayOfYear() + (4 - this.getDay());
michael@0 303 // Find the first Thursday of the year
michael@0 304 var jan1 = new Date(this.getFullYear(), 0, 1);
michael@0 305 var then = (7 - jan1.getDay() + 4);
michael@0 306 document.write(then);
michael@0 307 return String.leftPad(((now - then) / 7) + 1, 2, "0");
michael@0 308 }
michael@0 309
michael@0 310 Date.prototype.isLeapYear = function() {
michael@0 311 var year = this.getFullYear();
michael@0 312 return ((year & 3) == 0 && (year % 100 || (year % 400 == 0 && year)));
michael@0 313 }
michael@0 314
michael@0 315 Date.prototype.getFirstDayOfMonth = function() {
michael@0 316 var day = (this.getDay() - (this.getDate() - 1)) % 7;
michael@0 317 return (day < 0) ? (day + 7) : day;
michael@0 318 }
michael@0 319
michael@0 320 Date.prototype.getLastDayOfMonth = function() {
michael@0 321 var day = (this.getDay() + (Date.daysInMonth[this.getMonth()] - this.getDate())) % 7;
michael@0 322 return (day < 0) ? (day + 7) : day;
michael@0 323 }
michael@0 324
michael@0 325 Date.prototype.getDaysInMonth = function() {
michael@0 326 Date.daysInMonth[1] = this.isLeapYear() ? 29 : 28;
michael@0 327 return Date.daysInMonth[this.getMonth()];
michael@0 328 }
michael@0 329
michael@0 330 Date.prototype.getSuffix = function() {
michael@0 331 switch (this.getDate()) {
michael@0 332 case 1:
michael@0 333 case 21:
michael@0 334 case 31:
michael@0 335 return "st";
michael@0 336 case 2:
michael@0 337 case 22:
michael@0 338 return "nd";
michael@0 339 case 3:
michael@0 340 case 23:
michael@0 341 return "rd";
michael@0 342 default:
michael@0 343 return "th";
michael@0 344 }
michael@0 345 }
michael@0 346
michael@0 347 String.escape = function(string) {
michael@0 348 return string.replace(/('|\\)/g, "\\$1");
michael@0 349 }
michael@0 350
michael@0 351 String.leftPad = function (val, size, ch) {
michael@0 352 var result = new String(val);
michael@0 353 if (ch == null) {
michael@0 354 ch = " ";
michael@0 355 }
michael@0 356 while (result.length < size) {
michael@0 357 result = ch + result;
michael@0 358 }
michael@0 359 return result;
michael@0 360 }
michael@0 361
michael@0 362 Date.daysInMonth = [31,28,31,30,31,30,31,31,30,31,30,31];
michael@0 363 Date.monthNames =
michael@0 364 ["January",
michael@0 365 "February",
michael@0 366 "March",
michael@0 367 "April",
michael@0 368 "May",
michael@0 369 "June",
michael@0 370 "July",
michael@0 371 "August",
michael@0 372 "September",
michael@0 373 "October",
michael@0 374 "November",
michael@0 375 "December"];
michael@0 376 Date.dayNames =
michael@0 377 ["Sunday",
michael@0 378 "Monday",
michael@0 379 "Tuesday",
michael@0 380 "Wednesday",
michael@0 381 "Thursday",
michael@0 382 "Friday",
michael@0 383 "Saturday"];
michael@0 384 Date.y2kYear = 50;
michael@0 385 Date.monthNumbers = {
michael@0 386 Jan:0,
michael@0 387 Feb:1,
michael@0 388 Mar:2,
michael@0 389 Apr:3,
michael@0 390 May:4,
michael@0 391 Jun:5,
michael@0 392 Jul:6,
michael@0 393 Aug:7,
michael@0 394 Sep:8,
michael@0 395 Oct:9,
michael@0 396 Nov:10,
michael@0 397 Dec:11};
michael@0 398 Date.patterns = {
michael@0 399 ISO8601LongPattern:"Y-m-d H:i:s",
michael@0 400 ISO8601ShortPattern:"Y-m-d",
michael@0 401 ShortDatePattern: "n/j/Y",
michael@0 402 LongDatePattern: "l, F d, Y",
michael@0 403 FullDateTimePattern: "l, F d, Y g:i:s A",
michael@0 404 MonthDayPattern: "F d",
michael@0 405 ShortTimePattern: "g:i A",
michael@0 406 LongTimePattern: "g:i:s A",
michael@0 407 SortableDateTimePattern: "Y-m-d\\TH:i:s",
michael@0 408 UniversalSortableDateTimePattern: "Y-m-d H:i:sO",
michael@0 409 YearMonthPattern: "F, Y"};
michael@0 410
michael@0 411 var date = new Date("1/1/2007 1:11:11");
michael@0 412
michael@0 413 var ret;
michael@0 414 for (i = 0; i < 4000; ++i) {
michael@0 415 var shortFormat = date.dateFormat("Y-m-d");
michael@0 416 var longFormat = date.dateFormat("l, F d, Y g:i:s A");
michael@0 417 ret = shortFormat + longFormat;
michael@0 418 date.setTime(date.getTime() + 84266956);
michael@0 419 }
michael@0 420
michael@0 421 // No exact match because the output depends on the locale's time zone. See bug 524490.
michael@0 422 assertEq(/^2017-09-05Tuesday, September 05, 2017 [0-9:]* AM$/.exec(ret) != null, true);

mercurial