2011-09-14 2 views
0

나는 SQL 서버 데이터베이스를 가지고 있고 그것에서 날짜를 당겨 같은 INT64에 timestamp_t의 종류를 변환하고 있습니다 :SQL Server에서 timestamp_t를 부스트하는 ptime이 분 단위로 해제됩니다. 나는 무엇을 잘못 했는가?

Int64 from_timestamp_t(dtl::timestamp_t& t) 
{ 
    // create a new posix time structure 
    boost::posix_time::ptime pt 
    (
    boost::gregorian::date   (t.year, t.month, t.day), 
    boost::posix_time::time_duration (t.hour, t.minute, t.second, t.fraction) 
    ); 

    ptime epoch(date(1970, Jan, 1)); 
    boost::posix_time::time_duration fromEpoch = pt - epoch; 

    // return it to caller 
    return fromEpoch.total_milliseconds(); 
} 

나는 등의 INT64에서 부스트의 Ptime로 다시 변환하려고 시도 :

이유가 무엇인지, 내 날짜, 시간 등이 모두 정확한 이유는 알 수 없지만 내 분은 앞으로 몇 분이 걸릴 것입니다. 데이터베이스의 타임 스탬프가 초 단위이기 때문에 밀리 초를 사용하고 있습니까? 이 문제를 어떻게 해결할 수 있습니까?

Int64 from_timestamp_t(dtl::timestamp_t& t) 
{ 
    int count = t.fraction * (time_duration::ticks_per_second() % 1000); 

    boost::posix_time::ptime pt 
     (
     boost::gregorian::date   (t.year, t.month, t.day), 
     boost::posix_time::time_duration (t.hour, t.minute, t.second, count) 
     ); 

    ptime epoch(date(1970, Jan, 1), time_duration(0, 0, 0, 0)); 

    boost::posix_time::time_duration fromEpoch = pt - epoch; 

    return fromEpoch.total_milliseconds(); 
} 
+0

댄의 대답은 내가 찾을 – jjacksonRIAB

답변

1

내가 SQL 서버 2005에 익숙하지 해요,하지만 ticksFromEpoch가 일치하는 경우는 POSIX의 시간이 초에게 기능이 향상 : 댄 제안 다음 수정을 적용

문제를 해결 한 것 같다 일초.

이 처리하는 또 다른 방법은 코드를 작성 TIME_DURATION의 을 ticks_per_second() 메소드를 사용하는 것입니다

ptime time = epoch + boost::posix_time::seconds(ticksFromEpoch); 

은 그러나,이 처리하는 일반적인 방법은 부스트 ​​DATE_TIME documentation에 표시됩니다 이동 중에도 라이브러리가 컴파일됩니다. 다음과 같이 해상도 독립적 인 수를 계산하는 일반적인 공식은 다음과 같습니다

count*(time_duration_ticks_per_second/count_ticks_per_second) 

예를 들어, 우리는 초 단위를 나타내는 계수 를 사용하여 구성한다고 가정하자. 즉, 각 틱은 0.1 초입니다.

int number_of_tenths = 5; // create a resolution independent count -- 
          // divide by 10 since there are 
          //10 tenths in a second. 
int count = number_of_tenths*(time_duration::ticks_per_second()/10); 
time_duration td(1,2,3,count); //01:02:03.5 //no matter the resolution settings 
+0

위 내 솔루션을 수정, 속임수를 썼는지 그 내가 제대로 나오는 이전과 다음 total_milliseconds를 반환 from_time_t 사용의 Ptime 먼저 다음 time_t로 변환합니다. gregorian과 time_duration에서 posix_time을 생성하는 데는 잘못된 것이 있습니다. 그러나 나는 아직도 그것을 보지 못합니다. – jjacksonRIAB

관련 문제