license import fecha from 'fecha' ; import { DateService } from './date.service' ; import { I18nConfig , TranslationWidth , } from '../i18n/type' ; import { EN } from '../i18n/en' ; export const LOCALE_DEFAULT = 'en' ; export interface NativeDateServiceOptions { startDayOfWeek ? : 0 | 1 | 2 | 3 | 4 | 5 | 6 ; format ? : string ; i18n ? : I18nConfig ; } const DEFAULT_OPTIONS = { format : 'DD/MM/YYYY' , startDayOfWeek : 0 , } ; /**
 * The `NativeDateService` is basic implementation of `DateService` using
 * native js date objects.
 */ export class NativeDateService extends DateService < Date > { protected options : NativeDateServiceOptions ; protected i18nSettings = fecha . i18n ; constructor ( locale = LOCALE_DEFAULT , options ? ) { super ( ) ; this . options = { ... DEFAULT_OPTIONS , ... options } ; super . setLocale ( this . options . i18n ? locale : LOCALE_DEFAULT ) ; this . setFechaLocaleData ( this . options . i18n || EN ) ; } public setLocale ( locale ) { console . warn ( 'Runtime locale is not supported' ) ; } public isValidDateString ( date , format ) { return ! isNaN ( this . parse ( date , format ) . getTime ( ) ) ; } public today ( ) { return new Date ( ) ; } public getDate ( date ) { return date . getDate ( ) ; } public getMonth ( date ) { return date . getMonth ( ) ; } public getYear ( date ) { return date . getFullYear ( ) ; } public getDayOfWeek ( date ) { return date . getDay ( ) ; } /**
     * returns first day of the week, it can be 1 if week starts from monday
     * and 0 if from sunday and so on.
     * */ public getFirstDayOfWeek ( ) { return this . options . startDayOfWeek ; } public getMonthName ( date , style = TranslationWidth . SHORT ) { const index = date . getMonth ( ) ; return this . getMonthNameByIndex ( index , style ) ; } public getMonthNameByIndex ( index , style = TranslationWidth . SHORT ) { return this . getFechaMonthNames ( style ) [ index ] ; } public getDayOfWeekNames ( style = TranslationWidth . SHORT ) { const dayNames = this . getFechaDayNames ( style ) ; return this . shiftDayOfWeekNames ( [ ... dayNames ] , this . options . startDayOfWeek ) ; } public format ( date , format ) { return fecha . format ( date , format || this . options . format , this . i18nSettings ) ; } /**
     * We haven't got capability to parse date using formatting without third party libraries.
     * */ public parse ( date , format ) { return new Date ( Date . parse ( date ) ) ; } public addDay ( date , num ) { return this . createDate ( date . getFullYear ( ) , date . getMonth ( ) , date . getDate ( ) + num ) ; } public addMonth ( date , num ) { const month = this . createDate ( date . getFullYear ( ) , date . getMonth ( ) + num , 1 ) ; month . setDate ( Math . min ( date . getDate ( ) , this . getMonthEnd ( month ) . getDate ( ) ) ) ; return month ; } public addYear ( date , num ) { return this . createDate ( date . getFullYear ( ) + num , date . getMonth ( ) , date . getDate ( ) ) ; } public clone ( date ) { return new Date ( date . getTime ( ) ) ; } public compareDates ( date1 , date2 ) { return date1 . getTime ( ) - date2 . getTime ( ) ; } public compareDatesSafe ( date1 , date2 ) { if ( this . compareDates ( date1 , date2 ) < 0 ) { return - 1 ; } else if ( this . compareDates ( date1 , date2 ) > 0 ) { return 1 ; } else if ( this . compareDates ( date1 , date2 ) === 0 ) { return 0 ; } } public createDate ( year , month , date ) { const result = new Date ( year , month , date ) ; if ( year >= 0 && year < 100 ) { result . setFullYear ( result . getFullYear ( ) - 1900 ) ; } return result ; } public getMonthEnd ( date ) { return this . createDate ( date . getFullYear ( ) , date . getMonth ( ) + 1 , 0 ) ; } public getMonthStart ( date ) { return this . createDate ( date . getFullYear ( ) , date . getMonth ( ) , 1 ) ; } public getNumberOfDaysInMonth ( date ) { return this . getMonthEnd ( date ) . getDate ( ) ; } public getYearEnd ( date ) { return this . createDate ( date . getFullYear ( ) , 11 , 31 ) ; } public getYearStart ( date ) { return this . createDate ( date . getFullYear ( ) , 0 , 1 ) ; } public isSameDay ( date1 , date2 ) { return this . isSameMonth ( date1 , date2 ) && date1 . getDate ( ) === date2 . getDate ( ) ; } public isSameMonth ( date1 , date2 ) { return this . isSameYear ( date1 , date2 ) && date1 . getMonth ( ) === date2 . getMonth ( ) ; } public isSameYear ( date1 , date2 ) { return date1 . getFullYear ( ) === date2 . getFullYear ( ) ; } public getId ( ) { return 'native' ; } protected shiftDayOfWeekNames < T > ( value , offset ) { return value . splice ( offset ) . concat ( value ) ; } private getFechaDayNames ( style ) { switch ( style ) { case TranslationWidth . SHORT : return this . i18nSettings . dayNamesShort ; case TranslationWidth . LONG : return this . i18nSettings . dayNames ; } } private getFechaMonthNames ( style ) { switch ( style ) { case TranslationWidth . SHORT : return this . i18nSettings . monthNamesShort ; case TranslationWidth . LONG : return this . i18nSettings . monthNames ; } } private setFechaLocaleData ( config ) { this . i18nSettings = { ... fecha . i18n , dayNames : config . dayNames [ TranslationWidth . LONG ] , dayNamesShort : config . dayNames [ TranslationWidth . SHORT ] , monthNames : config . monthNames [ TranslationWidth . LONG ] , monthNamesShort : config . monthNames [ TranslationWidth . SHORT ] , } ; } }