gpt4 book ai didi

c++ - 执行循环直到系统时间到达特定日期/时间

转载 作者:行者123 更新时间:2023-11-28 03:00:44 25 4
gpt4 key购买 nike

我需要执行一个简单的循环,直到达到精确到秒的特定日期和时间。这将基于 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";

最佳答案

这可以通过 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);
}

关于c++ - 执行循环直到系统时间到达特定日期/时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20922563/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com