|
1 | 1 | --- |
2 | 2 | description: "Learn more about: <chrono> functions" |
3 | 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 | | -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"] |
| 4 | +ms.date: 10/15/2021 |
| 5 | +f1_keywords: ["chrono/std::duration_cast", "chrono/std::time_point_cast", "chrono/std::chrono::clock_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 | +helpviewer_keywords: ["std::duration_cast function", "std::time_point_cast function", "std::chrono::clock_cast", "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 |
@@ -120,6 +120,83 @@ Returns the smallest time point representable using `ToDuration` that's greater |
120 | 120 |
|
121 | 121 | `ceil` doesn't participate in overload resolution unless the `ToDuration` type is an instance of a [`duration`](../standard-library/duration-class.md). |
122 | 122 |
|
| 123 | +## <a name="std-chrono-clock-cast"></a> `clock_cast` |
| 124 | + |
| 125 | +Converts a [`time_point`](time-point-class.md) for one clock to an equivalent `time_point` for another clock. |
| 126 | + |
| 127 | +### Syntax |
| 128 | + |
| 129 | +```cpp |
| 130 | +template <class DestClock, class SourceClock, class Duration> |
| 131 | +auto clock_cast(const time_point<SourceClock, Duration>& t); // C++ 20 |
| 132 | +``` |
| 133 | +
|
| 134 | +### Parameters |
| 135 | +
|
| 136 | +*`DestClock`*\ |
| 137 | +The clock type to convert the `time_point` to. |
| 138 | +
|
| 139 | +*`Duration`*\ |
| 140 | +The [`duration`](duration-class.md) of the `SourceClock`, or one that you specify. |
| 141 | +
|
| 142 | +*`SourceClock`*\ |
| 143 | +The clock type that the `time_point` to convert is based on. |
| 144 | +
|
| 145 | +*`t`*\ |
| 146 | +The `time_point` to convert. |
| 147 | +
|
| 148 | +### Return value |
| 149 | +
|
| 150 | +A [`time_point`](time-point-class.md) equivalent to **`t`**, but specific to `DestClock`. |
| 151 | +
|
| 152 | +### Remarks |
| 153 | +
|
| 154 | +The parameters `SourceClock` and `Duration` can be inferred via class template argument deduction when not explicitly passed. For example, given `clock_cast<utc_clock>(file_clock::now())`, `SourceClock` is deduced to be `file_clock`, and `Duration` is deduced to be `file_clock::duration`. |
| 155 | +
|
| 156 | +From the following list of well-formed clock conversions, the one that requires the fewest conversion steps to get from the `SourceClock` to the `DestClock` is selected. |
| 157 | +
|
| 158 | +```cpp |
| 159 | +clock_time_conversion<DestClock, SourceClock>{}(t) |
| 160 | +
|
| 161 | +clock_time_conversion<DestClock, system_clock>{}( |
| 162 | + clock_time_conversion<system_clock, SourceClock>{}(t)) |
| 163 | +
|
| 164 | +clock_time_conversion<DestClock, utc_clock>{}( |
| 165 | + clock_time_conversion<utc_clock, SourceClock>{}(t)) |
| 166 | +
|
| 167 | +clock_time_conversion<DestClock, utc_clock>{}( |
| 168 | + clock_time_conversion<utc_clock, system_clock>{}( |
| 169 | + clock_time_conversion<system_clock, SourceClock>{}(t))) |
| 170 | +
|
| 171 | +clock_time_conversion<DestClock, system_clock>{}( |
| 172 | + clock_time_conversion<system_clock, utc_clock>{}( |
| 173 | + clock_time_conversion<utc_clock, SourceClock>{}(t))) |
| 174 | +``` |
| 175 | + |
| 176 | +For more information about what `clock_time_conversion` does, see [`clock_time_conversion` struct](clock-time-conversion-struct.md). |
| 177 | + |
| 178 | +### Example `clock_cast` |
| 179 | + |
| 180 | +```cpp |
| 181 | +// compile using: /std:c++latest |
| 182 | +#include <iostream> |
| 183 | +#include <chrono> |
| 184 | + |
| 185 | +using namespace std::chrono; |
| 186 | + |
| 187 | +int main() |
| 188 | +{ |
| 189 | + utc_clock::time_point t = clock_cast<utc_clock>(file_clock::now()); |
| 190 | + std::cout << t; |
| 191 | + |
| 192 | + return 0; |
| 193 | +} |
| 194 | +``` |
| 195 | + |
| 196 | +```output |
| 197 | +2021-10-11 22:58:17.8540720 |
| 198 | +``` |
| 199 | + |
123 | 200 | ## <a name="std-chrono-current-zone"></a> `current_zone` |
124 | 201 |
|
125 | 202 | Gets the current time zone object. |
|
0 commit comments