|
1 |
| -// Copyright (c) Microsoft Corporation. |
2 |
| -// Licensed under the MIT License. |
3 |
| - |
4 |
| -#pragma once |
5 |
| - |
6 |
| -#include <m/utility/compiler.h> |
7 |
| - |
8 |
| -#include <bit> |
9 |
| -#include <chrono> |
10 |
| - |
11 |
| -#include <m/cast/to.h> |
12 |
| -#include <m/cast/try_cast.h> |
13 |
| -#include <m/exception/exception.h> |
14 |
| -#include <m/tracing/tracing.h> |
15 |
| -#include <m/windows_wrappers/win32_dword_ms.h> |
16 |
| - |
17 |
| -#undef NOMINMAX |
18 |
| -#define NOMINMAX |
19 |
| - |
20 |
| -#include <Windows.h> |
21 |
| - |
22 |
| -namespace m |
23 |
| -{ |
24 |
| - template <typename ResultType, typename Clock, typename Duration = Clock::duration> |
25 |
| - struct time_point_cast_helper; |
26 |
| - |
27 |
| - template <typename ResultType, typename Clock, typename Duration> |
28 |
| - auto |
29 |
| - utc_time_point_cast(std::chrono::time_point<Clock, Duration> tp) |
30 |
| - { |
31 |
| - return time_point_cast_helper<ResultType, Clock, Duration>::cast_utc_time_point(tp); |
32 |
| - } |
33 |
| - |
34 |
| - template <typename Rep, typename Period> |
35 |
| - win32_dword_ms |
36 |
| - win32_dword_ms_cast(std::chrono::duration<Rep, Period> const& d) |
37 |
| - { |
38 |
| - auto const as_ms = std::chrono::duration_cast<std::chrono::milliseconds>(d); |
39 |
| - |
40 |
| - using value_type = typename win32_dword_ms::value_type; |
41 |
| - |
42 |
| - if (as_ms.count() < (std::numeric_limits<value_type>::min)() || |
43 |
| - as_ms.count() > (std::numeric_limits<value_type>::max)()) |
44 |
| - { |
45 |
| - m::trace_error("Attempted to convert value {} to DWORD milliseconds; out of range", |
46 |
| - as_ms.count()); |
47 |
| - throw m::invalid_parameter("d"); |
48 |
| - } |
49 |
| - |
50 |
| - return win32_dword_ms(m::to<DWORD>(as_ms.count())); |
51 |
| - } |
52 |
| - |
53 |
| - template <typename Rep, typename Period> |
54 |
| - requires(std::integral<Rep>) |
55 |
| - struct try_cast_helper<std::chrono::duration<Rep, Period>, win32_dword_ms, void> |
56 |
| - { |
57 |
| - static constexpr decltype(auto) |
58 |
| - do_cast(std::chrono::duration<Rep, Period> const& d) |
59 |
| - { |
60 |
| - return win32_dword_ms_cast(d); |
61 |
| - } |
62 |
| - }; |
63 |
| - |
64 |
| - template <typename Clock, typename Duration> |
65 |
| - struct time_point_cast_helper<SYSTEMTIME, Clock, Duration> |
66 |
| - { |
67 |
| - |
68 |
| - template <typename DestinationClock> |
69 |
| - static SYSTEMTIME |
70 |
| - cast_time_point(std::chrono::time_point<Clock, Duration> tp) |
71 |
| - { |
72 |
| - auto const as_destination_clock = std::chrono::clock_cast<DestinationClock>(tp); |
73 |
| - auto const sys_time = std::chrono::utc_clock::to_sys(as_destination_clock); |
74 |
| - auto const time_t_time = std::chrono::system_clock::to_time_t(sys_time); |
75 |
| - std::tm tm_time{}; |
76 |
| - |
77 |
| - gmtime_s(&tm_time, &time_t_time); |
78 |
| - |
79 |
| - SYSTEMTIME st{}; |
80 |
| - |
81 |
| - st.wDay = m::to<WORD>(tm_time.tm_mday); |
82 |
| - st.wDayOfWeek = m::to<WORD>(tm_time.tm_wday); |
83 |
| - st.wHour = m::to<WORD>(tm_time.tm_hour); |
84 |
| - st.wMilliseconds = 0; |
85 |
| - st.wMinute = m::to<WORD>(tm_time.tm_min); |
86 |
| - st.wMonth = m::to<WORD>(tm_time.tm_mon) + 1; |
87 |
| - st.wSecond = m::to<WORD>(tm_time.tm_sec); |
88 |
| - st.wYear = m::to<WORD>(tm_time.tm_year) + 1900; |
89 |
| - |
90 |
| - return st; |
91 |
| - } |
92 |
| - |
93 |
| - static SYSTEMTIME |
94 |
| - cast_utc_time_point(std::chrono::time_point<Clock, Duration> tp) |
95 |
| - { |
96 |
| - return cast_time_point<std::chrono::utc_clock>(tp); |
97 |
| - } |
98 |
| - }; |
99 |
| - |
100 |
| - template <typename Clock, typename Duration> |
101 |
| - struct try_cast_helper<std::chrono::time_point<Clock, Duration>, SYSTEMTIME, void> |
102 |
| - { |
103 |
| - static constexpr decltype(auto) |
104 |
| - do_cast(std::chrono::time_point<Clock, Duration> const& tp) |
105 |
| - { |
106 |
| - return time_point_cast_helper<SYSTEMTIME, Clock, Duration>::cast_utc_time_point(tp); |
107 |
| - } |
108 |
| - }; |
109 |
| - |
110 |
| - template <typename Rep, typename Period> |
111 |
| - struct try_cast_helper<std::chrono::duration<Rep, Period>, FILETIME, void> |
112 |
| - { |
113 |
| - template <typename Rep, typename Period> |
114 |
| - static FILETIME |
115 |
| - DurationToFILETIME(std::chrono::duration<Rep, Period> const& duration) |
116 |
| - { |
117 |
| - if (duration.count() < 0) |
118 |
| - { |
119 |
| - trace_error("Programming error: invalid duration passed in; count < 0: {}", |
120 |
| - duration.count()); |
121 |
| - throw m::invalid_parameter("duration"); |
122 |
| - } |
123 |
| - |
124 |
| - // |
125 |
| - // FILETIME is 100ns units, so for the ratio |
126 |
| - // have the denominator be 1 billion divided by |
127 |
| - // 100. |
128 |
| - // |
129 |
| - using FiletimeRatio = std::ratio<1, 1'000'000'000 / 100>; |
130 |
| - using FiletimeDuration = std::chrono::duration<int64_t, FiletimeRatio>; |
131 |
| - |
132 |
| - auto const as_filetime_duration = |
133 |
| - std::chrono::duration_cast<FiletimeDuration>(duration); |
134 |
| - |
135 |
| - static_assert(std::numeric_limits<int64_t>::digits == |
136 |
| - std::numeric_limits<typename FiletimeDuration::rep>::digits); |
137 |
| - |
138 |
| - int64_t count = as_filetime_duration.count(); |
139 |
| - |
140 |
| - // Durations in FILETIME are negative. |
141 |
| - // |
142 |
| - count = -count; |
143 |
| - |
144 |
| - return std::bit_cast<FILETIME>(count); |
145 |
| - } |
146 |
| - |
147 |
| - static constexpr decltype(auto) |
148 |
| - do_cast(std::chrono::duration<Rep, Period> const& d) |
149 |
| - { |
150 |
| - return DurationToFILETIME(d); |
151 |
| - } |
152 |
| - }; |
153 |
| - |
154 |
| -} // namespace m |
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +#pragma once |
| 5 | + |
| 6 | +#include <m/utility/compiler.h> |
| 7 | + |
| 8 | +#include <bit> |
| 9 | +#include <chrono> |
| 10 | + |
| 11 | +#include <m/cast/to.h> |
| 12 | +#include <m/cast/try_cast.h> |
| 13 | +#include <m/exception/exception.h> |
| 14 | +#include <m/tracing/tracing.h> |
| 15 | +#include <m/windows_wrappers/win32_dword_ms.h> |
| 16 | + |
| 17 | +#undef NOMINMAX |
| 18 | +#define NOMINMAX |
| 19 | + |
| 20 | +#include <Windows.h> |
| 21 | + |
| 22 | +namespace m |
| 23 | +{ |
| 24 | + template <typename ResultType, typename Clock, typename Duration = Clock::duration> |
| 25 | + struct time_point_cast_helper; |
| 26 | + |
| 27 | + template <typename ResultType, typename Clock, typename Duration> |
| 28 | + auto |
| 29 | + utc_time_point_cast(std::chrono::time_point<Clock, Duration> tp) |
| 30 | + { |
| 31 | + return time_point_cast_helper<ResultType, Clock, Duration>::cast_utc_time_point(tp); |
| 32 | + } |
| 33 | + |
| 34 | + template <typename Rep, typename Period> |
| 35 | + win32_dword_ms |
| 36 | + win32_dword_ms_cast(std::chrono::duration<Rep, Period> const& d) |
| 37 | + { |
| 38 | + auto const as_ms = std::chrono::duration_cast<std::chrono::milliseconds>(d); |
| 39 | + |
| 40 | + using value_type = typename win32_dword_ms::value_type; |
| 41 | + |
| 42 | + if (as_ms.count() < (std::numeric_limits<value_type>::min)() || |
| 43 | + as_ms.count() > (std::numeric_limits<value_type>::max)()) |
| 44 | + { |
| 45 | + m::trace_error("Attempted to convert value {} to DWORD milliseconds; out of range", |
| 46 | + as_ms.count()); |
| 47 | + throw m::invalid_parameter("d"); |
| 48 | + } |
| 49 | + |
| 50 | + return win32_dword_ms(m::to<DWORD>(as_ms.count())); |
| 51 | + } |
| 52 | + |
| 53 | + template <typename Rep, typename Period> |
| 54 | + requires(std::integral<Rep>) |
| 55 | + struct try_cast_helper<std::chrono::duration<Rep, Period>, win32_dword_ms, void> |
| 56 | + { |
| 57 | + static constexpr decltype(auto) |
| 58 | + do_cast(std::chrono::duration<Rep, Period> const& d) |
| 59 | + { |
| 60 | + return win32_dword_ms_cast(d); |
| 61 | + } |
| 62 | + }; |
| 63 | + |
| 64 | + template <typename Clock, typename Duration> |
| 65 | + struct time_point_cast_helper<SYSTEMTIME, Clock, Duration> |
| 66 | + { |
| 67 | + |
| 68 | + template <typename DestinationClock> |
| 69 | + static SYSTEMTIME |
| 70 | + cast_time_point(std::chrono::time_point<Clock, Duration> tp) |
| 71 | + { |
| 72 | + auto const as_destination_clock = std::chrono::clock_cast<DestinationClock>(tp); |
| 73 | + auto const sys_time = std::chrono::utc_clock::to_sys(as_destination_clock); |
| 74 | + auto const time_t_time = std::chrono::system_clock::to_time_t(sys_time); |
| 75 | + std::tm tm_time{}; |
| 76 | + |
| 77 | + gmtime_s(&tm_time, &time_t_time); |
| 78 | + |
| 79 | + SYSTEMTIME st{}; |
| 80 | + |
| 81 | + st.wDay = m::to<WORD>(tm_time.tm_mday); |
| 82 | + st.wDayOfWeek = m::to<WORD>(tm_time.tm_wday); |
| 83 | + st.wHour = m::to<WORD>(tm_time.tm_hour); |
| 84 | + st.wMilliseconds = 0; |
| 85 | + st.wMinute = m::to<WORD>(tm_time.tm_min); |
| 86 | + st.wMonth = m::to<WORD>(tm_time.tm_mon) + 1; |
| 87 | + st.wSecond = m::to<WORD>(tm_time.tm_sec); |
| 88 | + st.wYear = m::to<WORD>(tm_time.tm_year) + 1900; |
| 89 | + |
| 90 | + return st; |
| 91 | + } |
| 92 | + |
| 93 | + static SYSTEMTIME |
| 94 | + cast_utc_time_point(std::chrono::time_point<Clock, Duration> tp) |
| 95 | + { |
| 96 | + return cast_time_point<std::chrono::utc_clock>(tp); |
| 97 | + } |
| 98 | + }; |
| 99 | + |
| 100 | + template <typename Clock, typename Duration> |
| 101 | + struct try_cast_helper<std::chrono::time_point<Clock, Duration>, SYSTEMTIME, void> |
| 102 | + { |
| 103 | + static constexpr decltype(auto) |
| 104 | + do_cast(std::chrono::time_point<Clock, Duration> const& tp) |
| 105 | + { |
| 106 | + return time_point_cast_helper<SYSTEMTIME, Clock, Duration>::cast_utc_time_point(tp); |
| 107 | + } |
| 108 | + }; |
| 109 | + |
| 110 | + template <typename Rep, typename Period> |
| 111 | + struct try_cast_helper<std::chrono::duration<Rep, Period>, FILETIME, void> |
| 112 | + { |
| 113 | + template <typename Rep, typename Period> |
| 114 | + static FILETIME |
| 115 | + DurationToFILETIME(std::chrono::duration<Rep, Period> const& duration) |
| 116 | + { |
| 117 | + if (duration.count() < 0) |
| 118 | + { |
| 119 | + trace_error("Programming error: invalid duration passed in; count < 0: {}", |
| 120 | + duration.count()); |
| 121 | + throw m::invalid_parameter("duration"); |
| 122 | + } |
| 123 | + |
| 124 | + // |
| 125 | + // FILETIME is 100ns units, so for the ratio |
| 126 | + // have the denominator be 1 billion divided by |
| 127 | + // 100. |
| 128 | + // |
| 129 | + using FiletimeRatio = std::ratio<1, 1'000'000'000 / 100>; |
| 130 | + using FiletimeDuration = std::chrono::duration<int64_t, FiletimeRatio>; |
| 131 | + |
| 132 | + auto const as_filetime_duration = |
| 133 | + std::chrono::duration_cast<FiletimeDuration>(duration); |
| 134 | + |
| 135 | + static_assert(std::numeric_limits<int64_t>::digits == |
| 136 | + std::numeric_limits<typename FiletimeDuration::rep>::digits); |
| 137 | + |
| 138 | + int64_t count = as_filetime_duration.count(); |
| 139 | + |
| 140 | + // Durations in FILETIME are negative. |
| 141 | + // |
| 142 | + count = -count; |
| 143 | + |
| 144 | + return std::bit_cast<FILETIME>(count); |
| 145 | + } |
| 146 | + |
| 147 | + static constexpr decltype(auto) |
| 148 | + do_cast(std::chrono::duration<Rep, Period> const& d) |
| 149 | + { |
| 150 | + return DurationToFILETIME(d); |
| 151 | + } |
| 152 | + }; |
| 153 | + |
| 154 | +} // namespace m |
0 commit comments