import moment from 'moment' ; import { Time } from '@sap-cloud-sdk/odata-common/internal' ; internal export const durationRegex = /PT(?<hours>\d{1,2}H)?(?<minutes>\d{1,2}M)?(?<seconds>\d{1,2}S)?/ ; internal export function deserializeToTime ( value ) { const regexResult = durationRegex . exec ( value ) ; if ( ! regexResult ) { throw new Error ( `Failed to parse the value: ${ value } to time.` ) ; } const { hours , minutes , seconds } = regexResult ?. groups || { } ; return { hours : hours ? parseInt ( hours . replace ( 'H' , '' ) , 10 ) : 0 , minutes : minutes ? parseInt ( minutes . replace ( 'M' , '' ) , 10 ) : 0 , seconds : seconds ? parseInt ( seconds . replace ( 'S' , '' ) , 10 ) : 0 } ; } internal export function serializeFromTime ( value ) { return ( 'PT' + leftpad ( value . hours , 2 ) + 'H' + leftpad ( value . minutes , 2 ) + 'M' + leftpad ( value . seconds , 2 ) + 'S' ) ; } internal export function deserializeToMoment ( edmDateTime ) { const dateTimeOffsetComponents = /^\/Date\((?<ticks>\d+)((?<sign>[+-])(?<offset>\d{4}))?\)\/$/ . exec ( edmDateTime ) ?. groups ; if ( ! dateTimeOffsetComponents ) { throw new Error ( `Failed to parse edmDateTime: ${ edmDateTime } to moment.` ) ; } const timestamp = moment ( parseInt ( dateTimeOffsetComponents . ticks ) ) ; if ( dateTimeOffsetComponents . sign && dateTimeOffsetComponents . offset ) { const offsetMultiplier = dateTimeOffsetComponents . sign === '+' ? 1 : - 1 ; const offsetInMinutes = parseInt ( dateTimeOffsetComponents . offset ) ; return timestamp . utc ( ) . utcOffset ( offsetMultiplier * offsetInMinutes ) ; } return timestamp ; } internal export function serializeFromMoment ( momentInstance ) { const timestamp = momentInstance . unix ( ) * 1000 ; if ( momentInstance [ '_isUTC' ] ) { const offset = Math . abs ( momentInstance . utcOffset ( ) ) ; const operator = momentInstance . utcOffset ( ) >= 0 ? '+' : '-' ; return `/Date(${ timestamp }${ operator }${ leftpad ( offset , 4 ) })/` ; } return `/Date(${ timestamp })/` ; } function leftpad ( value , targetLength ) { const str = value . toString ( ) ; if ( str . length >= targetLength ) { return str ; } return '0' . repeat ( targetLength - str . length ) + str ; }