PageRenderTime 55ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/sunspider1/date-format-tofte.js

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