date-format-xparb.js 14 KB

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