1

I have a device which gives me boost::posix_time::ptime thorugh its API. To sychronize with other Data I need the time as NTP timestamp, differentiated in NTP seconds and NTP fract_seconds.

I tried something like:

#include "boost/date_time/posix_time/posix_time.hpp"
#include <iostream>

using namespace std;

using boost::posix_time::ptime;
using boost::posix_time::time_duration;
using boost::gregorian::date;

int main() 
  {
    ptime t = boost::posix_time::microsec_clock::local_time();

    ptime myEpoch(date(1900,boost::gregorian::Jan,1));

    time_duration myTimeFromEpoch = t - myEpoch;

    boost::uint64_t myTimeAsInt = myTimeFromEpoch.ticks();

    cout << myTimeAsInt << endl;

    boost::uint32_t seconds = (boost::uint32_t)((myTimeAsInt >> 32) & 0xFFFFFFFF);

    boost::uint32_t fraction = (boost::uint32_t)(myTimeAsInt & 0xFFFFFFFF);

    cout << seconds << endl;
    cout << fraction << endl;

    return 0;
  }

But it does not give me the correct results? Any help appreciated.

Nicolas Kaiser
  • 1,628
  • 2
  • 14
  • 26
metacircle
  • 11
  • 2
  • @person voting to close, not sure it's a duplicate of that `time_t` is different to NTP. – Nim Aug 17 '11 at 09:34

1 Answers1

0

NOTE: boost::ptime may not have the necessary granularity for NTP. However what you can do to obtain the quantity needed (seconds and fractional is as follows)

long seconds = myTimeFromEpoch.total_seconds();
long fractional = myTimeFromEpoch.fractional_seconds()

Now you should have two 32-bit values from which you can construct the necessary NTP timestamp.

Nim
  • 33,299
  • 2
  • 62
  • 101