|
1 | 1 | --- |
2 | | -description: "Learn more about: <chrono> functions" |
3 | | -title: "<chrono> functions" |
4 | | -ms.date: "08/13/2021" |
5 | | -f1_keywords: ["chrono/std::duration_cast", "chrono/std::time_point_cast", "chrono/std::chrono::duration_cast", "chrono/std::chrono::time_point_cast", "chrono/std::chrono::from_stream", "chrono/std::chrono::floor", "chrono/std::chrono::ceil", "chrono/std::chrono::round", "chrono/std::chrono::is_am", "chrono/std::chrono::is_pm", "chrono/std::chrono::make12", "chrono/std::chrono::make24", "chrono/std::chrono::get_leap_second_info", "chrono/std::chrono::get_tzdb", "chrono/std::chrono::get_tzdb_list", "chrono/std::chrono::locate_zone", "chrono/std::chrono::current_zone", "chrono/std::chrono::reload_tzdb", "chrono/std::chrono::remote_version"] |
| 2 | +description: "Learn more about: <chrono> functions" |
| 3 | +title: "<chrono> functions" |
| 4 | +ms.date: 10/06/2021 |
| 5 | +f1_keywords: ["chrono/std::duration_cast", "chrono/std::time_point_cast", "chrono/std::chrono::duration_cast", "chrono/std::chrono::time_point_cast", "chrono/std::chrono::from_stream", "chrono/std::chrono::abs", "chrono/std::chrono::floor", "chrono/std::chrono::ceil", "chrono/std::chrono::round", "chrono/std::chrono::is_am", "chrono/std::chrono::is_pm", "chrono/std::chrono::make12", "chrono/std::chrono::make24", "chrono/std::chrono::get_leap_second_info", "chrono/std::chrono::get_tzdb", "chrono/std::chrono::get_tzdb_list", "chrono/std::chrono::locate_zone", "chrono/std::chrono::current_zone", "chrono/std::chrono::reload_tzdb", "chrono/std::chrono::remote_version"] |
6 | 6 | helpviewer_keywords: ["std::duration_cast function", "std::time_point_cast function", "std::chrono::duration_cast function", "std::chrono::time_point_cast function", "std::chrono::from_stream function", "std::chrono::floor function", "std::chrono::ceil function", "std::chrono::round function", "std::chrono::is_am function", "std::chrono::is_pm function", "std::chrono::make12 function", "std::chrono::make24 function", "std::chrono::get_leap_second_info function", "std::chrono::get_tzdb function", "std::chrono::get_tzdb_list function", "std::chrono::locate_zone function", "std::chrono::current_zone function", "std::chrono::reload_tzdb function", "std::chrono::remote_version function"] |
7 | 7 | --- |
8 | 8 |
|
9 | 9 | # `<chrono>` functions |
10 | 10 |
|
| 11 | +## <a name="std-chrono-abs-duration"></a> `abs(duration)` |
| 12 | + |
| 13 | +Returns `d` if `d >= d.zero()`; otherwise returns `-d`. |
| 14 | + |
| 15 | +### Syntax |
| 16 | + |
| 17 | +```cpp |
| 18 | +template <class Rep, class Period> |
| 19 | +constexpr duration<Rep, Period> abs(duration<Rep, Period> d ); // C++17 |
| 20 | +``` |
| 21 | +
|
| 22 | +### Parameters |
| 23 | +
|
| 24 | +*`Rep`*\ |
| 25 | +The type of the internal representation of the source `duration` *`d`*. |
| 26 | +
|
| 27 | +*`Period`*\ |
| 28 | +A [`std::ratio`](./ratio.md) type representing the ratio of one second to the source `Rep` type (that is, seconds per `Rep`). |
| 29 | +
|
| 30 | +*`d`*\ |
| 31 | +The source `duration` object. |
| 32 | +
|
| 33 | +### Return value |
| 34 | +
|
| 35 | +The absolute value of `d`. |
| 36 | +
|
| 37 | +### Example: `abs(duration)` |
| 38 | +
|
| 39 | +```cpp |
| 40 | +// compile using: /std:c++latest |
| 41 | +#include <chrono> |
| 42 | +#include <iostream> |
| 43 | +
|
| 44 | +int main() |
| 45 | +{ |
| 46 | + std::cout << abs(-24h); |
| 47 | + return 0; |
| 48 | +} |
| 49 | +``` |
| 50 | + |
| 51 | +```output |
| 52 | +24h |
| 53 | +``` |
| 54 | + |
11 | 55 | ## <a name="std-chrono-ceil-duration"></a> `ceil(duration)` |
12 | 56 |
|
13 | 57 | Returns the smallest representable `duration` in the target type that's greater than or equal to the specified `duration`. |
@@ -92,7 +136,7 @@ Returns a pointer to a [`time_zone`](./time-zone-class.md) as if by a call to `g |
92 | 136 |
|
93 | 137 | ## <a name="std-chrono-duration-cast"></a> `duration_cast` |
94 | 138 |
|
95 | | -Casts a [`duration`](../standard-library/duration-class.md) object to a specified target `duration` type. |
| 139 | +Casts a [`duration`](../standard-library/duration-class.md) to the specified target `duration` type. |
96 | 140 |
|
97 | 141 | ### Syntax |
98 | 142 |
|
@@ -126,6 +170,30 @@ You don't need to use `duration_cast` to convert between `duration` types when t |
126 | 170 |
|
127 | 171 | `duration_cast` doesn't participate in overload resolution unless `ToDuration` is an instance of [`duration`](../standard-library/duration-class.md). It does all conversions by using **`static_cast`** instead of implicit conversions. Multiplications and divisions are avoided if possible. For example, when the compiler knows that the common ratio of the target and source periods has a numerator or denominator of 1. Computations are done in the widest type available, then converted as if by **`static_cast`** to the result type when finished. |
128 | 172 |
|
| 173 | +### Example `duration_cast` |
| 174 | +
|
| 175 | +```cpp |
| 176 | +// compile using: /std:c++latest |
| 177 | +#include <iostream> |
| 178 | +#include <chrono> |
| 179 | +
|
| 180 | +using namespace std::chrono; |
| 181 | +
|
| 182 | +int main() |
| 183 | +{ |
| 184 | + seconds s(1); |
| 185 | + std::cout << duration_cast<microseconds>(s) << '\n'; |
| 186 | + std::cout << duration_cast<nanoseconds>(s) << '\n'; |
| 187 | +
|
| 188 | + return 0; |
| 189 | +} |
| 190 | +``` |
| 191 | + |
| 192 | +```output |
| 193 | +1000000us |
| 194 | +1000000000ns |
| 195 | +``` |
| 196 | + |
129 | 197 | ## <a name="std-chrono-floor-duration"></a> `floor(duration)` |
130 | 198 |
|
131 | 199 | Returns the greatest representable `duration` in the target type that's less than or equal to the specified `duration`. |
|
0 commit comments