Adding and subtracting durations with std::chrono

In C++, the std::chrono library provides utilities for working with time durations. It allows you to add and subtract durations, representing time intervals, easily and accurately. In this blog post, we will explore how to perform these operations using std::chrono.

Understanding Time Durations

Before diving into adding and subtracting durations, it’s important to understand how durations are represented in std::chrono. The std::chrono::duration template represents a duration as a signed integral value with a specified period. For example, std::chrono::seconds represents a duration in seconds, std::chrono::milliseconds in milliseconds, and so on.

Adding Durations

To add two durations together, you can simply use the + operator. The result will be a new duration that represents the sum of the two durations. Here’s an example:

std::chrono::seconds duration1(5);
std::chrono::seconds duration2(3);

std::chrono::seconds sum = duration1 + duration2;

std::cout << "Sum: " << sum.count() << " seconds" << std::endl;

In this example, we create two std::chrono::seconds durations, duration1 and duration2, with values of 5 and 3 seconds respectively. We then use the + operator to add them together, storing the result in the sum variable. Finally, we print the value of sum using the count() member function, which returns the value of the duration in the specified period (in this case, seconds).

The output of this code will be:

Sum: 8 seconds

Subtracting Durations

Subtracting durations is similar to adding durations. You can use the - operator to subtract one duration from another, producing a new duration that represents the difference between them. Here’s an example:

std::chrono::minutes duration1(10);
std::chrono::minutes duration2(4);

std::chrono::minutes difference = duration1 - duration2;

std::cout << "Difference: " << difference.count() << " minutes" << std::endl;

In this example, we create two std::chrono::minutes durations, duration1 and duration2, with values of 10 and 4 minutes respectively. We then use the - operator to subtract duration2 from duration1, storing the result in the difference variable. Finally, we print the value of difference using the count() member function.

The output of this code will be:

Difference: 6 minutes

Conclusion

The std::chrono library in C++ provides a convenient and accurate way to perform arithmetic operations on time durations. By using the + and - operators, you can easily add and subtract durations, allowing you to work with time intervals in your applications.