astrotypes  0.0
TimePoint.cpp
Go to the documentation of this file.
1 /*
2  * The MIT License (MIT)
3  *
4  * Copyright (c) 2016 The SKA organisation
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 
25 // This class taken from the orignal ska::cheetah::units class
26 
27 #include "../TimePoint.h"
28 #include "../BoostDurationCast.h"
29 #pragma GCC diagnostic push
30 #pragma GCC diagnostic ignored "-Wall"
31 #pragma GCC diagnostic ignored "-Wpragmas"
32 #pragma GCC diagnostic ignored "-Wunused-parameter"
33 #pragma GCC diagnostic ignored "-Wunused-variable"
34 #include <boost/units/quantity.hpp>
35 #include <boost/units/conversion.hpp>
36 #include <boost/units/systems/si/time.hpp>
37 #pragma GCC diagnostic pop
38 #include <iomanip>
39 #include <ctime>
40 
41 namespace pss {
42 namespace astrotypes {
43 namespace units {
44 
45 template<typename ClockType, typename Duration>
47  : std::chrono::time_point<ClockType, Duration>(d)
48 {
49 }
50 
51 template<typename ClockType, typename Duration>
52 template<typename QuantityType>
53 TimePoint<ClockType, Duration>::TimePoint( QuantityType const& d, typename std::enable_if<
54  boost::units::is_quantity<QuantityType>::value
55  && boost::units::is_unit_of_dimension<typename QuantityType::unit_type, boost::units::time_dimension>::value
56  , QuantityType>::type)
57  : std::chrono::time_point<ClockType, Duration>(duration_cast<typename ClockType::duration>(d))
58 {
59 }
60 
61 template<typename ClockType, typename Duration>
62 TimePoint<ClockType, Duration>::TimePoint( const std::chrono::system_clock::time_point& tp )
63  : std::chrono::time_point<ClockType, Duration>(std::chrono::duration_cast<typename ClockType::duration>(tp.time_since_epoch()) + ClockType::diff_from_system_epoch)
64 {
65 }
66 
67 template<typename ClockType, typename Duration>
68 TimePoint<ClockType, Duration>::TimePoint( const std::chrono::time_point<ClockType, Duration>& tp )
69  : std::chrono::time_point<ClockType, Duration>(tp)
70 {
71 }
72 
73 template<typename ClockType, typename Duration>
74 TimePoint<ClockType, Duration>::TimePoint( std::chrono::time_point<ClockType, Duration>&& tp )
75  : std::chrono::time_point<ClockType, Duration>(std::move(tp))
76 {
77 }
78 
79 template<typename ClockType, typename Duration>
80 TimePoint<ClockType, Duration>::operator typename std::chrono::system_clock::time_point() const
81 {
82  auto dur = this->time_since_epoch() - ClockType::diff_from_system_epoch;
83  return std::chrono::system_clock::time_point(std::chrono::duration_cast<std::chrono::system_clock::duration>(dur));
84 }
85 
86 template<typename ClockType, typename Duration>
88 {
89  return std::chrono::system_clock::to_time_t(static_cast<std::chrono::system_clock::time_point>(*this));
90 }
91 
92 template<typename ClockType, typename Duration>
93 std::ostream& operator<<(std::ostream& os, TimePoint<ClockType, Duration> const& tp)
94 {
95  //std::time_t tt = tp.to_time_t();
96  //os << std::put_time(std::gmtime(&tt), "%F %T"); // not implemented till gcc 5.0
97  //os << asctime(std::gmtime(&tt));
98  os << tp.time_since_epoch().count() << " " << ClockType::symbol;
99  return os;
100 }
101 
102 template<typename ClockType, typename Duration>
104 {
106  return *this;
107 }
108 
109 template<typename ClockType, typename Duration>
111 {
113  return *this;
114 }
115 
116 template<typename ClockType, typename Duration, typename Duration2>
118  return TimePoint<ClockType, Duration>(static_cast<std::chrono::time_point<ClockType, Duration>>(lhs) + rhs);
119 }
120 
121 template<typename ClockType, typename DurationType, typename Rep>
122 TimePoint<ClockType, DurationType> operator+(TimePoint<ClockType, DurationType> const& lhs, boost::units::quantity<boost::units::si::time, Rep> const& rhs) {
123  typedef typename ClockType::duration Duration;
124  return lhs + duration_cast<Duration>(rhs);
125 }
126 
127 template<typename ClockType, typename DurationType, typename Rep>
128 TimePoint<ClockType, DurationType>& operator+=(TimePoint<ClockType, DurationType>& lhs, boost::units::quantity<boost::units::si::time, Rep> const& rhs)
129 {
130  typedef typename ClockType::duration Duration;
131  return lhs += duration_cast<Duration>(rhs);
132 }
133 
134 template<typename ClockType, typename DurationType, typename Rep>
135 TimePoint<ClockType, DurationType> operator-(TimePoint<ClockType, DurationType> const& lhs, boost::units::quantity<boost::units::si::time, Rep> const& rhs) {
136  typedef typename ClockType::duration Duration;
137  return lhs - duration_cast<Duration>(rhs);
138 }
139 
140 template<typename ClockType, typename DurationType, typename Rep>
141 TimePoint<ClockType, DurationType>& operator-=(TimePoint<ClockType, DurationType>& lhs, boost::units::quantity<boost::units::si::time, Rep> const& rhs)
142 {
143  typedef typename ClockType::duration Duration;
144  return lhs -= duration_cast<Duration>(rhs);
145 }
146 
147 template<typename ClockType, typename Duration, typename Duration2>
149  return TimePoint<ClockType, Duration>(static_cast<std::chrono::time_point<ClockType, Duration>>(lhs) - rhs);
150 }
151 
152 template<typename ClockType, typename Duration, typename Duration2>
154 {
155  return static_cast<std::chrono::time_point<ClockType, Duration>>(lhs) - static_cast<std::chrono::time_point<ClockType, Duration2>>(rhs);
156 }
157 
158 } // namespace units
159 } // namespace astrotypes
160 } // namespace pss
STL namespace.
TimePoint(const Duration &d=typename ClockType::duration())
Definition: TimePoint.cpp:46
std::time_t to_time_t() const
convert to a C style time struct. very useful if you want to output the time as a string with e...
Definition: TimePoint.cpp:87
TimePoint< ClockType, Duration > operator+(TimePoint< ClockType, Duration > const &lhs, Duration2 const &rhs)
Definition: TimePoint.cpp:117
TimePoint< ClockType, DurationType > & operator-=(TimePoint< ClockType, DurationType > &lhs, boost::units::quantity< boost::units::si::time, Rep > const &rhs)
Definition: TimePoint.cpp:141
TimePoint< ClockType, Duration > & operator-=(Duration const &)
Definition: TimePoint.cpp:110
TimePoint< ClockType, Duration > & operator+=(Duration const &)
Definition: TimePoint.cpp:103
TimePoint< ClockType, DurationType > operator-(TimePoint< ClockType, DurationType > const &lhs, boost::units::quantity< boost::units::si::time, Rep > const &rhs)
Definition: TimePoint.cpp:135
constexpr std::enable_if< boost::units::is_quantity< BoostQuantity >::value &&boost::units::is_unit_of_dimension< typename BoostQuantity::unit_type, boost::units::time_dimension >::value &&!is_equivalent< BoostQuantity, std::chrono::duration< ChronoNumericalRep, PeriodType > >::value, BoostQuantity >::type duration_cast(const std::chrono::duration< ChronoNumericalRep, PeriodType > &duration)
Mimic the std::duration_cast to convert to/from boost::units::quantity tyeps.
TimePoint< ClockType, DurationType > & operator+=(TimePoint< ClockType, DurationType > &lhs, boost::units::quantity< boost::units::si::time, Rep > const &rhs)
Definition: TimePoint.cpp:128
extension of std::chrono::time_point for astrotypes clocks
Definition: TimePoint.h:53