module Sfx { declare var moment ; export class TimeUtils { public static MaxDurationInMilliseconds : number = 922337203685477.5807 ; public static AddSeconds ( toDate , seconds ) { let date = new Date ( toDate . valueOf ( ) ) ; date . setTime ( date . getTime ( ) + ( seconds * 1000 ) ) ; return date ; } public static AddDays ( toDate , days ) { let date = new Date ( toDate . valueOf ( ) ) ; date . setDate ( date . getDate ( ) + days ) ; return date ; } param duration public static getDuration ( duration ) { let momentDuration ; if ( _ . isFinite ( duration ) ) { momentDuration = moment . duration ( duration ) ; } else if ( Utils . isNumeric ( duration ) ) { momentDuration = moment . duration ( parseFloat ( < string > duration ) ) ; } else { momentDuration = moment . duration ( duration ) ; } return this . formatDurationAsAspNetTimespan ( momentDuration ) ; } param duration public static getDurationFromSeconds ( duration ) { return this . getDuration ( parseFloat ( duration ) * 1000 ) ; } param datetime public static datetimeToString ( datetime ) { return moment ( datetime ) . format ( "MMM D, YYYY [at] h:mm:ss A (ZZ)" ) ; } param timestamp public static timestampToUTCString ( timestamp ) { let date = moment . utc ( timestamp ) ; return ! date . isValid ( ) || date . year ( ) === 1 ? Constants . InvalidTimestamp : date . toDate ( ) . toUTCString ( ) ; } param duration private static formatDurationAsAspNetTimespan ( duration ) { if ( duration . asMilliseconds ( ) >= TimeUtils . MaxDurationInMilliseconds ) { return Constants . DurationInfinity ; } return `${ Math . floor ( duration . asDays ( ) ) }.` + `${ _ . padStart ( Math . floor ( duration . hours ( ) ) . toString ( ) , 2 , "0" ) }:` + `${ _ . padStart ( Math . floor ( duration . minutes ( ) ) . toString ( ) , 2 , "0" ) }:` + `${ _ . padStart ( Math . floor ( duration . seconds ( ) ) . toString ( ) , 2 , "0" ) }.` + `${ Math . ceil ( duration . milliseconds ( ) ) }` ; } } }