GNU Radio's TEST Package
time_spec.h
Go to the documentation of this file.
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_OSMOSDR_TIME_SPEC_H
19#define INCLUDED_OSMOSDR_TIME_SPEC_H
20
21#include <osmosdr/api.h>
22#include <boost/operators.hpp>
23#include <ctime>
24
25namespace osmosdr{
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 OSMOSDR_API 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 */
48
49 /*!
50 * Create a time_spec_t from a real-valued seconds count.
51 * \param secs the real-valued seconds count (default = 0)
52 */
53 time_spec_t(double secs = 0);
54
55 /*!
56 * Create a time_spec_t from whole and fractional seconds.
57 * \param full_secs the whole/integer seconds count
58 * \param frac_secs the fractional seconds count (default = 0)
59 */
60 time_spec_t(time_t full_secs, double frac_secs = 0);
61
62 /*!
63 * Create a time_spec_t from whole seconds and fractional ticks.
64 * Translation from clock-domain specific units.
65 * \param full_secs the whole/integer seconds count
66 * \param tick_count the fractional seconds tick count
67 * \param tick_rate the number of ticks per second
68 */
69 time_spec_t(time_t full_secs, long tick_count, double tick_rate);
70
71 /*!
72 * Create a time_spec_t from a 64-bit tick count.
73 * Translation from clock-domain specific units.
74 * \param ticks an integer count of ticks
75 * \param tick_rate the number of ticks per second
76 */
77 static time_spec_t from_ticks(long long ticks, double tick_rate);
78
79 /*!
80 * Convert the fractional seconds to clock ticks.
81 * Translation into clock-domain specific units.
82 * \param tick_rate the number of ticks per second
83 * \return the fractional seconds tick count
84 */
85 long get_tick_count(double tick_rate) const;
86
87 /*!
88 * Convert the time spec into a 64-bit tick count.
89 * Translation into clock-domain specific units.
90 * \param tick_rate the number of ticks per second
91 * \return an integer number of ticks
92 */
93 long long to_ticks(const double tick_rate) const;
94
95 /*!
96 * Get the time as a real-valued seconds count.
97 * Note: If this time_spec_t represents an absolute time,
98 * the precision of the fractional seconds may be lost.
99 * \return the real-valued seconds
100 */
101 double get_real_secs(void) const;
102
103 /*!
104 * Get the whole/integer part of the time in seconds.
105 * \return the whole/integer seconds
106 */
107 time_t get_full_secs(void) const;
108
109 /*!
110 * Get the fractional part of the time in seconds.
111 * \return the fractional seconds
112 */
113 double get_frac_secs(void) const;
114
115 //! Implement addable interface
117
118 //! Implement subtractable interface
120
121 //private time storage details
122 private: time_t _full_secs; double _frac_secs;
123 };
124
125 //! Implement equality_comparable interface
127
128 //! Implement less_than_comparable interface
130
131 inline time_t time_spec_t::get_full_secs(void) const{
132 return this->_full_secs;
133 }
134
135 inline double time_spec_t::get_frac_secs(void) const{
136 return this->_frac_secs;
137 }
138
139} //namespace osmosdr
140
141#endif /* INCLUDED_OSMOSDR_TIME_SPEC_H */
#define OSMOSDR_API
Definition: api.h:30
Definition: time_spec.h:39
time_spec_t & operator+=(const time_spec_t &)
Implement addable interface.
time_t get_full_secs(void) const
Definition: time_spec.h:131
static time_spec_t get_system_time(void)
time_spec_t(double secs=0)
long get_tick_count(double tick_rate) const
double get_real_secs(void) const
static time_spec_t from_ticks(long long ticks, double tick_rate)
time_spec_t & operator-=(const time_spec_t &)
Implement subtractable interface.
time_spec_t(time_t full_secs, long tick_count, double tick_rate)
double get_frac_secs(void) const
Definition: time_spec.h:135
long long to_ticks(const double tick_rate) const
time_spec_t(time_t full_secs, double frac_secs=0)
Definition: device.h:35
OSMOSDR_API bool operator<(const time_spec_t &, const time_spec_t &)
Implement less_than_comparable interface.
OSMOSDR_API bool operator==(const time_spec_t &, const time_spec_t &)
Implement equality_comparable interface.