01d9ac7bc6a8e66bb873cb05fe35d95c5cea9cac4556e4dbe9e8b5fea44ce5dff941415d7181221a22edc13110f929623ff48ec6a65fc28deed93380133596 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import { makeGetSet } from '../moment/get-set';
  2. import { addFormatToken } from '../format/format';
  3. import {
  4. addRegexToken,
  5. match1to2,
  6. match1to4,
  7. match1to6,
  8. match2,
  9. match4,
  10. match6,
  11. matchSigned,
  12. } from '../parse/regex';
  13. import { addParseToken } from '../parse/token';
  14. import { isLeapYear } from '../utils/is-leap-year';
  15. import { hooks } from '../utils/hooks';
  16. import { YEAR } from './constants';
  17. import toInt from '../utils/to-int';
  18. import zeroFill from '../utils/zero-fill';
  19. // FORMATTING
  20. addFormatToken('Y', 0, 0, function () {
  21. var y = this.year();
  22. return y <= 9999 ? zeroFill(y, 4) : '+' + y;
  23. });
  24. addFormatToken(0, ['YY', 2], 0, function () {
  25. return this.year() % 100;
  26. });
  27. addFormatToken(0, ['YYYY', 4], 0, 'year');
  28. addFormatToken(0, ['YYYYY', 5], 0, 'year');
  29. addFormatToken(0, ['YYYYYY', 6, true], 0, 'year');
  30. // PARSING
  31. addRegexToken('Y', matchSigned);
  32. addRegexToken('YY', match1to2, match2);
  33. addRegexToken('YYYY', match1to4, match4);
  34. addRegexToken('YYYYY', match1to6, match6);
  35. addRegexToken('YYYYYY', match1to6, match6);
  36. addParseToken(['YYYYY', 'YYYYYY'], YEAR);
  37. addParseToken('YYYY', function (input, array) {
  38. array[YEAR] =
  39. input.length === 2 ? hooks.parseTwoDigitYear(input) : toInt(input);
  40. });
  41. addParseToken('YY', function (input, array) {
  42. array[YEAR] = hooks.parseTwoDigitYear(input);
  43. });
  44. addParseToken('Y', function (input, array) {
  45. array[YEAR] = parseInt(input, 10);
  46. });
  47. // HELPERS
  48. export function daysInYear(year) {
  49. return isLeapYear(year) ? 366 : 365;
  50. }
  51. export { isLeapYear };
  52. // HOOKS
  53. hooks.parseTwoDigitYear = function (input) {
  54. return toInt(input) + (toInt(input) > 68 ? 1900 : 2000);
  55. };
  56. // MOMENTS
  57. export var getSetYear = makeGetSet('FullYear', true);
  58. export function getIsLeapYear() {
  59. return isLeapYear(this.year());
  60. }