a3437d1fdc624034ebcef5357e9839f60b932a909f280aac852587ecc740ba532595a4f762ba8975d4a339b049acd99909dffa0ca963e0fba9d1503dd33092 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. import { get } from '../moment/get-set';
  2. import hasOwnProp from '../utils/has-own-prop';
  3. import { addFormatToken } from '../format/format';
  4. import { addUnitAlias } from './aliases';
  5. import { addRegexToken, match1to2, match2, matchWord, regexEscape } from '../parse/regex';
  6. import { addParseToken } from '../parse/token';
  7. import { hooks } from '../utils/hooks';
  8. import { MONTH } from './constants';
  9. import toInt from '../utils/to-int';
  10. import isArray from '../utils/is-array';
  11. import indexOf from '../utils/index-of';
  12. import { createUTC } from '../create/utc';
  13. import getParsingFlags from '../create/parsing-flags';
  14. export function daysInMonth(year, month) {
  15. return new Date(Date.UTC(year, month + 1, 0)).getUTCDate();
  16. }
  17. // FORMATTING
  18. addFormatToken('M', ['MM', 2], 'Mo', function () {
  19. return this.month() + 1;
  20. });
  21. addFormatToken('MMM', 0, 0, function (format) {
  22. return this.localeData().monthsShort(this, format);
  23. });
  24. addFormatToken('MMMM', 0, 0, function (format) {
  25. return this.localeData().months(this, format);
  26. });
  27. // ALIASES
  28. addUnitAlias('month', 'M');
  29. // PARSING
  30. addRegexToken('M', match1to2);
  31. addRegexToken('MM', match1to2, match2);
  32. addRegexToken('MMM', function (isStrict, locale) {
  33. return locale.monthsShortRegex(isStrict);
  34. });
  35. addRegexToken('MMMM', function (isStrict, locale) {
  36. return locale.monthsRegex(isStrict);
  37. });
  38. addParseToken(['M', 'MM'], function (input, array) {
  39. array[MONTH] = toInt(input) - 1;
  40. });
  41. addParseToken(['MMM', 'MMMM'], function (input, array, config, token) {
  42. var month = config._locale.monthsParse(input, token, config._strict);
  43. // if we didn't find a month name, mark the date as invalid.
  44. if (month != null) {
  45. array[MONTH] = month;
  46. } else {
  47. getParsingFlags(config).invalidMonth = input;
  48. }
  49. });
  50. // LOCALES
  51. var MONTHS_IN_FORMAT = /D[oD]?(\[[^\[\]]*\]|\s+)+MMMM?/;
  52. export var defaultLocaleMonths = 'January_February_March_April_May_June_July_August_September_October_November_December'.split('_');
  53. export function localeMonths (m, format) {
  54. return isArray(this._months) ? this._months[m.month()] :
  55. this._months[MONTHS_IN_FORMAT.test(format) ? 'format' : 'standalone'][m.month()];
  56. }
  57. export var defaultLocaleMonthsShort = 'Jan_Feb_Mar_Apr_May_Jun_Jul_Aug_Sep_Oct_Nov_Dec'.split('_');
  58. export function localeMonthsShort (m, format) {
  59. return isArray(this._monthsShort) ? this._monthsShort[m.month()] :
  60. this._monthsShort[MONTHS_IN_FORMAT.test(format) ? 'format' : 'standalone'][m.month()];
  61. }
  62. function handleStrictParse(monthName, format, strict) {
  63. var i, ii, mom, llc = monthName.toLocaleLowerCase();
  64. if (!this._monthsParse) {
  65. // this is not used
  66. this._monthsParse = [];
  67. this._longMonthsParse = [];
  68. this._shortMonthsParse = [];
  69. for (i = 0; i < 12; ++i) {
  70. mom = createUTC([2000, i]);
  71. this._shortMonthsParse[i] = this.monthsShort(mom, '').toLocaleLowerCase();
  72. this._longMonthsParse[i] = this.months(mom, '').toLocaleLowerCase();
  73. }
  74. }
  75. if (strict) {
  76. if (format === 'MMM') {
  77. ii = indexOf.call(this._shortMonthsParse, llc);
  78. return ii !== -1 ? ii : null;
  79. } else {
  80. ii = indexOf.call(this._longMonthsParse, llc);
  81. return ii !== -1 ? ii : null;
  82. }
  83. } else {
  84. if (format === 'MMM') {
  85. ii = indexOf.call(this._shortMonthsParse, llc);
  86. if (ii !== -1) {
  87. return ii;
  88. }
  89. ii = indexOf.call(this._longMonthsParse, llc);
  90. return ii !== -1 ? ii : null;
  91. } else {
  92. ii = indexOf.call(this._longMonthsParse, llc);
  93. if (ii !== -1) {
  94. return ii;
  95. }
  96. ii = indexOf.call(this._shortMonthsParse, llc);
  97. return ii !== -1 ? ii : null;
  98. }
  99. }
  100. }
  101. export function localeMonthsParse (monthName, format, strict) {
  102. var i, mom, regex;
  103. if (this._monthsParseExact) {
  104. return handleStrictParse.call(this, monthName, format, strict);
  105. }
  106. if (!this._monthsParse) {
  107. this._monthsParse = [];
  108. this._longMonthsParse = [];
  109. this._shortMonthsParse = [];
  110. }
  111. // TODO: add sorting
  112. // Sorting makes sure if one month (or abbr) is a prefix of another
  113. // see sorting in computeMonthsParse
  114. for (i = 0; i < 12; i++) {
  115. // make the regex if we don't have it already
  116. mom = createUTC([2000, i]);
  117. if (strict && !this._longMonthsParse[i]) {
  118. this._longMonthsParse[i] = new RegExp('^' + this.months(mom, '').replace('.', '') + '$', 'i');
  119. this._shortMonthsParse[i] = new RegExp('^' + this.monthsShort(mom, '').replace('.', '') + '$', 'i');
  120. }
  121. if (!strict && !this._monthsParse[i]) {
  122. regex = '^' + this.months(mom, '') + '|^' + this.monthsShort(mom, '');
  123. this._monthsParse[i] = new RegExp(regex.replace('.', ''), 'i');
  124. }
  125. // test the regex
  126. if (strict && format === 'MMMM' && this._longMonthsParse[i].test(monthName)) {
  127. return i;
  128. } else if (strict && format === 'MMM' && this._shortMonthsParse[i].test(monthName)) {
  129. return i;
  130. } else if (!strict && this._monthsParse[i].test(monthName)) {
  131. return i;
  132. }
  133. }
  134. }
  135. // MOMENTS
  136. export function setMonth (mom, value) {
  137. var dayOfMonth;
  138. if (!mom.isValid()) {
  139. // No op
  140. return mom;
  141. }
  142. if (typeof value === 'string') {
  143. if (/^\d+$/.test(value)) {
  144. value = toInt(value);
  145. } else {
  146. value = mom.localeData().monthsParse(value);
  147. // TODO: Another silent failure?
  148. if (typeof value !== 'number') {
  149. return mom;
  150. }
  151. }
  152. }
  153. dayOfMonth = Math.min(mom.date(), daysInMonth(mom.year(), value));
  154. mom._d['set' + (mom._isUTC ? 'UTC' : '') + 'Month'](value, dayOfMonth);
  155. return mom;
  156. }
  157. export function getSetMonth (value) {
  158. if (value != null) {
  159. setMonth(this, value);
  160. hooks.updateOffset(this, true);
  161. return this;
  162. } else {
  163. return get(this, 'Month');
  164. }
  165. }
  166. export function getDaysInMonth () {
  167. return daysInMonth(this.year(), this.month());
  168. }
  169. export var defaultMonthsShortRegex = matchWord;
  170. export function monthsShortRegex (isStrict) {
  171. if (this._monthsParseExact) {
  172. if (!hasOwnProp(this, '_monthsRegex')) {
  173. computeMonthsParse.call(this);
  174. }
  175. if (isStrict) {
  176. return this._monthsShortStrictRegex;
  177. } else {
  178. return this._monthsShortRegex;
  179. }
  180. } else {
  181. return this._monthsShortStrictRegex && isStrict ?
  182. this._monthsShortStrictRegex : this._monthsShortRegex;
  183. }
  184. }
  185. export var defaultMonthsRegex = matchWord;
  186. export function monthsRegex (isStrict) {
  187. if (this._monthsParseExact) {
  188. if (!hasOwnProp(this, '_monthsRegex')) {
  189. computeMonthsParse.call(this);
  190. }
  191. if (isStrict) {
  192. return this._monthsStrictRegex;
  193. } else {
  194. return this._monthsRegex;
  195. }
  196. } else {
  197. return this._monthsStrictRegex && isStrict ?
  198. this._monthsStrictRegex : this._monthsRegex;
  199. }
  200. }
  201. function computeMonthsParse () {
  202. function cmpLenRev(a, b) {
  203. return b.length - a.length;
  204. }
  205. var shortPieces = [], longPieces = [], mixedPieces = [],
  206. i, mom;
  207. for (i = 0; i < 12; i++) {
  208. // make the regex if we don't have it already
  209. mom = createUTC([2000, i]);
  210. shortPieces.push(this.monthsShort(mom, ''));
  211. longPieces.push(this.months(mom, ''));
  212. mixedPieces.push(this.months(mom, ''));
  213. mixedPieces.push(this.monthsShort(mom, ''));
  214. }
  215. // Sorting makes sure if one month (or abbr) is a prefix of another it
  216. // will match the longer piece.
  217. shortPieces.sort(cmpLenRev);
  218. longPieces.sort(cmpLenRev);
  219. mixedPieces.sort(cmpLenRev);
  220. for (i = 0; i < 12; i++) {
  221. shortPieces[i] = regexEscape(shortPieces[i]);
  222. longPieces[i] = regexEscape(longPieces[i]);
  223. mixedPieces[i] = regexEscape(mixedPieces[i]);
  224. }
  225. this._monthsRegex = new RegExp('^(' + mixedPieces.join('|') + ')', 'i');
  226. this._monthsShortRegex = this._monthsRegex;
  227. this._monthsStrictRegex = new RegExp('^(' + longPieces.join('|') + ')', 'i');
  228. this._monthsShortStrictRegex = new RegExp('^(' + shortPieces.join('|') + ')', 'i');
  229. }