PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/cfa2/sunspider/check-date-format-xparb.js

https://github.com/rwaldron/doctorjs
JavaScript | 422 lines | 386 code | 21 blank | 15 comment | 25 complexity | 8f39975b5c55dd4111f8ffdad0144dd6 MD5 | raw file
  1. /*
  2. * Copyright (C) 2004 Baron Schwartz <baron at sequent dot org>
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU Lesser General Public License as published by the
  6. * Free Software Foundation, version 2.1.
  7. *
  8. * This program is distributed in the hope that it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  10. * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  11. * details.
  12. */
  13. Date.parseFunctions = {count:0};
  14. Date.parseRegexes = [];
  15. Date.formatFunctions = {count:0};
  16. Date.prototype.dateFormat = function(format) {
  17. if (Date.formatFunctions[format] == null) {
  18. Date.createNewFormat(format);
  19. }
  20. var func = Date.formatFunctions[format];
  21. return this[func]();
  22. }
  23. Date.createNewFormat = function(format) {
  24. var funcName = "format" + Date.formatFunctions.count++;
  25. Date.formatFunctions[format] = funcName;
  26. var code = "Date.prototype." + funcName + " = function(){return ";
  27. var special = false;
  28. var ch = '';
  29. for (var i = 0; i < format.length; ++i) {
  30. ch = format.charAt(i);
  31. if (!special && ch == "\\") {
  32. special = true;
  33. }
  34. else if (special) {
  35. special = false;
  36. code += "'" + String.escape(ch) + "' + ";
  37. }
  38. else {
  39. code += Date.getFormatCode(ch);
  40. }
  41. }
  42. eval(code.substring(0, code.length - 3) + ";}");
  43. }
  44. Date.getFormatCode = function(character) {
  45. switch (character) {
  46. case "d":
  47. return "String.leftPad(this.getDate(), 2, '0') + ";
  48. case "D":
  49. return "Date.dayNames[this.getDay()].substring(0, 3) + ";
  50. case "j":
  51. return "this.getDate() + ";
  52. case "l":
  53. return "Date.dayNames[this.getDay()] + ";
  54. case "S":
  55. return "this.getSuffix() + ";
  56. case "w":
  57. return "this.getDay() + ";
  58. case "z":
  59. return "this.getDayOfYear() + ";
  60. case "W":
  61. return "this.getWeekOfYear() + ";
  62. case "F":
  63. return "Date.monthNames[this.getMonth()] + ";
  64. case "m":
  65. return "String.leftPad(this.getMonth() + 1, 2, '0') + ";
  66. case "M":
  67. return "Date.monthNames[this.getMonth()].substring(0, 3) + ";
  68. case "n":
  69. return "(this.getMonth() + 1) + ";
  70. case "t":
  71. return "this.getDaysInMonth() + ";
  72. case "L":
  73. return "(this.isLeapYear() ? 1 : 0) + ";
  74. case "Y":
  75. return "this.getFullYear() + ";
  76. case "y":
  77. return "('' + this.getFullYear()).substring(2, 4) + ";
  78. case "a":
  79. return "(this.getHours() < 12 ? 'am' : 'pm') + ";
  80. case "A":
  81. return "(this.getHours() < 12 ? 'AM' : 'PM') + ";
  82. case "g":
  83. return "((this.getHours() %12) ? this.getHours() % 12 : 12) + ";
  84. case "G":
  85. return "this.getHours() + ";
  86. case "h":
  87. return "String.leftPad((this.getHours() %12) ? this.getHours() % 12 : 12, 2, '0') + ";
  88. case "H":
  89. return "String.leftPad(this.getHours(), 2, '0') + ";
  90. case "i":
  91. return "String.leftPad(this.getMinutes(), 2, '0') + ";
  92. case "s":
  93. return "String.leftPad(this.getSeconds(), 2, '0') + ";
  94. case "O":
  95. return "this.getGMTOffset() + ";
  96. case "T":
  97. return "this.getTimezone() + ";
  98. case "Z":
  99. return "(this.getTimezoneOffset() * -60) + ";
  100. default:
  101. return "'" + String.escape(character) + "' + ";
  102. }
  103. }
  104. Date.parseDate = function(input, format) {
  105. if (Date.parseFunctions[format] == null) {
  106. Date.createParser(format);
  107. }
  108. var func = Date.parseFunctions[format];
  109. return Date[func](input);
  110. }
  111. Date.createParser = function(format) {
  112. var funcName = "parse" + Date.parseFunctions.count++;
  113. var regexNum = Date.parseRegexes.length;
  114. var currentGroup = 1;
  115. Date.parseFunctions[format] = funcName;
  116. var code = "Date." + funcName + " = function(input){\n"
  117. + "var y = -1, m = -1, d = -1, h = -1, i = -1, s = -1;\n"
  118. + "var d = new Date();\n"
  119. + "y = d.getFullYear();\n"
  120. + "m = d.getMonth();\n"
  121. + "d = d.getDate();\n"
  122. + "var results = input.match(Date.parseRegexes[" + regexNum + "]);\n"
  123. + "if (results && results.length > 0) {"
  124. var regex = "";
  125. var special = false;
  126. var ch = '';
  127. for (var i = 0; i < format.length; ++i) {
  128. ch = format.charAt(i);
  129. if (!special && ch == "\\") {
  130. special = true;
  131. }
  132. else if (special) {
  133. special = false;
  134. regex += String.escape(ch);
  135. }
  136. else {
  137. obj = Date.formatCodeToRegex(ch, currentGroup);
  138. currentGroup += obj.g;
  139. regex += obj.s;
  140. if (obj.g && obj.c) {
  141. code += obj.c;
  142. }
  143. }
  144. }
  145. code += "if (y > 0 && m >= 0 && d > 0 && h >= 0 && i >= 0 && s >= 0)\n"
  146. + "{return new Date(y, m, d, h, i, s);}\n"
  147. + "else if (y > 0 && m >= 0 && d > 0 && h >= 0 && i >= 0)\n"
  148. + "{return new Date(y, m, d, h, i);}\n"
  149. + "else if (y > 0 && m >= 0 && d > 0 && h >= 0)\n"
  150. + "{return new Date(y, m, d, h);}\n"
  151. + "else if (y > 0 && m >= 0 && d > 0)\n"
  152. + "{return new Date(y, m, d);}\n"
  153. + "else if (y > 0 && m >= 0)\n"
  154. + "{return new Date(y, m);}\n"
  155. + "else if (y > 0)\n"
  156. + "{return new Date(y);}\n"
  157. + "}return null;}";
  158. Date.parseRegexes[regexNum] = new RegExp("^" + regex + "$");
  159. eval(code);
  160. }
  161. Date.formatCodeToRegex = function(character, currentGroup) {
  162. switch (character) {
  163. case "D":
  164. return {g:0,
  165. c:null,
  166. s:"(?:Sun|Mon|Tue|Wed|Thu|Fri|Sat)"};
  167. case "j":
  168. case "d":
  169. return {g:1,
  170. c:"d = parseInt(results[" + currentGroup + "], 10);\n",
  171. s:"(\\d{1,2})"};
  172. case "l":
  173. return {g:0,
  174. c:null,
  175. s:"(?:" + Date.dayNames.join("|") + ")"};
  176. case "S":
  177. return {g:0,
  178. c:null,
  179. s:"(?:st|nd|rd|th)"};
  180. case "w":
  181. return {g:0,
  182. c:null,
  183. s:"\\d"};
  184. case "z":
  185. return {g:0,
  186. c:null,
  187. s:"(?:\\d{1,3})"};
  188. case "W":
  189. return {g:0,
  190. c:null,
  191. s:"(?:\\d{2})"};
  192. case "F":
  193. return {g:1,
  194. c:"m = parseInt(Date.monthNumbers[results[" + currentGroup + "].substring(0, 3)], 10);\n",
  195. s:"(" + Date.monthNames.join("|") + ")"};
  196. case "M":
  197. return {g:1,
  198. c:"m = parseInt(Date.monthNumbers[results[" + currentGroup + "]], 10);\n",
  199. s:"(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)"};
  200. case "n":
  201. case "m":
  202. return {g:1,
  203. c:"m = parseInt(results[" + currentGroup + "], 10) - 1;\n",
  204. s:"(\\d{1,2})"};
  205. case "t":
  206. return {g:0,
  207. c:null,
  208. s:"\\d{1,2}"};
  209. case "L":
  210. return {g:0,
  211. c:null,
  212. s:"(?:1|0)"};
  213. case "Y":
  214. return {g:1,
  215. c:"y = parseInt(results[" + currentGroup + "], 10);\n",
  216. s:"(\\d{4})"};
  217. case "y":
  218. return {g:1,
  219. c:"var ty = parseInt(results[" + currentGroup + "], 10);\n"
  220. + "y = ty > Date.y2kYear ? 1900 + ty : 2000 + ty;\n",
  221. s:"(\\d{1,2})"};
  222. case "a":
  223. return {g:1,
  224. c:"if (results[" + currentGroup + "] == 'am') {\n"
  225. + "if (h == 12) { h = 0; }\n"
  226. + "} else { if (h < 12) { h += 12; }}",
  227. s:"(am|pm)"};
  228. case "A":
  229. return {g:1,
  230. c:"if (results[" + currentGroup + "] == 'AM') {\n"
  231. + "if (h == 12) { h = 0; }\n"
  232. + "} else { if (h < 12) { h += 12; }}",
  233. s:"(AM|PM)"};
  234. case "g":
  235. case "G":
  236. case "h":
  237. case "H":
  238. return {g:1,
  239. c:"h = parseInt(results[" + currentGroup + "], 10);\n",
  240. s:"(\\d{1,2})"};
  241. case "i":
  242. return {g:1,
  243. c:"i = parseInt(results[" + currentGroup + "], 10);\n",
  244. s:"(\\d{2})"};
  245. case "s":
  246. return {g:1,
  247. c:"s = parseInt(results[" + currentGroup + "], 10);\n",
  248. s:"(\\d{2})"};
  249. case "O":
  250. return {g:0,
  251. c:null,
  252. s:"[+-]\\d{4}"};
  253. case "T":
  254. return {g:0,
  255. c:null,
  256. s:"[A-Z]{3}"};
  257. case "Z":
  258. return {g:0,
  259. c:null,
  260. s:"[+-]\\d{1,5}"};
  261. default:
  262. return {g:0,
  263. c:null,
  264. s:String.escape(character)};
  265. }
  266. }
  267. Date.prototype.getTimezone = function() {
  268. return this.toString().replace(
  269. /^.*? ([A-Z]{3}) [0-9]{4}.*$/, "$1").replace(
  270. /^.*?\(([A-Z])[a-z]+ ([A-Z])[a-z]+ ([A-Z])[a-z]+\)$/, "$1$2$3");
  271. }
  272. Date.prototype.getGMTOffset = function() {
  273. return (this.getTimezoneOffset() > 0 ? "-" : "+")
  274. + String.leftPad(Math.floor(this.getTimezoneOffset() / 60), 2, "0")
  275. + String.leftPad(this.getTimezoneOffset() % 60, 2, "0");
  276. }
  277. Date.prototype.getDayOfYear = function() {
  278. var num = 0;
  279. Date.daysInMonth[1] = this.isLeapYear() ? 29 : 28;
  280. for (var i = 0; i < this.getMonth(); ++i) {
  281. num += Date.daysInMonth[i];
  282. }
  283. return num + this.getDate() - 1;
  284. }
  285. Date.prototype.getWeekOfYear = function() {
  286. // Skip to Thursday of this week
  287. var now = this.getDayOfYear() + (4 - this.getDay());
  288. // Find the first Thursday of the year
  289. var jan1 = new Date(this.getFullYear(), 0, 1);
  290. var then = (7 - jan1.getDay() + 4);
  291. document.write(then);
  292. return String.leftPad(((now - then) / 7) + 1, 2, "0");
  293. }
  294. Date.prototype.isLeapYear = function() {
  295. var year = this.getFullYear();
  296. return ((year & 3) == 0 && (year % 100 || (year % 400 == 0 && year)));
  297. }
  298. Date.prototype.getFirstDayOfMonth = function() {
  299. var day = (this.getDay() - (this.getDate() - 1)) % 7;
  300. return (day < 0) ? (day + 7) : day;
  301. }
  302. Date.prototype.getLastDayOfMonth = function() {
  303. var day = (this.getDay() + (Date.daysInMonth[this.getMonth()] - this.getDate())) % 7;
  304. return (day < 0) ? (day + 7) : day;
  305. }
  306. Date.prototype.getDaysInMonth = function() {
  307. Date.daysInMonth[1] = this.isLeapYear() ? 29 : 28;
  308. return Date.daysInMonth[this.getMonth()];
  309. }
  310. Date.prototype.getSuffix = function() {
  311. switch (this.getDate()) {
  312. case 1:
  313. case 21:
  314. case 31:
  315. return "st";
  316. case 2:
  317. case 22:
  318. return "nd";
  319. case 3:
  320. case 23:
  321. return "rd";
  322. default:
  323. return "th";
  324. }
  325. }
  326. String.escape = function(string) {
  327. return string.replace(/('|\\)/g, "\\$1");
  328. }
  329. String.leftPad = function (val, size, ch) {
  330. var result = new String(val);
  331. if (ch == null) {
  332. ch = " ";
  333. }
  334. while (result.length < size) {
  335. result = ch + result;
  336. }
  337. return result;
  338. }
  339. Date.daysInMonth = [31,28,31,30,31,30,31,31,30,31,30,31];
  340. Date.monthNames =
  341. ["January",
  342. "February",
  343. "March",
  344. "April",
  345. "May",
  346. "June",
  347. "July",
  348. "August",
  349. "September",
  350. "October",
  351. "November",
  352. "December"];
  353. Date.dayNames =
  354. ["Sunday",
  355. "Monday",
  356. "Tuesday",
  357. "Wednesday",
  358. "Thursday",
  359. "Friday",
  360. "Saturday"];
  361. Date.y2kYear = 50;
  362. Date.monthNumbers = {
  363. Jan:0,
  364. Feb:1,
  365. Mar:2,
  366. Apr:3,
  367. May:4,
  368. Jun:5,
  369. Jul:6,
  370. Aug:7,
  371. Sep:8,
  372. Oct:9,
  373. Nov:10,
  374. Dec:11};
  375. Date.patterns = {
  376. ISO8601LongPattern:"Y-m-d H:i:s",
  377. ISO8601ShortPattern:"Y-m-d",
  378. ShortDatePattern: "n/j/Y",
  379. LongDatePattern: "l, F d, Y",
  380. FullDateTimePattern: "l, F d, Y g:i:s A",
  381. MonthDayPattern: "F d",
  382. ShortTimePattern: "g:i A",
  383. LongTimePattern: "g:i:s A",
  384. SortableDateTimePattern: "Y-m-d\\TH:i:s",
  385. UniversalSortableDateTimePattern: "Y-m-d H:i:sO",
  386. YearMonthPattern: "F, Y"};
  387. var date = new Date("1/1/2007 1:11:11");
  388. var ret;
  389. for (i = 0; i < 4000; ++i) {
  390. var shortFormat = date.dateFormat("Y-m-d");
  391. var longFormat = date.dateFormat("l, F d, Y g:i:s A");
  392. ret = shortFormat + longFormat;
  393. date.setTime(date.getTime() + 84266956);
  394. }
  395. // No exact match because the output depends on the locale's time zone. See bug 524490.
  396. assertEq(/^2017-09-05Tuesday, September 05, 2017 [0-9:]* AM$/.exec(ret) != null, true);