SF2 C++ FRC Class Library
Sensor Fusion Framework (SF2) for FRC
Timestamp.h
1 /* ============================================
2 SF2 source code is placed under the MIT license
3 Copyright (c) 2017 Kauai Labs
4 
5 Permission is hereby granted, free of charge, to any person obtaining a copy
6 of this software and associated documentation files (the "Software"), to deal
7 in the Software without restriction, including without limitation the rights
8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 copies of the Software, and to permit persons to whom the Software is
10 furnished to do so, subject to the following conditions:
11 
12 The above copyright notice and this permission notice shall be included in
13 all copies or substantial portions of the Software.
14 
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 THE SOFTWARE.
22 ===============================================
23 */
24 
25 #ifndef SRC_TIME_TIMESTAMP_H_
26 #define SRC_TIME_TIMESTAMP_H_
27 
28 #include "../quantity/IQuantity.h"
29 
30 class Timestamp : public IQuantity {
31 
32 public:
33  enum TimestampResolution { Second, Millisecond, Microsecond, Nanosecond };
34  long timestamp;
35  TimestampResolution resolution;
36 
37  Timestamp() {
38  this->timestamp = 0;
39  this->resolution = TimestampResolution::Millisecond;
40  }
41 
42  Timestamp(Timestamp& ts_copy) {
43  this->timestamp = ts_copy.timestamp;
44  this->resolution = ts_copy.resolution;
45  }
46 
47  Timestamp(long timestamp, TimestampResolution resolution) {
48  this->timestamp = timestamp;
49  this->resolution = resolution;
50  }
51 
52  Timestamp(double seconds, TimestampResolution resolution) {
53  this->resolution = resolution;
54  this->fromSeconds(seconds);
55  }
56 
57  ~Timestamp(){}
58 
59  long getTimestamp() {
60  return this->timestamp;
61  }
62 
63  void setTimestamp(long new_timestamp) {
64  this->timestamp = new_timestamp;
65  }
66 
67  void setResolution(TimestampResolution r) {
68  this->resolution = r;
69  }
70 
71  TimestampResolution getResolution() {
72  return this->resolution;
73  }
74 
75  static const long MILLISECONDS_PER_SECOND = 1000;
76  static const long MICROSECONDS_PER_SECOND = MILLISECONDS_PER_SECOND * 1000;
77  static const long NANOSECONDS_PER_SECOND = MICROSECONDS_PER_SECOND * 1000;
78  static const long NANOSECONDS_PER_MICROSECOND = 1000;
79  static const long MICROSECONDS_PER_MILLISECOND = 1000;
80  static const long NANOSECONDS_PER_MILLISECOND = NANOSECONDS_PER_MICROSECOND * 1000;
81 
82  long getNanoseconds() {
83  switch ( resolution ) {
84  case Second:
85  return timestamp * NANOSECONDS_PER_SECOND;
86  case Millisecond:
87  return timestamp * NANOSECONDS_PER_MILLISECOND;
88  case Microsecond:
89  return timestamp * NANOSECONDS_PER_MICROSECOND;
90  case Nanosecond:
91  default:
92  return timestamp;
93  }
94  }
95 
96  long getMicroseconds() {
97  switch ( resolution ) {
98  case Second:
99  return timestamp * MICROSECONDS_PER_SECOND;
100  case Millisecond:
101  return timestamp * MICROSECONDS_PER_MILLISECOND;
102  case Microsecond:
103  default:
104  return timestamp;
105  case Nanosecond:
106  return timestamp / NANOSECONDS_PER_MICROSECOND;
107  }
108  }
109 
110  long getMilliseconds() {
111  switch ( resolution ) {
112  case Second:
113  return timestamp * MILLISECONDS_PER_SECOND;
114  case Millisecond:
115  default:
116  return timestamp;
117  case Microsecond:
118  return timestamp / MICROSECONDS_PER_MILLISECOND;
119  case Nanosecond:
120  return timestamp / NANOSECONDS_PER_MILLISECOND;
121  }
122  }
123 
124  double getSeconds() {
125  switch ( resolution ) {
126  case Second:
127  return (double)timestamp;
128  case Millisecond:
129  default:
130  return ((double)timestamp)/MILLISECONDS_PER_SECOND;
131  case Microsecond:
132  return ((double)timestamp)/MICROSECONDS_PER_SECOND;
133  case Nanosecond:
134  return ((double)timestamp)/NANOSECONDS_PER_SECOND;
135  }
136  }
137 
138  void fromSeconds(double seconds) {
139  switch ( resolution ) {
140  case Second:
141  timestamp = (long)seconds;
142  break;
143  case Millisecond:
144  default:
145  timestamp = (long)(seconds * MILLISECONDS_PER_SECOND);
146  break;
147  case Microsecond:
148  timestamp = (long)(seconds * MICROSECONDS_PER_SECOND);
149  break;
150  case Nanosecond:
151  timestamp = (long)(seconds * NANOSECONDS_PER_SECOND);
152  break;
153  }
154  }
155 
156  /* IQuantity */
157  bool getPrintableString(vector<string>& printable_string) {
158  printable_string.push_back(to_string(timestamp));
159  return true;
160  }
161 
162  bool getContainedQuantities(vector<IQuantity *>& quantities) {
163  return false;
164  }
165 
166  bool getContainedQuantityNames(vector<string>& quantity_names) {
167  return false;
168  }
169 };
170 
171 #endif /* SRC_TIME_TIMESTAMP_H_ */
Definition: IQuantity.h:33
Definition: Timestamp.h:30