Piotr Krysik | df97869 | 2017-09-27 21:58:24 +0200 | [diff] [blame] | 1 | // |
| 2 | // Copyright 2010-2012 Ettus Research LLC |
| 3 | // |
| 4 | // This program is free software: you can redistribute it and/or modify |
| 5 | // it under the terms of the GNU General Public License as published by |
| 6 | // the Free Software Foundation, either version 3 of the License, or |
| 7 | // (at your option) any later version. |
| 8 | // |
| 9 | // This program is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU General Public License for more details. |
| 13 | // |
| 14 | // You should have received a copy of the GNU General Public License |
| 15 | // along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | // |
| 17 | |
| 18 | #ifndef INCLUDED_TYPES_TIME_SPEC_HPP |
| 19 | #define INCLUDED_TYPES_TIME_SPEC_HPP |
| 20 | |
| 21 | #include <boost/operators.hpp> |
| 22 | #include <ctime> |
| 23 | |
| 24 | namespace gr { |
| 25 | namespace gsm { |
| 26 | |
| 27 | /*! |
| 28 | * A time_spec_t holds a seconds and a fractional seconds time value. |
| 29 | * Depending upon usage, the time_spec_t can represent absolute times, |
| 30 | * relative times, or time differences (between absolute times). |
| 31 | * |
| 32 | * The time_spec_t provides clock-domain independent time storage, |
| 33 | * but can convert fractional seconds to/from clock-domain specific units. |
| 34 | * |
| 35 | * The fractional seconds are stored as double precision floating point. |
| 36 | * This gives the fractional seconds enough precision to unambiguously |
| 37 | * specify a clock-tick/sample-count up to rates of several petahertz. |
| 38 | */ |
| 39 | class time_spec_t : boost::additive<time_spec_t>, boost::totally_ordered<time_spec_t>{ |
| 40 | public: |
| 41 | |
| 42 | /*! |
| 43 | * Get the system time in time_spec_t format. |
| 44 | * Uses the highest precision clock available. |
| 45 | * \return the system time as a time_spec_t |
| 46 | */ |
| 47 | static time_spec_t get_system_time(void); |
| 48 | |
| 49 | /*! |
| 50 | * Copy constructor |
| 51 | */ |
| 52 | time_spec_t(const time_spec_t & spec); |
| 53 | |
| 54 | /*! |
| 55 | * Create a time_spec_t from a real-valued seconds count. |
| 56 | * \param secs the real-valued seconds count (default = 0) |
| 57 | */ |
| 58 | time_spec_t(double secs = 0); |
| 59 | |
| 60 | /*! |
| 61 | * Create a time_spec_t from whole and fractional seconds. |
| 62 | * \param full_secs the whole/integer seconds count |
| 63 | * \param frac_secs the fractional seconds count (default = 0) |
| 64 | */ |
| 65 | time_spec_t(time_t full_secs, double frac_secs = 0); |
| 66 | |
| 67 | /*! |
| 68 | * Create a time_spec_t from whole seconds and fractional ticks. |
| 69 | * Translation from clock-domain specific units. |
| 70 | * \param full_secs the whole/integer seconds count |
| 71 | * \param tick_count the fractional seconds tick count |
| 72 | * \param tick_rate the number of ticks per second |
| 73 | */ |
| 74 | time_spec_t(time_t full_secs, long tick_count, double tick_rate); |
| 75 | |
| 76 | /*! |
| 77 | * Create a time_spec_t from a 64-bit tick count. |
| 78 | * Translation from clock-domain specific units. |
| 79 | * \param ticks an integer count of ticks |
| 80 | * \param tick_rate the number of ticks per second |
| 81 | */ |
| 82 | static time_spec_t from_ticks(long long ticks, double tick_rate); |
| 83 | |
| 84 | /*! |
| 85 | * Convert the fractional seconds to clock ticks. |
| 86 | * Translation into clock-domain specific units. |
| 87 | * \param tick_rate the number of ticks per second |
| 88 | * \return the fractional seconds tick count |
| 89 | */ |
| 90 | long get_tick_count(double tick_rate) const; |
| 91 | |
| 92 | /*! |
| 93 | * Convert the time spec into a 64-bit tick count. |
| 94 | * Translation into clock-domain specific units. |
| 95 | * \param tick_rate the number of ticks per second |
| 96 | * \return an integer number of ticks |
| 97 | */ |
| 98 | long long to_ticks(const double tick_rate) const; |
| 99 | |
| 100 | /*! |
| 101 | * Get the time as a real-valued seconds count. |
| 102 | * Note: If this time_spec_t represents an absolute time, |
| 103 | * the precision of the fractional seconds may be lost. |
| 104 | * \return the real-valued seconds |
| 105 | */ |
| 106 | double get_real_secs(void) const; |
| 107 | |
| 108 | /*! |
| 109 | * Get the whole/integer part of the time in seconds. |
| 110 | * \return the whole/integer seconds |
| 111 | */ |
| 112 | time_t get_full_secs(void) const; |
| 113 | |
| 114 | /*! |
| 115 | * Get the fractional part of the time in seconds. |
| 116 | * \return the fractional seconds |
| 117 | */ |
| 118 | double get_frac_secs(void) const; |
| 119 | |
| 120 | //! Implement addable interface |
| 121 | time_spec_t &operator+=(const time_spec_t &); |
| 122 | |
| 123 | //! Implement subtractable interface |
| 124 | time_spec_t &operator-=(const time_spec_t &); |
| 125 | |
| 126 | //private time storage details |
| 127 | private: time_t _full_secs; double _frac_secs; |
| 128 | }; |
| 129 | |
| 130 | //! Implement equality_comparable interface |
| 131 | bool operator==(const time_spec_t &, const time_spec_t &); |
| 132 | |
| 133 | //! Implement less_than_comparable interface |
| 134 | bool operator<(const time_spec_t &, const time_spec_t &); |
| 135 | |
| 136 | inline time_t time_spec_t::get_full_secs(void) const{ |
| 137 | return this->_full_secs; |
| 138 | } |
| 139 | |
| 140 | inline double time_spec_t::get_frac_secs(void) const{ |
| 141 | return this->_frac_secs; |
| 142 | } |
| 143 | |
| 144 | } //namespace transceiver |
| 145 | } //namespace gr |
| 146 | |
| 147 | #endif /* INCLUDED_TYPES_TIME_SPEC_HPP */ |