Fawkes API Fawkes Development Version
time.h
1
2/***************************************************************************
3 * time.h - Time utils
4 *
5 * Created: Wed Jan 18 15:56:33 2006 (from FireVision)
6 * Copyright 2005-2006 Tim Niemueller [www.niemueller.de]
7 *
8 ****************************************************************************/
9
10/* This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version. A runtime exception applies to
14 * this software (see LICENSE.GPL_WRE file mentioned below for details).
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Library General Public License for more details.
20 *
21 * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22 */
23
24#ifndef _UTILS_TIME_TIME_H_
25#define _UTILS_TIME_TIME_H_
26
27#include <sys/time.h>
28
29#include <cmath>
30
31namespace fawkes {
32
33/** Calculate time difference of two time structs.
34 * The calculated time is t = a - b, where t is a represented as the number of
35 * seconds in a single precision float.
36 * @param a time to subtract from
37 * @param b time to subtract
38 * @return a - b
39 */
40inline double
41time_diff_sec(const timeval &a, const timeval &b)
42{
43 //double required if we do not want to loose the usecs
44 double res = a.tv_sec - b.tv_sec + (a.tv_usec - b.tv_usec) / 1000000.0;
45 return res;
46}
47
48/** Calculate time difference of two time structs.
49 * The calculated time is t = a - b, where t is a represented as the number of
50 * seconds in a single precision float.
51 * @param a_sec seconds of time to subtract from
52 * @param a_usec microseconds of time to subtract from
53 * @param b_sec seconds of time to subtract
54 * @param b_usec microseconds of time to subtract
55 * @return a_sec - b_sec + (a_usec - b_usec) / 1000000.f
56 */
57inline double
58time_diff_sec(const long int a_sec,
59 const long int a_usec,
60 const long int b_sec,
61 const long int b_usec)
62{
63 //double required if we do not want to loose the usecs
64 double res = a_sec - b_sec + (a_usec - b_usec) / 1000000.0;
65 return res;
66}
67
68/** Convert seconds to micro seconds.
69 * @param sec seconds to convert
70 * @return time in microseconds
71 */
72inline long int
74{
75 return (long)round(sec * 1000000.);
76}
77
78/** Get difference between two time structs in microseconds.
79 * The calculated time is t = a - b
80 * @param a time to subtract from
81 * @param b time to subtract
82 * @return difference between a and b in microseconds
83 */
84inline long int
85time_diff_usec(const timeval &a, const timeval &b)
86{
87 return (a.tv_sec - b.tv_sec) * 1000000 + (a.tv_usec - b.tv_usec);
88}
89
90class Clock;
91
92class Time
93{
94 friend Clock;
95
96public:
97 Time();
98 Time(const timeval *tv);
99 Time(long sec, long usec, Clock *clock = 0);
100 Time(long ms);
101 Time(double sec);
102 Time(Clock *clock);
103 Time(const Time &t);
104 Time(const Time *t);
105 ~Time();
106
107 double in_sec() const;
108 long in_msec() const;
109 long in_usec() const;
110
111 const timeval *
113 {
114 return &time_;
115 }
116 long
117 get_sec() const
118 {
119 return time_.tv_sec;
120 }
121 long
122 get_msec() const
123 {
124 return time_.tv_usec / 1000;
125 }
126 long
127 get_usec() const
128 {
129 return time_.tv_usec;
130 }
131 long
132 get_nsec() const
133 {
134 return time_.tv_usec * 1000;
135 }
136 void
137 get_timestamp(long &sec, long &usec) const
138 {
139 sec = time_.tv_sec;
140 usec = time_.tv_usec;
141 }
142 bool
143 is_zero() const
144 {
145 return (time_.tv_sec == 0) && (time_.tv_usec == 0);
146 }
147
148 void set_time(const timeval *tv);
149 void set_time(long int sec, long int usec);
150 void set_time(long ms);
151 void set_time(double sec);
152 void set_time(const Time &t);
153 void set_time(const Time *t);
154
155 void set_clock(Clock *clock);
156
157 void add(double seconds);
158
159 Time &stamp();
161
162 Time operator+(const double sec) const;
163 Time operator+(const long int usec) const;
164 Time operator+(const Time &t) const;
165 Time operator+(const Time *t) const;
166 Time operator-(const Time &t) const;
167 double operator-(const Time *t) const;
168 Time operator-(const long int usec) const;
169 Time operator-(const double sec) const;
170 Time & operator+=(const long int usec);
171 Time & operator+=(const Time &t);
172 Time & operator+=(const double sec);
173 Time & operator-=(const Time &t);
174 Time & operator-=(const double sec);
175 Time & operator-=(const long int usec);
176 Time & operator=(const Time &t);
177 bool operator==(const Time &t) const;
178 bool operator==(const Time *t) const;
179 bool operator!=(const Time &t) const;
180 bool operator!=(const Time *t) const;
181 bool operator>(const Time &t) const;
182 bool operator>(const Time *t) const;
183 bool operator>=(const Time &t) const;
184 bool operator>=(const Time *t) const;
185 bool operator<(const Time &t) const;
186 bool operator<(const Time *t) const;
187 bool operator<=(const Time &t) const;
188 bool operator<=(const Time *t) const;
189
190 void wait();
191 void wait_systime();
192
193 const char *str(bool utc = false) const;
194 void str_r(char *s, bool utc = false);
195
196 static const unsigned int TIMESTR_SIZE;
197
198private:
199 Clock * clock_;
200 timeval time_;
201 mutable char *timestr_;
202};
203
204extern const Time TIME_MAX;
205extern const Time TIME_MIN;
206
207} // end namespace fawkes
208
209#endif
This is supposed to be the central clock in Fawkes.
Definition: clock.h:35
A class for handling time.
Definition: time.h:93
void set_clock(Clock *clock)
Set clock for this instance.
Definition: time.cpp:308
void get_timestamp(long &sec, long &usec) const
Get time stamp.
Definition: time.h:137
long get_usec() const
Get microseconds.
Definition: time.h:127
Time & operator=(const Time &t)
Assign operator.
Definition: time.cpp:564
~Time()
Destructor.
Definition: time.cpp:206
static const unsigned int TIMESTR_SIZE
Maximum size of string returned by str() and the minimum size of the string passwd to str_r().
Definition: time.h:196
Time & stamp()
Set this time to the current time.
Definition: time.cpp:704
void str_r(char *s, bool utc=false)
Output function.
Definition: time.cpp:819
bool operator>(const Time &t) const
Greater than operator.
Definition: time.cpp:617
long get_msec() const
Get milliseconds.
Definition: time.h:122
void add(double seconds)
Add seconds.
Definition: time.cpp:322
void wait_systime()
Wait (sleep) for this system time.
Definition: time.cpp:760
bool is_zero() const
Check if time is zero.
Definition: time.h:143
void wait()
Wait (sleep) for this time.
Definition: time.cpp:736
Time & operator-=(const Time &t)
-= operator.
Definition: time.cpp:531
double in_sec() const
Convet time to seconds.
Definition: time.cpp:219
const timeval * get_timeval() const
Obtain the timeval where the time is stored.
Definition: time.h:112
bool operator<=(const Time &t) const
Less than or equal to operator.
Definition: time.cpp:683
bool operator<(const Time &t) const
Less than operator.
Definition: time.cpp:661
bool operator!=(const Time &t) const
Check inequality of times.
Definition: time.cpp:597
long get_nsec() const
Get nanoseconds.
Definition: time.h:132
Time & operator+=(const long int usec)
+= operator
Definition: time.cpp:492
bool operator==(const Time &t) const
Check equality of times.
Definition: time.cpp:577
bool operator>=(const Time &t) const
Greater than or equal to operator.
Definition: time.cpp:639
long in_usec() const
Convert the stored time into micro-seconds.
Definition: time.cpp:237
Time()
Constructor.
Definition: time.cpp:93
Time operator-(const Time &t) const
Operator that substracts one Time from another.
Definition: time.cpp:403
Time & stamp_systime()
Set this time to the current system time.
Definition: time.cpp:720
const char * str(bool utc=false) const
Output function.
Definition: time.cpp:790
void set_time(const timeval *tv)
Sets the time.
Definition: time.cpp:246
long get_sec() const
Get seconds.
Definition: time.h:117
Time operator+(const double sec) const
Operator that adds times.
Definition: time.cpp:361
long in_msec() const
Convert the stored time into milli-seconds.
Definition: time.cpp:228
Fawkes library namespace.
const Time TIME_MAX
Instance of Time denoting the maximum value possible.
Definition: time.cpp:43
long int time_diff_usec(const timeval &a, const timeval &b)
Get difference between two time structs in microseconds.
Definition: time.h:85
long int time_sec_to_usec(double sec)
Convert seconds to micro seconds.
Definition: time.h:73
double time_diff_sec(const timeval &a, const timeval &b)
Calculate time difference of two time structs.
Definition: time.h:41
const Time TIME_MIN
Instance of Time denoting the minimum value possible.
Definition: time.cpp:48