/js/src/metrics/jint/sunspider/date-format-tofte.js

http://github.com/zpao/v8monkey · JavaScript · 305 lines · 211 code · 15 blank · 79 comment · 49 complexity · 40e7aac34f4011a5922b6b4558502227 MD5 · raw file

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