Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members  

timestamp.h

00001 // timestamp.h (time functions, taken from Eris)
00002 //
00003 //  The WorldForge Project
00004 //  Copyright (C) 2002  The WorldForge Project
00005 //
00006 //  This program is free software; you can redistribute it and/or modify
00007 //  it under the terms of the GNU General Public License as published by
00008 //  the Free Software Foundation; either version 2 of the License, or
00009 //  (at your option) any later version.
00010 //
00011 //  This program is distributed in the hope that it will be useful,
00012 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //  GNU General Public License for more details.
00015 //
00016 //  You should have received a copy of the GNU General Public License
00017 //  along with this program; if not, write to the Free Software
00018 //  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00019 //
00020 //  For information about WorldForge and its authors, please contact
00021 //  the Worldforge Web Site at http://www.worldforge.org.
00022 
00023 // Author: Ron Steinke
00024 // Created: 2002-5-23
00025 
00026 #ifndef WFMATH_TIMESTAMP_H
00027 #define WFMATH_TIMESTAMP_H
00028 
00029 #include <wfmath/const.h>
00030 #include <algorithm> // For std::pair
00031 
00032 namespace WFMath {
00033 
00037 #ifdef _MSC_VER
00038 #include <sys/timeb.h>
00039 #else
00040 #include <sys/time.h>
00041 #endif
00042 #if defined ( __WIN32__ )
00043 #include <winsock.h> 
00044 extern "C" {
00045   struct eris_timeval  {
00046     long tv_sec;        /* seconds */
00047     long tv_usec;       /* microseconds */
00048   };
00049 }
00050 #endif
00051 
00052 class TimeStamp;
00053 
00055 
00060 class TimeDiff
00061 {
00062   TimeDiff(long sec, long usec, bool is_valid);
00063  public:
00065   TimeDiff() : m_isvalid(false) {}
00067   TimeDiff(long msec);
00068   // default copy constructor is fine
00069 
00071 
00075   long milliseconds() const;
00077   std::pair<long,long> full_time() const {return std::make_pair(m_sec,m_usec);}
00078 
00079   bool isValid() const {return m_isvalid;}
00080 
00082   friend TimeDiff& operator+=(TimeDiff&, const TimeDiff&);
00084   friend TimeDiff& operator-=(TimeDiff&, const TimeDiff&);
00086   TimeDiff operator-() const {return TimeDiff(-m_sec, -m_usec, m_isvalid);}
00087 
00089   friend TimeDiff operator+(const TimeDiff &a, const TimeDiff &b);
00091   friend TimeDiff operator-(const TimeDiff &a, const TimeDiff &b);
00092 
00094   friend TimeStamp& operator+=(TimeStamp&, const TimeDiff&);
00096   friend TimeStamp& operator-=(TimeStamp&, const TimeDiff&);
00097 
00099   friend TimeStamp operator+(const TimeStamp &a, const TimeDiff &msec);
00101   friend TimeStamp operator-(const TimeStamp &a, const TimeDiff &msec);
00102 
00104   friend TimeDiff operator-(const TimeStamp &a, const TimeStamp &b);
00105 
00106   friend bool operator<(const TimeDiff&, const TimeDiff&);
00107   friend bool operator==(const TimeDiff&, const TimeDiff&);
00108 
00109  private:
00110   bool m_isvalid;
00111   long m_sec, m_usec;
00112 };
00113 
00114 inline bool operator>(const TimeDiff &a, const TimeDiff &b) {return b < a;}
00115 inline bool operator<=(const TimeDiff &a, const TimeDiff &b) {return !(b < a);}
00116 inline bool operator>=(const TimeDiff &a, const TimeDiff &b) {return !(a < b);}
00117 inline bool operator!=(const TimeDiff &a, const TimeDiff &b) {return !(b == a);}
00118 
00120 
00125 class TimeStamp {
00126  private:
00127 #if defined( __WIN32__ )
00128         
00129   // We roll our own timeval... may only need to be done for mingw32.
00130   // FIXME since we're no longer doing a typedef, do we really need to do this?
00131   struct eris_timeval _val;
00132 
00133 #elif defined( macintosh ) // This doesn't appear to be supported
00134   UnsignedWide _val;    // micro-seconds
00135 #else
00136   // POSIX, BeOS, ....
00137   struct timeval _val;
00138 #endif
00139   bool _isvalid;
00140   TimeStamp(long sec, long usec, bool isvalid);
00141  public:
00143   TimeStamp() : _isvalid(false) {}
00144   // default copy constructor is fine
00145 
00146   friend bool operator<(const TimeStamp &a, const TimeStamp &b);
00147   friend bool operator==(const TimeStamp &a, const TimeStamp &b);
00148 
00149   friend std::ostream& operator<<(std::ostream& os, const TimeStamp&);
00150   friend std::istream& operator>>(std::istream& is, TimeStamp&);
00151 
00152   bool isValid() const {return _isvalid;}
00154   friend TimeStamp& operator+=(TimeStamp&, const TimeDiff&);
00156   friend TimeStamp& operator-=(TimeStamp&, const TimeDiff&);
00157 
00159   friend TimeStamp operator+(const TimeStamp &a, const TimeDiff &msec);
00161   friend TimeStamp operator-(const TimeStamp &a, const TimeDiff &msec);
00162 
00164   friend TimeDiff operator-(const TimeStamp &a, const TimeStamp &b);    
00165 
00167   static TimeStamp now();
00169   static TimeStamp epochStart();
00170 };
00171 
00173 inline TimeStamp operator+(TimeDiff msec, const TimeStamp &a) {return a + msec;}
00174 
00175 inline bool operator>(const TimeStamp &a, const TimeStamp &b) {return b < a;}
00176 inline bool operator<=(const TimeStamp &a, const TimeStamp &b) {return !(b < a);}
00177 inline bool operator>=(const TimeStamp &a, const TimeStamp &b) {return !(a < b);}
00178 inline bool operator!=(const TimeStamp &a, const TimeStamp &b) {return !(b == a);}
00179 
00180 } // namespace WFMath
00181 
00182 #endif  // WFMATH_TIMESTAMP_H

Generated on Wed May 28 09:20:33 2003 for WFMath by doxygen1.3-rc3