データのタイムスタンプを見て、データが 120 秒 (または 2 分) 古いかどうかを確認しようとしているのでchrono
、C++ でパッケージを使用しているため、以下のコードがあります。
uint64_t now = duration_cast<milliseconds>(steady_clock::now().time_since_epoch()).count();
// check for 2 minutes old data
bool is_old = (120 * 1000 < (now - data_holder->getTimestamp()));
uint64_t value = now;
while (now < data_holder->getTimestamp() + 80 * 1000
&& now < value + 80 * 1000) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
now = duration_cast<milliseconds>(steady_clock::now().time_since_epoch()).count();
}
上記のコードのdata_holder->getTimestamp()
uint64_t は、ミリ秒単位でタイムスタンプを返します。
now
変数値を印刷すると、これが表示され、値10011360
を印刷するとdata_holder->getTimestamp()
1437520382241
2015-07-21 16:13:02,530 WARN 0x7f35312d1700 data_check - now value: 10011360 , data holder timestamp: 1437520382241
そして、上記のデータホルダーのタイムスタンプから、120 秒前のデータではないように見えるので、コードに何か問題があると感じますか? そのデータホルダーのタイムスタンプを(エポックコンバーターを使用して)実際の時間に変換し、上記のようにログ時間と比較すると、ほぼ同じになります。
そこで、代わりに を使用することにし、system_clock
代わりにをsteady_clock
使用し始めた以下のコードを思いつきました。auto
uint64_t
ソリューション A:
auto now = system_clock::now();
auto dh_ts = system_clock::time_point{milliseconds{data_holder->getTimestamp()}};
bool is_old = (minutes{2} < (now - dh_ts));
以前は、now
変数の値をuint64_t
の代わりに使用していましauto
た。上記のコードの後、元のコードにこのようなものがあるので、そうでnow
はないuint64_t
ため、コードのコンパイル中にコンパイルエラーが発生します。
uint64_t value = now;
while (now < data_holder->getTimestamp() + 80 * 1000
&& now < value + 80 * 1000) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
now = duration_cast<milliseconds>(steady_clock::now().time_since_epoch()).count();
}
これを修正する正しい方法は何ですか? data_holder->getTimestamp()
データ型を変更できませんuint64_t
。他のコードでも使用されているためです。
エラーは次のとおりです。
error: cannot convert std::chrono::time_point<std::chrono::_V2::system_clock, std::chrono::duration<long int, std::ratio<1l, 1000000000l> > >â to âuint64_t {aka long unsigned int}â in initialization
アップデート:
Solution A
以下のすべてが良さそうに見える場合に使用する代わりに、このように使用できますか?
ソリューション B:
uint64_t now = duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count();
bool is_old = (120 * 1000 < (now - data_holder->getTimestamp()));