libutil++  1.9.3
 All Classes Functions Variables
TimeStamp.cpp
1 /*
2 ** libutil++
3 ** $Id: TimeStamp.cpp 1653 2016-02-28 19:54:59Z sella $
4 ** Copyright (c) 2011-2016 Digital Genesis, LLC. All Rights Reserved.
5 ** Released under the LGPL Version 2.1 License.
6 ** http://www.digitalgenesis.com
7 */
8 
9 static const char rcsid[] __attribute__((used)) = "$Id: TimeStamp.cpp 1653 2016-02-28 19:54:59Z sella $";
10 
11 #include "TimeStamp.h"
12 
13 #include <sys/time.h>
14 #include <stdint.h>
15 #include <stdlib.h>
16 
17 using namespace sella::util;
18 
19 TimeStamp::TimeStamp(const struct timeval &tv) throw () :
20  stamp((tv.tv_sec * 1000000) + tv.tv_usec)
21 { }
22 
23 TimeStamp::TimeStamp(int64_t usec) throw () :
24  stamp(usec)
25 { }
26 
27 TimeStamp::TimeStamp() throw () :
28  stamp(TimeStamp::now())
29 { }
30 
31 TimeStamp::TimeStamp(const TimeStamp &other) throw () :
32  stamp(other.stamp)
33 { }
34 
35 TimeStamp::~TimeStamp() throw () { }
36 
37 TimeStamp & TimeStamp::operator = (const TimeStamp &other) throw () {
38  if (this != &other) {
39  this->stamp = other.stamp;
40  }
41  return *this;
42 }
43 
44 TimeStamp & TimeStamp::operator = (const struct timeval &tv) throw () {
45  this->stamp = (tv.tv_sec * 1000000) + tv.tv_usec;
46 
47  return *this;
48 }
49 
50 TimeStamp & TimeStamp::operator = (int64_t micro) throw () {
51  this->stamp = micro;
52 
53  return *this;
54 }
55 
56 TimeStamp & TimeStamp::operator += (const TimeStamp &other) throw () {
57  if (this != &other) {
58  this->stamp += other.stamp;
59  }
60  return *this;
61 }
62 
63 TimeStamp & TimeStamp::operator += (const struct timeval &tv) throw () {
64  this->stamp += (tv.tv_sec * 1000000) + tv.tv_usec;
65 
66  return *this;
67 }
68 
69 TimeStamp & TimeStamp::operator += (int64_t micro) throw () {
70  this->stamp += micro;
71 
72  return *this;
73 }
74 
75 TimeStamp & TimeStamp::operator -= (const TimeStamp &other) throw () {
76  if (this != &other) {
77  this->stamp -= other.stamp;
78  }
79  return *this;
80 }
81 
82 TimeStamp & TimeStamp::operator -= (const struct timeval &tv) throw () {
83  this->stamp -= (tv.tv_sec * 1000000) + tv.tv_usec;
84 
85  return *this;
86 }
87 
88 TimeStamp & TimeStamp::operator -= (int64_t micro) throw () {
89  this->stamp -= micro;
90 
91  return *this;
92 }
93 
94 TimeStamp & TimeStamp::operator *= (const TimeStamp &other) throw () {
95  if (this != &other) {
96  this->stamp *= other.stamp;
97  }
98  return *this;
99 }
100 
101 TimeStamp & TimeStamp::operator *= (const struct timeval &tv) throw () {
102  this->stamp *= (tv.tv_sec * 1000000) + tv.tv_usec;
103 
104  return *this;
105 }
106 
107 TimeStamp & TimeStamp::operator *= (int64_t micro) throw () {
108  this->stamp *= micro;
109 
110  return *this;
111 }
112 
113 TimeStamp & TimeStamp::operator /= (const TimeStamp &other) throw () {
114  if (this != &other) {
115  this->stamp /= other.stamp;
116  }
117  return *this;
118 }
119 
120 TimeStamp & TimeStamp::operator /= (const struct timeval &tv) throw () {
121  this->stamp /= (tv.tv_sec * 1000000) + tv.tv_usec;
122 
123  return *this;
124 }
125 
126 TimeStamp & TimeStamp::operator /= (int64_t micro) throw () {
127  this->stamp /= micro;
128 
129  return *this;
130 }
131 
132 TimeStamp TimeStamp::operator + (const TimeStamp &other) throw () {
133  return TimeStamp(this->stamp + other.stamp);
134 }
135 
136 TimeStamp TimeStamp::operator + (const struct timeval &tv) throw () {
137  return TimeStamp(this->stamp + ((tv.tv_sec * 1000000) + tv.tv_usec));
138 }
139 
140 TimeStamp TimeStamp::operator + (int64_t micro) throw () {
141  return TimeStamp(this->stamp + micro);
142 }
143 
144 TimeStamp TimeStamp::operator - (const TimeStamp &other) throw () {
145  return TimeStamp(this->stamp - other.stamp);
146 }
147 
148 TimeStamp TimeStamp::operator - (const struct timeval &tv) throw () {
149  return TimeStamp(this->stamp - ((tv.tv_sec * 1000000) + tv.tv_usec));
150 }
151 
152 TimeStamp TimeStamp::operator - (int64_t micro) throw () {
153  return TimeStamp(this->stamp - micro);
154 }
155 
156 TimeStamp TimeStamp::operator * (const TimeStamp &other) throw () {
157  return TimeStamp(this->stamp * other.stamp);
158 }
159 
160 TimeStamp TimeStamp::operator * (const struct timeval &tv) throw () {
161  return TimeStamp(this->stamp * ((tv.tv_sec * 1000000) + tv.tv_usec));
162 }
163 
164 TimeStamp TimeStamp::operator * (int64_t micro) throw () {
165  return TimeStamp(this->stamp * micro);
166 }
167 
168 TimeStamp TimeStamp::operator / (const TimeStamp &other) throw () {
169  return TimeStamp(this->stamp / other.stamp);
170 }
171 
172 TimeStamp TimeStamp::operator / (const struct timeval &tv) throw () {
173  return TimeStamp(this->stamp / ((tv.tv_sec * 1000000) + tv.tv_usec));
174 }
175 
176 TimeStamp TimeStamp::operator / (int64_t micro) throw () {
177  return TimeStamp(this->stamp / micro);
178 }
179 
180 bool TimeStamp::operator == (const TimeStamp &other) const throw () {
181  return this->stamp == other.stamp;
182 }
183 
184 bool TimeStamp::operator != (const TimeStamp &other) const throw () {
185  return this->stamp != other.stamp;
186 }
187 
188 bool TimeStamp::operator >= (const TimeStamp &other) const throw () {
189  return this->stamp >= other.stamp;
190 }
191 
192 bool TimeStamp::operator <= (const TimeStamp &other) const throw () {
193  return this->stamp <= other.stamp;
194 }
195 
196 bool TimeStamp::operator > (const TimeStamp &other) const throw () {
197  return this->stamp > other.stamp;
198 }
199 
200 bool TimeStamp::operator < (const TimeStamp &other) const throw () {
201  return this->stamp < other.stamp;
202 }
203 
204 void TimeStamp::update() throw () {
205  this->stamp = TimeStamp::now();
206 }
207 
208 int64_t TimeStamp::now() throw () {
209  struct timeval tv;
210 
211  gettimeofday(&tv, NULL);
212 
213  return (tv.tv_sec * 1000000) + tv.tv_usec;
214 }
215 
216 int64_t TimeStamp::getEpochMicroseconds() const throw () {
217  return this->stamp;
218 }
219 
220 int64_t TimeStamp::getEpochSeconds() const throw () {
221  return this->stamp / 1000000;
222 }
223 
224 /*
225 ** vim: noet ts=3 sw=3
226 */