import { format as formatFns , getDay as dateGetDay , newDate as newDateFns , isValid as dateIsValid , isLeapYear as dateIsLeapYear , getDaysInMonth as dateGetDaysInMonth , getDate , getYear , getMonth , isEqual as isEqualFns , isBefore as isBeforeFns , isAfter as isAfterFns , getDayOfYear as getDayOfYearFns , } from 'date-fns-jalali' ; import { isValidJalaliDate as isValidJalaliDateFns } from 'date-fns-jalali/_jalali/index' ; import { generateAnArrayOfNumbers , generateArrayInRangeOfNumbers , toPositive , } from './' ; import type { PickerItemModel , WeekDayText , PickerDateModel , RequiredPickerDateModel , } from '../components/WheelPicker/index.types' ; export const weekDays = { 0 : 'شنبه' , 1 : 'یک‌شنبه' , 2 : 'دو‌شنبه' , 3 : 'سه‌شنبه' , 4 : 'چهار‌شنبه' , 5 : 'پنج‌شنبه' , 6 : 'جمعه' , } ; export const jalaliMonths = { 1 : 'فروردین' , 2 : 'اردیبهشت' , 3 : 'خرداد' , 4 : 'تیر' , 5 : 'مرداد' , 6 : 'شهریور' , 7 : 'مهر' , 8 : 'آبان' , 9 : 'آذر' , 10 : 'دی' , 11 : 'بهمن' , 12 : 'اسفند' , } ; public export function newDate ( { year , month , day = 1 } ) { month = month ! - 1 ; return newDateFns ( year ! , month , day , 0 , 0 , 0 ) ; } public export const convertDateInstanceToDateObject = ( date ) : RequiredPickerDateModel => { return { year : getYear ( date ) , month : getMonth ( date ) + 1 , day : getDate ( date ) , hour : 0 , minute : 0 , second : 0 , } ; } ; public export function daysInMonth ( year , month ) { return dateGetDaysInMonth ( newDate ( { year , month , day : 1 } ) ) ; } description public export function getWeekDay ( year , month , day ) { return ( dateGetDay ( newDate ( { year , month , day } ) ) + 1 ) % 7 ; } public export function getDayOfYear ( year , month , day ) { return getDayOfYearFns ( newDate ( { year , month , day } ) ) ; } public export function isWeekend ( year , month , day ) { return getWeekDay ( year , month , day ) === 6 ; } public export function getWeekDayText ( year , month , day ) { const result = getWeekDay ( year , month , day ) ; return weekDays [ result ] ; } public export function isValid ( date ) { return dateIsValid ( date ) ; } public export function isValidJalaaliDate ( jy , jm , jd ) { return isValidJalaliDateFns ( jy , jm , jd ) ; } public export function isBefore ( dateLeft , dateRight ) { return isBeforeFns ( dateLeft , dateRight ) ; } public export function isAfter ( dateLeft , dateRight ) { return isAfterFns ( dateLeft , dateRight ) ; } public export function format ( date , formatBy ) { return formatFns ( date , formatBy ) ; } public export function isEqual ( dateLeft , dateRight ) { return isEqualFns ( dateLeft , dateRight ) ; } public export function getCurrentYear ( ) { return currentDateObject ( ) . year ; } export type CurrentDateObject = Required < Record < keyof PickerDateModel , number > > ; public export function currentDateObject ( ) { const date = new Date ( ) ; return { year : Number ( format ( date , 'yyyy' ) ) , month : Number ( format ( date , 'M' ) ) , day : Number ( format ( date , 'd' ) ) , hour : Number ( format ( date , 'h' ) ) , minute : Number ( format ( date , 'm' ) ) , second : Number ( format ( date , 'S' ) ) , } ; } public export function isLeapYear ( year ) { return dateIsLeapYear ( newDate ( { year , month : 1 , day : 1 } ) ) ; } public example export function startYear ( year ) { const currentYear = getCurrentYear ( ) ; if ( currentYear / year > 2 ) { throw new Error ( `[PersianMobileDatePicker] Invalid Year, Usage: startYearFrom(1380), means Year picker's column should starts from 1380 up to now` ) ; } return toPositive ( currentYear - year ) ; } public example export function endYear ( year ) { const currentYear = getCurrentYear ( ) ; if ( currentYear / year > 2 ) { throw new Error ( `[PersianMobileDatePicker] Invalid Year, Usage: endYearTo(1410), means Year picker's column should end in 1410` ) ; } return toPositive ( currentYear - year ) ; } private export const pickerData = { getYears : ( { min , max } = { } ) => generateArrayInRangeOfNumbers ( min , max ) . map ( ( year ) => ( { value : year , type : 'year' , } ) ) , getMonths : ( monthsMap = jalaliMonths ) => Object . keys ( monthsMap ) . map ( ( value ) => ( { type : 'month' , value : Number ( value ) , } ) ) , getDays : ( days = 29 ) => generateAnArrayOfNumbers ( days ) . map ( ( day ) => ( { value : day , type : 'day' , } ) ) , getHours : ( ) => generateAnArrayOfNumbers ( 24 ) . map ( ( hour ) => ( { value : hour , type : 'hour' , } ) ) , getSeconds : ( ) => generateAnArrayOfNumbers ( 59 ) . map ( ( hour ) => ( { value : hour , type : 'second' , } ) ) , getMinutes : ( ) => generateAnArrayOfNumbers ( 59 ) . map ( ( hour ) => ( { value : hour , type : 'minute' , } ) ) , } ;