2016-09-13 16 views
2

関数が呼び出されたときの平均UTC時間を求めようとしています。だから私は:複数のptimeの平均

boost::posix_time::ptime current_time_before(boost::posix_time::microsec_clock::universal_time()); 
    DoStuff(); 
    boost::posix_time::ptime current_time_after(boost::posix_time::microsec_clock::universal_time()); 

これらの2つの時間の平均を計算するにはどうすればよいですか? は、私が試した:

double time_avg = (current_time_before+current_time_after)*0.5; 

しかし、私は、「+」ではなくて、問題を持っているようだLinuxシステム上のエラーを取得「 - 」を。

ありがとうございました。

+0

(http://www.boost.org [だけのドキュメントを見て]/doc/libs/1_61_0/doc/html/date_time/posix_ti me.html#date_time.posix_time.ptime_class)、私は「演算子+(ptime)」がリストされていないことに気づいています。 +は実装されていないようです。 'ptime'を追加可能なものに変換する必要があります。おそらく、時刻0で 'operator-'を使用して、エポックから継続時間を取得し、 'time_duration'を使用します。彼らには 'operator +'がありませんので、これが有効な解決策かどうかわかりません。 – user4581301

+0

@ user4581301日付を追加するにはどうすればいいですか?できません。これは典型的なものです。日付の違いは日付ではなく、期間です。 – sehe

答えて

2

ちょっと...自然に書きますか?

ptime midpoint(ptime const& a, ptime const& b) { 
    return a + (b-a)/2; // TODO check for special case `b==a` 
} 

ライブデモ:

Live On Coliru

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

using boost::posix_time::ptime; 

ptime midpoint(ptime const& a, ptime const& b) { 
    return a + (b-a)/2; 
} 

int main() { 

    ptime a = boost::posix_time::second_clock::local_time(); 
    ptime b = a + boost::posix_time::hours(3); 

    std::cout << "Mid of " << a << " and " << b << " is " << midpoint(a,b) << "\n"; 
    std::swap(a,b); 
    std::cout << "Mid of " << a << " and " << b << " is " << midpoint(a,b) << "\n"; 
} 

プリント

Mid of 2016-Sep-15 11:17:10 and 2016-Sep-15 14:17:10 is 2016-Sep-15 12:47:10 
Mid of 2016-Sep-15 14:17:10 and 2016-Sep-15 11:17:10 is 2016-Sep-15 12:47:10