Skip to content

Commit 3225708

Browse files
committed
Fix for any std::chrono clock
1 parent 339b2e5 commit 3225708

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

src/xtd.core/include/xtd/date_time.hpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,19 +211,21 @@ namespace xtd {
211211

212212
/// @brief Initializes a new instance of the xtd::date_time structure to a specified time point.
213213
/// @param time_point A [std::chrono::time_point](https://en.cppreference.com/w/cpp/chrono/time_point) value.
214-
/// @remarks This constructor is used to convert [std::chrono::system_clock::time_point](https://en.cppreference.com/w/cpp/chrono/system_clock) to xtd::date_time.
214+
/// @remarks This constructor can be used for example to convert [std::chrono::system_clock::time_point](https://en.cppreference.com/w/cpp/chrono/system_clock) to xtd::date_time.
215215
template<class clock_t, class duration_t = clock_t::duration> // Can't be explicit by design.
216216
date_time(const std::chrono::time_point<clock_t, duration_t>& time_point) {
217-
*this = from_time_t(clock_t::to_time_t(time_point), date_time_kind::local);
217+
using namespace std::chrono;
218+
*this = from_time_t(system_clock::to_time_t(system_clock::now() + duration_cast<system_clock::duration>(time_point - clock_t::now())), date_time_kind::local);
218219
}
219220

220221
/// @brief Initializes a new instance of the xtd::date_time structure to a specified time point, and Coordinated Universal Time (UTC) or local time.
221222
/// @param time_point A [std::chrono::time_point](https://en.cppreference.com/w/cpp/chrono/time_point) value.
222223
/// @param kind One of the enumeration values that indicates whether year, month, day, hour, minute and second specify a local time, Coordinated Universal Time (UTC), or neither.
223-
/// @remarks This constructor is used to convert [std::chrono::system_clock::time_point](https://en.cppreference.com/w/cpp/chrono/system_clock) to xtd::date_time.
224+
/// @remarks This constructor can be used for example to convert [std::chrono::system_clock::time_point](https://en.cppreference.com/w/cpp/chrono/system_clock) to xtd::date_time.
224225
template<class clock_t, class duration_t = clock_t::duration>
225226
date_time(const std::chrono::time_point<clock_t, duration_t>& time_point, date_time_kind kind) {
226-
*this = from_time_t(clock_t::to_time_t(time_point), kind);
227+
using namespace std::chrono;
228+
*this = from_time_t(system_clock::to_time_t(system_clock::now() + duration_cast<system_clock::duration>(time_point - clock_t::now())), kind);
227229
}
228230
/// @}
229231

0 commit comments

Comments
 (0)