date-parser.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /**
  2. * angular-strap
  3. * @version v2.3.5 - 2015-10-29
  4. * @link http://mgcrea.github.io/angular-strap
  5. * @author Olivier Louvignes <olivier@mg-crea.com> (https://github.com/mgcrea)
  6. * @license MIT License, http://www.opensource.org/licenses/MIT
  7. */
  8. 'use strict';
  9. angular.module('mgcrea.ngStrap.helpers.dateParser', []).provider('$dateParser', [ '$localeProvider', function($localeProvider) {
  10. function ParseDate() {
  11. this.year = 1970;
  12. this.month = 0;
  13. this.day = 1;
  14. this.hours = 0;
  15. this.minutes = 0;
  16. this.seconds = 0;
  17. this.milliseconds = 0;
  18. }
  19. ParseDate.prototype.setMilliseconds = function(value) {
  20. this.milliseconds = value;
  21. };
  22. ParseDate.prototype.setSeconds = function(value) {
  23. this.seconds = value;
  24. };
  25. ParseDate.prototype.setMinutes = function(value) {
  26. this.minutes = value;
  27. };
  28. ParseDate.prototype.setHours = function(value) {
  29. this.hours = value;
  30. };
  31. ParseDate.prototype.getHours = function() {
  32. return this.hours;
  33. };
  34. ParseDate.prototype.setDate = function(value) {
  35. this.day = value;
  36. };
  37. ParseDate.prototype.setMonth = function(value) {
  38. this.month = value;
  39. };
  40. ParseDate.prototype.setFullYear = function(value) {
  41. this.year = value;
  42. };
  43. ParseDate.prototype.fromDate = function(value) {
  44. this.year = value.getFullYear();
  45. this.month = value.getMonth();
  46. this.day = value.getDate();
  47. this.hours = value.getHours();
  48. this.minutes = value.getMinutes();
  49. this.seconds = value.getSeconds();
  50. this.milliseconds = value.getMilliseconds();
  51. return this;
  52. };
  53. ParseDate.prototype.toDate = function() {
  54. return new Date(this.year, this.month, this.day, this.hours, this.minutes, this.seconds, this.milliseconds);
  55. };
  56. var proto = ParseDate.prototype;
  57. function noop() {}
  58. function isNumeric(n) {
  59. return !isNaN(parseFloat(n)) && isFinite(n);
  60. }
  61. function indexOfCaseInsensitive(array, value) {
  62. var len = array.length, str = value.toString().toLowerCase();
  63. for (var i = 0; i < len; i++) {
  64. if (array[i].toLowerCase() === str) {
  65. return i;
  66. }
  67. }
  68. return -1;
  69. }
  70. var defaults = this.defaults = {
  71. format: 'shortDate',
  72. strict: false
  73. };
  74. this.$get = [ '$locale', 'dateFilter', function($locale, dateFilter) {
  75. var DateParserFactory = function(config) {
  76. var options = angular.extend({}, defaults, config);
  77. var $dateParser = {};
  78. var regExpMap = {
  79. sss: '[0-9]{3}',
  80. ss: '[0-5][0-9]',
  81. s: options.strict ? '[1-5]?[0-9]' : '[0-9]|[0-5][0-9]',
  82. mm: '[0-5][0-9]',
  83. m: options.strict ? '[1-5]?[0-9]' : '[0-9]|[0-5][0-9]',
  84. HH: '[01][0-9]|2[0-3]',
  85. H: options.strict ? '1?[0-9]|2[0-3]' : '[01]?[0-9]|2[0-3]',
  86. hh: '[0][1-9]|[1][012]',
  87. h: options.strict ? '[1-9]|1[012]' : '0?[1-9]|1[012]',
  88. a: 'AM|PM',
  89. EEEE: $locale.DATETIME_FORMATS.DAY.join('|'),
  90. EEE: $locale.DATETIME_FORMATS.SHORTDAY.join('|'),
  91. dd: '0[1-9]|[12][0-9]|3[01]',
  92. d: options.strict ? '[1-9]|[1-2][0-9]|3[01]' : '0?[1-9]|[1-2][0-9]|3[01]',
  93. MMMM: $locale.DATETIME_FORMATS.MONTH.join('|'),
  94. MMM: $locale.DATETIME_FORMATS.SHORTMONTH.join('|'),
  95. MM: '0[1-9]|1[012]',
  96. M: options.strict ? '[1-9]|1[012]' : '0?[1-9]|1[012]',
  97. yyyy: '[1]{1}[0-9]{3}|[2]{1}[0-9]{3}',
  98. yy: '[0-9]{2}',
  99. y: options.strict ? '-?(0|[1-9][0-9]{0,3})' : '-?0*[0-9]{1,4}'
  100. };
  101. var setFnMap = {
  102. sss: proto.setMilliseconds,
  103. ss: proto.setSeconds,
  104. s: proto.setSeconds,
  105. mm: proto.setMinutes,
  106. m: proto.setMinutes,
  107. HH: proto.setHours,
  108. H: proto.setHours,
  109. hh: proto.setHours,
  110. h: proto.setHours,
  111. EEEE: noop,
  112. EEE: noop,
  113. dd: proto.setDate,
  114. d: proto.setDate,
  115. a: function(value) {
  116. var hours = this.getHours() % 12;
  117. return this.setHours(value.match(/pm/i) ? hours + 12 : hours);
  118. },
  119. MMMM: function(value) {
  120. return this.setMonth(indexOfCaseInsensitive($locale.DATETIME_FORMATS.MONTH, value));
  121. },
  122. MMM: function(value) {
  123. return this.setMonth(indexOfCaseInsensitive($locale.DATETIME_FORMATS.SHORTMONTH, value));
  124. },
  125. MM: function(value) {
  126. return this.setMonth(1 * value - 1);
  127. },
  128. M: function(value) {
  129. return this.setMonth(1 * value - 1);
  130. },
  131. yyyy: proto.setFullYear,
  132. yy: function(value) {
  133. return this.setFullYear(2e3 + 1 * value);
  134. },
  135. y: function(value) {
  136. return 1 * value <= 50 && value.length === 2 ? this.setFullYear(2e3 + 1 * value) : this.setFullYear(1 * value);
  137. }
  138. };
  139. var regex, setMap;
  140. $dateParser.init = function() {
  141. $dateParser.$format = $locale.DATETIME_FORMATS[options.format] || options.format;
  142. regex = regExpForFormat($dateParser.$format);
  143. setMap = setMapForFormat($dateParser.$format);
  144. };
  145. $dateParser.isValid = function(date) {
  146. if (angular.isDate(date)) return !isNaN(date.getTime());
  147. return regex.test(date);
  148. };
  149. $dateParser.parse = function(value, baseDate, format, timezone) {
  150. if (format) format = $locale.DATETIME_FORMATS[format] || format;
  151. if (angular.isDate(value)) value = dateFilter(value, format || $dateParser.$format, timezone);
  152. var formatRegex = format ? regExpForFormat(format) : regex;
  153. var formatSetMap = format ? setMapForFormat(format) : setMap;
  154. var matches = formatRegex.exec(value);
  155. if (!matches) return false;
  156. var date = baseDate && !isNaN(baseDate.getTime()) ? new ParseDate().fromDate(baseDate) : new ParseDate().fromDate(new Date(1970, 0, 1, 0));
  157. for (var i = 0; i < matches.length - 1; i++) {
  158. formatSetMap[i] && formatSetMap[i].call(date, matches[i + 1]);
  159. }
  160. var newDate = date.toDate();
  161. if (parseInt(date.day, 10) !== newDate.getDate()) {
  162. return false;
  163. }
  164. return newDate;
  165. };
  166. $dateParser.getDateForAttribute = function(key, value) {
  167. var date;
  168. if (value === 'today') {
  169. var today = new Date();
  170. date = new Date(today.getFullYear(), today.getMonth(), today.getDate() + (key === 'maxDate' ? 1 : 0), 0, 0, 0, key === 'minDate' ? 0 : -1);
  171. } else if (angular.isString(value) && value.match(/^".+"$/)) {
  172. date = new Date(value.substr(1, value.length - 2));
  173. } else if (isNumeric(value)) {
  174. date = new Date(parseInt(value, 10));
  175. } else if (angular.isString(value) && 0 === value.length) {
  176. date = key === 'minDate' ? -Infinity : +Infinity;
  177. } else {
  178. date = new Date(value);
  179. }
  180. return date;
  181. };
  182. $dateParser.getTimeForAttribute = function(key, value) {
  183. var time;
  184. if (value === 'now') {
  185. time = new Date().setFullYear(1970, 0, 1);
  186. } else if (angular.isString(value) && value.match(/^".+"$/)) {
  187. time = new Date(value.substr(1, value.length - 2)).setFullYear(1970, 0, 1);
  188. } else if (isNumeric(value)) {
  189. time = new Date(parseInt(value, 10)).setFullYear(1970, 0, 1);
  190. } else if (angular.isString(value) && 0 === value.length) {
  191. time = key === 'minTime' ? -Infinity : +Infinity;
  192. } else {
  193. time = $dateParser.parse(value, new Date(1970, 0, 1, 0));
  194. }
  195. return time;
  196. };
  197. $dateParser.daylightSavingAdjust = function(date) {
  198. if (!date) {
  199. return null;
  200. }
  201. date.setHours(date.getHours() > 12 ? date.getHours() + 2 : 0);
  202. return date;
  203. };
  204. $dateParser.timezoneOffsetAdjust = function(date, timezone, undo) {
  205. if (!date) {
  206. return null;
  207. }
  208. if (timezone && timezone === 'UTC') {
  209. date = new Date(date.getTime());
  210. date.setMinutes(date.getMinutes() + (undo ? -1 : 1) * date.getTimezoneOffset());
  211. }
  212. return date;
  213. };
  214. function setMapForFormat(format) {
  215. var keys = Object.keys(setFnMap), i;
  216. var map = [], sortedMap = [];
  217. var clonedFormat = format;
  218. for (i = 0; i < keys.length; i++) {
  219. if (format.split(keys[i]).length > 1) {
  220. var index = clonedFormat.search(keys[i]);
  221. format = format.split(keys[i]).join('');
  222. if (setFnMap[keys[i]]) {
  223. map[index] = setFnMap[keys[i]];
  224. }
  225. }
  226. }
  227. angular.forEach(map, function(v) {
  228. if (v) sortedMap.push(v);
  229. });
  230. return sortedMap;
  231. }
  232. function escapeReservedSymbols(text) {
  233. return text.replace(/\//g, '[\\/]').replace('/-/g', '[-]').replace(/\./g, '[.]').replace(/\\s/g, '[\\s]');
  234. }
  235. function regExpForFormat(format) {
  236. var keys = Object.keys(regExpMap), i;
  237. var re = format;
  238. for (i = 0; i < keys.length; i++) {
  239. re = re.split(keys[i]).join('${' + i + '}');
  240. }
  241. for (i = 0; i < keys.length; i++) {
  242. re = re.split('${' + i + '}').join('(' + regExpMap[keys[i]] + ')');
  243. }
  244. format = escapeReservedSymbols(format);
  245. return new RegExp('^' + re + '$', [ 'i' ]);
  246. }
  247. $dateParser.init();
  248. return $dateParser;
  249. };
  250. return DateParserFactory;
  251. } ];
  252. } ]);