static uint64_t calculateElapsedTimeMs()

in source/core_sntp_client.c [146:185]


static uint64_t calculateElapsedTimeMs( const SntpTimestamp_t * pCurrentTime,
                                        const SntpTimestamp_t * pOlderTime )
{
    uint64_t timeDiffMs = 0UL;
    uint32_t timeDiffSec = 0U;

    assert( pCurrentTime != NULL );
    assert( pOlderTime != NULL );

    /* Detect if SNTP time has overflown between the 2 timestamps. */
    if( pCurrentTime->seconds < pOlderTime->seconds )
    {
        /* Handle the SNTP time overflow by calculating the actual time
         * duration from pOlderTime, that exists in NTP era 0, to pCurrentTime,
         * that exists in NTP era 1. */
        timeDiffSec = ( UINT32_MAX - pOlderTime->seconds ) + /* Time in NTP era 0. */
                      1U +                                   /* Epoch time in NTP era 1, i.e. 7 Feb 2036 6h:14m:28s. */
                      pCurrentTime->seconds;                 /* Time in NTP era 1. */

        timeDiffMs = ( uint64_t ) timeDiffSec * 1000UL;
    }
    else
    {
        timeDiffSec = ( pCurrentTime->seconds - pOlderTime->seconds );
        timeDiffMs = ( uint64_t ) timeDiffSec * 1000UL;
    }

    if( pCurrentTime->fractions > pOlderTime->fractions )
    {
        timeDiffMs += ( ( uint64_t ) pCurrentTime->fractions - ( uint64_t ) pOlderTime->fractions ) /
                      ( SNTP_FRACTION_VALUE_PER_MICROSECOND * 1000UL );
    }
    else
    {
        timeDiffMs -= ( ( uint64_t ) pOlderTime->fractions - ( uint64_t ) pCurrentTime->fractions ) /
                      ( SNTP_FRACTION_VALUE_PER_MICROSECOND * 1000UL );
    }

    return timeDiffMs;
}