packageDocumentation module import { Logger } from "@itwin/core-bentley" ; import { AlternateDateFormats , Primitives , StandardTypeNames , TimeDisplay } from "@itwin/appui-abstract" ; import { TimeFormat } from "@itwin/core-react" ; import { formatInputDate } from "../datepicker/DateField" ; import { adjustDateToTimezone } from "../common/DateUtils" ; import { UiComponents } from "../UiComponents" ; import { LessGreaterOperatorProcessor , TypeConverter } from "./TypeConverter" ; import { TypeConverterManager } from "./TypeConverterManager" ; import { ConvertedPrimitives } from "./valuetypes/ConvertedTypes" ; public export abstract class DateTimeTypeConverterBase extends TypeConverter implements LessGreaterOperatorProcessor { public override convertToString ( value ? ) { if ( value === undefined ) return "" ; if ( typeof value === "string" ) value = new Date ( value ) ; if ( value instanceof Date ) { switch ( this . getTimeFormat ( ) ) { case TimeFormat . Short : return value . toLocaleDateString ( ) ; case TimeFormat . Long : return value . toLocaleString ( ) ; } return value . toISOString ( ) ; } return value . toString ( ) ; } public static isValidTimeDisplay ( type ) { return Object . keys ( TimeDisplay ) . some ( ( key ) => ( TimeDisplay as any ) [ key ] === type ) ; } public static isAlternateDateFormats ( type ) { return Object . keys ( AlternateDateFormats ) . some ( ( key ) => ( AlternateDateFormats as any ) [ key ] === type ) ; } public override convertToStringWithOptions ( value ? , options ? ) { if ( value === undefined ) return "" ; if ( options ) { let timeDisplay = ( this . getTimeFormat ( ) === TimeFormat . Long ) ? TimeDisplay . H12MC : undefined ; let alternateDateFormat = AlternateDateFormats . None ; if ( "alternateDateFormat" in options && DateTimeTypeConverterBase . isAlternateDateFormats ( options . alternateDateFormat ) ) { alternateDateFormat = options . alternateDateFormat ; } if ( this . getTimeFormat ( ) === TimeFormat . Long ) { if ( "timeDisplay" in options ) { if ( alternateDateFormat ) { Logger . logInfo ( UiComponents . loggerCategory ( this ) , `Invalid specification of timeDisplay with alternateDateFormat specification` ) ; } else { if ( DateTimeTypeConverterBase . isValidTimeDisplay ( options . timeDisplay ) ) timeDisplay = options . timeDisplay ; } } } if ( typeof value === "string" ) { value = new Date ( value ) ; if ( value instanceof Date && alternateDateFormat ) { value = adjustDateToTimezone ( value , value . getTimezoneOffset ( ) * - 1 ) ; } } if ( value instanceof Date ) { if ( this . getTimeFormat ( ) === TimeFormat . Long ) { if ( alternateDateFormat === AlternateDateFormats . IsoShort ) alternateDateFormat = AlternateDateFormats . IsoDateTime ; if ( alternateDateFormat === AlternateDateFormats . UtcShort ) alternateDateFormat = AlternateDateFormats . UtcDateTime ; if ( alternateDateFormat === AlternateDateFormats . UtcShortWithDay ) alternateDateFormat = AlternateDateFormats . UtcDateTimeWithDay ; } else { if ( alternateDateFormat === AlternateDateFormats . IsoDateTime ) alternateDateFormat = AlternateDateFormats . IsoShort ; if ( alternateDateFormat === AlternateDateFormats . UtcDateTime ) alternateDateFormat = AlternateDateFormats . UtcShort ; if ( alternateDateFormat === AlternateDateFormats . UtcDateTimeWithDay ) alternateDateFormat = AlternateDateFormats . UtcShortWithDay ; } const formattedDateTime = formatInputDate ( value , timeDisplay , undefined , alternateDateFormat ) ; if ( formattedDateTime ) return formattedDateTime ; } } return this . convertToString ( value ) ; } /** Default implementation just calls convertFromString with no options */ public override convertFromStringWithOptions ( value , options ? ) { if ( ! value ) return undefined ; if ( options ) { let alternateDateFormat = AlternateDateFormats . None ; if ( "alternateDateFormat" in options && DateTimeTypeConverterBase . isAlternateDateFormats ( options . alternateDateFormat ) ) { alternateDateFormat = options . alternateDateFormat ; } let date = new Date ( value ) ; if ( date instanceof Date && alternateDateFormat ) { date = adjustDateToTimezone ( date , date . getTimezoneOffset ( ) * - 1 ) ; } } return this . convertFromString ( value ) ; } private isDateValid ( date ) { return date instanceof Date && ! isNaN ( + date ) ; } public override convertFromString ( value ) { if ( ! value ) return undefined ; const dateValue = new Date ( value ) ; if ( ! this . isDateValid ( dateValue ) ) return undefined ; return dateValue ; } protected abstract getTimeFormat ( ) ; public override get isLessGreaterType ( ) : boolean { return true ; } public sortCompare ( valueA , valueB , _ignoreCase ? ) { return valueA . valueOf ( ) - valueB . valueOf ( ) ; } public override isEqualTo ( valueA , valueB ) { return valueA . valueOf ( ) === valueB . valueOf ( ) ; } public override isNotEqualTo ( valueA , valueB ) { return valueA . valueOf ( ) !== valueB . valueOf ( ) ; } public isLessThan ( a , b ) { return a . valueOf ( ) < b . valueOf ( ) ; } public isLessThanOrEqualTo ( a , b ) { return a . valueOf ( ) <= b . valueOf ( ) ; } public isGreaterThan ( a , b ) { return a . valueOf ( ) > b . valueOf ( ) ; } public isGreaterThanOrEqualTo ( a , b ) { return a . valueOf ( ) >= b . valueOf ( ) ; } } public export class ShortDateTypeConverter extends DateTimeTypeConverterBase { protected getTimeFormat ( ) { return TimeFormat . Short ; } } TypeConverterManager . registerConverter ( StandardTypeNames . ShortDate , ShortDateTypeConverter ) ; public export class DateTimeTypeConverter extends DateTimeTypeConverterBase { protected getTimeFormat ( ) { return TimeFormat . Long ; } } TypeConverterManager . registerConverter ( StandardTypeNames . DateTime , DateTimeTypeConverter ) ;