Open Inventor Release 2024.2.1
 
Loading...
Searching...
No Matches
SbTime.h
1/*=======================================================================
2 * Copyright 1991-1996, Silicon Graphics, Inc.
3 * ALL RIGHTS RESERVED
4 *
5 * UNPUBLISHED -- Rights reserved under the copyright laws of the United
6 * States. Use of a copyright notice is precautionary only and does not
7 * imply publication or disclosure.
8 *
9 * U.S. GOVERNMENT RESTRICTED RIGHTS LEGEND:
10 * Use, duplication or disclosure by the Government is subject to restrictions
11 * as set forth in FAR 52.227.19(c)(2) or subparagraph (c)(1)(ii) of the Rights
12 * in Technical Data and Computer Software clause at DFARS 252.227-7013 and/or
13 * in similar or successor clauses in the FAR, or the DOD or NASA FAR
14 * Supplement. Contractor/manufacturer is Silicon Graphics, Inc.,
15 * 2011 N. Shoreline Blvd. Mountain View, CA 94039-7311.
16 *
17 * THE CONTENT OF THIS WORK CONTAINS CONFIDENTIAL AND PROPRIETARY
18 * INFORMATION OF SILICON GRAPHICS, INC. ANY DUPLICATION, MODIFICATION,
19 * DISTRIBUTION, OR DISCLOSURE IN ANY FORM, IN WHOLE, OR IN PART, IS STRICTLY
20 * PROHIBITED WITHOUT THE PRIOR EXPRESS WRITTEN PERMISSION OF SILICON
21 * GRAPHICS, INC.
22**=======================================================================*/
23/*=======================================================================
24** Author : Nick Thompson (MMM yyyy)
25**=======================================================================*/
26/*=======================================================================
27 *** THE CONTENT OF THIS WORK IS PROPRIETARY TO FEI S.A.S, (FEI S.A.S.), ***
28 *** AND IS DISTRIBUTED UNDER A LICENSE AGREEMENT. ***
29 *** ***
30 *** REPRODUCTION, DISCLOSURE, OR USE, IN WHOLE OR IN PART, OTHER THAN AS ***
31 *** SPECIFIED IN THE LICENSE ARE NOT TO BE UNDERTAKEN EXCEPT WITH PRIOR ***
32 *** WRITTEN AUTHORIZATION OF FEI S.A.S. ***
33 *** ***
34 *** RESTRICTED RIGHTS LEGEND ***
35 *** USE, DUPLICATION, OR DISCLOSURE BY THE GOVERNMENT OF THE CONTENT OF THIS ***
36 *** WORK OR RELATED DOCUMENTATION IS SUBJECT TO RESTRICTIONS AS SET FORTH IN ***
37 *** SUBPARAGRAPH (C)(1) OF THE COMMERCIAL COMPUTER SOFTWARE RESTRICTED RIGHT ***
38 *** CLAUSE AT FAR 52.227-19 OR SUBPARAGRAPH (C)(1)(II) OF THE RIGHTS IN ***
39 *** TECHNICAL DATA AND COMPUTER SOFTWARE CLAUSE AT DFARS 52.227-7013. ***
40 *** ***
41 *** COPYRIGHT (C) 1996-2014 BY FEI S.A.S, ***
42 *** BORDEAUX, FRANCE ***
43 *** ALL RIGHTS RESERVED ***
44**=======================================================================*/
45/*=======================================================================
46** Modified by : VSG (MMM YYYY)
47**=======================================================================*/
48
49
50#ifndef _SB_TIME_
51#define _SB_TIME_
52
53#include <Inventor/sys/port.h>
54
55#ifdef _WIN32
56# include <time.h>
57#else
58# include <sys/time.h>
59#endif
60
61#include <math.h>
62#include <limits.h>
63#include <Inventor/SbBase.h>
64#include <Inventor/SbString.h>
65
67//
68// Class: SbTime
69//
70// Representation of a time. Some parts are not adequately debugged:
71// for example, it is not clear when it is legal to have negative
72// values.
73//
75
91class SbTime {
92
93 public:
97 SbTime() {}
98
108 SbTime(double sec);
109
113 SbTime(time_t sec, long usec);
114
115 private:
116 // Constructor taking milliseconds
117 //
118 // NOTE! This constructor has been removed. Change existing uses of
119 // SbTime(msec)
120 // to
121 // time_t secs = msec / 1000;
122 // SbTime(secs, 1000 * (msec - 1000 * sec))
123 // The constructor was removed because it led to unexpected results --
124 // while SbTime(1.0) results in 1 second, SbTime(1) resulted in 1
125 // MILLIsecond). Its declaration has been kept, as "private", so that
126 // existing code using it will get compilation errors; if it was removed
127 // completely, an existing use of SbTime(1) would silently cast to
128 // SbTime(1.0) resulting in hard-to-find bugs. This declaration
129 // will be removed entirely in a future release, so that SbTime(1)
130 // will be equivalent to SbTime(1.0).
131 SbTime(uint32_t msec);
132
133 public:
137 SbTime(const struct timeval *tv);
138
143
148
152 static SbTime zero();
153
158 static void sleep(const int msec);
159
164 static void usleep(size_t usec);
165
166#if !defined(_WIN32)
171 static SbTime max();
172#endif
173
178 static SbTime maxTime();
179
183 void setValue(double sec);
184
188 void setValue(time_t sec, long usec);
189
193 void setValue(const struct timeval *tv);
194
198 void setMsecValue(unsigned long msec);
199
204 double getValue() const;
205
210 void getValue(time_t &sec, long &usec) const;
211
216 void getValue(struct timeval *tv) const;
217
221 unsigned long getMsecValue() const;
222
251 SbString format(const char *fmt = "%S.%i") const;
252
253#ifdef _WIN32
254# define DEFAULT_FORMAT_DATE "%#c"
255#else
256# define DEFAULT_FORMAT_DATE "%A, %D %r"
257#endif
263 SbString formatDate(const char *fmt = DEFAULT_FORMAT_DATE) const;
264
268 friend SbTime operator +(const SbTime &t0, const SbTime &t1);
269
273 friend SbTime operator -(const SbTime &t0, const SbTime &t1);
274
279
284
289
293 friend SbTime operator *(const SbTime &tm, double s);
297 friend SbTime operator *(double s, const SbTime &tm);
298
302 SbTime &operator *=(double s);
303
307 friend SbTime operator /(const SbTime &tm, double s);
308
312 SbTime & operator /=(double s);
313
317 double operator /(const SbTime &tm) const;
318
322 SbTime operator %(const SbTime &tm) const;
323
327 int operator ==(const SbTime &tm) const;
328
332 int operator !=(const SbTime &tm) const;
333
337 inline SbBool operator <(const SbTime &tm) const;
341 inline SbBool operator >(const SbTime &tm) const;
345 inline SbBool operator <=(const SbTime &tm) const;
349 inline SbBool operator >=(const SbTime &tm) const;
350
351 private:
352 struct timeval t;
353
354};
355
356inline SbBool
357SbTime::operator <(const SbTime &tm) const
358{
359 if ((t.tv_sec < tm.t.tv_sec) ||
360 (t.tv_sec == tm.t.tv_sec && t.tv_usec < tm.t.tv_usec))
361 return TRUE;
362 else
363 return FALSE;
364}
365
366inline SbBool
368{
369 if ((t.tv_sec > tm.t.tv_sec) ||
370 (t.tv_sec == tm.t.tv_sec && t.tv_usec > tm.t.tv_usec))
371 return TRUE;
372 else
373 return FALSE;
374}
375
376inline SbBool
377SbTime::operator <=(const SbTime &tm) const
378{
379 if ((t.tv_sec < tm.t.tv_sec) ||
380 (t.tv_sec == tm.t.tv_sec && t.tv_usec <= tm.t.tv_usec))
381 return TRUE;
382 else
383 return FALSE;
384}
385
386inline SbBool
388{
389 if ((t.tv_sec > tm.t.tv_sec) ||
390 (t.tv_sec == tm.t.tv_sec && t.tv_usec >= tm.t.tv_usec))
391 return TRUE;
392 else
393 return FALSE;
394}
395
396#endif /* _SB_TIME_ */
397
Class for smart character strings.
Definition SbString.h:202
Class for representation of a time.
Definition SbTime.h:91
void getValue(struct timeval *tv) const
Get time in a struct timeval.
SbBool operator>=(const SbTime &tm) const
Relational (greater than or equal to) operator.
Definition SbTime.h:387
static void usleep(size_t usec)
Sleep for specified time (in microsec).
SbTime & operator*=(double s)
Destructive multiplication by scalar.
SbBool operator<(const SbTime &tm) const
Relational (less than) operator.
Definition SbTime.h:357
SbString format(const char *fmt="%S.%i") const
Convert to a string.
SbTime(time_t sec, long usec)
Constructor taking seconds and microseconds.
SbTime & operator-=(const SbTime &tm)
Subtraction of two times which modifies the time structure.
static SbTime maxTime()
Get a time far, far into the future.
double getValue() const
Get time in seconds as a double.
int operator==(const SbTime &tm) const
Equality operator.
int operator!=(const SbTime &tm) const
Inequality operator.
SbTime()
Default constructor.
Definition SbTime.h:97
SbBool operator<=(const SbTime &tm) const
Relational (less than or equal to) operator.
Definition SbTime.h:377
SbTime & operator+=(const SbTime &tm)
Addition of two times which modifies the time structure.
void setMsecValue(unsigned long msec)
Set time from milliseconds.
unsigned long getMsecValue() const
Get time in milliseconds (for Xt).
static SbTime zero()
Get a zero time.
friend SbTime operator+(const SbTime &t0, const SbTime &t1)
Addition of two times.
friend SbTime operator/(const SbTime &tm, double s)
Division by scalar.
SbTime(double sec)
Constructor taking seconds.
SbBool operator>(const SbTime &tm) const
Relational (greater than) operator.
Definition SbTime.h:367
void setToTimeOfDay()
Set to the current time (seconds since Jan 1, 1970).
void setValue(const struct timeval *tv)
Set time from a struct timeval.
static SbTime max()
Deprecated.
SbTime & operator/=(double s)
Destructive division by scalar.
SbString formatDate(const char *fmt=DEFAULT_FORMAT_DATE) const
Convert to a date string, interpreting the time as seconds since Jan 1, 1970.
void setValue(double sec)
Set time from a double (in seconds).
SbTime(const struct timeval *tv)
Constructor taking a struct timeval.
void setValue(time_t sec, long usec)
Set time from seconds + microseconds.
SbTime operator%(const SbTime &tm) const
Modulus for two times (remainder when time1 is divided by time2).
static SbTime getTimeOfDay()
Get the current time (seconds since Jan 1, 1970).
void getValue(time_t &sec, long &usec) const
Get time in seconds and microseconds.
friend SbTime operator*(const SbTime &tm, double s)
Multiplication by scalar.
friend SbTime operator-(const SbTime &t0, const SbTime &t1)
Subtraction of two times.
static void sleep(const int msec)
Sleep for specified time (in msec).
int SbBool
Boolean type.
Definition SbBase.h:87