2011-08-17 4 views
1

나는 boost :: posix_time :: ptime thorugh를주는 장치가 있습니다. 다른 데이터와 동기화하려면 NTP 타임 스탬프로 시간이 필요하며 NTP 초 및 NTP fract_seconds로 구분됩니다.부스트 :: posix_time :: ptime을 NTP 타임 스탬프로 변환

#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; 
    } 

그러나 그것은 나에게주지 않는 올바른 결과 :

내가 좋아하는 뭔가를 시도? 어떤 도움을 주셔서 감사합니다.

+0

@person이 투표를 끝내기 위해 'time_t'이 (가) NTP와 다른 것을 확인하지 못했습니다. – Nim

답변

0

참고 : boost::ptime may에는 NTP에 필요한 세분성이 없습니다. 그러나 당신이 필요로하는 양을 얻기 위해 무엇을 할 수 있는지

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

지금 당신은 당신이 필요한 NTP 타임 스탬프를 구성 할 수있는 두 개의 32 비트 값을 가져야한다 (다음과 같이 초 부분이다).

관련 문제