繁体   English   中英

如何根据经过的时间做出if语句?

[英]How to make an if statement based on how much time passed?

我正在寻找一种添加if语句的方法,该语句将如下所示,只是不确定如何执行。

if(120 seconds have passed)
{
    // do stuff
}

alt将是(假设x是每次检查之间经过的时间)

do
{
    // do stuff
    x = 0;
}while(x=120)

因此,它将不断循环。 谁能告诉我解决方案?

您可以使用Boost.DateTime来完成这项工作。

这是描述每个步骤的示例:

const boost::posix_time::ptime start = boost::posix_time::microsec_clock::local_time();
// Do stuff
const boost::posix_time::ptime end = boost::posix_time::microsec_clock::local_time();
const boost::posix_time::time_duration duration = end - start;
const boost::posix_time::time_duration remaining = boost::posix_time::seconds(120) - duration;
boost::this_thread::sleep(remaining);

可以通过以下方式简化:

const boost::posix_time::ptime start = boost::posix_time::microsec_clock::local_time();
// Do stuff
boost::this_thread::sleep(boost::posix_time::seconds(120) - boost::posix_time::microsec_clock::local_time() + start);

或通过这种方式简化:

const boost::posix_time::ptime end = boost::posix_time::microsec_clock::local_time() + boost::posix_time::seconds(120);
// Do stuff
boost::this_thread::sleep(end - boost::posix_time::microsec_clock::local_time());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM