PageRenderTime 47ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/django/contrib/admin/media/js/dateparse.js

https://code.google.com/p/mango-py/
JavaScript | 239 lines | 201 code | 7 blank | 31 comment | 22 complexity | 81bc0017faa9db4b58c5c40d9ab87f62 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. /* 'Magic' date parsing, by Simon Willison (6th October 2003)
  2. http://simon.incutio.com/archive/2003/10/06/betterDateInput
  3. Adapted for 6newslawrence.com, 28th January 2004
  4. */
  5. /* Finds the index of the first occurence of item in the array, or -1 if not found */
  6. if (typeof Array.prototype.indexOf == 'undefined') {
  7. Array.prototype.indexOf = function(item) {
  8. var len = this.length;
  9. for (var i = 0; i < len; i++) {
  10. if (this[i] == item) {
  11. return i;
  12. }
  13. }
  14. return -1;
  15. };
  16. }
  17. /* Returns an array of items judged 'true' by the passed in test function */
  18. if (typeof Array.prototype.filter == 'undefined') {
  19. Array.prototype.filter = function(test) {
  20. var matches = [];
  21. var len = this.length;
  22. for (var i = 0; i < len; i++) {
  23. if (test(this[i])) {
  24. matches[matches.length] = this[i];
  25. }
  26. }
  27. return matches;
  28. };
  29. }
  30. var monthNames = gettext("January February March April May June July August September October November December").split(" ");
  31. var weekdayNames = gettext("Sunday Monday Tuesday Wednesday Thursday Friday Saturday").split(" ");
  32. /* Takes a string, returns the index of the month matching that string, throws
  33. an error if 0 or more than 1 matches
  34. */
  35. function parseMonth(month) {
  36. var matches = monthNames.filter(function(item) {
  37. return new RegExp("^" + month, "i").test(item);
  38. });
  39. if (matches.length == 0) {
  40. throw new Error("Invalid month string");
  41. }
  42. if (matches.length > 1) {
  43. throw new Error("Ambiguous month");
  44. }
  45. return monthNames.indexOf(matches[0]);
  46. }
  47. /* Same as parseMonth but for days of the week */
  48. function parseWeekday(weekday) {
  49. var matches = weekdayNames.filter(function(item) {
  50. return new RegExp("^" + weekday, "i").test(item);
  51. });
  52. if (matches.length == 0) {
  53. throw new Error("Invalid day string");
  54. }
  55. if (matches.length > 1) {
  56. throw new Error("Ambiguous weekday");
  57. }
  58. return weekdayNames.indexOf(matches[0]);
  59. }
  60. /* Array of objects, each has 're', a regular expression and 'handler', a
  61. function for creating a date from something that matches the regular
  62. expression. Handlers may throw errors if string is unparseable.
  63. */
  64. var dateParsePatterns = [
  65. // Today
  66. { re: /^tod/i,
  67. handler: function() {
  68. return new Date();
  69. }
  70. },
  71. // Tomorrow
  72. { re: /^tom/i,
  73. handler: function() {
  74. var d = new Date();
  75. d.setDate(d.getDate() + 1);
  76. return d;
  77. }
  78. },
  79. // Yesterday
  80. { re: /^yes/i,
  81. handler: function() {
  82. var d = new Date();
  83. d.setDate(d.getDate() - 1);
  84. return d;
  85. }
  86. },
  87. // 4th
  88. { re: /^(\d{1,2})(st|nd|rd|th)?$/i,
  89. handler: function(bits) {
  90. var d = new Date();
  91. d.setDate(parseInt(bits[1], 10));
  92. return d;
  93. }
  94. },
  95. // 4th Jan
  96. { re: /^(\d{1,2})(?:st|nd|rd|th)? (\w+)$/i,
  97. handler: function(bits) {
  98. var d = new Date();
  99. d.setDate(1);
  100. d.setMonth(parseMonth(bits[2]));
  101. d.setDate(parseInt(bits[1], 10));
  102. return d;
  103. }
  104. },
  105. // 4th Jan 2003
  106. { re: /^(\d{1,2})(?:st|nd|rd|th)? (\w+),? (\d{4})$/i,
  107. handler: function(bits) {
  108. var d = new Date();
  109. d.setDate(1);
  110. d.setYear(bits[3]);
  111. d.setMonth(parseMonth(bits[2]));
  112. d.setDate(parseInt(bits[1], 10));
  113. return d;
  114. }
  115. },
  116. // Jan 4th
  117. { re: /^(\w+) (\d{1,2})(?:st|nd|rd|th)?$/i,
  118. handler: function(bits) {
  119. var d = new Date();
  120. d.setDate(1);
  121. d.setMonth(parseMonth(bits[1]));
  122. d.setDate(parseInt(bits[2], 10));
  123. return d;
  124. }
  125. },
  126. // Jan 4th 2003
  127. { re: /^(\w+) (\d{1,2})(?:st|nd|rd|th)?,? (\d{4})$/i,
  128. handler: function(bits) {
  129. var d = new Date();
  130. d.setDate(1);
  131. d.setYear(bits[3]);
  132. d.setMonth(parseMonth(bits[1]));
  133. d.setDate(parseInt(bits[2], 10));
  134. return d;
  135. }
  136. },
  137. // next Tuesday - this is suspect due to weird meaning of "next"
  138. { re: /^next (\w+)$/i,
  139. handler: function(bits) {
  140. var d = new Date();
  141. var day = d.getDay();
  142. var newDay = parseWeekday(bits[1]);
  143. var addDays = newDay - day;
  144. if (newDay <= day) {
  145. addDays += 7;
  146. }
  147. d.setDate(d.getDate() + addDays);
  148. return d;
  149. }
  150. },
  151. // last Tuesday
  152. { re: /^last (\w+)$/i,
  153. handler: function(bits) {
  154. throw new Error("Not yet implemented");
  155. }
  156. },
  157. // mm/dd/yyyy (American style)
  158. { re: /(\d{1,2})\/(\d{1,2})\/(\d{4})/,
  159. handler: function(bits) {
  160. var d = new Date();
  161. d.setDate(1);
  162. d.setYear(bits[3]);
  163. d.setMonth(parseInt(bits[1], 10) - 1); // Because months indexed from 0
  164. d.setDate(parseInt(bits[2], 10));
  165. return d;
  166. }
  167. },
  168. // yyyy-mm-dd (ISO style)
  169. { re: /(\d{4})-(\d{1,2})-(\d{1,2})/,
  170. handler: function(bits) {
  171. var d = new Date();
  172. d.setDate(1);
  173. d.setYear(parseInt(bits[1]));
  174. d.setMonth(parseInt(bits[2], 10) - 1);
  175. d.setDate(parseInt(bits[3], 10));
  176. return d;
  177. }
  178. },
  179. ];
  180. function parseDateString(s) {
  181. for (var i = 0; i < dateParsePatterns.length; i++) {
  182. var re = dateParsePatterns[i].re;
  183. var handler = dateParsePatterns[i].handler;
  184. var bits = re.exec(s);
  185. if (bits) {
  186. return handler(bits);
  187. }
  188. }
  189. throw new Error("Invalid date string");
  190. }
  191. function fmt00(x) {
  192. // fmt00: Tags leading zero onto numbers 0 - 9.
  193. // Particularly useful for displaying results from Date methods.
  194. //
  195. if (Math.abs(parseInt(x)) < 10){
  196. x = "0"+ Math.abs(x);
  197. }
  198. return x;
  199. }
  200. function parseDateStringISO(s) {
  201. try {
  202. var d = parseDateString(s);
  203. return d.getFullYear() + '-' + (fmt00(d.getMonth() + 1)) + '-' + fmt00(d.getDate())
  204. }
  205. catch (e) { return s; }
  206. }
  207. function magicDate(input) {
  208. var messagespan = input.id + 'Msg';
  209. try {
  210. var d = parseDateString(input.value);
  211. input.value = d.getFullYear() + '-' + (fmt00(d.getMonth() + 1)) + '-' +
  212. fmt00(d.getDate());
  213. input.className = '';
  214. // Human readable date
  215. if (document.getElementById(messagespan)) {
  216. document.getElementById(messagespan).firstChild.nodeValue = d.toDateString();
  217. document.getElementById(messagespan).className = 'normal';
  218. }
  219. }
  220. catch (e) {
  221. input.className = 'error';
  222. var message = e.message;
  223. // Fix for IE6 bug
  224. if (message.indexOf('is null or not an object') > -1) {
  225. message = 'Invalid date string';
  226. }
  227. if (document.getElementById(messagespan)) {
  228. document.getElementById(messagespan).firstChild.nodeValue = message;
  229. document.getElementById(messagespan).className = 'error';
  230. }
  231. }
  232. }