Lely core libraries  2.2.5
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 __cplusplus <= 201703L
34 
35 template <class Dest, class Source, class Duration>
36 // clang-format off
37 inline typename ::std::enable_if<
38  ::std::is_same<Dest, Source>::value,
39  ::std::chrono::time_point<Dest, Duration>>::type
40 clock_cast(const ::std::chrono::time_point<Source, Duration>& t) {
41  // clang-format on
42  return t;
43 }
44 
49 template <class Dest, class Source, class Duration>
50 // clang-format off
51 inline typename ::std::enable_if<
52  !::std::is_same<Dest, Source>::value,
53  ::std::chrono::time_point<Dest, Duration>>::type
54 clock_cast(const ::std::chrono::time_point<Source, Duration>& t) {
55  // clang-format on
56  using time_point = ::std::chrono::time_point<Dest, Duration>;
57  auto d1 = Dest::now().time_since_epoch();
58  auto s = Source::now().time_since_epoch();
59  auto d2 = Dest::now().time_since_epoch();
60  return time_point{t.time_since_epoch() + ((d1 - s) + (d2 - s)) / 2};
61 }
62 
63 #endif // __cplusplus <= 201703L
64 
65 } // namespace compat
66 } // namespace lely
67 
68 #endif // !LELY_LIBC_CHRONO_HPP_
This header file is part of the Lely libraries; it contains the compiler feature definitions.