date-format-tofte.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. function arrayExists(array, x) {
  37. for (var i = 0; i < array.length; i++) {
  38. if (array[i] == x) return true;
  39. }
  40. return false;
  41. }
  42. Date.prototype.formatDate = function (input,time) {
  43. // formatDate :
  44. // a PHP date like function, for formatting date strings
  45. // See: http://www.php.net/date
  46. //
  47. // input : format string
  48. // time : epoch time (seconds, and optional)
  49. //
  50. // if time is not passed, formatting is based on
  51. // the current "this" date object's set time.
  52. //
  53. // supported:
  54. // a, A, B, d, D, F, g, G, h, H, i, j, l (lowercase L), L,
  55. // m, M, n, O, r, s, S, t, U, w, W, y, Y, z
  56. //
  57. // unsupported:
  58. // I (capital i), T, Z
  59. var switches = ["a", "A", "B", "d", "D", "F", "g", "G", "h", "H",
  60. "i", "j", "l", "L", "m", "M", "n", "O", "r", "s",
  61. "S", "t", "U", "w", "W", "y", "Y", "z"];
  62. var daysLong = ["Sunday", "Monday", "Tuesday", "Wednesday",
  63. "Thursday", "Friday", "Saturday"];
  64. var daysShort = ["Sun", "Mon", "Tue", "Wed",
  65. "Thu", "Fri", "Sat"];
  66. var monthsShort = ["Jan", "Feb", "Mar", "Apr",
  67. "May", "Jun", "Jul", "Aug", "Sep",
  68. "Oct", "Nov", "Dec"];
  69. var monthsLong = ["January", "February", "March", "April",
  70. "May", "June", "July", "August", "September",
  71. "October", "November", "December"];
  72. var daysSuffix = ["st", "nd", "rd", "th", "th", "th", "th", // 1st - 7th
  73. "th", "th", "th", "th", "th", "th", "th", // 8th - 14th
  74. "th", "th", "th", "th", "th", "th", "st", // 15th - 21st
  75. "nd", "rd", "th", "th", "th", "th", "th", // 22nd - 28th
  76. "th", "th", "st"]; // 29th - 31st
  77. function a() {
  78. // Lowercase Ante meridiem and Post meridiem
  79. return self.getHours() > 11? "pm" : "am";
  80. }
  81. function A() {
  82. // Uppercase Ante meridiem and Post meridiem
  83. return self.getHours() > 11? "PM" : "AM";
  84. }
  85. function B(){
  86. // Swatch internet time. code simply grabbed from ppk,
  87. // since I was feeling lazy:
  88. // http://www.xs4all.nl/~ppk/js/beat.html
  89. var off = (self.getTimezoneOffset() + 60)*60;
  90. var theSeconds = (self.getHours() * 3600) +
  91. (self.getMinutes() * 60) +
  92. self.getSeconds() + off;
  93. var beat = Math.floor(theSeconds/86.4);
  94. if (beat > 1000) beat -= 1000;
  95. if (beat < 0) beat += 1000;
  96. if ((""+beat).length == 1) beat = "00"+beat;
  97. if ((""+beat).length == 2) beat = "0"+beat;
  98. return beat;
  99. }
  100. function d() {
  101. // Day of the month, 2 digits with leading zeros
  102. return new String(self.getDate()).length == 1?
  103. "0"+self.getDate() : self.getDate();
  104. }
  105. function D() {
  106. // A textual representation of a day, three letters
  107. return daysShort[self.getDay()];
  108. }
  109. function F() {
  110. // A full textual representation of a month
  111. return monthsLong[self.getMonth()];
  112. }
  113. function g() {
  114. // 12-hour format of an hour without leading zeros
  115. return self.getHours() > 12? self.getHours()-12 : self.getHours();
  116. }
  117. function G() {
  118. // 24-hour format of an hour without leading zeros
  119. return self.getHours();
  120. }
  121. function h() {
  122. // 12-hour format of an hour with leading zeros
  123. if (self.getHours() > 12) {
  124. var s = new String(self.getHours()-12);
  125. return s.length == 1?
  126. "0"+ (self.getHours()-12) : self.getHours()-12;
  127. } else {
  128. var s = new String(self.getHours());
  129. return s.length == 1?
  130. "0"+self.getHours() : self.getHours();
  131. }
  132. }
  133. function H() {
  134. // 24-hour format of an hour with leading zeros
  135. return new String(self.getHours()).length == 1?
  136. "0"+self.getHours() : self.getHours();
  137. }
  138. function i() {
  139. // Minutes with leading zeros
  140. return new String(self.getMinutes()).length == 1?
  141. "0"+self.getMinutes() : self.getMinutes();
  142. }
  143. function j() {
  144. // Day of the month without leading zeros
  145. return self.getDate();
  146. }
  147. function l() {
  148. // A full textual representation of the day of the week
  149. return daysLong[self.getDay()];
  150. }
  151. function L() {
  152. // leap year or not. 1 if leap year, 0 if not.
  153. // the logic should match iso's 8601 standard.
  154. var y_ = Y();
  155. if (
  156. (y_ % 4 == 0 && y_ % 100 != 0) ||
  157. (y_ % 4 == 0 && y_ % 100 == 0 && y_ % 400 == 0)
  158. ) {
  159. return 1;
  160. } else {
  161. return 0;
  162. }
  163. }
  164. function m() {
  165. // Numeric representation of a month, with leading zeros
  166. return self.getMonth() < 9?
  167. "0"+(self.getMonth()+1) :
  168. self.getMonth()+1;
  169. }
  170. function M() {
  171. // A short textual representation of a month, three letters
  172. return monthsShort[self.getMonth()];
  173. }
  174. function n() {
  175. // Numeric representation of a month, without leading zeros
  176. return self.getMonth()+1;
  177. }
  178. function O() {
  179. // Difference to Greenwich time (GMT) in hours
  180. var os = Math.abs(self.getTimezoneOffset());
  181. var h = ""+Math.floor(os/60);
  182. var m = ""+(os%60);
  183. h.length == 1? h = "0"+h:1;
  184. m.length == 1? m = "0"+m:1;
  185. return self.getTimezoneOffset() < 0 ? "+"+h+m : "-"+h+m;
  186. }
  187. function r() {
  188. // RFC 822 formatted date
  189. var r; // result
  190. // Thu , 21 Dec 2000
  191. r = D() + ", " + j() + " " + M() + " " + Y() +
  192. // 16 : 01 : 07 +0200
  193. " " + H() + ":" + i() + ":" + s() + " " + O();
  194. return r;
  195. }
  196. function S() {
  197. // English ordinal suffix for the day of the month, 2 characters
  198. return daysSuffix[self.getDate()-1];
  199. }
  200. function s() {
  201. // Seconds, with leading zeros
  202. return new String(self.getSeconds()).length == 1?
  203. "0"+self.getSeconds() : self.getSeconds();
  204. }
  205. function t() {
  206. // thanks to Matt Bannon for some much needed code-fixes here!
  207. var daysinmonths = [null,31,28,31,30,31,30,31,31,30,31,30,31];
  208. if (L()==1 && n()==2) return 29; // leap day
  209. return daysinmonths[n()];
  210. }
  211. function U() {
  212. // Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)
  213. return Math.round(self.getTime()/1000);
  214. }
  215. function W() {
  216. // Weeknumber, as per ISO specification:
  217. // http://www.cl.cam.ac.uk/~mgk25/iso-time.html
  218. // if the day is three days before newyears eve,
  219. // there's a chance it's "week 1" of next year.
  220. // here we check for that.
  221. var beforeNY = 364+L() - z();
  222. var afterNY = z();
  223. var weekday = w()!=0?w()-1:6; // makes sunday (0), into 6.
  224. if (beforeNY <= 2 && weekday <= 2-beforeNY) {
  225. return 1;
  226. }
  227. // similarly, if the day is within threedays of newyears
  228. // there's a chance it belongs in the old year.
  229. var ny = new Date("January 1 " + Y() + " 00:00:00");
  230. var nyDay = ny.getDay()!=0?ny.getDay()-1:6;
  231. if (
  232. (afterNY <= 2) &&
  233. (nyDay >=4) &&
  234. (afterNY >= (6-nyDay))
  235. ) {
  236. // Since I'm not sure we can just always return 53,
  237. // i call the function here again, using the last day
  238. // of the previous year, as the date, and then just
  239. // return that week.
  240. var prevNY = new Date("December 31 " + (Y()-1) + " 00:00:00");
  241. return prevNY.formatDate("W");
  242. }
  243. // week 1, is the week that has the first thursday in it.
  244. // note that this value is not zero index.
  245. if (nyDay <= 3) {
  246. // first day of the year fell on a thursday, or earlier.
  247. return 1 + Math.floor( ( z() + nyDay ) / 7 );
  248. } else {
  249. // first day of the year fell on a friday, or later.
  250. return 1 + Math.floor( ( z() - ( 7 - nyDay ) ) / 7 );
  251. }
  252. }
  253. function w() {
  254. // Numeric representation of the day of the week
  255. return self.getDay();
  256. }
  257. function Y() {
  258. // A full numeric representation of a year, 4 digits
  259. // we first check, if getFullYear is supported. if it
  260. // is, we just use that. ppks code is nice, but wont
  261. // work with dates outside 1900-2038, or something like that
  262. if (self.getFullYear) {
  263. var newDate = new Date("January 1 2001 00:00:00 +0000");
  264. var x = newDate .getFullYear();
  265. if (x == 2001) {
  266. // i trust the method now
  267. return self.getFullYear();
  268. }
  269. }
  270. // else, do this:
  271. // codes thanks to ppk:
  272. // http://www.xs4all.nl/~ppk/js/introdate.html
  273. var x = self.getYear();
  274. var y = x % 100;
  275. y += (y < 38) ? 2000 : 1900;
  276. return y;
  277. }
  278. function y() {
  279. // A two-digit representation of a year
  280. var y = Y()+"";
  281. return y.substring(y.length-2,y.length);
  282. }
  283. function z() {
  284. // The day of the year, zero indexed! 0 through 366
  285. var t = new Date("January 1 " + Y() + " 00:00:00");
  286. var diff = self.getTime() - t.getTime();
  287. return Math.floor(diff/1000/60/60/24);
  288. }
  289. var self = this;
  290. if (time) {
  291. // save time
  292. var prevTime = self.getTime();
  293. self.setTime(time);
  294. }
  295. var ia = input.split("");
  296. var ij = 0;
  297. while (ia[ij]) {
  298. if (ia[ij] == "\\\\") {
  299. // this is our way of allowing users to escape stuff
  300. ia.splice(ij,1);
  301. } else {
  302. if (arrayExists(switches,ia[ij])) {
  303. ia[ij] = eval(ia[ij] + "()");
  304. }
  305. }
  306. ij++;
  307. }
  308. // reset time, back to what it was
  309. if (prevTime) {
  310. self.setTime(prevTime);
  311. }
  312. return ia.join("");
  313. }
  314. var date = new Date("1/1/2007 1:11:11");
  315. for (i = 0; i < 500; ++i) {
  316. var shortFormat = date.formatDate("Y-m-d");
  317. var longFormat = date.formatDate("l, F d, Y g:i:s A");
  318. date.setTime(date.getTime() + 84266956);
  319. }
  320. // FIXME: Find a way to validate this test.
  321. // https://bugs.webkit.org/show_bug.cgi?id=114849
  322. var _sunSpiderInterval = new Date() - _sunSpiderStartDate;
  323. WScript.Echo("### TIME:", _sunSpiderInterval, "ms");