Adding and subtracting time

Hi guys, I'm new to C ++. I wanted to make a program to add and subtract time using the HH: MM format. Input Example:

12:00 + 3:00 - 6:00 

Output Example:

 9:00 

Another input example:

 9:00 + 8:00 + 13:00 - 2:25 

Output Example:

 27:35 

How do I know this? I thought to translate everything for a few seconds, and then apply the math, then use the function of module 60 to return the time. Any help creating such a program?

+7
source share
4 answers

this (part 1) and this (part 2) should be exactly what you want.

you get a clear explanation, and the author goes through the code line by line, and he also uses best practice.

+1
source

You need to think about what you mean by "time." There are two concepts, time points and durations. It makes no sense to add or subtract time points from each other. It makes sense to add and subtract durations (leading to duration), and it makes sense to add and subtract durations with a time point (the result is a time point.

Many time APIs do not do an excellent job of differentiating the two concepts, but the standard C ++ <chrono> works pretty well.

Here is some code that abuses the C tm type to get a couple of durations from the user, adds them together, and then abuses tm again to print the result.

 #include <iostream> // cout, cin #include <iomanip> // get_time, put_time #include <chrono> // hours, minutes, duration_cast int main() { // input, get a couple durations to do arithmetic on // technically std::tm represents a time point and get_time is supposed to // parse a time point, but we treat the results as a duration std::tm t; std::cin >> std::get_time(&t, "%H:%M"); auto duration1 = std::chrono::hours(t.tm_hour) + std::chrono::minutes(t.tm_min); std::cin >> std::get_time(&t, "%H:%M"); auto duration2 = std::chrono::hours(t.tm_hour) + std::chrono::minutes(t.tm_min); // do the arithmetic auto sum = duration1 + duration2; // output auto hours = std::chrono::duration_cast<std::chrono::hours>(sum); auto minutes = std::chrono::duration_cast<std::chrono::minutes>(sum - hours); t.tm_hour = hours.count(); t.tm_min = minutes.count(); std::cout << std::put_time(&t, "%H:%M") << '\n'; } 
+1
source

The simplest solution is to simply parse input into integers (using std::istream ), insert them into tm (the type defined in <time.h> ), and call mktime (also in <time.h> ). (There are several new things for handling time in C ++ 11, but I'm not familiar with it yet.)

0
source

I wanted to share some code as queries for a program as a beginner in C ++. This is not perfect code, but it can be a good hands-on experience for someone new to C ++. He will consider adding and subtracting timestamps. ie You may need to add additional checks and can be extended to represent days, seconds and milliseconds ...

 #include <iostream> #include <string> using namespace std; /// Represents the timestamp in HH:MM format class Time { public: // Construct the time with hours and minutes Time(size_t hours, size_t mins); // Construct the time using a string Time(const string& str); // Destructor ~Time() {} // Add a given time Time operator + (const Time& rTime) const; // Subtract a given time Time operator - (const Time& rTime) const; // Get the members int Hours() const { return m_Hours; } int Minutes() const { return m_Minutes; } // Get the time as a string in HH:MM format string TimeStr(); private: // Private members int m_Hours; // Hours int m_Minutes; // Minutes }; // Constructor Time::Time(size_t hours, size_t mins) { if (hours >= 60 || mins >= 60) { cout << "Invalid input" << endl; exit(0); } // Update the members m_Hours = hours; m_Minutes = mins; } // Add the given time to this Time Time::operator + (const Time& rTime) const { // Construct the time int nTotalMinutes(m_Minutes + rTime.Minutes()); int nMinutes(nTotalMinutes % 60); int nHours(nTotalMinutes/60 + (m_Hours + rTime.Hours())); // Return the constructed time return Time(nHours, nMinutes); } // Construct the time using a string Time::Time(const string& str) { if(str.length() != 5 || str[2] != ':') { cout << "Invalid time string. Expected format [HH:MM]" << endl; exit(0); } // Update the members m_Hours = stoi(str.substr(0, 2)); m_Minutes = stoi(str.substr(3, 2)); } // Substact the given time from this Time Time::operator - (const Time& rTime) const { // Get the diff in seconds int nDiff(m_Hours*3600 + m_Minutes*60 - (rTime.Hours()*3600 + rTime.Minutes()*60)); int nHours(nDiff/3600); int nMins((nDiff%3600)/60); // Return the constructed time return Time(nHours, nMins); } // Get the time in "HH:MM" format string Time::TimeStr() { // Fill with a leading 0 if HH/MM are in single digits return ((m_Hours < 10) ? string("0") + to_string(m_Hours) : to_string(m_Hours)) + string(":") + ((m_Minutes < 10) ? string("0") + to_string(m_Minutes) : to_string(m_Minutes)); } int main() { Time time1("09:00"); // Time 1 Time time2("08:00"); // Time 2 Time time3("13:00"); // Time 3 Time time4("02:25"); // Time 4 //time1 + time 2 + time3 - time4 cout << (time1 + time2 + time3 - time4).TimeStr() << endl; return 0; } 

Conclusion: 27:35

0
source

All Articles