DUDS
Distributed Update of Data from Something
Interstellar.hpp
Go to the documentation of this file.
1 /*
2  * This file is part of the DUDS project. It is subject to the BSD-style
3  * license terms in the LICENSE file found in the top-level directory of this
4  * distribution and at https://github.com/jjackowski/duds/blob/master/LICENSE.
5  * No part of DUDS, including this file, may be copied, modified, propagated,
6  * or distributed except according to the terms contained in the LICENSE file.
7  *
8  * Copyright (C) 2017 Jeff Jackowski
9  */
10 #ifndef INTERSTELLAR_HPP
11 #define INTERSTELLAR_HPP
12 
13 #include <chrono>
14 #include <boost/date_time/posix_time/posix_time.hpp>
15 #include <duds/data/Int128.hpp>
16 
17 namespace duds { namespace time { namespace interstellar {
18 
20 
24 extern const int128_t OneE6;
28 extern const int128_t OneE12;
32 extern const int128_t OneE15;
33 
52 template <class Clock, class Duration>
53 class TimePoint : public std::chrono::time_point<Clock, Duration> {
54 public:
55  constexpr TimePoint() = default;
56  TimePoint(const TimePoint &) = default;
61  template <class OtherRep, class OtherPeriod>
62  constexpr TimePoint(const std::chrono::duration<OtherRep, OtherPeriod> &od) :
63  std::chrono::time_point<Clock, Duration>(
64  std::chrono::duration_cast<Duration, typename Duration::rep, typename Duration::period>(od)
65  ) { }
66  /*
67  * Constructs a TimePoint from another TimePoint.
68  * @param otp The source TimePoint. It may use a different period and a
69  * a different clock type.
70  */
71  /*
72  template <class OtherClock, class OtherDuration>
73  constexpr TimePoint(const TimePoint<OtherClock, OtherDuration> &otp) :
74  std::chrono::time_point<Clock, Duration>(
75  std::chrono::duration_cast<Duration>(otp.time_since_epoch())
76  ) { }
77  */
83  template <class OtherClock, class OtherDuration>
84  constexpr TimePoint(const std::chrono::time_point<OtherClock, OtherDuration> &otp) :
85  std::chrono::time_point<Clock, Duration>(
86  std::chrono::duration_cast<Duration>(otp.time_since_epoch())
87  ) { }
88  TimePoint &operator=(const TimePoint &) = default;
95  template <class OtherClock, class OtherDuration>
97  *this = TimePoint(otp);
98  return *this;
99  }
103  static constexpr TimePoint min() {
104  return TimePoint(Duration(
105  std::numeric_limits<typename Duration::rep>::lowest()
106  ) );
107  }
111  static constexpr TimePoint max() {
112  return TimePoint(Duration(
113  std::numeric_limits<typename Duration::rep>::max()
114  ) );
115  }
116 };
117 
126 typedef std::chrono::duration<int128_t, std::femto> Femtoseconds;
127 
137 struct FemtoClock {
138  typedef Femtoseconds duration;
139  typedef duration::rep rep;
140  typedef duration::period period;
142  static constexpr bool is_steady = false;
143  static time_point now() noexcept {
144  return time_point(std::chrono::duration_cast<duration, rep, period>(
145  std::chrono::system_clock::now().time_since_epoch()));
146  }
147 };
148 
161 
169 typedef std::chrono::duration<std::int64_t, std::milli> Milliseconds;
170 
178 struct MilliClock {
179  typedef Milliseconds duration;
180  typedef duration::rep rep;
181  typedef duration::period period;
183  static constexpr bool is_steady = false;
184  static time_point now() noexcept {
185  return time_point(std::chrono::duration_cast<duration>(
186  std::chrono::system_clock::now().time_since_epoch()));
187  }
188 };
189 
196 
207 typedef std::chrono::duration<std::uint64_t, std::nano> Nanoseconds;
208 
216 struct NanoClock {
217  typedef Nanoseconds duration;
218  typedef duration::rep rep;
219  typedef duration::period period;
221  static constexpr bool is_steady = false;
222  static time_point now() noexcept {
223  return time_point(std::chrono::duration_cast<duration>(
224  std::chrono::system_clock::now().time_since_epoch()));
225  }
226 };
227 
234 
243 typedef std::chrono::duration<std::int64_t> Seconds;
244 
253 struct SecondClock {
254  typedef Seconds duration;
255  typedef duration::rep rep;
256  typedef duration::period period;
258  static constexpr bool is_steady = false;
259  static time_point now() noexcept {
260  return time_point(std::chrono::duration_cast<duration>(
261  std::chrono::system_clock::now().time_since_epoch()));
262  }
263 };
264 
271 
272 #ifdef BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG
273 #warning POSIX nanosecond
274 #else
275 //#warning POSIX microsecond
276 #endif
277 
278 template <class ISTime>
279 boost::posix_time::ptime EarthTimeTAI(const ISTime &t) { // ???
280  return boost::posix_time::ptime((std::time_t)t.seconds());
281 }
282 
283 } } }
284 
285 #endif // #ifndef INTERSTELLAR_HPP
TimePoint< FemtoClock, duration > time_point
Provides Interstellar Time in Nanoseconds.
TimePoint< SecondClock, duration > time_point
constexpr TimePoint(const std::chrono::duration< OtherRep, OtherPeriod > &od)
Constructs a TimePoint from a duration.
Provides Interstellar Time in Femtoseconds.
static constexpr TimePoint max()
Returns the latest time that can be represented.
MilliClock::time_point MilliTime
A point in time in Interstellar Time stored in Milliseconds.
TimePoint & operator=(const TimePoint< OtherClock, OtherDuration > &otp)
Assigns a new time from another TimePoint using different template parameters.
boost::multiprecision::int128_t int128_t
The type used for 128-bit integers.
Definition: Int128.hpp:66
static time_point now() noexcept
std::chrono::duration< std::int64_t > Seconds
Stores a duration in seconds.
SecondClock::time_point SecondTime
A point in time in Interstellar Time stored in Seconds.
STL namespace.
const int128_t OneE15
1e15 in a 128-bit integer.
TimePoint< MilliClock, duration > time_point
static time_point now() noexcept
128-bit integer support.
An extention to the C++ std::chrono::time_point template to make time points easier to work with...
static time_point now() noexcept
std::chrono::duration< int128_t, std::femto > Femtoseconds
Stores a duration in femtoseconds.
Provides Interstellar Time in seconds.
FemtoClock::time_point FemtoTime
A point in time in Interstellar Time stored in Femtoseconds.
TimePoint< NanoClock, duration > time_point
TimePoint & operator=(const TimePoint &)=default
constexpr TimePoint(const std::chrono::time_point< OtherClock, OtherDuration > &otp)
Constructs a TimePoint from a std::chrono::time_point.
static time_point now() noexcept
const int128_t OneE12
1e12 in a 128-bit integer.
static constexpr TimePoint min()
Returns the earlist time that can be represented.
std::chrono::duration< std::int64_t, std::milli > Milliseconds
Stores a duration in milliseconds.
const int128_t OneE6
1e6 in a 128-bit integer.
std::chrono::duration< std::uint64_t, std::nano > Nanoseconds
Stores a duration in nanoseconds.
boost::posix_time::ptime EarthTimeTAI(const ISTime &t)
Provides Interstellar Time in Milliseconds.
GenericClock< duds::data::GenericValue, double, duds::time::interstellar::NanoTime, float > Clock
General use clock driver type.
Definition: Clock.hpp:261
NanoClock::time_point NanoTime
A point in time in Interstellar Time stored in Nanoseconds.