timer.hpp
Go to the documentation of this file.
1 /* Copyright 2008 Renato Florentino Garcia <fgar.renato@gmail.com>
2  *
3  * This program is free software: you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License version 2, as
5  * published by the Free Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program. If not, see <http://www.gnu.org/licenses/>.
14  */
15 
16 #ifndef TIMER_HPP
17 #define TIMER_HPP
18 
19 #include <sys/timeb.h>
20 #include <time.h>
21 
26 #define TIMEB timeb
27 #define FTIME ftime
28 
36 {
37 public:
38 
39  EpuckTimer()
40  :intervalRunning(false)
41  {}
42 
49  double initialize(double startTime=0.0)
50  {
51  this->offset = startTime;
52 
53  FTIME(&t);
54  t0 = (double)(t.time + t.millitm/1000.0);
55  return t0;
56  }
57 
64  double elapsedTime()
65  {
66  currentTime();
67  return(t2-t0 + offset);
68  }
69 
75  {
76  intervalRunning = true;
77  FTIME(&t);
78  t1 = (double)(t.time + t.millitm/1000.0);
79  }
80 
87  double intervalDelay()
88  {
89  if(intervalRunning == false)
90  {
91  return 0;
92  }
93  FTIME(&t);
94  t2 = (double)(t.time + t.millitm/1000.0);
95  return t2-t1;
96  }
97 
98 private:
99 
100  // Offset that will be added at current delta time from start instant.
101  double offset;
102  struct TIMEB t;
103  double t0, t1, t2;
104  bool intervalRunning;
105 
106  // return The elapsed time since 00:00:00 UTC, in seconds.
107  double currentTime()
108  {
109  FTIME(&t);
110  t2 = (double)(t.time + t.millitm/1000.0);
111  return t2;
112  }
113 
114 };
115 
116 
117 #endif
118 
double initialize(double startTime=0.0)
Set the start time.
Definition: timer.hpp:49
double intervalDelay()
Get the time elapsed since the last call at resetInterval.
Definition: timer.hpp:87
double elapsedTime()
Get the time elapsed since the initialization.
Definition: timer.hpp:64
A timer class.
Definition: timer.hpp:35
void resetInterval()
Start a new interval.
Definition: timer.hpp:74