Advertisement
Guest User

Untitled

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