| 1 |
import { addFormatToken } from '../format/format'; |
| 2 |
import { |
| 3 |
addRegexToken, |
| 4 |
match1to2, |
| 5 |
match1to4, |
| 6 |
match1to6, |
| 7 |
match2, |
| 8 |
match4, |
| 9 |
match6, |
| 10 |
matchSigned, |
| 11 |
} from '../parse/regex'; |
| 12 |
import { addWeekParseToken } from '../parse/token'; |
| 13 |
import { |
| 14 |
weekOfYear, |
| 15 |
weeksInYear, |
| 16 |
dayOfYearFromWeeks, |
| 17 |
} from './week-calendar-utils'; |
| 18 |
import toInt from '../utils/to-int'; |
| 19 |
import { hooks } from '../utils/hooks'; |
| 20 |
import { createUTCDate } from '../create/date-from-array'; |
| 21 |
|
| 22 |
// FORMATTING |
| 23 |
|
| 24 |
addFormatToken(0, ['gg', 2], 0, function () { |
| 25 |
return this.weekYear() % 100; |
| 26 |
}); |
| 27 |
|
| 28 |
addFormatToken(0, ['GG', 2], 0, function () { |
| 29 |
return this.isoWeekYear() % 100; |
| 30 |
}); |
| 31 |
|
| 32 |
function addWeekYearFormatToken(token, getter) { |
| 33 |
addFormatToken(0, [token, token.length], 0, getter); |
| 34 |
} |
| 35 |
|
| 36 |
addWeekYearFormatToken('gggg', 'weekYear'); |
| 37 |
addWeekYearFormatToken('ggggg', 'weekYear'); |
| 38 |
addWeekYearFormatToken('GGGG', 'isoWeekYear'); |
| 39 |
addWeekYearFormatToken('GGGGG', 'isoWeekYear'); |
| 40 |
|
| 41 |
// ALIASES |
| 42 |
|
| 43 |
// PARSING |
| 44 |
|
| 45 |
addRegexToken('G', matchSigned); |
| 46 |
addRegexToken('g', matchSigned); |
| 47 |
addRegexToken('GG', match1to2, match2); |
| 48 |
addRegexToken('gg', match1to2, match2); |
| 49 |
addRegexToken('GGGG', match1to4, match4); |
| 50 |
addRegexToken('gggg', match1to4, match4); |
| 51 |
addRegexToken('GGGGG', match1to6, match6); |
| 52 |
addRegexToken('ggggg', match1to6, match6); |
| 53 |
|
| 54 |
addWeekParseToken( |
| 55 |
['gggg', 'ggggg', 'GGGG', 'GGGGG'], |
| 56 |
function (input, week, config, token) { |
| 57 |
week[token.substr(0, 2)] = toInt(input); |
| 58 |
} |
| 59 |
); |
| 60 |
|
| 61 |
addWeekParseToken(['gg', 'GG'], function (input, week, config, token) { |
| 62 |
week[token] = hooks.parseTwoDigitYear(input); |
| 63 |
}); |
| 64 |
|
| 65 |
// MOMENTS |
| 66 |
|
| 67 |
export function getSetWeekYear(input) { |
| 68 |
return getSetWeekYearHelper.call( |
| 69 |
this, |
| 70 |
input, |
| 71 |
this.week(), |
| 72 |
this.weekday() + this.localeData()._week.dow, |
| 73 |
this.localeData()._week.dow, |
| 74 |
this.localeData()._week.doy |
| 75 |
); |
| 76 |
} |
| 77 |
|
| 78 |
export function getSetISOWeekYear(input) { |
| 79 |
return getSetWeekYearHelper.call( |
| 80 |
this, |
| 81 |
input, |
| 82 |
this.isoWeek(), |
| 83 |
this.isoWeekday(), |
| 84 |
1, |
| 85 |
4 |
| 86 |
); |
| 87 |
} |
| 88 |
|
| 89 |
export function getISOWeeksInYear() { |
| 90 |
return weeksInYear(this.year(), 1, 4); |
| 91 |
} |
| 92 |
|
| 93 |
export function getISOWeeksInISOWeekYear() { |
| 94 |
return weeksInYear(this.isoWeekYear(), 1, 4); |
| 95 |
} |
| 96 |
|
| 97 |
export function getWeeksInYear() { |
| 98 |
var weekInfo = this.localeData()._week; |
| 99 |
return weeksInYear(this.year(), weekInfo.dow, weekInfo.doy); |
| 100 |
} |
| 101 |
|
| 102 |
export function getWeeksInWeekYear() { |
| 103 |
var weekInfo = this.localeData()._week; |
| 104 |
return weeksInYear(this.weekYear(), weekInfo.dow, weekInfo.doy); |
| 105 |
} |
| 106 |
|
| 107 |
function getSetWeekYearHelper(input, week, weekday, dow, doy) { |
| 108 |
var weeksTarget; |
| 109 |
if (input == null) { |
| 110 |
return weekOfYear(this, dow, doy).year; |
| 111 |
} else { |
| 112 |
weeksTarget = weeksInYear(input, dow, doy); |
| 113 |
if (week > weeksTarget) { |
| 114 |
week = weeksTarget; |
| 115 |
} |
| 116 |
return setWeekAll.call(this, input, week, weekday, dow, doy); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
function setWeekAll(weekYear, week, weekday, dow, doy) { |
| 121 |
var dayOfYearData = dayOfYearFromWeeks(weekYear, week, weekday, dow, doy), |
| 122 |
date = createUTCDate(dayOfYearData.year, 0, dayOfYearData.dayOfYear); |
| 123 |
|
| 124 |
this.year(date.getUTCFullYear()); |
| 125 |
this.month(date.getUTCMonth()); |
| 126 |
this.date(date.getUTCDate()); |
| 127 |
return this; |
| 128 |
} |