Lesson 3

Time Calculation and String Manipulation in C++

Introduction

Welcome! In today's lesson, we will explore the practical applications of string operations and type conversions in C++. These concepts are crucial and are deployed in many programming spheres. We'll examine a real-world example: time parsing. Have you ever wondered how to add a certain number of seconds to a specific time? By the end of today's session, you'll be equipped to calculate this using C++. Let's get started!

Task Statement and Description

Imagine this: You receive a time, formatted as a string in HH:MM:SS where HH, MM, and SS denote the hour, minute, and second, respectively. You are also given an integer representing a certain number of seconds. Your task is to calculate the new time after adding the provided seconds, then output the result in the HH:MM:SS format.

For example, if the input time is 05:10:30 and the number of seconds to add is 123, the output should be 05:12:33 since 123 seconds translate to 2 minutes and 3 seconds.

Please note these points when resolving this task:

  • The input time is always a valid time string in the HH:MM:SS format, with HH ranging from 00 to 23, MM, and SS ranging from 00 to 59.
  • The output should be a time in the same format.

Now let's go ahead and break down how to achieve this in our step-by-step solution guide.

Step-by-Step Solution Building: Step 1

Our first step involves parsing the input time string. We aim to extract the hours, minutes, and seconds from this string as integer values for further calculations. In C++, we can use std::stringstream to divide the string at ":" and convert each substring into an integer:

C++
1std::string time = "12:34:56"; 2int seconds = 10; 3std::stringstream ss(time); 4std::string item; 5std::vector<int> time_parts; 6 7while (std::getline(ss, item, ':')) { 8 time_parts.push_back(std::stoi(item)); 9}

By executing this operation, we've successfully parsed the time string and converted the hours, minutes, and seconds into integers.

Step-by-Step Solution Building: Step 2

Now that we have the hours, minutes, and seconds in integer format, we can efficiently calculate the total number of seconds that have elapsed since the start of the day:

C++
1int seconds_since_start = time_parts[0] * 3600 + time_parts[1] * 60 + time_parts[2];

At this point, your function should compute the cumulative number of seconds from the start of the day.

Step-by-Step Solution Building: Step 3

Now we need to add the integer, representing a number of seconds, to our computed seconds_since_start, and also consider cases where the added seconds roll over into the next day:

C++
1int total_seconds = (seconds_since_start + seconds) % (24 * 3600);

The modulus operator % ensures that our total_seconds value doesn't exceed the total number of seconds in a day (86400 seconds or 24*3600 seconds).

Step-by-Step Solution Building: Step 4

In this step, we reverse the previous operation. We're given an integer number of seconds, and we have to convert this into a time string in the HH:MM:SS format.

We will use the division / and modulo % operations directly to convert these values:

C++
1int hours = total_seconds / 3600; 2total_seconds %= 3600; 3int minutes = total_seconds / 60; 4seconds = total_seconds % 60;
Step-by-Step Solution Building: Step 5

The final step is to assemble these values into our HH:MM:SS format string:

C++
1std::ostringstream os; 2os << std::setfill('0') << std::setw(2) << hours << ":" << std::setw(2) << minutes << ":" << std::setw(2) << seconds; 3std::string result = os.str();

In C++, setw and setfill are manipulator functions used for formatting output, particularly when dealing with streams like std::ostringstream.

  • std::setw(int width): Sets the width of the next input/output field. This means that the next item inserted into the stream will occupy at least width spaces. If the item is smaller than this width, it is padded. In this context, using std::setw(2) ensures that each time unit (hours, minutes, and seconds) will be at least 2 characters wide. If hours, minutes, or seconds are single digits (e.g., 7), they will be padded to make them two characters (07).

  • std::setfill(char c): Specifies the character to be used for padding. By default, padding is typically done with spaces, but in this case, we explicitly set the fill character to '0'. Therefore, std::setfill('0') will pad single-digit time components with 0, leading to outputs like 05, 08, etc.

Combining these two in the output stream ensures that all time units are consistently formatted as two digits, no matter whether they are single or double-digit numbers.

Final Solution

Let's collate all the individual steps and formulate the final function:

C++
1#include <sstream> 2#include <vector> 3#include <iomanip> 4#include <iostream> 5 6std::string time_adder(std::string time, int seconds) { 7 std::vector<int> time_parts; 8 std::string item; 9 std::stringstream ss(time); 10 while (std::getline(ss, item, ':')) { 11 time_parts.push_back(std::stoi(item)); 12 } 13 14 int seconds_since_start = time_parts[0] * 3600 + time_parts[1] * 60 + time_parts[2]; 15 int total_seconds = (seconds_since_start + seconds) % (24 * 3600); 16 int hours = total_seconds / 3600; 17 total_seconds %= 3600; 18 int minutes = total_seconds / 60; 19 seconds = total_seconds % 60; 20 21 std::ostringstream os; 22 os << std::setfill('0') << std::setw(2) << hours << ":" << std::setw(2) << minutes << ":" << std::setw(2) << seconds; 23 return os.str(); 24} 25 26int main() { 27 // Call the function 28 std::cout << time_adder("05:10:30", 123) << std::endl; 29 return 0; 30}

And voila! You've crafted a function that accurately calculates the new time based on the provided time and the number of seconds that have elapsed since then.

Lesson Summary

Congratulations! You skillfully learned how to parse a time string and utilize type conversions to calculate the number of seconds elapsed from the beginning of the day. Following this, you learned how to perform the reverse operation: to calculate the time based on the number of seconds that have passed since the beginning of the day. In this lesson, we practiced essential C++ skills, including string operations and fundamental integer operations. Continue practicing with similar problems to reinforce your learning, and these tasks will soon become second nature. See you in our next session, and happy coding!

Enjoy this lesson? Now it's time to practice with Cosmo!

Practice is how you turn knowledge into actual skills.