date-format-xparb.html 13 KB

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