Open Inventor Reference
SbTime.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright (C) 2000 Silicon Graphics, Inc. All Rights Reserved.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * Further, this software is distributed without any warranty that it is
16  * free of the rightful claim of any third person regarding infringement
17  * or the like. Any license provided herein, whether implied or
18  * otherwise, applies only to this software file. Patent licenses, if
19  * any, provided herein do not apply to combinations of this program with
20  * other software, or any other product whatsoever.
21  *
22  * You should have received a copy of the GNU Lesser General Public
23  * License along with this library; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25  *
26  * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
27  * Mountain View, CA 94043, or:
28  *
29  * http://www.sgi.com
30  *
31  * For further information regarding this notice, see:
32  *
33  * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
34  *
35  */
36 
37 
38 /*
39  * Copyright (C) 1990,91 Silicon Graphics, Inc.
40  *
41  _______________________________________________________________________
42  ______________ S I L I C O N G R A P H I C S I N C . ____________
43  |
44  | $Revision: 1.3 $
45  |
46  | Description:
47  | This file defines the SbTime class for manipulating times
48  |
49  | Classes:
50  | SbTime
51  |
52  | Author(s) : Nick Thompson
53  |
54  ______________ S I L I C O N G R A P H I C S I N C . ____________
55  _______________________________________________________________________
56  */
57 
58 #ifndef _SB_TIME_
59 #define _SB_TIME_
60 
62 #ifdef WIN32
63 #include <sys/timeb.h>
64 #else
65 #include <sys/time.h>
66 #endif
67 #include <math.h>
68 #include <limits.h>
69 #include <Inventor/SbBasic.h>
70 #include <Inventor/SbString.h>
71 
72 
75 
88 
90  public:
91 
93  SbTime() {}
94 
96  SbTime(double sec);
97 
99  SbTime(time_t sec, long usec)
100  { t.tv_sec = static_cast<long>(sec); t.tv_usec = usec; }
101 
102  private:
118  SbTime(uint32_t msec);
119  public:
120 
122  SbTime(const struct timeval *tv)
123  { t.tv_sec = tv->tv_sec; t.tv_usec = tv->tv_usec; }
124 
127 
130 
132  static SbTime zero()
133  { return SbTime(0, 0); }
134 
135 #ifndef INT32_MAX
136 #define INT32_MAX INT_MAX
137 #endif // !INT32_MAX
138 
139 #ifdef WIN32
140 #undef max
141 #endif
142 
144  static SbTime max()
145  { return SbTime(INT32_MAX, 999999); }
146 
148  void setValue(double sec)
149 #ifdef __sgi
150  { t.tv_sec = time_t(trunc(sec));
151 #else
152  { t.tv_sec = time_t(int(sec));
153 #endif // __sgi
154  t.tv_usec = long((sec - t.tv_sec) * 1000000.0); }
155 
157  void setValue(time_t sec, long usec)
158  { t.tv_sec = static_cast<long>(sec); t.tv_usec = usec; }
159 
161  void setValue(const struct timeval *tv)
162  { t.tv_sec = tv->tv_sec; t.tv_usec = tv->tv_usec; }
163 
165  void setMsecValue(unsigned long msec) // System long
166  { t.tv_sec = time_t(msec/1000);
167  t.tv_usec = long(1000 * (msec % 1000)); }
168 
170  double getValue() const
171  { return (double) t.tv_sec + (double) t.tv_usec / 1000000.0; }
172 
174  void getValue(time_t &sec, long &usec) const
175  { sec = t.tv_sec; usec = t.tv_usec; }
176 
178  void getValue(struct timeval *tv) const
179  { tv->tv_sec = t.tv_sec; tv->tv_usec = t.tv_usec; }
180 
182  unsigned long getMsecValue() const // System long
183  { return t.tv_sec * 1000 + t.tv_usec / 1000; }
184 
211  SbString format(const char *fmt = "%S.%i") const;
212 
216  SbString formatDate(const char *fmt = "%A, %D %r") const;
217 
219  friend INVENTOR_API SbTime operator +(const SbTime &t0, const SbTime &t1);
220 
222  friend INVENTOR_API SbTime operator -(const SbTime &t0, const SbTime &t1);
223 
225  SbTime & operator +=(const SbTime &tm)
226  { return (*this = *this + tm); }
227 
229  SbTime & operator -=(const SbTime &tm)
230  { return (*this = *this - tm); }
231 
234  { return (t.tv_usec == 0) ? SbTime(- t.tv_sec, 0)
235  : SbTime(- t.tv_sec - 1, 1000000 - t.tv_usec); }
236 
238  friend INVENTOR_API SbTime operator *(const SbTime &tm, double s);
239 
240  friend INVENTOR_API SbTime operator *(double s, const SbTime &tm);
241 
243  SbTime & operator *=(double s)
244  { *this = *this * s; return *this; }
245 
247  friend INVENTOR_API SbTime operator /(const SbTime &tm, double s);
248 
250  SbTime & operator /=(double s)
251  { return (*this = *this / s); }
252 
254  double operator /(const SbTime &tm) const
255  { return getValue() / tm.getValue(); }
256 
258  SbTime operator %(const SbTime &tm) const
259  { return *this - tm * floor(*this / tm); }
260 
262  bool operator ==(const SbTime &tm) const
263  { return (t.tv_sec == tm.t.tv_sec) && (t.tv_usec == tm.t.tv_usec); }
264 
266  bool operator !=(const SbTime &tm) const
267  { return ! (*this == tm); }
268 
270  inline bool operator <(const SbTime &tm) const;
272  inline bool operator >(const SbTime &tm) const;
274  inline bool operator <=(const SbTime &tm) const;
276  inline bool operator >=(const SbTime &tm) const;
277 
278  private:
279  struct timeval t;
280 };
281 
284 INVENTOR_API SbTime operator *(const SbTime &tm, double s);
285 inline INVENTOR_API SbTime operator *(double s, const SbTime &tm)
286 { return tm * s; }
287 INVENTOR_API SbTime operator /(const SbTime &tm, double s);
288 
289 
290 inline bool
291 SbTime::operator <(const SbTime &tm) const
292 {
293  if ((t.tv_sec < tm.t.tv_sec) ||
294  (t.tv_sec == tm.t.tv_sec && t.tv_usec < tm.t.tv_usec))
295  return TRUE;
296  else
297  return FALSE;
298 }
299 
300 inline bool
301 SbTime::operator >(const SbTime &tm) const
302 {
303  if ((t.tv_sec > tm.t.tv_sec) ||
304  (t.tv_sec == tm.t.tv_sec && t.tv_usec > tm.t.tv_usec))
305  return TRUE;
306  else
307  return FALSE;
308 }
309 
310 inline bool
311 SbTime::operator <=(const SbTime &tm) const
312 {
313  if ((t.tv_sec < tm.t.tv_sec) ||
314  (t.tv_sec == tm.t.tv_sec && t.tv_usec <= tm.t.tv_usec))
315  return TRUE;
316  else
317  return FALSE;
318 }
319 
320 inline bool
321 SbTime::operator >=(const SbTime &tm) const
322 {
323  if ((t.tv_sec > tm.t.tv_sec) ||
324  (t.tv_sec == tm.t.tv_sec && t.tv_usec >= tm.t.tv_usec))
325  return TRUE;
326  else
327  return FALSE;
328 }
329 
330 
331 #endif /* _SB_TIME_ */
#define TRUE
Definition: SbBasic.h:76
#define FALSE
Definition: SbBasic.h:79
INVENTOR_API bool operator!=(const SbBox3f &b1, const SbBox3f &b2)
Definition: SbBox.h:207
INVENTOR_API bool operator==(const SbBox3f &b1, const SbBox3f &b2)
#define INVENTOR_API
Disable some annoying warnings on MSVC 6.
Definition: SbSystem.h:81
#define INT32_MAX
Definition: SbTime.h:136
INVENTOR_API SbTime operator*(const SbTime &tm, double s)
INVENTOR_API SbTime operator+(const SbTime &t0, const SbTime &t1)
INVENTOR_API SbTime operator-(const SbTime &t0, const SbTime &t1)
INVENTOR_API SbTime operator/(const SbTime &tm, double s)
unsigned int uint32_t
Definition: SbTypeDefs.h:44
Class for smart character strings.
Definition: SbString.h:85
Class for representation of a time.
Definition: SbTime.h:89
bool operator>=(const SbTime &tm) const
Relational operators.
Definition: SbTime.h:321
void getValue(struct timeval *tv) const
Get time in a struct timeval.
Definition: SbTime.h:178
bool operator>(const SbTime &tm) const
Relational operators.
Definition: SbTime.h:301
SbString format(const char *fmt="%S.%i") const
Convert to a string.
SbTime(time_t sec, long usec)
Constructor taking seconds + microseconds.
Definition: SbTime.h:99
double getValue() const
Get time in seconds as a double.
Definition: SbTime.h:170
SbTime()
Default constructor.
Definition: SbTime.h:93
void setMsecValue(unsigned long msec)
Set time from milliseconds.
Definition: SbTime.h:165
unsigned long getMsecValue() const
Get time in milliseconds (for Xt).
Definition: SbTime.h:182
static SbTime zero()
Get a zero time.
Definition: SbTime.h:132
SbTime(double sec)
Constructor taking seconds.
bool operator<(const SbTime &tm) const
Relational operators.
Definition: SbTime.h:291
void setToTimeOfDay()
Set to the current time (seconds since Jan 1, 1970).
void setValue(const struct timeval *tv)
Set time from a struct timeval.
Definition: SbTime.h:161
bool operator<=(const SbTime &tm) const
Relational operators.
Definition: SbTime.h:311
static SbTime max()
Get a time far, far into the future.
Definition: SbTime.h:144
void setValue(double sec)
Set time from a double (in seconds).
Definition: SbTime.h:148
SbTime(const struct timeval *tv)
Constructors taking timeval.
Definition: SbTime.h:122
void setValue(time_t sec, long usec)
Set time from seconds + microseconds.
Definition: SbTime.h:157
static SbTime getTimeOfDay()
Get the current time (seconds since Jan 1, 1970).
void getValue(time_t &sec, long &usec) const
Get time in seconds & microseconds.
Definition: SbTime.h:174
SbString formatDate(const char *fmt="%A, %D %r") const
Convert to a date string, interpreting the time as seconds since Jan 1, 1970.