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 #include <cmath>
29 
30 namespace fawkes {
31 
32 /** Calculate time difference of two time structs.
33  * The calculated time is t = a - b, where t is a represented as the number of
34  * seconds in a single precision float.
35  * @param a time to subtract from
36  * @param b time to subtract
37  * @return a - b
38  */
39 inline double
40 time_diff_sec(const timeval &a, const timeval &b)
41 {
42  //double required if we do not want to loose the usecs
43  double res = a.tv_sec - b.tv_sec + (a.tv_usec - b.tv_usec) / 1000000.0;
44  return res;
45 }
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, const long int a_usec,
59  const long int b_sec, const long int b_usec)
60 {
61  //double required if we do not want to loose the usecs
62  double res = a_sec - b_sec + (a_usec - b_usec) / 1000000.0;
63  return res;
64 }
65 
66 
67 /** Convert seconds to micro seconds.
68  * @param sec seconds to convert
69  * @return time in microseconds
70  */
71 inline long int
72 time_sec_to_usec(double sec)
73 {
74  return (long)round(sec * 1000000.);
75 }
76 
77 /** Get difference between two time structs in microseconds.
78  * The calculated time is t = a - b
79  * @param a time to subtract from
80  * @param b time to subtract
81  * @return difference between a and b in microseconds
82  */
83 inline long int
84 time_diff_usec(const timeval &a, const timeval &b)
85 {
86  return (a.tv_sec - b.tv_sec) * 1000000 + (a.tv_usec - b.tv_usec);
87 }
88 
89 class Clock;
90 
91 class Time
92 {
93  friend class Clock;
94  public:
95  Time();
96  Time(const timeval* tv);
97  Time(long sec, long usec, Clock *clock = 0);
98  Time(long ms);
99  Time(double sec);
100  Time(Clock *clock);
101  Time(const Time &t);
102  Time(const Time *t);
103  ~Time();
104 
105  double in_sec() const;
106  long in_msec() const;
107  long in_usec() const;
108 
109  const timeval * get_timeval() const { return &__time; }
110  long get_sec() const { return __time.tv_sec; }
111  long get_msec() const { return __time.tv_usec / 1000; }
112  long get_usec() const { return __time.tv_usec; }
113  long get_nsec() const { return __time.tv_usec * 1000; }
114  void get_timestamp(long &sec, long &usec) const
115  { sec = __time.tv_sec; usec = __time.tv_usec; }
116  bool is_zero() const { return (__time.tv_sec == 0) && (__time.tv_usec == 0); }
117 
118  void set_time(const timeval* tv);
119  void set_time(long int sec, long int usec);
120  void set_time(long ms);
121  void set_time(double sec);
122  void set_time(const Time &t);
123  void set_time(const Time *t);
124 
125  void set_clock(Clock *clock);
126 
127  void add(double seconds);
128 
129  Time & stamp();
130  Time & stamp_systime();
131 
132  Time operator+(const double sec) const;
133  Time operator+(const long int usec) const;
134  Time operator+(const Time& t) const;
135  Time operator+(const Time* t) const;
136  Time operator-(const Time& t) const;
137  double operator-(const Time* t) const;
138  Time operator-(const long int usec) const;
139  Time operator-(const double sec) const;
140  Time & operator+=(const long int usec);
141  Time & operator+=(const Time& t);
142  Time & operator+=(const double sec);
143  Time & operator-=(const Time& t);
144  Time & operator-=(const double sec);
145  Time & operator-=(const long int usec);
146  Time & operator=(const Time& t);
147  bool operator==(const Time& t) const;
148  bool operator==(const Time* t) const;
149  bool operator!=(const Time& t) const;
150  bool operator!=(const Time* t) const;
151  bool operator>(const Time& t) const;
152  bool operator>(const Time* t) const;
153  bool operator>=(const Time& t) const;
154  bool operator>=(const Time* t) const;
155  bool operator<(const Time& t) const;
156  bool operator<(const Time* t) const;
157  bool operator<=(const Time& t) const;
158  bool operator<=(const Time* t) const;
159 
160  void wait();
161  void wait_systime();
162 
163  const char * str(bool utc = false) const;
164  void str_r(char *s, bool utc = false);
165 
166  static const unsigned int TIMESTR_SIZE;
167 
168  private:
169  Clock *__clock;
170  timeval __time;
171  mutable char *__timestr;
172 };
173 
174 extern const Time TIME_MAX;
175 extern const Time TIME_MIN;
176 
177 } // end namespace fawkes
178 
179 #endif
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:166
Time & operator=(const Time &t)
Assign operator.
Definition: time.cpp:626
double in_sec() const
Convet time to seconds.
Definition: time.cpp:232
long int time_sec_to_usec(double sec)
Convert seconds to micro seconds.
Definition: time.h:72
Time & operator-=(const Time &t)
-= operator.
Definition: time.cpp:590
~Time()
Destructor.
Definition: time.cpp:219
const timeval * get_timeval() const
Obtain the timeval where the time is stored.
Definition: time.h:109
const char * str(bool utc=false) const
Output function.
Definition: time.cpp:872
void get_timestamp(long &sec, long &usec) const
Get time stamp.
Definition: time.h:114
Time & stamp_systime()
Set this time to the current system time.
Definition: time.cpp:800
bool operator<=(const Time &t) const
Less than or equal to operator.
Definition: time.cpp:760
Time()
Constructor.
Definition: time.cpp:98
Fawkes library namespace.
This is supposed to be the central clock in Fawkes.
Definition: clock.h:34
Time & operator+=(const long int usec)
+= operator
Definition: time.cpp:543
A class for handling time.
Definition: time.h:91
bool operator<(const Time &t) const
Less than operator.
Definition: time.cpp:736
void str_r(char *s, bool utc=false)
Output function.
Definition: time.cpp:901
bool operator==(const Time &t) const
Check equality of times.
Definition: time.cpp:640
void wait()
Wait (sleep) for this time.
Definition: time.cpp:817
void wait_systime()
Wait (sleep) for this system time.
Definition: time.cpp:842
Time operator-(const Time &t) const
Operator that substracts one Time from another.
Definition: time.cpp:437
const Time TIME_MIN
Instance of Time denoting the minimum value possible.
Definition: time.cpp:52
long in_msec() const
Convert the stored time into milli-seconds.
Definition: time.cpp:242
void add(double seconds)
Add seconds.
Definition: time.cpp:343
void set_clock(Clock *clock)
Set clock for this instance.
Definition: time.cpp:329
bool operator>=(const Time &t) const
Greater than or equal to operator.
Definition: time.cpp:712
double time_diff_sec(const timeval &a, const timeval &b)
Calculate time difference of two time structs.
Definition: time.h:40
long get_nsec() const
Get nanoseconds.
Definition: time.h:113
bool is_zero() const
Check if time is zero.
Definition: time.h:116
long int time_diff_usec(const timeval &a, const timeval &b)
Get difference between two time structs in microseconds.
Definition: time.h:84
long get_sec() const
Get seconds.
Definition: time.h:110
long get_usec() const
Get microseconds.
Definition: time.h:112
const Time TIME_MAX
Instance of Time denoting the maximum value possible.
Definition: time.cpp:47
void set_time(const timeval *tv)
Sets the time.
Definition: time.cpp:262
bool operator>(const Time &t) const
Greater than operator.
Definition: time.cpp:688
Time operator+(const double sec) const
Operator that adds times.
Definition: time.cpp:387
long in_usec() const
Convert the stored time into micro-seconds.
Definition: time.cpp:252
Time & stamp()
Set this time to the current time.
Definition: time.cpp:783
long get_msec() const
Get milliseconds.
Definition: time.h:111
bool operator!=(const Time &t) const
Check inequality of times.
Definition: time.cpp:664