Add rounding functions for chrono durations and timepoints.

This commit is contained in:
Anthony Calandra
2024-10-12 22:49:56 -04:00
parent 452a63bd90
commit 3f054364ad
2 changed files with 24 additions and 0 deletions

View File

@@ -39,6 +39,7 @@ C++17 includes the following new library features:
- [gcd and lcm](#gcd-and-lcm)
- [std::not_fn](#stdnot_fn)
- [string conversion to/from numbers](#string-conversion-tofrom-numbers)
- [rounding functions for chrono durations and timepoints](#rounding-functions-for-chrono-durations-and-timepoints)
## C++17 Language Features
@@ -686,6 +687,17 @@ if (ec == std::errc{}) { std::cout << n << std::endl; } // 123
else { /* handle failure */ }
```
### Rounding functions for chrono durations and timepoints
Provides abs, round, ceil, and floor helper functions for `std::chrono::duration` and `std::chrono::time_point`.
```c++
using seconds = std::chrono::seconds;
std::chrono::milliseconds d{ 5500 };
std::chrono::abs(d); // == 5s
std::chrono::round<seconds>(d); // == 6s
std::chrono::ceil<seconds>(d); // == 6s
std::chrono::floor<seconds>(d); // == 5s
```
## Acknowledgements
* [cppreference](http://en.cppreference.com/w/cpp) - especially useful for finding examples and documentation of new library features.
* [C++ Rvalue References Explained](http://web.archive.org/web/20240324121501/http://thbecker.net/articles/rvalue_references/section_01.html) - a great introduction I used to understand rvalue references, perfect forwarding, and move semantics.

View File

@@ -70,6 +70,7 @@ C++17 includes the following new library features:
- [gcd and lcm](#gcd-and-lcm)
- [std::not_fn](#stdnot_fn)
- [string conversion to/from numbers](#string-conversion-tofrom-numbers)
- [rounding functions for chrono durations and timepoints](#rounding-functions-for-chrono-durations-and-timepoints)
C++14 includes the following new language features:
- [binary literals](#binary-literals)
@@ -1360,6 +1361,17 @@ if (ec == std::errc{}) { std::cout << n << std::endl; } // 123
else { /* handle failure */ }
```
### Rounding functions for chrono durations and timepoints
Provides abs, round, ceil, and floor helper functions for `std::chrono::duration` and `std::chrono::time_point`.
```c++
using seconds = std::chrono::seconds;
std::chrono::milliseconds d{ 5500 };
std::chrono::abs(d); // == 5s
std::chrono::round<seconds>(d); // == 6s
std::chrono::ceil<seconds>(d); // == 6s
std::chrono::floor<seconds>(d); // == 5s
```
## C++14 Language Features
### Binary literals