2 #ifndef _GLIBMM_TIMEVAL_H
3 #define _GLIBMM_TIMEVAL_H
5 /* $Id: timeval.h 2 2003-01-07 16:59:16Z murrayc $ */
9 * Copyright (C) 2002 The gtkmm Development Team
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Library General Public
13 * License as published by the Free Software Foundation; either
14 * version 2 of the License, or (at your option) any later version.
16 * This library 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 GNU
19 * Library General Public License for more details.
21 * You should have received a copy of the GNU Library General Public
22 * License along with this library; if not, write to the Free
23 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include <glib/gtypes.h>
27 #include <glib/gtimer.h>
32 /** Glib::TimeVal is a wrapper around the glib structure GTimeVal.
33 * The glib structure GTimeVal itself is equivalent to struct timeval,
34 * which is returned by the gettimeofday() UNIX call. Additionally
35 * this wrapper provides an assortment of time manipulation functions.
37 struct TimeVal : public GTimeVal
40 inline TimeVal(long seconds, long microseconds);
42 inline TimeVal(const GTimeVal& gtimeval);
43 inline TimeVal& operator=(const GTimeVal& gtimeval);
45 /** Assigns the current time to the TimeVal instance.
46 * Equivalent to the UNIX gettimeofday() function, but is portable and
47 * works also on Win32.
49 void assign_current_time();
51 void add(const TimeVal& rhs);
52 void subtract(const TimeVal& rhs);
53 void add_seconds(long seconds);
54 void subtract_seconds(long seconds);
55 void add_milliseconds(long milliseconds);
56 void subtract_milliseconds(long milliseconds);
57 void add_microseconds(long microseconds);
58 void subtract_microseconds(long microseconds);
60 inline TimeVal& operator+=(const TimeVal& gtimeval);
61 inline TimeVal& operator-=(const TimeVal& gtimeval);
62 inline TimeVal& operator+=(long seconds);
63 inline TimeVal& operator-=(long seconds);
65 /** Returns a double representation of the time interval.
66 * This member function converts the time interval, that is
67 * internally stored as two long values for seconds and microseconds,
68 * to a double representation, whose unit is seconds.
70 inline double as_double() const;
72 inline bool negative() const;
74 /** Checks whether the stored time interval is positive.
75 * Returns true if the stored time / time interval is positive.
77 inline bool valid() const;
88 TimeVal::TimeVal(long seconds, long microseconds)
91 tv_usec = microseconds;
95 TimeVal::TimeVal(const GTimeVal& gtimeval)
97 tv_sec = gtimeval.tv_sec;
98 tv_usec = gtimeval.tv_usec;
102 TimeVal& TimeVal::operator=(const GTimeVal& gtimeval)
104 tv_sec = gtimeval.tv_sec;
105 tv_usec = gtimeval.tv_usec;
110 TimeVal& TimeVal::operator+=(const TimeVal& gtimeval)
118 TimeVal& TimeVal::operator-=(const TimeVal& gtimeval)
126 TimeVal& TimeVal::operator+=(long seconds)
128 add_seconds(seconds);
134 TimeVal& TimeVal::operator-=(long seconds)
136 subtract_seconds(seconds);
142 double TimeVal::as_double() const
144 return tv_sec + ((double) tv_usec / (double) G_USEC_PER_SEC);
148 bool TimeVal::negative() const
154 bool TimeVal::valid() const
156 return (tv_usec >= 0 && tv_usec < G_USEC_PER_SEC);
159 /** @relates Glib::TimeVal */
161 TimeVal operator+(const TimeVal& lhs, const TimeVal& rhs)
162 { return TimeVal(lhs) += rhs; }
164 /** @relates Glib::TimeVal */
166 TimeVal operator+(const TimeVal& lhs, long seconds)
167 { return TimeVal(lhs) += seconds; }
169 /** @relates Glib::TimeVal */
171 TimeVal operator-(const TimeVal& lhs, const TimeVal& rhs)
172 { return TimeVal(lhs) -= rhs; }
174 /** @relates Glib::TimeVal */
176 TimeVal operator-(const TimeVal& lhs, long seconds)
177 { return TimeVal(lhs) -= seconds; }
180 /** @relates Glib::TimeVal */
182 bool operator==(const TimeVal& lhs, const TimeVal& rhs)
184 return (lhs.tv_sec == rhs.tv_sec && lhs.tv_usec == rhs.tv_usec);
187 /** @relates Glib::TimeVal */
189 bool operator!=(const TimeVal& lhs, const TimeVal& rhs)
191 return (lhs.tv_sec != rhs.tv_sec || lhs.tv_usec != rhs.tv_usec);
194 /** @relates Glib::TimeVal */
196 bool operator<(const TimeVal& lhs, const TimeVal& rhs)
198 return ((lhs.tv_sec < rhs.tv_sec) ||
199 (lhs.tv_sec == rhs.tv_sec && lhs.tv_usec < rhs.tv_usec));
202 /** @relates Glib::TimeVal */
204 bool operator>(const TimeVal& lhs, const TimeVal& rhs)
206 return ((lhs.tv_sec > rhs.tv_sec) ||
207 (lhs.tv_sec == rhs.tv_sec && lhs.tv_usec > rhs.tv_usec));
210 /** @relates Glib::TimeVal */
212 bool operator<=(const TimeVal& lhs, const TimeVal& rhs)
214 return ((lhs.tv_sec < rhs.tv_sec) ||
215 (lhs.tv_sec == rhs.tv_sec && lhs.tv_usec <= rhs.tv_usec));
218 /** @relates Glib::TimeVal */
220 bool operator>=(const TimeVal& lhs, const TimeVal& rhs)
222 return ((lhs.tv_sec > rhs.tv_sec) ||
223 (lhs.tv_sec == rhs.tv_sec && lhs.tv_usec >= rhs.tv_usec));
229 #endif /* _GLIBMM_TIMEVAL_H */