Lely core libraries  2.3.4
chrono.hpp
Go to the documentation of this file.
1 
22 #ifndef LELY_LIBC_CHRONO_HPP_
23 #define LELY_LIBC_CHRONO_HPP_
24 
25 #include <lely/features.h>
26 
27 #include <chrono>
28 #include <type_traits>
29 
30 namespace lely {
31 namespace compat {
32 
33 #if __cpp_lib_chrono >= 201907L
34 
35 using ::std::chrono::clock_cast;
36 
37 #else // __cpp_lib_chrono < 201907L
38 
39 template <class Dest, class Source, class Duration>
40 // clang-format off
41 inline typename ::std::enable_if<
42  ::std::is_same<Dest, Source>::value,
43  ::std::chrono::time_point<Dest, Duration>>::type
44 clock_cast(const ::std::chrono::time_point<Source, Duration>& t) {
45  // clang-format on
46  return t;
47 }
48 
53 template <class Dest, class Source, class Duration>
54 // clang-format off
55 inline typename ::std::enable_if<
56  !::std::is_same<Dest, Source>::value,
57  ::std::chrono::time_point<Dest, Duration>>::type
58 clock_cast(const ::std::chrono::time_point<Source, Duration>& t) {
59  // clang-format on
60  using time_point = ::std::chrono::time_point<Dest, Duration>;
61  auto d1 = Dest::now().time_since_epoch();
62  auto s = Source::now().time_since_epoch();
63  auto d2 = Dest::now().time_since_epoch();
64  return time_point{t.time_since_epoch() + ((d1 - s) + (d2 - s)) / 2};
65 }
66 
67 #endif // __cpp_lib_chrono < 201907L
68 
69 } // namespace compat
70 } // namespace lely
71 
72 #endif // !LELY_LIBC_CHRONO_HPP_
This header file is part of the Lely libraries; it contains the compiler feature definitions.