1 #ifndef _GLIBMM_TIMEVAL_H
2 #define _GLIBMM_TIMEVAL_H
3
4 /* timeval.h
5 *
6 * Copyright (C) 2002 The gtkmm Development Team
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include <glib.h>
23 #include <glibmm/ustring.h>
24
25 namespace Glib
26 {
27
28 // GTimeVal is deprecated.
29 // The deprecated GTimeVal is used in the non-deprecated gdk_pixbuf_animation_iter_advance().
30 // That's why not all of struct Glib::TimeVal is surrounded by
31 // #ifndef GLIBMM_DISABLE_DEPRECATED/#endif. It would result in compilation errors,
32 // if you define GLIBMM_DISABLE_DEPRECATED and include gdkmm/pixbufanimationiter.h.
33 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
34 /** Glib::TimeVal is a wrapper around the glib structure GTimeVal.
35 * The glib structure GTimeVal itself is equivalent to struct timeval,
36 * which is returned by the gettimeofday() UNIX call. Additionally
37 * this wrapper provides an assortment of time manipulation functions.
38 *
39 * @deprecated Use Glib::DateTime instead.
40 */
41 struct GLIBMM_API TimeVal : public GTimeVal
42 {
43 inline TimeVal();
44 inline TimeVal(long seconds, long microseconds);
45
46 inline TimeVal(const GTimeVal& gtimeval);
47 inline TimeVal& operator=(const GTimeVal& gtimeval);
48
49 #ifndef GLIBMM_DISABLE_DEPRECATED
50 /** Assigns the current time to the TimeVal instance.
51 * Equivalent to the UNIX gettimeofday() function, but is portable and
52 * works also on Win32.
53 */
54 void assign_current_time();
55
56 /** Converts a string containing an ISO 8601 encoded date and time
57 * to a Glib::TimeVal and puts it in TimeVal instance.
58 * @param iso_date ISO 8601 encoded string.
59 * @return <tt>true</tt> if conversion was successful.
60 *
61 * @newin{2,22}
62 */
63 bool assign_from_iso8601(const Glib::ustring& iso_date);
64
65 void add(const TimeVal& rhs);
66 void subtract(const TimeVal& rhs);
67 void add_seconds(long seconds);
68 void subtract_seconds(long seconds);
69 void add_milliseconds(long milliseconds);
70 void subtract_milliseconds(long milliseconds);
71 void add_microseconds(long microseconds);
72 void subtract_microseconds(long microseconds);
73
74 inline TimeVal& operator+=(const TimeVal& gtimeval);
75 inline TimeVal& operator-=(const TimeVal& gtimeval);
76 inline TimeVal& operator+=(long seconds);
77 inline TimeVal& operator-=(long seconds);
78
79 /** Returns a double representation of the time interval.
80 * This member function converts the time interval, that is
81 * internally stored as two long values for seconds and microseconds,
82 * to a double representation, whose unit is seconds.
83 */
84 inline double as_double() const;
85
86 /** Returns an ISO 8601 encoded string, relative to the Coordinated
87 * Universal Time (UTC).
88 *
89 * @newin{2,22}
90 */
91 Glib::ustring as_iso8601() const;
92
93 inline bool negative() const;
94
95 /** Checks whether the stored time interval is positive.
96 * Returns true if the stored time / time interval is positive.
97 */
98 inline bool valid() const;
99 #endif // GLIBMM_DISABLE_DEPRECATED
100 };
101
TimeVal()102 inline TimeVal::TimeVal()
103 {
104 tv_sec = 0;
105 tv_usec = 0;
106 }
107
TimeVal(long seconds,long microseconds)108 inline TimeVal::TimeVal(long seconds, long microseconds)
109 {
110 tv_sec = seconds;
111 tv_usec = microseconds;
112 }
113
TimeVal(const GTimeVal & gtimeval)114 inline TimeVal::TimeVal(const GTimeVal& gtimeval)
115 {
116 tv_sec = gtimeval.tv_sec;
117 tv_usec = gtimeval.tv_usec;
118 }
119
120 inline TimeVal&
121 TimeVal::operator=(const GTimeVal& gtimeval)
122 {
123 tv_sec = gtimeval.tv_sec;
124 tv_usec = gtimeval.tv_usec;
125 return *this;
126 }
127
128 #ifndef GLIBMM_DISABLE_DEPRECATED
129 inline TimeVal&
130 TimeVal::operator+=(const TimeVal& gtimeval)
131 {
132 add(gtimeval);
133
134 return *this;
135 }
136
137 inline TimeVal&
138 TimeVal::operator-=(const TimeVal& gtimeval)
139 {
140 subtract(gtimeval);
141
142 return *this;
143 }
144
145 inline TimeVal&
146 TimeVal::operator+=(long seconds)
147 {
148 add_seconds(seconds);
149
150 return *this;
151 }
152
153 inline TimeVal&
154 TimeVal::operator-=(long seconds)
155 {
156 subtract_seconds(seconds);
157
158 return *this;
159 }
160
161 inline double
as_double()162 TimeVal::as_double() const
163 {
164 return double(tv_sec) + double(tv_usec) / double(G_USEC_PER_SEC);
165 }
166
167 inline bool
negative()168 TimeVal::negative() const
169 {
170 return (tv_sec < 0);
171 }
172
173 inline bool
valid()174 TimeVal::valid() const
175 {
176 return (tv_usec >= 0 && tv_usec < G_USEC_PER_SEC);
177 }
178
179 /** @relates Glib::TimeVal */
180 inline TimeVal
181 operator+(const TimeVal& lhs, const TimeVal& rhs)
182 {
183 return TimeVal(lhs) += rhs;
184 }
185
186 /** @relates Glib::TimeVal */
187 inline TimeVal
188 operator+(const TimeVal& lhs, long seconds)
189 {
190 return TimeVal(lhs) += seconds;
191 }
192
193 /** @relates Glib::TimeVal */
194 inline TimeVal
195 operator-(const TimeVal& lhs, const TimeVal& rhs)
196 {
197 return TimeVal(lhs) -= rhs;
198 }
199
200 /** @relates Glib::TimeVal */
201 inline TimeVal
202 operator-(const TimeVal& lhs, long seconds)
203 {
204 return TimeVal(lhs) -= seconds;
205 }
206
207 /** @relates Glib::TimeVal */
208 inline bool
209 operator==(const TimeVal& lhs, const TimeVal& rhs)
210 {
211 return (lhs.tv_sec == rhs.tv_sec && lhs.tv_usec == rhs.tv_usec);
212 }
213
214 /** @relates Glib::TimeVal */
215 inline bool
216 operator!=(const TimeVal& lhs, const TimeVal& rhs)
217 {
218 return (lhs.tv_sec != rhs.tv_sec || lhs.tv_usec != rhs.tv_usec);
219 }
220
221 /** @relates Glib::TimeVal */
222 inline bool
223 operator<(const TimeVal& lhs, const TimeVal& rhs)
224 {
225 return ((lhs.tv_sec < rhs.tv_sec) || (lhs.tv_sec == rhs.tv_sec && lhs.tv_usec < rhs.tv_usec));
226 }
227
228 /** @relates Glib::TimeVal */
229 inline bool
230 operator>(const TimeVal& lhs, const TimeVal& rhs)
231 {
232 return ((lhs.tv_sec > rhs.tv_sec) || (lhs.tv_sec == rhs.tv_sec && lhs.tv_usec > rhs.tv_usec));
233 }
234
235 /** @relates Glib::TimeVal */
236 inline bool
237 operator<=(const TimeVal& lhs, const TimeVal& rhs)
238 {
239 return ((lhs.tv_sec < rhs.tv_sec) || (lhs.tv_sec == rhs.tv_sec && lhs.tv_usec <= rhs.tv_usec));
240 }
241
242 /** @relates Glib::TimeVal */
243 inline bool
244 operator>=(const TimeVal& lhs, const TimeVal& rhs)
245 {
246 return ((lhs.tv_sec > rhs.tv_sec) || (lhs.tv_sec == rhs.tv_sec && lhs.tv_usec >= rhs.tv_usec));
247 }
248 #endif // GLIBMM_DISABLE_DEPRECATED
249 G_GNUC_END_IGNORE_DEPRECATIONS
250
251 } // namespace Glib
252
253 #endif /* _GLIBMM_TIMEVAL_H */
254