DUDS
Distributed Update of Data from Something
Planetary.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 PLANETARY_HPP
11 #define PLANETARY_HPP
12 
14 #include <boost/date_time/gregorian/gregorian.hpp>
16 
17 namespace duds {
18 
28 namespace time {
29 
30 namespace planetary {
31 
32 using std::unique_ptr;
33 /*
34 class PlanetaryTime {
35  LeapSeconds leaps;
36  int days;
37 };
38 */
39 
40 // move to earth.hpp
44 class Earth {
46 public:
47  Earth();
49  static const boost::gregorian::date &dateZero();
50  static const boost::posix_time::ptime &timeZero();
55  static void make(const std::string &path = "/usr/share/zoneinfo-leaps/UTC");
57  typedef duration::rep rep;
58  typedef duration::period period;
59  typedef std::chrono::time_point<Earth, duration> time_point;
60  static constexpr bool is_steady = false;
61  time_point now() noexcept {
62  // wrong
63  return time_point(std::chrono::duration_cast<duration, rep, period>(
64  std::chrono::system_clock::now().time_since_epoch()));
65  }
69  boost::gregorian::date date(
71  ) const;
72  template <class ISTime>
76  boost::gregorian::date date(const ISTime &t) const {
78  std::chrono::duration_cast<interstellar::Seconds>(
79  t.time_since_epoch()
80  )
81  ));
82  }
86  std::time_t timeUtc(
88  ) const;
89  template <class ISTime>
93  std::time_t timeUtc(const ISTime &t) const {
95  std::chrono::duration_cast<interstellar::Seconds>(
96  t.time_since_epoch()
97  )
98  ));
99  }
100 
104  boost::gregorian::date dateUtcToTai(
106  ) const;
107  template <class ISTime>
111  boost::gregorian::date dateUtcToTai(const ISTime &t) const {
113  std::chrono::duration_cast<interstellar::Seconds>(
114  t.time_since_epoch()
115  )
116  ));
117  }
121  boost::posix_time::ptime posix(
123  ) const;
124  template <class ISTime>
128  boost::posix_time::ptime posix(const ISTime &t) const {
130  std::chrono::duration_cast<duds::time::interstellar::Milliseconds>(
131  t.time_since_epoch()
132  )
133  ));
134  }
138  boost::posix_time::ptime posixUtcToTai(
140  ) const;
141  template <class ISTime>
145  boost::posix_time::ptime posixUtcToTai(const ISTime &t) const {
147  std::chrono::duration_cast<duds::time::interstellar::Milliseconds>(
148  t.time_since_epoch()
149  )
150  ));
151  }
152  // UTC
153 
158  template <class Clock, class Duration>
161  ) {
162  //duds::time::interstellar::SecondTime comp = time;
163  if (!bound.within(time)) {
164  bound = leaps.getLeapBounds(time);
165  }
166  assert(bound.within(time));
167  time += bound.leaps;
168  }
169 
174  template <class ISTime>
175  void date(ISTime &dest, const boost::gregorian::date &src) {
177  duds::time::interstellar::Seconds((src - dateZero()).days() * 86400)
178  );
180  dest += ls;
181  }
186  template <class ISTime>
187  void time(ISTime &dest, const boost::posix_time::ptime &src) {
190  (src - timeZero()).total_milliseconds()
191  )
192  );
194  dest += ls;
195  }
196 };
197 
198 extern unique_ptr<Earth> earth;
199 
205 template <class ISTime>
206 inline boost::gregorian::date ToDate(const ISTime &t) {
207  return Earth::dateZero() + boost::gregorian::days(
208  std::chrono::duration_cast<duds::time::interstellar::Seconds>(
209  t.time_since_epoch()
210  ).count()
211  );
212 }
213 
219 template <class ISTime>
220 inline boost::posix_time::ptime ToPosix(const ISTime &t) {
221  return Earth::timeZero() + boost::posix_time::milliseconds(
222  std::chrono::duration_cast<duds::time::interstellar::Milliseconds>(
223  t.time_since_epoch()
224  ).count()
225  );
226 }
227 
228 } } }
229 
230 #endif // #ifndef PLANETARY_HPP
boost::posix_time::ptime posix(const ISTime &t) const
Converts from IST to UTC and provides a ptime result.
Definition: Planetary.hpp:128
boost::gregorian::date dateUtcToTai(const ISTime &t) const
Converts from IST to UTC and provides a date result.
Definition: Planetary.hpp:111
static const boost::gregorian::date & dateZero()
Definition: Planetary.cpp:22
std::time_t timeUtc(const duds::time::interstellar::SecondTime &t) const
Converts from IST to UTC and provides a std::time_t result.
Definition: Planetary.cpp:35
MilliClock::time_point MilliTime
A point in time in Interstellar Time stored in Milliseconds.
boost::gregorian::date ToDate(const ISTime &t)
Converts without applying any modification to the time, so if the input is in TAI, the result will be, too.
Definition: Planetary.hpp:206
boost::gregorian::date date(const ISTime &t) const
Converts from IST to UTC and provides a date result.
Definition: Planetary.hpp:76
std::chrono::duration< std::int64_t > Seconds
Stores a duration in seconds.
duds::time::interstellar::Seconds leapSeconds(const duds::time::interstellar::SecondTime &when) const
Returns the sum of all leap seconds in use at the given time.
SecondClock::time_point SecondTime
A point in time in Interstellar Time stored in Seconds.
Terran time stuffs.
Definition: Planetary.hpp:44
const Count & leaps() const
Returns the leap seconds in this period.
Definition: LeapBounds.hpp:118
std::chrono::time_point< Earth, duration > time_point
Definition: Planetary.hpp:59
boost::posix_time::ptime ToPosix(const ISTime &t)
Converts without applying any modification to the time, so if the input is in TAI, the result will be, too.
Definition: Planetary.hpp:220
boost::gregorian::date date(const duds::time::interstellar::SecondTime &t) const
Converts from IST to UTC and provides a date result.
Definition: Planetary.cpp:42
std::time_t timeUtc(const ISTime &t) const
Converts from IST to UTC and provides a std::time_t result.
Definition: Planetary.hpp:93
Stores when leap seconds occur to allow conversions between Interstellar Time or TAI and planetary ti...
Definition: LeapSeconds.hpp:32
An extention to the C++ std::chrono::time_point template to make time points easier to work with...
void time(ISTime &dest, const boost::posix_time::ptime &src)
Converts a POSIX time into one of the time formats defined inside the duds::time::interstellar namesp...
Definition: Planetary.hpp:187
std::chrono::duration< int128_t, std::femto > Femtoseconds
Stores a duration in femtoseconds.
bool within(const duds::time::interstellar::TimePoint< OtherClock, OtherDuration > &time) const
Returns true if the given provided time is within bounds.
Definition: LeapBounds.hpp:127
static constexpr bool is_steady
Definition: Planetary.hpp:60
boost::gregorian::date dateUtcToTai(const duds::time::interstellar::SecondTime &t) const
Converts from UTC to TAI and provides a date result.
Definition: Planetary.cpp:52
void date(ISTime &dest, const boost::gregorian::date &src)
Converts a Gregorian calendar date into one of the time formats defined inside the duds::time::inters...
Definition: Planetary.hpp:175
duration::period period
Definition: Planetary.hpp:58
boost::posix_time::ptime posixUtcToTai(const ISTime &t) const
Converts from UTC to TAI and provides a ptime result.
Definition: Planetary.hpp:145
duds::time::interstellar::Femtoseconds duration
Definition: Planetary.hpp:56
unique_ptr< Earth > earth
Definition: Planetary.cpp:18
std::chrono::duration< std::int64_t, std::milli > Milliseconds
Stores a duration in milliseconds.
void addLeapSeconds(duds::time::interstellar::TimePoint< Clock, Duration > &time)
Adds leap seconds to the given time in UTC, resulting in TAI.
Definition: Planetary.hpp:159
LeapBounds getLeapBounds(const duds::time::interstellar::SecondTime time) const
Create a LeapBounds object with the data for the time period covered for the given time...
static const boost::posix_time::ptime & timeZero()
Definition: Planetary.cpp:26
static void make(const std::string &path="/usr/share/zoneinfo-leaps/UTC")
Makes the Earth planetary time object using the given zoneinfo file for data on leap seconds...
Definition: Planetary.cpp:30
boost::posix_time::ptime posix(const duds::time::interstellar::MilliTime &t) const
Converts from IST to UTC and provides a ptime result.
Definition: Planetary.cpp:62
time_point now() noexcept
Definition: Planetary.hpp:61
A container holding the number of leap seconds and the time bounds over which the leap seconds are ap...
Definition: LeapBounds.hpp:38
boost::posix_time::ptime posixUtcToTai(const duds::time::interstellar::MilliTime &t) const
Converts from UTC to TAI and provides a ptime result.
Definition: Planetary.cpp:72