Skip to content

Commit 3f05436

Browse files
Add rounding functions for chrono durations and timepoints.
1 parent 452a63b commit 3f05436

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

CPP17.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ C++17 includes the following new library features:
3939
- [gcd and lcm](#gcd-and-lcm)
4040
- [std::not_fn](#stdnot_fn)
4141
- [string conversion to/from numbers](#string-conversion-tofrom-numbers)
42+
- [rounding functions for chrono durations and timepoints](#rounding-functions-for-chrono-durations-and-timepoints)
4243

4344
## C++17 Language Features
4445

@@ -686,6 +687,17 @@ if (ec == std::errc{}) { std::cout << n << std::endl; } // 123
686687
else { /* handle failure */ }
687688
```
688689
690+
### Rounding functions for chrono durations and timepoints
691+
Provides abs, round, ceil, and floor helper functions for `std::chrono::duration` and `std::chrono::time_point`.
692+
```c++
693+
using seconds = std::chrono::seconds;
694+
std::chrono::milliseconds d{ 5500 };
695+
std::chrono::abs(d); // == 5s
696+
std::chrono::round<seconds>(d); // == 6s
697+
std::chrono::ceil<seconds>(d); // == 6s
698+
std::chrono::floor<seconds>(d); // == 5s
699+
```
700+
689701
## Acknowledgements
690702
* [cppreference](http://en.cppreference.com/w/cpp) - especially useful for finding examples and documentation of new library features.
691703
* [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.

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ C++17 includes the following new library features:
7070
- [gcd and lcm](#gcd-and-lcm)
7171
- [std::not_fn](#stdnot_fn)
7272
- [string conversion to/from numbers](#string-conversion-tofrom-numbers)
73+
- [rounding functions for chrono durations and timepoints](#rounding-functions-for-chrono-durations-and-timepoints)
7374

7475
C++14 includes the following new language features:
7576
- [binary literals](#binary-literals)
@@ -1360,6 +1361,17 @@ if (ec == std::errc{}) { std::cout << n << std::endl; } // 123
13601361
else { /* handle failure */ }
13611362
```
13621363
1364+
### Rounding functions for chrono durations and timepoints
1365+
Provides abs, round, ceil, and floor helper functions for `std::chrono::duration` and `std::chrono::time_point`.
1366+
```c++
1367+
using seconds = std::chrono::seconds;
1368+
std::chrono::milliseconds d{ 5500 };
1369+
std::chrono::abs(d); // == 5s
1370+
std::chrono::round<seconds>(d); // == 6s
1371+
std::chrono::ceil<seconds>(d); // == 6s
1372+
std::chrono::floor<seconds>(d); // == 5s
1373+
```
1374+
13631375
## C++14 Language Features
13641376

13651377
### Binary literals

0 commit comments

Comments
 (0)