NTP timestamp is not correctly parsed c++

0

I want to send an SNTP req and then parse the response, hence I started by simply creating a timestamp convert it to ntp and then try to read the seconds and the fractional part. The issue is that the seconds part is not correctly parsed in the first place, always shifted by a few seconds. This is what I have done:

#include <stdio.h> 
#include <stdlib.h>
#include <stdint.h>
#include <iostream>    // Needed to perform IO operations
#include <ctime>

constexpr auto SECONDS_SINCE_FIRST_EPOCH = (2208988800ULL);
constexpr auto NTP_SCALE_FRAC = (4294967296ULL);

using namespace std; 
int main() 
{ 

    std::time_t _timeEpoch = std::time(nullptr);
    unsigned long long tv_ntp, tv_usecs, tv_time;
    tv_ntp = _timeEpoch + SECONDS_SINCE_FIRST_EPOCH; 
    tv_usecs = (NTP_SCALE_FRAC * _timeEpoch) / 1000000UL;
    tv_time = (tv_ntp << 32) | tv_usecs;

    std::cout << "GetUtcTimeMilliSeconds UTC sec: " << _timeEpoch << " 1900: " << tv_ntp << " fraction of second " << tv_usecs << " tv_time " << tv_time << std::endl;

    uint32_t msw = (tv_time >> 32) & 0xFFFFFFFF;
    uint32_t lsw = tv_time & 0xFFFFFFFF;

    std::cout << "GetUtcTimeMilliSeconds msw " << msw << " lsw: " << lsw << std::endl;  

    return 0; 
} 

I expected msw to be equal to tv_ntp and lsw to tv_usecs but they are not

GetUtcTimeMilliSeconds UTC sec: 1572103823 1900: 3781092623 fraction of second 6752134505701 tv_time 16239671495839982821
GetUtcTimeMilliSeconds msw 3781093167 lsw: 445916389
c++
windows
bit-manipulation
bitwise-operators
ntp
asked on Stack Overflow Oct 26, 2019 by lainos88 • edited Oct 26, 2019 by lainos88

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0