OLD | NEW |
(Empty) | |
| 1 <!DOCTYPE html> |
| 2 <head> |
| 3 <!-- |
| 4 Copyright (C) 2007 Apple Inc. All rights reserved. |
| 5 |
| 6 Redistribution and use in source and binary forms, with or without |
| 7 modification, are permitted provided that the following conditions |
| 8 are met: |
| 9 1. Redistributions of source code must retain the above copyright |
| 10 notice, this list of conditions and the following disclaimer. |
| 11 2. Redistributions in binary form must reproduce the above copyright |
| 12 notice, this list of conditions and the following disclaimer in the |
| 13 documentation and/or other materials provided with the distribution. |
| 14 |
| 15 THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY |
| 16 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 17 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 18 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR |
| 19 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 20 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 22 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 23 OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 25 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 --> |
| 27 |
| 28 <title>SunSpider date-format-xparb</title> |
| 29 <link rel="stylesheet" href="sunspider.css"> |
| 30 </head> |
| 31 |
| 32 <body> |
| 33 <h3>date-format-xparb</h3> |
| 34 <div id="console"> |
| 35 </div> |
| 36 <script src="sunspider-record-result.js"></script> |
| 37 <script> |
| 38 |
| 39 var _sunSpiderStartDate = new Date(); |
| 40 |
| 41 /* |
| 42 * Copyright (C) 2004 Baron Schwartz <baron at sequent dot org> |
| 43 * |
| 44 * This program is free software; you can redistribute it and/or modify it |
| 45 * under the terms of the GNU Lesser General Public License as published by the |
| 46 * Free Software Foundation, version 2.1. |
| 47 * |
| 48 * This program is distributed in the hope that it will be useful, but WITHOUT |
| 49 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 50 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
| 51 * details. |
| 52 */ |
| 53 |
| 54 Date.parseFunctions = {count:0}; |
| 55 Date.parseRegexes = []; |
| 56 Date.formatFunctions = {count:0}; |
| 57 |
| 58 Date.prototype.dateFormat = function(format) { |
| 59 if (Date.formatFunctions[format] == null) { |
| 60 Date.createNewFormat(format); |
| 61 } |
| 62 var func = Date.formatFunctions[format]; |
| 63 return this[func](); |
| 64 } |
| 65 |
| 66 Date.createNewFormat = function(format) { |
| 67 var funcName = "format" + Date.formatFunctions.count++; |
| 68 Date.formatFunctions[format] = funcName; |
| 69 var code = "Date.prototype." + funcName + " = function(){return "; |
| 70 var special = false; |
| 71 var ch = ''; |
| 72 for (var i = 0; i < format.length; ++i) { |
| 73 ch = format.charAt(i); |
| 74 if (!special && ch == "\\") { |
| 75 special = true; |
| 76 } |
| 77 else if (special) { |
| 78 special = false; |
| 79 code += "'" + String.escape(ch) + "' + "; |
| 80 } |
| 81 else { |
| 82 code += Date.getFormatCode(ch); |
| 83 } |
| 84 } |
| 85 eval(code.substring(0, code.length - 3) + ";}"); |
| 86 } |
| 87 |
| 88 Date.getFormatCode = function(character) { |
| 89 switch (character) { |
| 90 case "d": |
| 91 return "String.leftPad(this.getDate(), 2, '0') + "; |
| 92 case "D": |
| 93 return "Date.dayNames[this.getDay()].substring(0, 3) + "; |
| 94 case "j": |
| 95 return "this.getDate() + "; |
| 96 case "l": |
| 97 return "Date.dayNames[this.getDay()] + "; |
| 98 case "S": |
| 99 return "this.getSuffix() + "; |
| 100 case "w": |
| 101 return "this.getDay() + "; |
| 102 case "z": |
| 103 return "this.getDayOfYear() + "; |
| 104 case "W": |
| 105 return "this.getWeekOfYear() + "; |
| 106 case "F": |
| 107 return "Date.monthNames[this.getMonth()] + "; |
| 108 case "m": |
| 109 return "String.leftPad(this.getMonth() + 1, 2, '0') + "; |
| 110 case "M": |
| 111 return "Date.monthNames[this.getMonth()].substring(0, 3) + "; |
| 112 case "n": |
| 113 return "(this.getMonth() + 1) + "; |
| 114 case "t": |
| 115 return "this.getDaysInMonth() + "; |
| 116 case "L": |
| 117 return "(this.isLeapYear() ? 1 : 0) + "; |
| 118 case "Y": |
| 119 return "this.getFullYear() + "; |
| 120 case "y": |
| 121 return "('' + this.getFullYear()).substring(2, 4) + "; |
| 122 case "a": |
| 123 return "(this.getHours() < 12 ? 'am' : 'pm') + "; |
| 124 case "A": |
| 125 return "(this.getHours() < 12 ? 'AM' : 'PM') + "; |
| 126 case "g": |
| 127 return "((this.getHours() %12) ? this.getHours() % 12 : 12) + "; |
| 128 case "G": |
| 129 return "this.getHours() + "; |
| 130 case "h": |
| 131 return "String.leftPad((this.getHours() %12) ? this.getHours() % 12 : 12
, 2, '0') + "; |
| 132 case "H": |
| 133 return "String.leftPad(this.getHours(), 2, '0') + "; |
| 134 case "i": |
| 135 return "String.leftPad(this.getMinutes(), 2, '0') + "; |
| 136 case "s": |
| 137 return "String.leftPad(this.getSeconds(), 2, '0') + "; |
| 138 case "O": |
| 139 return "this.getGMTOffset() + "; |
| 140 case "T": |
| 141 return "this.getTimezone() + "; |
| 142 case "Z": |
| 143 return "(this.getTimezoneOffset() * -60) + "; |
| 144 default: |
| 145 return "'" + String.escape(character) + "' + "; |
| 146 } |
| 147 } |
| 148 |
| 149 Date.parseDate = function(input, format) { |
| 150 if (Date.parseFunctions[format] == null) { |
| 151 Date.createParser(format); |
| 152 } |
| 153 var func = Date.parseFunctions[format]; |
| 154 return Date[func](input); |
| 155 } |
| 156 |
| 157 Date.createParser = function(format) { |
| 158 var funcName = "parse" + Date.parseFunctions.count++; |
| 159 var regexNum = Date.parseRegexes.length; |
| 160 var currentGroup = 1; |
| 161 Date.parseFunctions[format] = funcName; |
| 162 |
| 163 var code = "Date." + funcName + " = function(input){\n" |
| 164 + "var y = -1, m = -1, d = -1, h = -1, i = -1, s = -1;\n" |
| 165 + "var d = new Date();\n" |
| 166 + "y = d.getFullYear();\n" |
| 167 + "m = d.getMonth();\n" |
| 168 + "d = d.getDate();\n" |
| 169 + "var results = input.match(Date.parseRegexes[" + regexNum + "]);\n" |
| 170 + "if (results && results.length > 0) {" |
| 171 var regex = ""; |
| 172 |
| 173 var special = false; |
| 174 var ch = ''; |
| 175 for (var i = 0; i < format.length; ++i) { |
| 176 ch = format.charAt(i); |
| 177 if (!special && ch == "\\") { |
| 178 special = true; |
| 179 } |
| 180 else if (special) { |
| 181 special = false; |
| 182 regex += String.escape(ch); |
| 183 } |
| 184 else { |
| 185 obj = Date.formatCodeToRegex(ch, currentGroup); |
| 186 currentGroup += obj.g; |
| 187 regex += obj.s; |
| 188 if (obj.g && obj.c) { |
| 189 code += obj.c; |
| 190 } |
| 191 } |
| 192 } |
| 193 |
| 194 code += "if (y > 0 && m >= 0 && d > 0 && h >= 0 && i >= 0 && s >= 0)\n" |
| 195 + "{return new Date(y, m, d, h, i, s);}\n" |
| 196 + "else if (y > 0 && m >= 0 && d > 0 && h >= 0 && i >= 0)\n" |
| 197 + "{return new Date(y, m, d, h, i);}\n" |
| 198 + "else if (y > 0 && m >= 0 && d > 0 && h >= 0)\n" |
| 199 + "{return new Date(y, m, d, h);}\n" |
| 200 + "else if (y > 0 && m >= 0 && d > 0)\n" |
| 201 + "{return new Date(y, m, d);}\n" |
| 202 + "else if (y > 0 && m >= 0)\n" |
| 203 + "{return new Date(y, m);}\n" |
| 204 + "else if (y > 0)\n" |
| 205 + "{return new Date(y);}\n" |
| 206 + "}return null;}"; |
| 207 |
| 208 Date.parseRegexes[regexNum] = new RegExp("^" + regex + "$"); |
| 209 eval(code); |
| 210 } |
| 211 |
| 212 Date.formatCodeToRegex = function(character, currentGroup) { |
| 213 switch (character) { |
| 214 case "D": |
| 215 return {g:0, |
| 216 c:null, |
| 217 s:"(?:Sun|Mon|Tue|Wed|Thu|Fri|Sat)"}; |
| 218 case "j": |
| 219 case "d": |
| 220 return {g:1, |
| 221 c:"d = parseInt(results[" + currentGroup + "], 10);\n", |
| 222 s:"(\\d{1,2})"}; |
| 223 case "l": |
| 224 return {g:0, |
| 225 c:null, |
| 226 s:"(?:" + Date.dayNames.join("|") + ")"}; |
| 227 case "S": |
| 228 return {g:0, |
| 229 c:null, |
| 230 s:"(?:st|nd|rd|th)"}; |
| 231 case "w": |
| 232 return {g:0, |
| 233 c:null, |
| 234 s:"\\d"}; |
| 235 case "z": |
| 236 return {g:0, |
| 237 c:null, |
| 238 s:"(?:\\d{1,3})"}; |
| 239 case "W": |
| 240 return {g:0, |
| 241 c:null, |
| 242 s:"(?:\\d{2})"}; |
| 243 case "F": |
| 244 return {g:1, |
| 245 c:"m = parseInt(Date.monthNumbers[results[" + currentGroup + "].subs
tring(0, 3)], 10);\n", |
| 246 s:"(" + Date.monthNames.join("|") + ")"}; |
| 247 case "M": |
| 248 return {g:1, |
| 249 c:"m = parseInt(Date.monthNumbers[results[" + currentGroup + "]], 10
);\n", |
| 250 s:"(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)"}; |
| 251 case "n": |
| 252 case "m": |
| 253 return {g:1, |
| 254 c:"m = parseInt(results[" + currentGroup + "], 10) - 1;\n", |
| 255 s:"(\\d{1,2})"}; |
| 256 case "t": |
| 257 return {g:0, |
| 258 c:null, |
| 259 s:"\\d{1,2}"}; |
| 260 case "L": |
| 261 return {g:0, |
| 262 c:null, |
| 263 s:"(?:1|0)"}; |
| 264 case "Y": |
| 265 return {g:1, |
| 266 c:"y = parseInt(results[" + currentGroup + "], 10);\n", |
| 267 s:"(\\d{4})"}; |
| 268 case "y": |
| 269 return {g:1, |
| 270 c:"var ty = parseInt(results[" + currentGroup + "], 10);\n" |
| 271 + "y = ty > Date.y2kYear ? 1900 + ty : 2000 + ty;\n", |
| 272 s:"(\\d{1,2})"}; |
| 273 case "a": |
| 274 return {g:1, |
| 275 c:"if (results[" + currentGroup + "] == 'am') {\n" |
| 276 + "if (h == 12) { h = 0; }\n" |
| 277 + "} else { if (h < 12) { h += 12; }}", |
| 278 s:"(am|pm)"}; |
| 279 case "A": |
| 280 return {g:1, |
| 281 c:"if (results[" + currentGroup + "] == 'AM') {\n" |
| 282 + "if (h == 12) { h = 0; }\n" |
| 283 + "} else { if (h < 12) { h += 12; }}", |
| 284 s:"(AM|PM)"}; |
| 285 case "g": |
| 286 case "G": |
| 287 case "h": |
| 288 case "H": |
| 289 return {g:1, |
| 290 c:"h = parseInt(results[" + currentGroup + "], 10);\n", |
| 291 s:"(\\d{1,2})"}; |
| 292 case "i": |
| 293 return {g:1, |
| 294 c:"i = parseInt(results[" + currentGroup + "], 10);\n", |
| 295 s:"(\\d{2})"}; |
| 296 case "s": |
| 297 return {g:1, |
| 298 c:"s = parseInt(results[" + currentGroup + "], 10);\n", |
| 299 s:"(\\d{2})"}; |
| 300 case "O": |
| 301 return {g:0, |
| 302 c:null, |
| 303 s:"[+-]\\d{4}"}; |
| 304 case "T": |
| 305 return {g:0, |
| 306 c:null, |
| 307 s:"[A-Z]{3}"}; |
| 308 case "Z": |
| 309 return {g:0, |
| 310 c:null, |
| 311 s:"[+-]\\d{1,5}"}; |
| 312 default: |
| 313 return {g:0, |
| 314 c:null, |
| 315 s:String.escape(character)}; |
| 316 } |
| 317 } |
| 318 |
| 319 Date.prototype.getTimezone = function() { |
| 320 return this.toString().replace( |
| 321 /^.*? ([A-Z]{3}) [0-9]{4}.*$/, "$1").replace( |
| 322 /^.*?\(([A-Z])[a-z]+ ([A-Z])[a-z]+ ([A-Z])[a-z]+\)$/, "$1$2$3"); |
| 323 } |
| 324 |
| 325 Date.prototype.getGMTOffset = function() { |
| 326 return (this.getTimezoneOffset() > 0 ? "-" : "+") |
| 327 + String.leftPad(Math.floor(this.getTimezoneOffset() / 60), 2, "0") |
| 328 + String.leftPad(this.getTimezoneOffset() % 60, 2, "0"); |
| 329 } |
| 330 |
| 331 Date.prototype.getDayOfYear = function() { |
| 332 var num = 0; |
| 333 Date.daysInMonth[1] = this.isLeapYear() ? 29 : 28; |
| 334 for (var i = 0; i < this.getMonth(); ++i) { |
| 335 num += Date.daysInMonth[i]; |
| 336 } |
| 337 return num + this.getDate() - 1; |
| 338 } |
| 339 |
| 340 Date.prototype.getWeekOfYear = function() { |
| 341 // Skip to Thursday of this week |
| 342 var now = this.getDayOfYear() + (4 - this.getDay()); |
| 343 // Find the first Thursday of the year |
| 344 var jan1 = new Date(this.getFullYear(), 0, 1); |
| 345 var then = (7 - jan1.getDay() + 4); |
| 346 document.write(then); |
| 347 return String.leftPad(((now - then) / 7) + 1, 2, "0"); |
| 348 } |
| 349 |
| 350 Date.prototype.isLeapYear = function() { |
| 351 var year = this.getFullYear(); |
| 352 return ((year & 3) == 0 && (year % 100 || (year % 400 == 0 && year))); |
| 353 } |
| 354 |
| 355 Date.prototype.getFirstDayOfMonth = function() { |
| 356 var day = (this.getDay() - (this.getDate() - 1)) % 7; |
| 357 return (day < 0) ? (day + 7) : day; |
| 358 } |
| 359 |
| 360 Date.prototype.getLastDayOfMonth = function() { |
| 361 var day = (this.getDay() + (Date.daysInMonth[this.getMonth()] - this.getDate
())) % 7; |
| 362 return (day < 0) ? (day + 7) : day; |
| 363 } |
| 364 |
| 365 Date.prototype.getDaysInMonth = function() { |
| 366 Date.daysInMonth[1] = this.isLeapYear() ? 29 : 28; |
| 367 return Date.daysInMonth[this.getMonth()]; |
| 368 } |
| 369 |
| 370 Date.prototype.getSuffix = function() { |
| 371 switch (this.getDate()) { |
| 372 case 1: |
| 373 case 21: |
| 374 case 31: |
| 375 return "st"; |
| 376 case 2: |
| 377 case 22: |
| 378 return "nd"; |
| 379 case 3: |
| 380 case 23: |
| 381 return "rd"; |
| 382 default: |
| 383 return "th"; |
| 384 } |
| 385 } |
| 386 |
| 387 String.escape = function(string) { |
| 388 return string.replace(/('|\\)/g, "\\$1"); |
| 389 } |
| 390 |
| 391 String.leftPad = function (val, size, ch) { |
| 392 var result = new String(val); |
| 393 if (ch == null) { |
| 394 ch = " "; |
| 395 } |
| 396 while (result.length < size) { |
| 397 result = ch + result; |
| 398 } |
| 399 return result; |
| 400 } |
| 401 |
| 402 Date.daysInMonth = [31,28,31,30,31,30,31,31,30,31,30,31]; |
| 403 Date.monthNames = |
| 404 ["January", |
| 405 "February", |
| 406 "March", |
| 407 "April", |
| 408 "May", |
| 409 "June", |
| 410 "July", |
| 411 "August", |
| 412 "September", |
| 413 "October", |
| 414 "November", |
| 415 "December"]; |
| 416 Date.dayNames = |
| 417 ["Sunday", |
| 418 "Monday", |
| 419 "Tuesday", |
| 420 "Wednesday", |
| 421 "Thursday", |
| 422 "Friday", |
| 423 "Saturday"]; |
| 424 Date.y2kYear = 50; |
| 425 Date.monthNumbers = { |
| 426 Jan:0, |
| 427 Feb:1, |
| 428 Mar:2, |
| 429 Apr:3, |
| 430 May:4, |
| 431 Jun:5, |
| 432 Jul:6, |
| 433 Aug:7, |
| 434 Sep:8, |
| 435 Oct:9, |
| 436 Nov:10, |
| 437 Dec:11}; |
| 438 Date.patterns = { |
| 439 ISO8601LongPattern:"Y-m-d H:i:s", |
| 440 ISO8601ShortPattern:"Y-m-d", |
| 441 ShortDatePattern: "n/j/Y", |
| 442 LongDatePattern: "l, F d, Y", |
| 443 FullDateTimePattern: "l, F d, Y g:i:s A", |
| 444 MonthDayPattern: "F d", |
| 445 ShortTimePattern: "g:i A", |
| 446 LongTimePattern: "g:i:s A", |
| 447 SortableDateTimePattern: "Y-m-d\\TH:i:s", |
| 448 UniversalSortableDateTimePattern: "Y-m-d H:i:sO", |
| 449 YearMonthPattern: "F, Y"}; |
| 450 |
| 451 var date = new Date("1/1/2007 1:11:11"); |
| 452 |
| 453 for (i = 0; i < 4000; ++i) { |
| 454 var shortFormat = date.dateFormat("Y-m-d"); |
| 455 var longFormat = date.dateFormat("l, F d, Y g:i:s A"); |
| 456 date.setTime(date.getTime() + 84266956); |
| 457 } |
| 458 |
| 459 |
| 460 var _sunSpiderInterval = new Date() - _sunSpiderStartDate; |
| 461 |
| 462 record(_sunSpiderInterval); |
| 463 </script> |
| 464 |
| 465 |
| 466 </body> |
| 467 </html> |
OLD | NEW |