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 
31 namespace 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  */
40 inline double
41 time_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  */
57 inline double
58 time_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  */
72 inline long int
73 time_sec_to_usec(double sec)
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  */
84 inline long int
85 time_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 
90 class Clock;
91 
92 class Time
93 {
94  friend Clock;
95 
96 public:
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 *
112  get_timeval() const
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();
160  Time &stamp_systime();
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 
198 private:
199  Clock * clock_;
200  timeval time_;
201  mutable char *timestr_;
202 };
203 
204 extern const Time TIME_MAX;
205 extern const Time TIME_MIN;
206 
207 } // end namespace fawkes
208 
209 #endif
fawkes::Time::in_msec
long in_msec() const
Convert the stored time into milli-seconds.
Definition: time.cpp:235
fawkes::Time::set_time
void set_time(const timeval *tv)
Sets the time.
Definition: time.cpp:253
fawkes::Time::str
const char * str(bool utc=false) const
Output function.
Definition: time.cpp:797
fawkes::TIME_MIN
const Time TIME_MIN
Instance of Time denoting the minimum value possible.
Definition: time.cpp:55
fawkes::Time::in_usec
long in_usec() const
Convert the stored time into micro-seconds.
Definition: time.cpp:244
fawkes::Time::get_sec
long get_sec() const
Definition: time.h:123
fawkes::Time::operator<
bool operator<(const Time &t) const
Less than operator.
Definition: time.cpp:668
fawkes::Time::operator+=
Time & operator+=(const long int usec)
+= operator
Definition: time.cpp:499
fawkes::Time::get_nsec
long get_nsec() const
Definition: time.h:138
fawkes::TIME_MAX
const Time TIME_MAX
Instance of Time denoting the maximum value possible.
Definition: time.cpp:50
fawkes::Time::get_timeval
const timeval * get_timeval() const
Definition: time.h:118
fawkes::Time::Time
Time()
Constructor.
Definition: time.cpp:100
fawkes::Time::stamp_systime
Time & stamp_systime()
Set this time to the current system time.
Definition: time.cpp:727
fawkes::time_diff_usec
long int time_diff_usec(const timeval &a, const timeval &b)
Get difference between two time structs in microseconds.
Definition: time.h:91
fawkes::time_diff_sec
double time_diff_sec(const timeval &a, const timeval &b)
Calculate time difference of two time structs.
Definition: time.h:47
fawkes::Time::str_r
void str_r(char *s, bool utc=false)
Output function.
Definition: time.cpp:826
fawkes::Time::wait
void wait()
Wait (sleep) for this time.
Definition: time.cpp:743
fawkes::Time::operator>
bool operator>(const Time &t) const
Greater than operator.
Definition: time.cpp:624
fawkes::Time::~Time
~Time()
Destructor.
Definition: time.cpp:213
fawkes::Time::operator<=
bool operator<=(const Time &t) const
Less than or equal to operator.
Definition: time.cpp:690
fawkes
fawkes::Time::wait_systime
void wait_systime()
Wait (sleep) for this system time.
Definition: time.cpp:767
fawkes::Time::get_msec
long get_msec() const
Definition: time.h:128
fawkes::Time::is_zero
bool is_zero() const
Definition: time.h:149
fawkes::Time::set_clock
void set_clock(Clock *clock)
Set clock for this instance.
Definition: time.cpp:315
fawkes::Time
Definition: time.h:98
fawkes::Time::add
void add(double seconds)
Add seconds.
Definition: time.cpp:329
fawkes::Time::operator-=
Time & operator-=(const Time &t)
-= operator.
Definition: time.cpp:538
fawkes::time_sec_to_usec
long int time_sec_to_usec(double sec)
Convert seconds to micro seconds.
Definition: time.h:79
fawkes::Time::operator+
Time operator+(const double sec) const
Operator that adds times.
Definition: time.cpp:368
fawkes::Time::stamp
Time & stamp()
Set this time to the current time.
Definition: time.cpp:711
fawkes::Time::operator>=
bool operator>=(const Time &t) const
Greater than or equal to operator.
Definition: time.cpp:646
fawkes::Time::operator-
Time operator-(const Time &t) const
Operator that substracts one Time from another.
Definition: time.cpp:410
fawkes::Time::get_usec
long get_usec() const
Definition: time.h:133
fawkes::Time::get_timestamp
void get_timestamp(long &sec, long &usec) const
Definition: time.h:143
fawkes::Time::TIMESTR_SIZE
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:202
fawkes::Time::in_sec
double in_sec() const
Convet time to seconds.
Definition: time.cpp:226
fawkes::Time::operator=
Time & operator=(const Time &t)
Assign operator.
Definition: time.cpp:571
fawkes::Time::operator==
bool operator==(const Time &t) const
Check equality of times.
Definition: time.cpp:584
fawkes::Clock
Definition: clock.h:41
fawkes::Time::operator!=
bool operator!=(const Time &t) const
Check inequality of times.
Definition: time.cpp:604