Date-min.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. /*
  2. * Ext JS Library 2.0 Copyright(c) 2006-2007, Ext JS, LLC. licensing@extjs.com
  3. *
  4. * http://extjs.com/license
  5. */
  6. Date.parseFunctions = {
  7. count : 0
  8. };
  9. Date.parseRegexes = [];
  10. Date.formatFunctions = {
  11. count : 0
  12. };
  13. Date.prototype.dateFormat = function(B) {
  14. if (Date.formatFunctions[B] == null) {
  15. Date.createNewFormat(B)
  16. }
  17. var A = Date.formatFunctions[B];
  18. return this[A]()
  19. };
  20. Date.prototype.format = Date.prototype.dateFormat;
  21. Date.createNewFormat = function(format) {
  22. var funcName = "format" + Date.formatFunctions.count++;
  23. Date.formatFunctions[format] = funcName;
  24. var code = "Date.prototype." + funcName + " = function(){return ";
  25. var special = false;
  26. var ch = "";
  27. for (var i = 0; i < format.length; ++i) {
  28. ch = format.charAt(i);
  29. if (!special && ch == "\\") {
  30. special = true
  31. } else {
  32. if (special) {
  33. special = false;
  34. code += "'" + String.escape(ch) + "' + "
  35. } else {
  36. code += Date.getFormatCode(ch)
  37. }
  38. }
  39. }
  40. eval(code.substring(0, code.length - 3) + ";}")
  41. };
  42. Date.getFormatCode = function(D) {
  43. switch (D) {
  44. case "d" :
  45. return "String.leftPad(this.getDate(), 2, '0') + ";
  46. case "D" :
  47. return "Date.getShortDayName(this.getDay()) + ";
  48. case "j" :
  49. return "this.getDate() + ";
  50. case "l" :
  51. return "Date.dayNames[this.getDay()] + ";
  52. case "N" :
  53. return "(this.getDay() ? this.getDay() : 7) + ";
  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 "String.leftPad(this.getWeekOfYear(), 2, '0') + ";
  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.getShortMonthName(this.getMonth()) + ";
  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 "o" :
  75. return "(this.getFullYear() + (this.getWeekOfYear() == 1 && this.getMonth() > 0 ? +1 : (this.getWeekOfYear() >= 52 && this.getMonth() < 11 ? -1 : 0))) + ";
  76. case "Y" :
  77. return "this.getFullYear() + ";
  78. case "y" :
  79. return "('' + this.getFullYear()).substring(2, 4) + ";
  80. case "a" :
  81. return "(this.getHours() < 12 ? 'am' : 'pm') + ";
  82. case "A" :
  83. return "(this.getHours() < 12 ? 'AM' : 'PM') + ";
  84. case "g" :
  85. return "((this.getHours() % 12) ? this.getHours() % 12 : 12) + ";
  86. case "G" :
  87. return "this.getHours() + ";
  88. case "h" :
  89. return "String.leftPad((this.getHours() % 12) ? this.getHours() % 12 : 12, 2, '0') + ";
  90. case "H" :
  91. return "String.leftPad(this.getHours(), 2, '0') + ";
  92. case "i" :
  93. return "String.leftPad(this.getMinutes(), 2, '0') + ";
  94. case "s" :
  95. return "String.leftPad(this.getSeconds(), 2, '0') + ";
  96. case "u" :
  97. return "String.leftPad(this.getMilliseconds(), 3, '0') + ";
  98. case "O" :
  99. return "this.getGMTOffset() + ";
  100. case "P" :
  101. return "this.getGMTOffset(true) + ";
  102. case "T" :
  103. return "this.getTimezone() + ";
  104. case "Z" :
  105. return "(this.getTimezoneOffset() * -60) + ";
  106. case "c" :
  107. for (var F = Date.getFormatCode, G = "Y-m-dTH:i:sP", C = "", B = 0, A = G.length; B < A; ++B) {
  108. var E = G.charAt(B);
  109. C += E == "T" ? "'T' + " : F(E)
  110. }
  111. return C;
  112. case "U" :
  113. return "Math.round(this.getTime() / 1000) + ";
  114. default :
  115. return "'" + String.escape(D) + "' + "
  116. }
  117. };
  118. Date.parseDate = function(A, C) {
  119. if (Date.parseFunctions[C] == null) {
  120. Date.createParser(C)
  121. }
  122. var B = Date.parseFunctions[C];
  123. return Date[B](A)
  124. };
  125. Date.createParser = function(format) {
  126. var funcName = "parse" + Date.parseFunctions.count++;
  127. var regexNum = Date.parseRegexes.length;
  128. var currentGroup = 1;
  129. Date.parseFunctions[format] = funcName;
  130. var code = "Date."
  131. + funcName
  132. + " = function(input){\n"
  133. + "var y = -1, m = -1, d = -1, h = -1, i = -1, s = -1, ms = -1, o, z, u, v;\n"
  134. + "var d = allGetServerTime();\n" + "y = d.getFullYear();\n"
  135. + "m = d.getMonth();\n" + "d = d.getDate();\n"
  136. + "var results = input.match(Date.parseRegexes[" + regexNum
  137. + "]);\n" + "if (results && results.length > 0) {";
  138. var regex = "";
  139. var special = false;
  140. var ch = "";
  141. for (var i = 0; i < format.length; ++i) {
  142. ch = format.charAt(i);
  143. if (!special && ch == "\\") {
  144. special = true
  145. } else {
  146. if (special) {
  147. special = false;
  148. regex += String.escape(ch)
  149. } else {
  150. var obj = Date.formatCodeToRegex(ch, currentGroup);
  151. currentGroup += obj.g;
  152. regex += obj.s;
  153. if (obj.g && obj.c) {
  154. code += obj.c
  155. }
  156. }
  157. }
  158. }
  159. code += "if (u)\n"
  160. + "{v = new Date(u * 1000);}"
  161. + "else if (y >= 0 && m >= 0 && d > 0 && h >= 0 && i >= 0 && s >= 0 && ms >= 0)\n"
  162. + "{v = new Date(y, m, d, h, i, s, ms);}\n"
  163. + "else if (y >= 0 && m >= 0 && d > 0 && h >= 0 && i >= 0 && s >= 0)\n"
  164. + "{v = new Date(y, m, d, h, i, s);}\n"
  165. + "else if (y >= 0 && m >= 0 && d > 0 && h >= 0 && i >= 0)\n"
  166. + "{v = new Date(y, m, d, h, i);}\n"
  167. + "else if (y >= 0 && m >= 0 && d > 0 && h >= 0)\n"
  168. + "{v = new Date(y, m, d, h);}\n"
  169. + "else if (y >= 0 && m >= 0 && d > 0)\n"
  170. + "{v = new Date(y, m, d);}\n"
  171. + "else if (y >= 0 && m >= 0)\n"
  172. + "{v = new Date(y, m);}\n"
  173. + "else if (y >= 0)\n"
  174. + "{v = new Date(y);}\n"
  175. + "}return (v && (z || o))?\n"
  176. + " (z ? v.add(Date.SECOND, (v.getTimezoneOffset() * 60) + (z*1)) :\n"
  177. + " v.add(Date.HOUR, (v.getGMTOffset() / 100) + (o / -100))) : v\n"
  178. + ";}";
  179. Date.parseRegexes[regexNum] = new RegExp("^" + regex + "$", "i");
  180. eval(code)
  181. };
  182. Date.formatCodeToRegex = function(G, F) {
  183. switch (G) {
  184. case "d" :
  185. return {
  186. g : 1,
  187. c : "d = parseInt(results[" + F + "], 10);\n",
  188. s : "(\\d{2})"
  189. };
  190. case "D" :
  191. for (var C = [], E = 0; E < 7; C.push(Date.getShortDayName(E)), ++E) {
  192. }
  193. return {
  194. g : 0,
  195. c : null,
  196. s : "(?:" + C.join("|") + ")"
  197. };
  198. case "j" :
  199. return {
  200. g : 1,
  201. c : "d = parseInt(results[" + F + "], 10);\n",
  202. s : "(\\d{1,2})"
  203. };
  204. case "l" :
  205. return {
  206. g : 0,
  207. c : null,
  208. s : "(?:" + Date.dayNames.join("|") + ")"
  209. };
  210. case "N" :
  211. return {
  212. g : 0,
  213. c : null,
  214. s : "[1-7]"
  215. };
  216. case "S" :
  217. return {
  218. g : 0,
  219. c : null,
  220. s : "(?:st|nd|rd|th)"
  221. };
  222. case "w" :
  223. return {
  224. g : 0,
  225. c : null,
  226. s : "[0-6]"
  227. };
  228. case "z" :
  229. return {
  230. g : 0,
  231. c : null,
  232. s : "(?:\\d{1,3}"
  233. };
  234. case "W" :
  235. return {
  236. g : 0,
  237. c : null,
  238. s : "(?:\\d{2})"
  239. };
  240. case "F" :
  241. return {
  242. g : 1,
  243. c : "m = parseInt(Date.getMonthNumber(results[" + F
  244. + "]), 10);\n",
  245. s : "(" + Date.monthNames.join("|") + ")"
  246. };
  247. case "m" :
  248. return {
  249. g : 1,
  250. c : "m = parseInt(results[" + F + "], 10) - 1;\n",
  251. s : "(\\d{2})"
  252. };
  253. case "M" :
  254. for (var C = [], E = 0; E < 12; C.push(Date.getShortMonthName(E)), ++E) {
  255. }
  256. return {
  257. g : 1,
  258. c : "m = parseInt(Date.getMonthNumber(results[" + F
  259. + "]), 10);\n",
  260. s : "(" + C.join("|") + ")"
  261. };
  262. case "n" :
  263. return {
  264. g : 1,
  265. c : "m = parseInt(results[" + F + "], 10) - 1;\n",
  266. s : "(\\d{1,2})"
  267. };
  268. case "t" :
  269. return {
  270. g : 0,
  271. c : null,
  272. s : "(?:\\d{2})"
  273. };
  274. case "L" :
  275. return {
  276. g : 0,
  277. c : null,
  278. s : "(?:1|0)"
  279. };
  280. case "o" :
  281. case "Y" :
  282. return {
  283. g : 1,
  284. c : "y = parseInt(results[" + F + "], 10);\n",
  285. s : "(\\d{4})"
  286. };
  287. case "y" :
  288. return {
  289. g : 1,
  290. c : "var ty = parseInt(results[" + F + "], 10);\n"
  291. + "y = ty > Date.y2kYear ? 1900 + ty : 2000 + ty;\n",
  292. s : "(\\d{1,2})"
  293. };
  294. case "a" :
  295. return {
  296. g : 1,
  297. c : "if (results[" + F + "] == 'am') {\n"
  298. + "if (h == 12) { h = 0; }\n"
  299. + "} else { if (h < 12) { h += 12; }}",
  300. s : "(am|pm)"
  301. };
  302. case "A" :
  303. return {
  304. g : 1,
  305. c : "if (results[" + F + "] == 'AM') {\n"
  306. + "if (h == 12) { h = 0; }\n"
  307. + "} else { if (h < 12) { h += 12; }}",
  308. s : "(AM|PM)"
  309. };
  310. case "g" :
  311. case "G" :
  312. return {
  313. g : 1,
  314. c : "h = parseInt(results[" + F + "], 10);\n",
  315. s : "(\\d{1,2})"
  316. };
  317. case "h" :
  318. case "H" :
  319. return {
  320. g : 1,
  321. c : "h = parseInt(results[" + F + "], 10);\n",
  322. s : "(\\d{2})"
  323. };
  324. case "i" :
  325. return {
  326. g : 1,
  327. c : "i = parseInt(results[" + F + "], 10);\n",
  328. s : "(\\d{2})"
  329. };
  330. case "s" :
  331. return {
  332. g : 1,
  333. c : "s = parseInt(results[" + F + "], 10);\n",
  334. s : "(\\d{2})"
  335. };
  336. case "u" :
  337. return {
  338. g : 1,
  339. c : "ms = parseInt(results[" + F + "], 10);\n",
  340. s : "(\\d{3})"
  341. };
  342. case "O" :
  343. return {
  344. g : 1,
  345. c : [
  346. "o = results[",
  347. F,
  348. "];\n",
  349. "var sn = o.substring(0,1);\n",
  350. "var hr = o.substring(1,3)*1 + Math.floor(o.substring(3,5) / 60);\n",
  351. "var mn = o.substring(3,5) % 60;\n",
  352. "o = ((-12 <= (hr*60 + mn)/60) && ((hr*60 + mn)/60 <= 14))?\n",
  353. " (sn + String.leftPad(hr, 2, 0) + String.leftPad(mn, 2, 0)) : null;\n"]
  354. .join(""),
  355. s : "([+-]\\d{4})"
  356. };
  357. case "P" :
  358. return {
  359. g : 1,
  360. c : [
  361. "o = results[",
  362. F,
  363. "];\n",
  364. "var sn = o.substring(0,1);\n",
  365. "var hr = o.substring(1,3)*1 + Math.floor(o.substring(4,6) / 60);\n",
  366. "var mn = o.substring(4,6) % 60;\n",
  367. "o = ((-12 <= (hr*60 + mn)/60) && ((hr*60 + mn)/60 <= 14))?\n",
  368. " (sn + String.leftPad(hr, 2, 0) + String.leftPad(mn, 2, 0)) : null;\n"]
  369. .join(""),
  370. s : "([+-]\\d{2}:\\d{2})"
  371. };
  372. case "T" :
  373. return {
  374. g : 0,
  375. c : null,
  376. s : "[A-Z]{1,4}"
  377. };
  378. case "Z" :
  379. return {
  380. g : 1,
  381. c : "z = results[" + F + "] * 1;\n"
  382. + "z = (-43200 <= z && z <= 50400)? z : null;\n",
  383. s : "([+-]?\\d{1,5})"
  384. };
  385. case "c" :
  386. var H = Date.formatCodeToRegex, D = [];
  387. var A = [H("Y", 1), H("m", 2), H("d", 3), H("h", 4), H("i", 5),
  388. H("s", 6), H("P", 7)];
  389. for (var E = 0, B = A.length; E < B; ++E) {
  390. D.push(A[E].c)
  391. }
  392. return {
  393. g : 1,
  394. c : D.join(""),
  395. s : A[0].s + "-" + A[1].s + "-" + A[2].s + "T" + A[3].s + ":"
  396. + A[4].s + ":" + A[5].s + A[6].s
  397. };
  398. case "U" :
  399. return {
  400. g : 1,
  401. c : "u = parseInt(results[" + F + "], 10);\n",
  402. s : "(-?\\d+)"
  403. };
  404. default :
  405. return {
  406. g : 0,
  407. c : null,
  408. s : Ext.escapeRe(G)
  409. }
  410. }
  411. };
  412. Date.prototype.getTimezone = function() {
  413. return this.toString().replace(
  414. /^.* (?:\((.*)\)|([A-Z]{1,4})(?:[\-+][0-9]{4})?(?: -?\d+)?)$/,
  415. "$1$2").replace(/[^A-Z]/g, "")
  416. };
  417. Date.prototype.getGMTOffset = function(A) {
  418. return (this.getTimezoneOffset() > 0 ? "-" : "+")
  419. + String.leftPad(Math
  420. .abs(Math.floor(this.getTimezoneOffset() / 60)), 2,
  421. "0") + (A ? ":" : "")
  422. + String.leftPad(this.getTimezoneOffset() % 60, 2, "0")
  423. };
  424. Date.prototype.getDayOfYear = function() {
  425. var A = 0;
  426. Date.daysInMonth[1] = this.isLeapYear() ? 29 : 28;
  427. for (var B = 0; B < this.getMonth(); ++B) {
  428. A += Date.daysInMonth[B]
  429. }
  430. return A + this.getDate() - 1
  431. };
  432. Date.prototype.getWeekOfYear = function() {
  433. var B = 86400000;
  434. var C = 7 * B;
  435. var D = Date.UTC(this.getFullYear(), this.getMonth(), this.getDate() + 3)
  436. / B;
  437. var A = Math.floor(D / 7);
  438. var E = new Date(A * C).getUTCFullYear();
  439. return A - Math.floor(Date.UTC(E, 0, 7) / C) + 1
  440. };
  441. Date.prototype.isLeapYear = function() {
  442. var A = this.getFullYear();
  443. return ((A & 3) == 0 && (A % 100 || (A % 400 == 0 && A)))
  444. };
  445. Date.prototype.getFirstDayOfMonth = function() {
  446. var A = (this.getDay() - (this.getDate() - 1)) % 7;
  447. return (A < 0) ? (A + 7) : A
  448. };
  449. Date.prototype.getLastDayOfMonth = function() {
  450. var A = (this.getDay() + (Date.daysInMonth[this.getMonth()] - this
  451. .getDate()))
  452. % 7;
  453. return (A < 0) ? (A + 7) : A
  454. };
  455. Date.prototype.getFirstDateOfMonth = function() {
  456. return new Date(this.getFullYear(), this.getMonth(), 1)
  457. };
  458. Date.prototype.getLastDateOfMonth = function() {
  459. return new Date(this.getFullYear(), this.getMonth(), this.getDaysInMonth())
  460. };
  461. Date.prototype.getDaysInMonth = function() {
  462. Date.daysInMonth[1] = this.isLeapYear() ? 29 : 28;
  463. return Date.daysInMonth[this.getMonth()]
  464. };
  465. Date.prototype.getSuffix = function() {
  466. switch (this.getDate()) {
  467. case 1 :
  468. case 21 :
  469. case 31 :
  470. return "st";
  471. case 2 :
  472. case 22 :
  473. return "nd";
  474. case 3 :
  475. case 23 :
  476. return "rd";
  477. default :
  478. return "th"
  479. }
  480. };
  481. Date.daysInMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
  482. Date.monthNames = ["January", "February", "March", "April", "May", "June",
  483. "July", "August", "September", "October", "November", "December"];
  484. Date.getShortMonthName = function(A) {
  485. return Date.monthNames[A].substring(0, 3)
  486. };
  487. Date.dayNames = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
  488. "Friday", "Saturday"];
  489. Date.getShortDayName = function(A) {
  490. return Date.dayNames[A].substring(0, 3)
  491. };
  492. Date.y2kYear = 50;
  493. Date.monthNumbers = {
  494. Jan : 0,
  495. Feb : 1,
  496. Mar : 2,
  497. Apr : 3,
  498. May : 4,
  499. Jun : 5,
  500. Jul : 6,
  501. Aug : 7,
  502. Sep : 8,
  503. Oct : 9,
  504. Nov : 10,
  505. Dec : 11
  506. };
  507. Date.getMonthNumber = function(A) {
  508. return Date.monthNumbers[A.substring(0, 1).toUpperCase()
  509. + A.substring(1, 3).toLowerCase()]
  510. };
  511. Date.prototype.clone = function() {
  512. return new Date(this.getTime())
  513. };
  514. Date.prototype.clearTime = function(A) {
  515. if (A) {
  516. return this.clone().clearTime()
  517. }
  518. this.setHours(0);
  519. this.setMinutes(0);
  520. this.setSeconds(0);
  521. this.setMilliseconds(0);
  522. return this
  523. };
  524. if (Ext.isSafari) {
  525. Date.brokenSetMonth = Date.prototype.setMonth;
  526. Date.prototype.setMonth = function(A) {
  527. if (A <= -1) {
  528. var D = Math.ceil(-A);
  529. var C = Math.ceil(D / 12);
  530. var B = (D % 12) ? 12 - D % 12 : 0;
  531. this.setFullYear(this.getFullYear() - C);
  532. return Date.brokenSetMonth.call(this, B)
  533. } else {
  534. return Date.brokenSetMonth.apply(this, arguments)
  535. }
  536. }
  537. }
  538. Date.MILLI = "ms";
  539. Date.SECOND = "s";
  540. Date.MINUTE = "mi";
  541. Date.HOUR = "h";
  542. Date.DAY = "d";
  543. Date.MONTH = "mo";
  544. Date.YEAR = "y";
  545. Date.prototype.add = function(B, C) {
  546. var D = this.clone();
  547. if (!B || C === 0) {
  548. return D
  549. }
  550. switch (B.toLowerCase()) {
  551. case Date.MILLI :
  552. D.setMilliseconds(this.getMilliseconds() + C);
  553. break;
  554. case Date.SECOND :
  555. D.setSeconds(this.getSeconds() + C);
  556. break;
  557. case Date.MINUTE :
  558. D.setMinutes(this.getMinutes() + C);
  559. break;
  560. case Date.HOUR :
  561. D.setHours(this.getHours() + C);
  562. break;
  563. case Date.DAY :
  564. D.setDate(this.getDate() + C);
  565. break;
  566. case Date.MONTH :
  567. var A = this.getDate();
  568. if (A > 28) {
  569. A = Math.min(A, this.getFirstDateOfMonth().add("mo", C)
  570. .getLastDateOfMonth().getDate())
  571. }
  572. D.setDate(A);
  573. D.setMonth(this.getMonth() + C);
  574. break;
  575. case Date.YEAR :
  576. D.setFullYear(this.getFullYear() + C);
  577. break
  578. }
  579. return D
  580. };
  581. Date.prototype.between = function(C, A) {
  582. var B = this.getTime();
  583. return C.getTime() <= B && B <= A.getTime()
  584. };