blob: 5293da26b02a1373eb85d49cae8348ca8e370ec4 [file] [log] [blame]
Piotr Krysikdf978692017-09-27 21:58:24 +02001//
2// Copyright 2011-2013 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
Piotr Krysik8926c1f2017-10-31 13:01:43 +010018#include <grgsm/misc_utils/time_spec.h>
Piotr Krysikdf978692017-09-27 21:58:24 +020019
20namespace gr {
21 namespace gsm {
22
23 /***********************************************************************
Piotr Krysikdf978692017-09-27 21:58:24 +020024 * Time spec constructors
25 **********************************************************************/
26 #define time_spec_init(full, frac) { \
27 const time_t _full = time_t(full); \
28 const double _frac = double(frac); \
29 const int _frac_int = int(_frac); \
30 _full_secs = _full + _frac_int; \
31 _frac_secs = _frac - _frac_int; \
32 if (_frac_secs < 0) {\
33 _full_secs -= 1; \
34 _frac_secs += 1; \
35 } \
36 }
37
38 inline long long fast_llround(const double x){
39 return (long long)(x + 0.5); // assumption of non-negativity
40 }
41
42 time_spec_t::time_spec_t(const time_spec_t & spec){
43 time_spec_init(spec.get_full_secs(), spec.get_frac_secs());
44 }
45
46 time_spec_t::time_spec_t(double secs){
47 time_spec_init(0, secs);
48 }
49
50 time_spec_t::time_spec_t(time_t full_secs, double frac_secs){
51 time_spec_init(full_secs, frac_secs);
52 }
53
54 time_spec_t::time_spec_t(time_t full_secs, long tick_count, double tick_rate){
55 const double frac_secs = tick_count/tick_rate;
56 time_spec_init(full_secs, frac_secs);
57 }
58
59 time_spec_t time_spec_t::from_ticks(long long ticks, double tick_rate){
60 const long long rate_i = (long long)(tick_rate);
61 const double rate_f = tick_rate - rate_i;
62 const time_t secs_full = time_t(ticks/rate_i);
63 const long long ticks_error = ticks - (secs_full*rate_i);
64 const double ticks_frac = ticks_error - secs_full*rate_f;
65 return time_spec_t(secs_full, ticks_frac/tick_rate);
66 }
67
68 /***********************************************************************
69 * Time spec accessors
70 **********************************************************************/
71 long time_spec_t::get_tick_count(double tick_rate) const{
72 return long(fast_llround(this->get_frac_secs()*tick_rate));
73 }
74
75 long long time_spec_t::to_ticks(double tick_rate) const{
76 const long long rate_i = (long long)(tick_rate);
77 const double rate_f = tick_rate - rate_i;
78 const long long ticks_full = this->get_full_secs()*rate_i;
79 const double ticks_error = this->get_full_secs()*rate_f;
80 const double ticks_frac = this->get_frac_secs()*tick_rate;
81 return ticks_full + fast_llround(ticks_error + ticks_frac);
82 }
83
84 double time_spec_t::get_real_secs(void) const{
85 return this->get_full_secs() + this->get_frac_secs();
86 }
87
88 /***********************************************************************
89 * Time spec math overloads
90 **********************************************************************/
91 time_spec_t &time_spec_t::operator+=(const time_spec_t &rhs){
92 time_spec_init(
93 this->get_full_secs() + rhs.get_full_secs(),
94 this->get_frac_secs() + rhs.get_frac_secs()
95 );
96 return *this;
97 }
98
99 time_spec_t &time_spec_t::operator-=(const time_spec_t &rhs){
100 time_spec_init(
101 this->get_full_secs() - rhs.get_full_secs(),
102 this->get_frac_secs() - rhs.get_frac_secs()
103 );
104 return *this;
105 }
106
107 bool operator==(const time_spec_t &lhs, const time_spec_t &rhs){
108 return
109 lhs.get_full_secs() == rhs.get_full_secs() and
110 lhs.get_frac_secs() == rhs.get_frac_secs()
111 ;
112 }
113
114 bool operator<(const time_spec_t &lhs, const time_spec_t &rhs){
115 return (
116 (lhs.get_full_secs() < rhs.get_full_secs()) or (
117 (lhs.get_full_secs() == rhs.get_full_secs()) and
118 (lhs.get_frac_secs() < rhs.get_frac_secs())
119 ));
120 }
121 }
122}