import { DateLabel } from './index' ; import { DateKey } from './component/PickerView' ; import { PickerData } from '../Picker' ; export interface Diff { min : number ; max : number ; } export interface TimerDiff { year ? : Diff ; month ? : Diff ; day ? : Diff ; hour ? : Diff ; minute ? : Diff ; second ? : Diff ; } export interface ResultDate { year ? : PickerData [ ] ; month ? : PickerData [ ] ; day ? : PickerData [ ] ; hour ? : PickerData [ ] ; minute ? : PickerData [ ] ; second ? : PickerData [ ] ; } export interface ChangeParams { MAXTIMER ? : DateLabel ; MINTIMER ? : DateLabel ; nextLabel ? : DateLabel ; prevLabel ? : DateLabel ; } const bigMonths = [ 1 , 3 , 5 , 7 , 8 , 10 , 12 ] ; param precision returns export function precisionValue ( precision ) { if ( [ 'year' , 'month' , 'day' , 'hour' , 'minute' , 'second' ] . includes ( precision ) ) { return precision ; } return 'day' ; } param value param type returns export function setTimer ( type , value ? ) { const arr = [ ] ; const date = value ? new Date ( value ) : new Date ( ) ; arr [ 0 ] = date . getFullYear ( ) ; if ( type === 'year' ) return arr ; arr [ 1 ] = date . getMonth ( ) + 1 ; if ( type === 'month' ) return arr ; arr [ 2 ] = date . getDate ( ) ; if ( type === 'day' ) return arr ; arr [ 3 ] = date . getHours ( ) ; if ( type === 'hour' ) return arr ; arr [ 4 ] = date . getMinutes ( ) ; if ( type === 'minute' ) return arr ; arr [ 5 ] = date . getSeconds ( ) ; return arr ; } param min param max param type returns export function setTimerDiff ( min , max , type ) { const result = { } ; result . year = { min : min [ 0 ] , max : max [ 0 ] , } ; if ( type === 'year' ) return result ; result . month = { min : min [ 1 ] , max : max [ 1 ] , } ; if ( type === 'month' ) return result ; result . day = { min : min [ 2 ] , max : max [ 2 ] , } ; if ( type === 'day' ) return result ; result . hour = { min : min [ 3 ] , max : max [ 3 ] , } ; if ( type === 'hour' ) return result ; result . minute = { min : min [ 4 ] , max : max [ 4 ] , } ; if ( type === 'minute' ) return result ; result . second = { min : min [ 5 ] , max : max [ 5 ] , } ; return result ; } param star param end returns export function getYears ( star , end ) { const arr = [ ] ; for ( let i = star ; i <= end ; i ++ ) { arr . push ( { label : i + '' , value : i } ) ; } return arr ; } /** 生成数据 */ function createDate ( star , end ) { const arr = [ ] ; for ( let i = star ; i <= end ; i ++ ) { if ( i < 10 ) { arr . push ( { label : '0' + i , value : i } ) ; } else { arr . push ( { label : i + '' , value : i } ) ; } } return arr ; } /** 得到月份 */ export function getMonths ( initial = 1 , type = 'star' ) { const star = type === 'star' ? initial : 1 ; const end = type === 'end' ? initial : 12 ; const arr = createDate ( star , end ) ; return arr ; } /** 得到每月天数 */ export function getDays ( year , month , initial = 1 , type = 'star' ) { let max = bigMonths . includes ( month ) ? 31 : 30 ; if ( month === 2 ) { if ( ( year % 100 !== 0 && year % 4 === 0 ) || year % 400 === 0 ) { max = 29 ; } else { max = 28 ; } } const star = type === 'star' ? initial : 1 ; const end = type === 'end' ? initial : max ; const arr = createDate ( star , end ) ; return arr ; } /** 得到小时 */ export function getHours ( initial = 0 , type = 'star' ) { const star = type === 'star' ? initial : 0 ; const end = type === 'end' ? initial : 23 ; const arr = createDate ( star , end ) ; return arr ; } /** 得到分钟或者秒 */ export function getMinutesOrSeconds ( initial = 1 , type = 'star' ) { const star = type === 'star' ? initial : 1 ; const end = type === 'end' ? initial : 59 ; const arr = createDate ( star , end ) ; return arr ; } param star param end param PrecisionType returns export function getPickerData ( timerDiff , type ) { const obj = { } ; obj . year = getYears ( timerDiff . year ! . min , timerDiff . year ! . max ) ; if ( type === 'year' ) return obj ; obj . month = getMonths ( timerDiff . month ! . min ) ; if ( type === 'month' ) return obj ; obj . day = getDays ( timerDiff . year ! . min , timerDiff . month ! . min , timerDiff . day ?. min ) ; if ( type === 'day' ) return obj ; obj . hour = getHours ( timerDiff . hour ! . min ) ; if ( type === 'hour' ) return obj ; obj . minute = getMinutesOrSeconds ( timerDiff . minute ! . min ) ; if ( type === 'minute' ) return obj ; obj . second = getMinutesOrSeconds ( timerDiff . second ! . min ) ; return obj ; } param key param MINTIMER param nextLabel returns export function minData ( key , changeParams , precision ) { const { MINTIMER = { } , nextLabel = { } } = changeParams ; const newObj = { } ; if ( key === 'second' ) return newObj ; if ( MINTIMER . second !== '00' ) { newObj . second = getMinutesOrSeconds ( ) ; } if ( key === 'minute' ) return newObj ; if ( MINTIMER . minute !== '00' ) { newObj . minute = getMinutesOrSeconds ( ) ; } if ( key === 'hour' ) return newObj ; if ( MINTIMER . hour !== '00' ) { newObj . hour = getHours ( ) ; } if ( key === 'day' ) return newObj ; if ( MINTIMER . day !== '01' || Number ( nextLabel . month ) !== 2 ) { newObj . day = getDays ( Number ( nextLabel . year ) , Number ( nextLabel . month ) ) ; } if ( key === 'month' ) return newObj ; if ( MINTIMER . month !== '01' ) { newObj . month = getMonths ( ) ; } return newObj ; } /** 最大时间改变的 */ export function maxData ( key , changeParams , precision ) { const newObj = { } ; const { MAXTIMER = { } , nextLabel = { } } = changeParams ; if ( key === 'second' ) return newObj ; if ( MAXTIMER . second !== '59' ) { newObj . second = getMinutesOrSeconds ( ) ; } if ( key === 'minute' ) return newObj ; if ( MAXTIMER . minute !== '59' ) { newObj . minute = getMinutesOrSeconds ( ) ; } if ( key === 'hour' ) return newObj ; if ( MAXTIMER . hour !== '23' ) { newObj . hour = getHours ( ) ; } if ( key === 'day' ) return newObj ; getDays ( Number ( nextLabel . year ) , Number ( nextLabel . month ) ) ; if ( key === 'month' ) return newObj ; if ( MAXTIMER . month !== '12' ) { newObj . month = getMonths ( ) ; } return newObj ; } export function changeData ( key , changeParams , precision ) { let newObj = { } ; const { MAXTIMER = { } , nextLabel = { } , MINTIMER = { } , prevLabel = { } } = changeParams ; if ( nextLabel . year === MINTIMER . year ) { newObj = minData ( key , changeParams , precision ) ; return newObj ; } if ( nextLabel . year === MAXTIMER . year ) { newObj = maxData ( key , changeParams , precision ) ; return newObj ; } if ( nextLabel . year !== MAXTIMER . year && nextLabel . year !== MINTIMER . year && nextLabel . month === '02' ) { newObj . day = getDays ( Number ( nextLabel . year ) , Number ( nextLabel . month ) ) ; } return newObj ; }