C++ 执行循环,直到系统时间达到特定日期/时间
我需要执行一个简单的循环,直到到达第二个特定的日期和时间。这将基于Linux系统时间。我在网上找不到任何能证明这一点的样品。我使用的是boost库,所以任何包含boost的想法都可以。谢谢 要修改我的问题,您如何将一个时间结构与另一个时间结构进行比较,以确定哪个时间结构更大C++ 执行循环,直到系统时间达到特定日期/时间,c++,boost,C++,Boost,我需要执行一个简单的循环,直到到达第二个特定的日期和时间。这将基于Linux系统时间。我在网上找不到任何能证明这一点的样品。我使用的是boost库,所以任何包含boost的想法都可以。谢谢 要修改我的问题,您如何将一个时间结构与另一个时间结构进行比较,以确定哪个时间结构更大 using namespace std; using namespace boost::local_time; stringstream ss; string strStartTime = "1/5/2014 10:59
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";
使用名称空间std;
使用名称空间boost::local_time;
细流ss;
字符串strStartTime=“1/5/2014 10:59:59 AM”;
本地时间输入面*输入面=新的本地时间输入面(“%m/%d/%Y%H:%m:%S%p!”);
imbue(std::locale(ss.getloc(),input_facet));
本地日期时间ldt(非日期时间);
字符串stringDateOnly=EasySplit(strStartTime,“”)[0];
字符串stringTimeOnly=EasySplit(strStartTime,“”)[1];
字符串stringMonthOnly=EasySplit(stringDateOnly,“/”[0];
字符串stringDayOnly=EasySplit(stringDateOnly,“/”[1];
字符串stringYearOnly=EasySplit(stringDateOnly,“/”[2];
字符串stringTimeHourOnly=EasySplit(stringTimeOnly,“:”)[0];
字符串stringTimeMinuteOnly=EasySplit(stringTimeOnly,“:”)[1];
字符串stringTimeSecondOnly=EasySplit(EasySplit(stringTimeOnly,“:”)[2],“)[0];
字符串StringTimeAmmonly=EasySplit(EasySplit(stringTimeOnly,“:”)[2],“)[1];
//if(stringMonthOnly.length()=1)
//{
//stringMonthOnly=“0”+stringMonthOnly;
//}
//如果(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+”+StringTimeAmmonly;
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\U 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使用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);
}
使用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);
}
使用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);
}
使用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);
}
只是一个你无法回答的想法
如果您不需要惊人的精度,您可以在循环中的每个第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;
}
}
只是一个你无法回答的想法
如果您不需要惊人的精度,您可以在循环中的每个第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;
}
}
只是一个你无法回答的想法
如果您不需要惊人的精度,您可以在循环中的每个第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;
}
}
只是一个你无法回答的想法
如果您不需要惊人的精度,您可以在循环中的每个第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;
}
}
这看起来像。请说明您试图实现的目标。请向我们展示您尝试过的目标。总体策略是检查循环内的系统时间,并在规定的时间内突破。这看起来像。请说明您试图实现的目标。请向我们展示您尝试过的目标。总体策略是检查循环内的系统时间,并在规定的时间内中断。这看起来像。请说明您试图实现的目标。请向我们展示您的尝试。一般策略是检查循环内的系统时间,并在规定的时间中断。这看起来像。请说明您试图实现的目标。请向我们展示你的尝试。一般的策略是检查循环中的系统时间,并在规定的时间中断。然后他可以填写一个time\t
结构,并从中构造end\u time
(std::chrono::system\u clock::from\u time\t
)是的,我想用这种格式的字符串“2014年1月4日上午10:48:20“转换为时间,然后与现在进行比较。我编辑了我的问题,因为看起来你的答案非常接近,请查看我关于使用C++x0的编辑。谢谢你的gcc--version
?你使用了什么标志进行编译?然后他可以填写time\t
结构并从中构造end\u time
(std::chrono::system\u clock::from\u time\t
)是的,我想用这种格式的字符串“1/4/2014 10:48:20 AM”转换为时间,然后与现在比较。我编辑了我的问题,因为看起来你的答案非常接近,请查看我关于使用C++x0的编辑。谢谢你的gcc--version
?你用什么标记编译过?然后他可以填写time\t
结构并从中构造end\u time
(std::chrono::system\u clock::from\u time\t
)是的,我想用这种格式的字符串“1/4/2014 10:48:20 AM”转换为时间,然后与现在比较。我编辑了我的问题,因为看起来你的答案非常接近,请查看我关于使用C++x0的编辑。谢谢你的gcc--version
?你用什么标记编译过?然后他可以填写time\t
结构并从中构造end\u time
(std::chrono::system\u clock::from\u time\t
)是的,我想要一个字符串