00001 /*======================================================================= 00002 * Copyright 1991-1996, Silicon Graphics, Inc. 00003 * ALL RIGHTS RESERVED 00004 * 00005 * UNPUBLISHED -- Rights reserved under the copyright laws of the United 00006 * States. Use of a copyright notice is precautionary only and does not 00007 * imply publication or disclosure. 00008 * 00009 * U.S. GOVERNMENT RESTRICTED RIGHTS LEGEND: 00010 * Use, duplication or disclosure by the Government is subject to restrictions 00011 * as set forth in FAR 52.227.19(c)(2) or subparagraph (c)(1)(ii) of the Rights 00012 * in Technical Data and Computer Software clause at DFARS 252.227-7013 and/or 00013 * in similar or successor clauses in the FAR, or the DOD or NASA FAR 00014 * Supplement. Contractor/manufacturer is Silicon Graphics, Inc., 00015 * 2011 N. Shoreline Blvd. Mountain View, CA 94039-7311. 00016 * 00017 * THE CONTENT OF THIS WORK CONTAINS CONFIDENTIAL AND PROPRIETARY 00018 * INFORMATION OF SILICON GRAPHICS, INC. ANY DUPLICATION, MODIFICATION, 00019 * DISTRIBUTION, OR DISCLOSURE IN ANY FORM, IN WHOLE, OR IN PART, IS STRICTLY 00020 * PROHIBITED WITHOUT THE PRIOR EXPRESS WRITTEN PERMISSION OF SILICON 00021 * GRAPHICS, INC. 00022 **=======================================================================*/ 00023 /*======================================================================= 00024 ** Author : Nick Thompson (MMM yyyy) 00025 **=======================================================================*/ 00026 /*======================================================================= 00027 *** THE CONTENT OF THIS WORK IS PROPRIETARY TO FEI S.A.S, (FEI S.A.S.), *** 00028 *** AND IS DISTRIBUTED UNDER A LICENSE AGREEMENT. *** 00029 *** *** 00030 *** REPRODUCTION, DISCLOSURE, OR USE, IN WHOLE OR IN PART, OTHER THAN AS *** 00031 *** SPECIFIED IN THE LICENSE ARE NOT TO BE UNDERTAKEN EXCEPT WITH PRIOR *** 00032 *** WRITTEN AUTHORIZATION OF FEI S.A.S. *** 00033 *** *** 00034 *** RESTRICTED RIGHTS LEGEND *** 00035 *** USE, DUPLICATION, OR DISCLOSURE BY THE GOVERNMENT OF THE CONTENT OF THIS *** 00036 *** WORK OR RELATED DOCUMENTATION IS SUBJECT TO RESTRICTIONS AS SET FORTH IN *** 00037 *** SUBPARAGRAPH (C)(1) OF THE COMMERCIAL COMPUTER SOFTWARE RESTRICTED RIGHT *** 00038 *** CLAUSE AT FAR 52.227-19 OR SUBPARAGRAPH (C)(1)(II) OF THE RIGHTS IN *** 00039 *** TECHNICAL DATA AND COMPUTER SOFTWARE CLAUSE AT DFARS 52.227-7013. *** 00040 *** *** 00041 *** COPYRIGHT (C) 1996-2020 BY FEI S.A.S, *** 00042 *** BORDEAUX, FRANCE *** 00043 *** ALL RIGHTS RESERVED *** 00044 **=======================================================================*/ 00045 /*======================================================================= 00046 ** Modified by : VSG (MMM YYYY) 00047 **=======================================================================*/ 00048 00049 00050 #ifndef _SB_TIME_ 00051 #define _SB_TIME_ 00052 00053 #include <Inventor/sys/port.h> 00054 00055 #ifdef _WIN32 00056 # include <time.h> 00057 #else 00058 # include <sys/time.h> 00059 #endif 00060 00061 #include <math.h> 00062 #include <limits.h> 00063 #include <Inventor/SbBase.h> 00064 #include <Inventor/SbString.h> 00065 00067 // 00068 // Class: SbTime 00069 // 00070 // Representation of a time. Some parts are not adequately debugged: 00071 // for example, it is not clear when it is legal to have negative 00072 // values. 00073 // 00075 00092 class SbTime { 00093 00094 public: 00098 SbTime() {} 00099 00109 SbTime(double sec); 00110 00114 SbTime(time_t sec, long usec); 00115 00116 private: 00117 // Constructor taking milliseconds 00118 // 00119 // NOTE! This constructor has been removed. Change existing uses of 00120 // SbTime(msec) 00121 // to 00122 // time_t secs = msec / 1000; 00123 // SbTime(secs, 1000 * (msec - 1000 * sec)) 00124 // The constructor was removed because it led to unexpected results -- 00125 // while SbTime(1.0) results in 1 second, SbTime(1) resulted in 1 00126 // MILLIsecond). Its declaration has been kept, as "private", so that 00127 // existing code using it will get compilation errors; if it was removed 00128 // completely, an existing use of SbTime(1) would silently cast to 00129 // SbTime(1.0) resulting in hard-to-find bugs. This declaration 00130 // will be removed entirely in a future release, so that SbTime(1) 00131 // will be equivalent to SbTime(1.0). 00132 SbTime(uint32_t msec); 00133 00134 public: 00138 SbTime(const struct timeval *tv); 00139 00143 static SbTime getTimeOfDay(); 00144 00148 void setToTimeOfDay(); 00149 00153 static SbTime zero(); 00154 00159 static void sleep(const int msec); 00160 00165 static void usleep(size_t usec); 00166 00167 #if !defined(_WIN32) && !defined(__NUTC__) 00168 00172 static SbTime max(); 00173 #endif 00174 00179 static SbTime maxTime(); 00180 00184 void setValue(double sec); 00185 00189 void setValue(time_t sec, long usec); 00190 00194 void setValue(const struct timeval *tv); 00195 00199 void setMsecValue(unsigned long msec); 00200 00205 double getValue() const; 00206 00211 void getValue(time_t &sec, long &usec) const; 00212 00217 void getValue(struct timeval *tv) const; 00218 00222 unsigned long getMsecValue() const; 00223 00252 SbString format(const char *fmt = "%S.%i") const; 00253 00254 #ifdef _WIN32 00255 # define DEFAULT_FORMAT_DATE "%#c" 00256 #else 00257 # define DEFAULT_FORMAT_DATE "%A, %D %r" 00258 #endif 00259 00264 SbString formatDate(const char *fmt = DEFAULT_FORMAT_DATE) const; 00265 00269 friend SbTime operator +(const SbTime &t0, const SbTime &t1); 00270 00274 friend SbTime operator -(const SbTime &t0, const SbTime &t1); 00275 00279 SbTime &operator +=(const SbTime &tm); 00280 00284 SbTime &operator -=(const SbTime &tm); 00285 00289 SbTime operator -() const; 00290 00294 friend SbTime operator *(const SbTime &tm, double s); 00298 friend SbTime operator *(double s, const SbTime &tm); 00299 00303 SbTime &operator *=(double s); 00304 00308 friend SbTime operator /(const SbTime &tm, double s); 00309 00313 SbTime & operator /=(double s); 00314 00318 double operator /(const SbTime &tm) const; 00319 00323 SbTime operator %(const SbTime &tm) const; 00324 00328 int operator ==(const SbTime &tm) const; 00329 00333 int operator !=(const SbTime &tm) const; 00334 00338 inline SbBool operator <(const SbTime &tm) const; 00342 inline SbBool operator >(const SbTime &tm) const; 00346 inline SbBool operator <=(const SbTime &tm) const; 00350 inline SbBool operator >=(const SbTime &tm) const; 00351 00352 private: 00353 struct timeval t; 00354 00355 }; 00356 00357 inline SbBool 00358 SbTime::operator <(const SbTime &tm) const 00359 { 00360 if ((t.tv_sec < tm.t.tv_sec) || 00361 (t.tv_sec == tm.t.tv_sec && t.tv_usec < tm.t.tv_usec)) 00362 return TRUE; 00363 else 00364 return FALSE; 00365 } 00366 00367 inline SbBool 00368 SbTime::operator >(const SbTime &tm) const 00369 { 00370 if ((t.tv_sec > tm.t.tv_sec) || 00371 (t.tv_sec == tm.t.tv_sec && t.tv_usec > tm.t.tv_usec)) 00372 return TRUE; 00373 else 00374 return FALSE; 00375 } 00376 00377 inline SbBool 00378 SbTime::operator <=(const SbTime &tm) const 00379 { 00380 if ((t.tv_sec < tm.t.tv_sec) || 00381 (t.tv_sec == tm.t.tv_sec && t.tv_usec <= tm.t.tv_usec)) 00382 return TRUE; 00383 else 00384 return FALSE; 00385 } 00386 00387 inline SbBool 00388 SbTime::operator >=(const SbTime &tm) const 00389 { 00390 if ((t.tv_sec > tm.t.tv_sec) || 00391 (t.tv_sec == tm.t.tv_sec && t.tv_usec >= tm.t.tv_usec)) 00392 return TRUE; 00393 else 00394 return FALSE; 00395 } 00396 00397 #endif /* _SB_TIME_ */ 00398 00399