summaryrefslogtreecommitdiff
path: root/glib/glibmm/timeval.h
blob: 8897ee1cac26e69c5207827d8be329c6b43d1fe9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// -*- c++ -*-
#ifndef _GLIBMM_TIMEVAL_H
#define _GLIBMM_TIMEVAL_H

/* $Id$ */

/* timeval.h
 *
 * Copyright (C) 2002 The gtkmm Development Team
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <glib.h>
#include <glibmm/ustring.h>

namespace Glib
{

/** Glib::TimeVal is a wrapper around the glib structure GTimeVal.
 * The glib structure GTimeVal itself is equivalent to struct timeval,
 * which is returned by the gettimeofday() UNIX call. Additionally
 * this wrapper provides an assortment of time manipulation functions.
 */
struct TimeVal : public GTimeVal
{
  inline TimeVal();
  inline TimeVal(long seconds, long microseconds);

  inline TimeVal(const GTimeVal& gtimeval);
  inline TimeVal& operator=(const GTimeVal& gtimeval);

  /** Assigns the current time to the TimeVal instance.
   * Equivalent to the UNIX gettimeofday() function, but is portable and 
   * works also on Win32. 
   */
  void assign_current_time();
  
  /** Converts a string containing an ISO 8601 encoded date and time
   * to a Glib::TimeVal and puts it in TimeVal instance.
   * @param iso_date ISO 8601 encoded string.
   * @return <tt>true</tt> if conversion was successful.
   *
   * @newin{2,22}
   */
  bool assign_from_iso8601(const Glib::ustring& iso_date);

  void add(const TimeVal& rhs);
  void subtract(const TimeVal& rhs);
  void add_seconds(long seconds);
  void subtract_seconds(long seconds);
  void add_milliseconds(long milliseconds);
  void subtract_milliseconds(long milliseconds);
  void add_microseconds(long microseconds);
  void subtract_microseconds(long microseconds);

  inline TimeVal& operator+=(const TimeVal& gtimeval);
  inline TimeVal& operator-=(const TimeVal& gtimeval);
  inline TimeVal& operator+=(long seconds);
  inline TimeVal& operator-=(long seconds);

  /** Returns a double representation of the time interval.
   * This member function converts the time interval, that is
   * internally stored as two long values for seconds and microseconds,
   * to a double representation, whose unit is seconds.
   */
  inline double as_double() const;

  /** Returns an ISO 8601 encoded string, relative to the Coordinated
   * Universal Time (UTC).
   *
   * @newin{2,22}
   */
  Glib::ustring as_iso8601() const;

  inline bool negative() const;

  /** Checks whether the stored time interval is positive.
   * Returns true if the stored time / time interval is positive.
   */
  inline bool valid() const;
};

inline
TimeVal::TimeVal()
{
  tv_sec  = 0;
  tv_usec = 0;
}

inline
TimeVal::TimeVal(long seconds, long microseconds)
{
  tv_sec  = seconds;
  tv_usec = microseconds;
}

inline
TimeVal::TimeVal(const GTimeVal& gtimeval)
{
  tv_sec  = gtimeval.tv_sec;
  tv_usec = gtimeval.tv_usec;
}

inline
TimeVal& TimeVal::operator=(const GTimeVal& gtimeval)
{
  tv_sec  = gtimeval.tv_sec;
  tv_usec = gtimeval.tv_usec;
  return *this;
}

inline
TimeVal& TimeVal::operator+=(const TimeVal& gtimeval)
{
  add(gtimeval);

  return *this;
}

inline
TimeVal& TimeVal::operator-=(const TimeVal& gtimeval)
{
  subtract(gtimeval);

  return *this;
}

inline
TimeVal& TimeVal::operator+=(long seconds)
{
  add_seconds(seconds);

  return *this;
}

inline
TimeVal& TimeVal::operator-=(long seconds)
{
  subtract_seconds(seconds);

  return *this;
}

inline
double TimeVal::as_double() const
{
  return double(tv_sec) + double(tv_usec) / double(G_USEC_PER_SEC);
}

inline
bool TimeVal::negative() const
{
  return (tv_sec < 0);
}

inline
bool TimeVal::valid() const
{
  return (tv_usec >= 0 && tv_usec < G_USEC_PER_SEC);
}

/** @relates Glib::TimeVal */
inline
TimeVal operator+(const TimeVal& lhs, const TimeVal& rhs)
{ return TimeVal(lhs) += rhs; }

/** @relates Glib::TimeVal */
inline 
TimeVal operator+(const TimeVal& lhs, long seconds)
{ return TimeVal(lhs) += seconds; }

/** @relates Glib::TimeVal */
inline 
TimeVal operator-(const TimeVal& lhs, const TimeVal& rhs)
{ return TimeVal(lhs) -= rhs; }

/** @relates Glib::TimeVal */
inline 
TimeVal operator-(const TimeVal& lhs, long seconds)
{ return TimeVal(lhs) -= seconds; }


/** @relates Glib::TimeVal */
inline
bool operator==(const TimeVal& lhs, const TimeVal& rhs)
{
  return (lhs.tv_sec == rhs.tv_sec && lhs.tv_usec == rhs.tv_usec);
}

/** @relates Glib::TimeVal */
inline
bool operator!=(const TimeVal& lhs, const TimeVal& rhs)
{
  return (lhs.tv_sec != rhs.tv_sec || lhs.tv_usec != rhs.tv_usec);
}

/** @relates Glib::TimeVal */
inline
bool operator<(const TimeVal& lhs, const TimeVal& rhs)
{
  return ((lhs.tv_sec < rhs.tv_sec) ||
          (lhs.tv_sec == rhs.tv_sec && lhs.tv_usec < rhs.tv_usec));
}

/** @relates Glib::TimeVal */
inline
bool operator>(const TimeVal& lhs, const TimeVal& rhs)
{
  return ((lhs.tv_sec > rhs.tv_sec) ||
          (lhs.tv_sec == rhs.tv_sec && lhs.tv_usec > rhs.tv_usec));
}

/** @relates Glib::TimeVal */
inline
bool operator<=(const TimeVal& lhs, const TimeVal& rhs)
{
  return ((lhs.tv_sec < rhs.tv_sec) ||
          (lhs.tv_sec == rhs.tv_sec && lhs.tv_usec <= rhs.tv_usec));
}

/** @relates Glib::TimeVal */
inline
bool operator>=(const TimeVal& lhs, const TimeVal& rhs)
{
  return ((lhs.tv_sec > rhs.tv_sec) ||
          (lhs.tv_sec == rhs.tv_sec && lhs.tv_usec >= rhs.tv_usec));
}

} // namespace Glib


#endif /* _GLIBMM_TIMEVAL_H */