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 
00035 #ifdef _MSC_VER
00036 #include <sys/timeb.h>
00037 #else
00038 #include <sys/time.h>
00039 #endif
00040 
00041 namespace WFMath {
00042 
00043 class TimeStamp;
00044 
00046 
00051 class TimeDiff
00052 {
00053   TimeDiff(long sec, long usec, bool is_valid);
00054  public:
00056   TimeDiff() : m_isvalid(false) {}
00058   TimeDiff(long msec);
00059   // default copy constructor is fine
00060 
00062 
00066   long milliseconds() const;
00068   std::pair<long,long> full_time() const {return std::make_pair(m_sec,m_usec);}
00069 
00070   bool isValid() const {return m_isvalid;}
00071 
00073   friend TimeDiff& operator+=(TimeDiff&, const TimeDiff&);
00075   friend TimeDiff& operator-=(TimeDiff&, const TimeDiff&);
00077   TimeDiff operator-() const {return TimeDiff(-m_sec, -m_usec, m_isvalid);}
00078 
00080   friend TimeDiff operator+(const TimeDiff &a, const TimeDiff &b);
00082   friend TimeDiff operator-(const TimeDiff &a, const TimeDiff &b);
00083 
00085   friend TimeStamp& operator+=(TimeStamp&, const TimeDiff&);
00087   friend TimeStamp& operator-=(TimeStamp&, const TimeDiff&);
00088 
00090   friend TimeStamp operator+(const TimeStamp &a, const TimeDiff &msec);
00092   friend TimeStamp operator-(const TimeStamp &a, const TimeDiff &msec);
00093 
00095   friend TimeDiff operator-(const TimeStamp &a, const TimeStamp &b);
00096 
00097   friend bool operator<(const TimeDiff&, const TimeDiff&);
00098   friend bool operator==(const TimeDiff&, const TimeDiff&);
00099 
00100  private:
00101   bool m_isvalid;
00102   long m_sec, m_usec;
00103 };
00104 
00105 inline bool operator>(const TimeDiff &a, const TimeDiff &b) {return b < a;}
00106 inline bool operator<=(const TimeDiff &a, const TimeDiff &b) {return !(b < a);}
00107 inline bool operator>=(const TimeDiff &a, const TimeDiff &b) {return !(a < b);}
00108 inline bool operator!=(const TimeDiff &a, const TimeDiff &b) {return !(b == a);}
00109 
00111 
00116 class TimeStamp {
00117  private:
00118 #ifdef __WIN32__ 
00119   // We roll our own timeval... may only need to be done for mingw32.
00120   struct {
00121     long tv_sec;        /* seconds */
00122     long tv_usec;       /* microseconds */
00123   } _val;
00124 #else
00125   // POSIX, BeOS, ....
00126   struct timeval _val;
00127 #endif
00128   bool _isvalid;
00129   TimeStamp(long sec, long usec, bool isvalid);
00130  public:
00132   TimeStamp() : _isvalid(false) {}
00133   // default copy constructor is fine
00134 
00135   friend bool operator<(const TimeStamp &a, const TimeStamp &b);
00136   friend bool operator==(const TimeStamp &a, const TimeStamp &b);
00137 
00138   friend std::ostream& operator<<(std::ostream& os, const TimeStamp&);
00139   friend std::istream& operator>>(std::istream& is, TimeStamp&);
00140 
00141   bool isValid() const {return _isvalid;}
00143   friend TimeStamp& operator+=(TimeStamp&, const TimeDiff&);
00145   friend TimeStamp& operator-=(TimeStamp&, const TimeDiff&);
00146 
00148   friend TimeStamp operator+(const TimeStamp &a, const TimeDiff &msec);
00150   friend TimeStamp operator-(const TimeStamp &a, const TimeDiff &msec);
00151 
00153   friend TimeDiff operator-(const TimeStamp &a, const TimeStamp &b);    
00154 
00156   static TimeStamp now();
00158   static TimeStamp epochStart();
00159 };
00160 
00162 inline TimeStamp operator+(TimeDiff msec, const TimeStamp &a) {return a + msec;}
00163 
00164 inline bool operator>(const TimeStamp &a, const TimeStamp &b) {return b < a;}
00165 inline bool operator<=(const TimeStamp &a, const TimeStamp &b) {return !(b < a);}
00166 inline bool operator>=(const TimeStamp &a, const TimeStamp &b) {return !(a < b);}
00167 inline bool operator!=(const TimeStamp &a, const TimeStamp &b) {return !(b == a);}
00168 
00169 } // namespace WFMath
00170 
00171 #endif  // WFMATH_TIMESTAMP_H

Generated on Sun Aug 27 21:30:58 2006 for WFMath by  doxygen 1.4.7