Cheetah - SKA - PSS - Prototype Time Domain Search Pipeline
TimePoint.cpp
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 #include "cheetah/utils/detail/TimePoint.h"
25 #pragma GCC diagnostic push
26 #pragma GCC diagnostic ignored "-Wall"
27 #pragma GCC diagnostic ignored "-Wpragmas"
28 #pragma GCC diagnostic ignored "-Wunused-parameter"
29 #pragma GCC diagnostic ignored "-Wunused-variable"
30 #include <boost/units/quantity.hpp>
31 #include <boost/units/conversion.hpp>
32 #include <boost/units/systems/si/time.hpp>
33 #pragma GCC diagnostic pop
34 #include <iomanip>
35 #include <ctime>
36 
37 namespace ska {
38 namespace cheetah {
39 namespace utils {
40 
41 template<typename ClockType, typename Duration>
42 TimePoint<ClockType, Duration>::TimePoint( const Duration& d )
43  : std::chrono::time_point<ClockType, Duration>(d)
44 {
45 }
46 
47 template<typename ClockType, typename Duration>
48 TimePoint<ClockType, Duration>::TimePoint( const std::chrono::system_clock::time_point& tp )
49  : std::chrono::time_point<ClockType, Duration>(std::chrono::duration_cast<typename ClockType::duration>(tp.time_since_epoch()) + ClockType::diff_from_system_epoch)
50 {
51 }
52 
53 template<typename ClockType, typename Duration>
54 TimePoint<ClockType, Duration>::TimePoint( const std::chrono::time_point<ClockType, Duration>& tp )
55  : std::chrono::time_point<ClockType, Duration>(tp)
56 {
57 }
58 
59 template<typename ClockType, typename Duration>
60 TimePoint<ClockType, Duration>::TimePoint( std::chrono::time_point<ClockType, Duration>&& tp )
61  : std::chrono::time_point<ClockType, Duration>(std::move(tp))
62 {
63 }
64 
65 template<typename ClockType, typename Duration>
66 TimePoint<ClockType, Duration>::operator typename std::chrono::system_clock::time_point() const
67 {
68  auto dur = this->time_since_epoch() - ClockType::diff_from_system_epoch;
69  return std::chrono::system_clock::time_point(std::chrono::duration_cast<std::chrono::system_clock::duration>(dur));
70 }
71 
72 template<typename ClockType, typename Duration>
74 {
75  return std::chrono::system_clock::to_time_t(static_cast<std::chrono::system_clock::time_point>(*this));
76 }
77 
78 template<typename ClockType, typename Duration>
79 std::ostream& operator<<(std::ostream& os, TimePoint<ClockType, Duration> const& tp)
80 {
81  //std::time_t tt = tp.to_time_t();
82  //os << std::put_time(std::gmtime(&tt), "%F %T"); // not implemented till gcc 5.0
83  //os << asctime(std::gmtime(&tt));
84  os << tp.time_since_epoch().count() << " " << ClockType::symbol;
85  return os;
86 }
87 
88 template<typename ClockType, typename Duration>
90 {
91  std::chrono::time_point<ClockType, Duration>::operator+=(d);
92  return *this;
93 }
94 
95 template<typename ClockType, typename Duration>
97 {
98  std::chrono::time_point<ClockType, Duration>::operator-=(d);
99  return *this;
100 }
101 
102 template<typename ClockType, typename Duration, typename Duration2>
103 TimePoint<ClockType, Duration> operator+(TimePoint<ClockType, Duration> const& lhs, Duration2 const& rhs) {
104  return TimePoint<ClockType, Duration>(static_cast<std::chrono::time_point<ClockType, Duration>>(lhs) + rhs);
105 }
106 
107 template<typename ClockType, typename DurationType, typename Rep>
108 TimePoint<ClockType, DurationType> operator+(TimePoint<ClockType, DurationType> const& lhs, boost::units::quantity<boost::units::si::time, Rep> const& rhs) {
109  typedef typename ClockType::duration Duration;
110  return lhs + Duration(boost::units::quantity_cast<typename Duration::rep>(rhs));
111 }
112 
113 template<typename ClockType, typename Duration, typename Duration2>
114 TimePoint<ClockType, Duration> operator-(TimePoint<ClockType, Duration> const& lhs, Duration2 const& rhs) {
115  return TimePoint<ClockType, Duration>(static_cast<std::chrono::time_point<ClockType, Duration>>(lhs) - rhs);
116 }
117 
118 template<typename ClockType, typename Duration, typename Duration2>
119 Duration operator-(TimePoint<ClockType, Duration> const& lhs, TimePoint<ClockType, Duration2> const& rhs)
120 {
121  return static_cast<std::chrono::time_point<ClockType, Duration>>(lhs) - static_cast<std::chrono::time_point<ClockType, Duration2>>(rhs);
122 }
123 
124 } // namespace utils
125 } // namespace cheetah
126 } // namespace ska
Some limits and constants for FLDO.
Definition: Brdz.h:35
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:73
extension of std::chrono::time_point for chhetah clocks
Definition: TimePoint.h:40