Gerome Schutte
Gerome Schutte

Reputation: 184

how can I convert time stored in a string variable to time format?

I googled some topics on this, but they all seem quite vague.

I have 4 string variables, containing time representing the begin time and end time of 2 events in the format "17:30" or "01:20" etc.

I want to write a function to determine whether they clash or not. So what I'm looking for is something like

string beginTime1 = "01:30";
string beginTime2 = "03:30";
string endTime1 = "01:30";
string endTime2 = "01:30";
time begin1, begin2, end1, end2;

begin1 = toTime(beginTime1);
begin2 = toTime(beginTime2);
end1 = toTime(endTime1);
end2 = toTime(endTime2);

If (begin2 > begin1 && begin2 < end1)
    return clash;

Or something like that.

Upvotes: 0

Views: 121

Answers (2)

John Zwinck
John Zwinck

Reputation: 249582

What does it mean for two events to "clash"? This happens when one starts before the other ends. So begin1 < end2 || begin2 < end1.

If your times are always formatted like HH:MM, there is no need to convert them to another format before converting, because the lexicographical order is already correct.

Upvotes: 0

Manu343726
Manu343726

Reputation: 14184

Boost Posix Time Library has a function to get a posix_time value from a std::string: from_string().

Upvotes: 1

Related Questions