import { Constants } from '../Common/Constants' ; import { Utils } from './Utils' ; import dayjs from 'dayjs' ; import Duration from 'luxon/src/duration' ; export class TimeUtils { public static MaxDurationInMilliseconds = 922337203685477.5807 ; public static AddSeconds ( toDate , seconds ) { const date = new Date ( toDate . valueOf ( ) ) ; date . setTime ( date . getTime ( ) + ( seconds * 1000 ) ) ; return date ; } public static AddHours ( toDate , hours ) { const date = new Date ( toDate . valueOf ( ) ) ; date . setTime ( date . getTime ( ) + ( hours * 1000 * 60 * 60 ) ) ; return date ; } public static AddDays ( toDate , days ) { const date = new Date ( toDate . valueOf ( ) ) ; date . setDate ( date . getDate ( ) + days ) ; return date ; } public static getDurationMilliseconds ( duration ) { let momentDuration ; if ( Number . isFinite ( duration ) ) { momentDuration = duration ; } else if ( Utils . isNumeric ( duration ) ) { momentDuration = parseFloat ( duration as string ) ; } else { momentDuration = Duration . fromISO ( duration ) . as ( 'milliseconds' ) ; } return momentDuration ; } param duration public static getDuration ( duration ) { let momentDuration ; if ( Number . isFinite ( duration ) ) { momentDuration = duration ; } else if ( Utils . isNumeric ( duration ) ) { momentDuration = parseFloat ( duration as string ) ; } else { momentDuration = Duration . fromISO ( duration ) . as ( 'milliseconds' ) ; } return this . formatDurationAsAspNetTimespan ( momentDuration ) ; } param duration public static getDurationFromSeconds ( duration ) { return this . getDuration ( parseFloat ( duration ) * 1000 ) ; } param datetime public static datetimeToString ( datetime ) { const d = new Date ( datetime ) ; return d . toLocaleString ( ) + `(${ - 1 * ( d . getTimezoneOffset ( ) / 60 ) }00)` ; } param timestamp public static timestampToUTCString ( timestamp ) { return dayjs ( timestamp ) . toISOString ( ) ; } param duration public static formatDurationAsAspNetTimespan ( duration ) { if ( duration >= TimeUtils . MaxDurationInMilliseconds ) { return Constants . DurationInfinity ; } const milliseconds = duration % 1000 ; const seconds = duration / 1000 ; const minutes = seconds / 60 ; const hours = minutes / 60 ; const days = hours / 24 ; if ( duration < 1000 ) { return `${ milliseconds } milliseconds` ; } if ( duration < ( 60 * 1000 ) ) { return `${ seconds } seconds` ; } if ( duration < ( 60 * 60 * 1000 ) ) { return `${ this . formatIndividualTime ( minutes % 60 , 0 ) }:` + `${ this . formatIndividualTime ( seconds % 60 , 2 , 2 ) } minutes` ; } if ( duration < ( 60 * 60 * 1000 * 24 ) ) { return `${ Math . floor ( hours % 24 ) }:` + `${ this . formatIndividualTime ( minutes % 60 ) }:` + `${ this . formatIndividualTime ( seconds % 60 , 2 , 2 ) }` + ` hours` ; } return `${ Math . floor ( days ) > 0 ? Math . floor ( days ) + ' days ' : '' }` + `${ this . formatIndividualTime ( hours % 24 ) }:` + `${ this . formatIndividualTime ( minutes % 60 ) }:` + `${ this . formatIndividualTime ( seconds % 60 , 2 , 2 ) }` ; } private static formatIndividualTime ( duration , padStart = 2 , substringSize = 0 ) { let floored = Math . floor ( duration ) . toString ( ) ; if ( substringSize > 0 ) { floored = floored . substring ( 0 , substringSize ) ; } return floored . padStart ( padStart , '0' ) ; } }