| 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 |
|
| 20 |
// FORMATTING |
| 21 |
|
| 22 |
addFormatToken('Y', 0, 0, function () { |
| 23 |
var y = this.year(); |
| 24 |
return y <= 9999 ? zeroFill(y, 4) : '+' + y; |
| 25 |
}); |
| 26 |
|
| 27 |
addFormatToken(0, ['YY', 2], 0, function () { |
| 28 |
return this.year() % 100; |
| 29 |
}); |
| 30 |
|
| 31 |
addFormatToken(0, ['YYYY', 4], 0, 'year'); |
| 32 |
addFormatToken(0, ['YYYYY', 5], 0, 'year'); |
| 33 |
addFormatToken(0, ['YYYYYY', 6, true], 0, 'year'); |
| 34 |
|
| 35 |
// PARSING |
| 36 |
|
| 37 |
addRegexToken('Y', matchSigned); |
| 38 |
addRegexToken('YY', match1to2, match2); |
| 39 |
addRegexToken('YYYY', match1to4, match4); |
| 40 |
addRegexToken('YYYYY', match1to6, match6); |
| 41 |
addRegexToken('YYYYYY', match1to6, match6); |
| 42 |
|
| 43 |
addParseToken(['YYYYY', 'YYYYYY'], YEAR); |
| 44 |
addParseToken('YYYY', function (input, array) { |
| 45 |
array[YEAR] = |
| 46 |
input.length === 2 ? hooks.parseTwoDigitYear(input) : toInt(input); |
| 47 |
}); |
| 48 |
addParseToken('YY', function (input, array) { |
| 49 |
array[YEAR] = hooks.parseTwoDigitYear(input); |
| 50 |
}); |
| 51 |
addParseToken('Y', function (input, array) { |
| 52 |
array[YEAR] = parseInt(input, 10); |
| 53 |
}); |
| 54 |
|
| 55 |
// HELPERS |
| 56 |
|
| 57 |
export function daysInYear(year) { |
| 58 |
return isLeapYear(year) ? 366 : 365; |
| 59 |
} |
| 60 |
|
| 61 |
export { isLeapYear }; |
| 62 |
|
| 63 |
// HOOKS |
| 64 |
|
| 65 |
hooks.parseTwoDigitYear = function (input) { |
| 66 |
return toInt(input) + (toInt(input) > 68 ? 1900 : 2000); |
| 67 |
}; |
| 68 |
|
| 69 |
// MOMENTS |
| 70 |
|
| 71 |
export var getSetYear = makeGetSet('FullYear', true); |
| 72 |
|
| 73 |
export function getIsLeapYear() { |
| 74 |
return isLeapYear(this.year()); |
| 75 |
} |