我需要执行一个简单的循环,直到达到第二个特定的日期和时间。这将基于Linux系统时间。我在网上找不到任何证明这一点的样本。我正在使用boost库,所以任何包含boost的想法都可以。感谢。
修改我的问题如何将一个时间结构与另一个结构进行比较以查看哪个更大?
using namespace std;
using namespace boost::local_time;
stringstream ss;
string strStartTime = "1/5/2014 10:59:59 AM";
local_time_input_facet *input_facet = new local_time_input_facet("%m/%d/%Y %H:%M:%S %p !");
ss.imbue(std::locale(ss.getloc(), input_facet));
local_date_time ldt(not_a_date_time);
string stringDateOnly = EasySplit(strStartTime, " ")[0];
string stringTimeOnly = EasySplit(strStartTime, " ")[1];
string stringMonthOnly = EasySplit(stringDateOnly, "/")[0];
string stringDayOnly = EasySplit(stringDateOnly, "/")[1];
string stringYearOnly = EasySplit(stringDateOnly, "/")[2];
string stringTimeHourOnly = EasySplit(stringTimeOnly, ":")[0];
string stringTimeMinuteOnly = EasySplit(stringTimeOnly, ":")[1];
string stringTimeSecondOnly = EasySplit(EasySplit(stringTimeOnly, ":")[2], " ")[0];
string stringTimeAMPMOnly = EasySplit(EasySplit(stringTimeOnly, ":")[2], " ")[1];
//if(stringMonthOnly.length()==1)
//{
// stringMonthOnly = "0" + stringMonthOnly;
//}
//if(stringDayOnly.length()==1)
//{
// stringDayOnly = "0" + stringDayOnly;
//}
//if(stringTimeHourOnly.length()==1)
//{
// stringTimeHourOnly = "0" + stringTimeHourOnly;
//}
//if(stringTimeMinuteOnly.length()==1)
//{
// stringTimeMinuteOnly = "0" + stringTimeMinuteOnly;
//}
//if(stringTimeSecondOnly.length()==1)
//{
// stringTimeSecondOnly = "0" + stringTimeSecondOnly;
//}
stringDateOnly = stringMonthOnly + "/" + stringDayOnly + "/" + stringYearOnly;
stringTimeOnly = stringTimeHourOnly + ":" + stringTimeMinuteOnly + ":" + stringTimeSecondOnly + " " + stringTimeAMPMOnly;
strStartTime = stringDateOnly + " " + stringTimeOnly;
ss.str(strStartTime);
ss >> ldt;
std::tm btm = {0};
btm.tm_sec = atoi(stringTimeSecondOnly.c_str());
btm.tm_min = atoi(stringTimeMinuteOnly.c_str());
btm.tm_hour = atoi(stringTimeHourOnly.c_str());
btm.tm_mday = atoi(stringDayOnly.c_str());
btm.tm_mon = atoi(stringMonthOnly.c_str());
btm.tm_year = atoi(stringYearOnly.c_str());
btm.tm_isdst = 1;
std::time_t tt = mktime(&btm);
boost::chrono::system_clock::time_point end_time = boost::chrono::system_clock::from_time_t(tt);
while (end_time > boost::chrono::system_clock::now())
{
cout << "Waiting\n";
}
cout << "EXIT\n";
答案 0 :(得分:2)
这可以通过std::chrono
库轻松完成,如下所示:
// For a timepoint that is relative to now
std::chrono::system_clock::time_point end_time =
std::chrono::system_clock::now() + std::chrono::milliseconds(500);
// For a certain point in time
std::time_t tt;
std::tm tm = {0};
tm.tm_sec = 0;
tm.tm_min = 9;
tm.tm_hour = 19;
tm.tm_mday = 5;
tm.tm_mon = 1 - 1;
tm.tm_year = 2014 - 1900;
tm.tm_isdst = -1;
tt = mktime(&tm);
std::chrono::system_clock::time_point end_time =
std::chrono::system_clock::from_time_t(tt);
while (end_time > std::chrono::system_clock::now()) {
foo(bar);
}
答案 1 :(得分:0)
超越你的问题只是一个想法。
如果你不需要疯狂的精确度,你可以检查循环中每第n次迭代的时间,而不是每一次:
int n = 1000;
int i = 0;
while (true) {
foo(bar);
i++;
if (i >= n) {
if (end_time > std::chrono::system_clock::now()) {
break;
}
i = 0;
}
}