import toDate from '../../toDate/index' ; import type { FirstWeekContainsDateOptions , LocaleOptions , WeekStartOptions , } from '../../types' ; import requiredArgs from '../requiredArgs/index' ; import startOfUTCWeek from '../startOfUTCWeek/index' ; import toInteger from '../toInteger/index' ; export default function getUTCWeekYear ( dirtyDate , dirtyOptions ? ) { requiredArgs ( 1 , arguments ) ; const date = toDate ( dirtyDate ) ; const year = date . getUTCFullYear ( ) ; const options = dirtyOptions || { } ; const locale = options . locale ; const localeFirstWeekContainsDate = locale && locale . options && locale . options . firstWeekContainsDate ; const defaultFirstWeekContainsDate = localeFirstWeekContainsDate == null ? 1 : toInteger ( localeFirstWeekContainsDate ) ; const firstWeekContainsDate = options . firstWeekContainsDate == null ? defaultFirstWeekContainsDate : toInteger ( options . firstWeekContainsDate ) ; if ( ! ( firstWeekContainsDate >= 1 && firstWeekContainsDate <= 7 ) ) { throw new RangeError ( 'firstWeekContainsDate must be between 1 and 7 inclusively' ) ; } const firstWeekOfNextYear = new Date ( 0 ) ; firstWeekOfNextYear . setUTCFullYear ( year + 1 , 0 , firstWeekContainsDate ) ; firstWeekOfNextYear . setUTCHours ( 0 , 0 , 0 , 0 ) ; const startOfNextYear = startOfUTCWeek ( firstWeekOfNextYear , dirtyOptions ) ; const firstWeekOfThisYear = new Date ( 0 ) ; firstWeekOfThisYear . setUTCFullYear ( year , 0 , firstWeekContainsDate ) ; firstWeekOfThisYear . setUTCHours ( 0 , 0 , 0 , 0 ) ; const startOfThisYear = startOfUTCWeek ( firstWeekOfThisYear , dirtyOptions ) ; if ( date . getTime ( ) >= startOfNextYear . getTime ( ) ) { return year + 1 ; } else if ( date . getTime ( ) >= startOfThisYear . getTime ( ) ) { return year ; } else { return year - 1 ; } }