PageRenderTime 18ms CodeModel.GetById 12ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

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