int UTILS_TimespecAdd()

in FreeRTOS-Plus-POSIX/source/FreeRTOS_POSIX_utils.c [185:223]


int UTILS_TimespecAdd( const struct timespec * const x,
                       const struct timespec * const y,
                       struct timespec * const pxResult )
{
    int64_t llPartialSec = 0;
    int iStatus = 0;

    /* Check parameters. */
    if( ( pxResult == NULL ) || ( x == NULL ) || ( y == NULL ) )
    {
        iStatus = -1;
    }

    if( iStatus == 0 )
    {
        /* Perform addition. */
        pxResult->tv_nsec = x->tv_nsec + y->tv_nsec;

        /* check for overflow in case nsec value was invalid */
        if( pxResult->tv_nsec < 0 )
        {
            iStatus = 1;
        }
        else
        {
            llPartialSec = ( pxResult->tv_nsec ) / NANOSECONDS_PER_SECOND;
            pxResult->tv_nsec = ( pxResult->tv_nsec ) % NANOSECONDS_PER_SECOND;
            pxResult->tv_sec = x->tv_sec + y->tv_sec + llPartialSec;

            /* check for overflow */
            if( pxResult->tv_sec < 0 )
            {
                iStatus = 1;
            }
        }
    }

    return iStatus;
}