/node_modules/moment/src/lib/units/year.js

https://bitbucket.org/coleman333/smartsite · JavaScript · 75 lines · 49 code · 19 blank · 7 comment · 2 complexity · 83d17e870ca4a7228e5071a7389f4d4c MD5 · raw file

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