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-2014 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 00091 class SbTime { 00092 00093 public: 00097 SbTime() {} 00098 00108 SbTime(double sec); 00109 00113 SbTime(time_t sec, long usec); 00114 00115 private: 00116 // Constructor taking milliseconds 00117 // 00118 // NOTE! This constructor has been removed. Change existing uses of 00119 // SbTime(msec) 00120 // to 00121 // time_t secs = msec / 1000; 00122 // SbTime(secs, 1000 * (msec - 1000 * sec)) 00123 // The constructor was removed because it led to unexpected results -- 00124 // while SbTime(1.0) results in 1 second, SbTime(1) resulted in 1 00125 // MILLIsecond). Its declaration has been kept, as "private", so that 00126 // existing code using it will get compilation errors; if it was removed 00127 // completely, an existing use of SbTime(1) would silently cast to 00128 // SbTime(1.0) resulting in hard-to-find bugs. This declaration 00129 // will be removed entirely in a future release, so that SbTime(1) 00130 // will be equivalent to SbTime(1.0). 00131 SbTime(uint32_t msec); 00132 00133 public: 00137 SbTime(const struct timeval *tv); 00138 00142 static SbTime getTimeOfDay(); 00143 00147 void setToTimeOfDay(); 00148 00152 static SbTime zero(); 00153 00158 static void sleep(const int msec); 00159 00164 static void usleep(size_t usec); 00165 00166 #if !defined(_WIN32) 00167 00171 static SbTime max(); 00172 #endif 00173 00178 static SbTime maxTime(); 00179 00183 void setValue(double sec); 00184 00188 void setValue(time_t sec, long usec); 00189 00193 void setValue(const struct timeval *tv); 00194 00198 void setMsecValue(unsigned long msec); 00199 00204 double getValue() const; 00205 00210 void getValue(time_t &sec, long &usec) const; 00211 00216 void getValue(struct timeval *tv) const; 00217 00221 unsigned long getMsecValue() const; 00222 00251 SbString format(const char *fmt = "%S.%i") const; 00252 00253 #ifdef _WIN32 00254 # define DEFAULT_FORMAT_DATE "%#c" 00255 #else 00256 # define DEFAULT_FORMAT_DATE "%A, %D %r" 00257 #endif 00258 00263 SbString formatDate(const char *fmt = DEFAULT_FORMAT_DATE) const; 00264 00268 friend SbTime operator +(const SbTime &t0, const SbTime &t1); 00269 00273 friend SbTime operator -(const SbTime &t0, const SbTime &t1); 00274 00278 SbTime &operator +=(const SbTime &tm); 00279 00283 SbTime &operator -=(const SbTime &tm); 00284 00288 SbTime operator -() const; 00289 00293 friend SbTime operator *(const SbTime &tm, double s); 00297 friend SbTime operator *(double s, const SbTime &tm); 00298 00302 SbTime &operator *=(double s); 00303 00307 friend SbTime operator /(const SbTime &tm, double s); 00308 00312 SbTime & operator /=(double s); 00313 00317 double operator /(const SbTime &tm) const; 00318 00322 SbTime operator %(const SbTime &tm) const; 00323 00327 int operator ==(const SbTime &tm) const; 00328 00332 int operator !=(const SbTime &tm) const; 00333 00337 inline SbBool operator <(const SbTime &tm) const; 00341 inline SbBool operator >(const SbTime &tm) const; 00345 inline SbBool operator <=(const SbTime &tm) const; 00349 inline SbBool operator >=(const SbTime &tm) const; 00350 00351 private: 00352 struct timeval t; 00353 00354 }; 00355 00356 inline SbBool 00357 SbTime::operator <(const SbTime &tm) const 00358 { 00359 if ((t.tv_sec < tm.t.tv_sec) || 00360 (t.tv_sec == tm.t.tv_sec && t.tv_usec < tm.t.tv_usec)) 00361 return TRUE; 00362 else 00363 return FALSE; 00364 } 00365 00366 inline SbBool 00367 SbTime::operator >(const SbTime &tm) const 00368 { 00369 if ((t.tv_sec > tm.t.tv_sec) || 00370 (t.tv_sec == tm.t.tv_sec && t.tv_usec > tm.t.tv_usec)) 00371 return TRUE; 00372 else 00373 return FALSE; 00374 } 00375 00376 inline SbBool 00377 SbTime::operator <=(const SbTime &tm) const 00378 { 00379 if ((t.tv_sec < tm.t.tv_sec) || 00380 (t.tv_sec == tm.t.tv_sec && t.tv_usec <= tm.t.tv_usec)) 00381 return TRUE; 00382 else 00383 return FALSE; 00384 } 00385 00386 inline SbBool 00387 SbTime::operator >=(const SbTime &tm) const 00388 { 00389 if ((t.tv_sec > tm.t.tv_sec) || 00390 (t.tv_sec == tm.t.tv_sec && t.tv_usec >= tm.t.tv_usec)) 00391 return TRUE; 00392 else 00393 return FALSE; 00394 } 00395 00396 #endif /* _SB_TIME_ */ 00397 00398