0

I need to perform a simple loop until a specific date and time down to the second is reached. This would be based off of the Linux system time. I cannot find any samples online that demonstrate this. I'm using the boost library so any ideas that would incorporate boost are fine. Thanks.

to modify my question how do you compare one time structure to another to see which is larger?

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";
user246181
  • 17
  • 1
  • 8
  • 1
    This seems like [an XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). Please state what you are trying to achieve. – Robin Green Jan 04 '14 at 15:05
  • 1
    Please show us what you've tried. The general strategy would be to check the system time inside the loop and break out of it at the prescribed time. – Adam Liss Jan 04 '14 at 15:05

2 Answers2

3

This can easily be done with the std::chrono library like this:

// 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);
}
  • Then he could fill out a `time_t` structure and construct `end_time` from it (`std::chrono::system_clock::from_time_t`) –  Jan 04 '14 at 15:39
  • Yes I'd like to take a string in this format "1/4/2014 10:48:20 AM" and convert to time and then compare against now. – user246181 Jan 04 '14 at 15:50
  • I edited my question, as it appears your answer is very close, please see my edit in regard to using the C++x0. Thanks – user246181 Jan 05 '14 at 06:13
  • What's your `gcc --version`? –  Jan 05 '14 at 13:24
  • What flags have you used to compile? –  Jan 05 '14 at 16:39
  • I have fixed the c++ox issue however the loop exits even though end_time should be greater than now(). This seems to be the last problem. – user246181 Jan 05 '14 at 17:33
  • Subtract `1900` from `tm_year`, `1` from `tm_mon` and get sure that the time is properly adjusted to your local timezone. –  Jan 05 '14 at 17:59
  • I edited the above code to newest version, I tried subtracting 1900 and also 1 from month, also subtracting 10 from second as "11:07:59 AM" outputs to "11:07:69 AM" for some reason. I still can't get it to work. – user246181 Jan 05 '14 at 19:08
  • finally got it to work tweaking a few things. Thanks! – user246181 Jan 05 '14 at 19:11
0

Just an idea beyond your question.

If you don't need insane precision, you could check for time every n'th iteration in the loop, not on every one:

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;
  }
}
bolov
  • 72,283
  • 15
  • 145
  • 224
  • I do need fast precision to the second however in the .NET app I'm porting to C++ I do use code similar to this to report the time left til launch every n'th interation as the GUI interaction hogs resources. – user246181 Jan 05 '14 at 19:10
  • @user246181 depending to the function, a iteration can take even a few hundreds of microseconds, so if you need up to the second, every 1000 or 100 iteration to check should be ok. Unless the function does take indeed very long (I/O operations, GUI etc). You should benchmark with different values for n. – bolov Jan 05 '14 at 19:47