Open Inventor Reference
MeVis/ThirdParty/Sources/Inventor/inventor/lib/database/include/Inventor/SbTime.h
Go to the documentation of this file.
00001 /*
00002  *
00003  *  Copyright (C) 2000 Silicon Graphics, Inc.  All Rights Reserved.
00004  *
00005  *  This library is free software; you can redistribute it and/or
00006  *  modify it under the terms of the GNU Lesser General Public
00007  *  License as published by the Free Software Foundation; either
00008  *  version 2.1 of the License, or (at your option) any later version.
00009  *
00010  *  This library is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  *  Lesser General Public License for more details.
00014  *
00015  *  Further, this software is distributed without any warranty that it is
00016  *  free of the rightful claim of any third person regarding infringement
00017  *  or the like.  Any license provided herein, whether implied or
00018  *  otherwise, applies only to this software file.  Patent licenses, if
00019  *  any, provided herein do not apply to combinations of this program with
00020  *  other software, or any other product whatsoever.
00021  *
00022  *  You should have received a copy of the GNU Lesser General Public
00023  *  License along with this library; if not, write to the Free Software
00024  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00025  *
00026  *  Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
00027  *  Mountain View, CA  94043, or:
00028  *
00029  *  http://www.sgi.com
00030  *
00031  *  For further information regarding this notice, see:
00032  *
00033  *  http://oss.sgi.com/projects/GenInfo/NoticeExplan/
00034  *
00035  */
00036 
00037 
00038 /*
00039  * Copyright (C) 1990,91   Silicon Graphics, Inc.
00040  *
00041  _______________________________________________________________________
00042  ______________  S I L I C O N   G R A P H I C S   I N C .  ____________
00043  |
00044  |   $Revision: 1.3 $
00045  |
00046  |   Description:
00047  |      This file defines the SbTime class for manipulating times
00048  |
00049  |   Classes:
00050  |      SbTime
00051  |
00052  |   Author(s)          : Nick Thompson
00053  |
00054  ______________  S I L I C O N   G R A P H I C S   I N C .  ____________
00055  _______________________________________________________________________
00056  */
00057 
00058 #ifndef _SB_TIME_
00059 #define _SB_TIME_
00060 
00061 #include <Inventor/system/SbSystem.h>
00062 #ifdef WIN32
00063 #include <sys/timeb.h>
00064 #else
00065 #include <sys/time.h>
00066 #endif
00067 #include <math.h>
00068 #include <limits.h>
00069 #include <Inventor/SbBasic.h>
00070 #include <Inventor/SbString.h>
00071 
00072 
00075 
00087 
00088 
00089 class INVENTOR_API SbTime {
00090   public:
00091 
00093     SbTime()                                    {}
00094 
00096     SbTime(double sec);
00097 
00099     SbTime(time_t sec, long usec)               
00100         { t.tv_sec = sec; t.tv_usec = usec; }
00101 
00102   private:
00118     SbTime(uint32_t msec);
00119   public:
00120 
00122     SbTime(const struct timeval *tv)
00123         { t.tv_sec = tv->tv_sec; t.tv_usec = tv->tv_usec; }
00124 
00126     static SbTime               getTimeOfDay();
00127 
00129     void                        setToTimeOfDay();
00130 
00132     static SbTime               zero()
00133         { return SbTime(0, 0); }
00134 
00135 #ifndef INT32_MAX
00136 #define INT32_MAX INT_MAX
00137 #endif // !INT32_MAX
00138 
00139 #ifdef WIN32
00140 #undef max
00141 #endif
00142 
00144     static SbTime               max()
00145         { return SbTime(INT32_MAX, 999999); }
00146 
00148     void                setValue(double sec)
00149 #ifdef __sgi
00150         { t.tv_sec = time_t(trunc(sec));
00151 #else
00152         { t.tv_sec = time_t(int(sec));
00153 #endif // __sgi
00154           t.tv_usec = long((sec - t.tv_sec) * 1000000.0); }
00155 
00157     void                setValue(time_t sec, long usec)         
00158         { t.tv_sec = sec; t.tv_usec = usec; }
00159 
00161     void                setValue(const struct timeval *tv)
00162         { t.tv_sec = tv->tv_sec; t.tv_usec = tv->tv_usec; }
00163 
00165     void                setMsecValue(unsigned long msec)        // System long
00166         { t.tv_sec = time_t(msec/1000);
00167           t.tv_usec = long(1000 * (msec % 1000)); }
00168 
00170     double              getValue() const
00171         { return (double) t.tv_sec + (double) t.tv_usec / 1000000.0; }
00172 
00174     void                getValue(time_t &sec, long &usec) const  
00175         { sec = t.tv_sec; usec = t.tv_usec; }
00176 
00178     void                getValue(struct timeval *tv) const
00179         { tv->tv_sec = t.tv_sec; tv->tv_usec = t.tv_usec; }
00180 
00182     unsigned long       getMsecValue() const                    // System long
00183         { return t.tv_sec * 1000 + t.tv_usec / 1000; }
00184 
00211     SbString                    format(const char *fmt = "%S.%i") const;
00212 
00216     SbString                    formatDate(const char *fmt = "%A, %D %r") const;
00217 
00219     friend INVENTOR_API SbTime          operator +(const SbTime &t0, const SbTime &t1);
00220 
00222     friend INVENTOR_API SbTime          operator -(const SbTime &t0, const SbTime &t1);
00223 
00225     SbTime &                    operator +=(const SbTime &tm)
00226         { return (*this = *this + tm); }
00227 
00229     SbTime &                    operator -=(const SbTime &tm)
00230         { return (*this = *this - tm); }
00231 
00233     SbTime                      operator -() const
00234         { return (t.tv_usec == 0) ? SbTime(- t.tv_sec, 0)
00235               : SbTime(- t.tv_sec - 1, 1000000 - t.tv_usec); }
00236 
00238     friend INVENTOR_API SbTime          operator *(const SbTime &tm, double s);
00239 
00240     friend INVENTOR_API SbTime          operator *(double s, const SbTime &tm)
00241         { return tm * s; }
00242 
00244     SbTime &                    operator *=(double s)
00245         { *this = *this * s; return *this; }
00246 
00248     friend INVENTOR_API SbTime          operator /(const SbTime &tm, double s);
00249 
00251     SbTime &                    operator /=(double s)
00252         { return (*this = *this / s); }
00253 
00255     double                      operator /(const SbTime &tm) const
00256         { return getValue() / tm.getValue(); }
00257 
00259     SbTime                      operator %(const SbTime &tm) const
00260         { return *this - tm * floor(*this / tm); }
00261 
00263     int                         operator ==(const SbTime &tm) const
00264         { return (t.tv_sec == tm.t.tv_sec) && (t.tv_usec == tm.t.tv_usec); }
00265 
00267     int                         operator !=(const SbTime &tm) const
00268         { return ! (*this == tm); }
00269 
00271     inline SbBool               operator <(const SbTime &tm) const;
00273     inline SbBool               operator >(const SbTime &tm) const;
00275     inline SbBool               operator <=(const SbTime &tm) const;
00277     inline SbBool               operator >=(const SbTime &tm) const;
00278 
00279   private:
00280     struct timeval              t;
00281 };
00282 
00283 
00284 
00285 inline SbBool
00286 SbTime::operator <(const SbTime &tm) const
00287 {
00288     if ((t.tv_sec < tm.t.tv_sec) ||
00289         (t.tv_sec == tm.t.tv_sec && t.tv_usec < tm.t.tv_usec))
00290         return TRUE;
00291     else
00292         return FALSE;
00293 }
00294 
00295 inline SbBool
00296 SbTime::operator >(const SbTime &tm) const
00297 {
00298     if ((t.tv_sec > tm.t.tv_sec) ||
00299         (t.tv_sec == tm.t.tv_sec && t.tv_usec > tm.t.tv_usec))
00300         return TRUE;
00301     else
00302         return FALSE;
00303 }
00304 
00305 inline SbBool
00306 SbTime::operator <=(const SbTime &tm) const
00307 {
00308     if ((t.tv_sec < tm.t.tv_sec) ||
00309         (t.tv_sec == tm.t.tv_sec && t.tv_usec <= tm.t.tv_usec))
00310         return TRUE;
00311     else
00312         return FALSE;
00313 }
00314 
00315 inline SbBool
00316 SbTime::operator >=(const SbTime &tm) const
00317 {
00318     if ((t.tv_sec > tm.t.tv_sec) ||
00319         (t.tv_sec == tm.t.tv_sec && t.tv_usec >= tm.t.tv_usec))
00320         return TRUE;
00321     else
00322         return FALSE;
00323 }
00324 
00325 
00326 #endif /* _SB_TIME_ */