From 3f054364ad8636a72cf8c9ef15e0b0457865bf44 Mon Sep 17 00:00:00 2001 From: Anthony Calandra Date: Sat, 12 Oct 2024 22:49:56 -0400 Subject: [PATCH] Add rounding functions for chrono durations and timepoints. --- CPP17.md | 12 ++++++++++++ README.md | 12 ++++++++++++ 2 files changed, 24 insertions(+) diff --git a/CPP17.md b/CPP17.md index 0e0383a..56b966f 100644 --- a/CPP17.md +++ b/CPP17.md @@ -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(d); // == 6s +std::chrono::ceil(d); // == 6s +std::chrono::floor(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. diff --git a/README.md b/README.md index da32511..f9f4149 100644 --- a/README.md +++ b/README.md @@ -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(d); // == 6s +std::chrono::ceil(d); // == 6s +std::chrono::floor(d); // == 5s +``` + ## C++14 Language Features ### Binary literals