• Main Page
  • Namespaces
  • Classes
  • Files
  • File List

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 #include <iosfwd>
00032 
00036 #ifdef _MSC_VER
00037 #include <sys/timeb.h>
00038 #else
00039 #include <sys/time.h>
00040 #endif
00041 
00042 namespace WFMath {
00043 
00044 class TimeStamp;
00045 
00047 
00052 class TimeDiff
00053 {
00054   TimeDiff(long sec, long usec, bool is_valid);
00055  public:
00057   TimeDiff() : m_isvalid(false) {}
00059   TimeDiff(long msec);
00060   // default copy constructor is fine
00061 
00063 
00067   long milliseconds() const;
00069   std::pair<long,long> full_time() const {return std::make_pair(m_sec,m_usec);}
00070 
00071   bool isValid() const {return m_isvalid;}
00072 
00074   friend TimeDiff& operator+=(TimeDiff&, const TimeDiff&);
00076   friend TimeDiff& operator-=(TimeDiff&, const TimeDiff&);
00078   TimeDiff operator-() const {return TimeDiff(-m_sec, -m_usec, m_isvalid);}
00079 
00081   friend TimeDiff operator+(const TimeDiff &a, const TimeDiff &b);
00083   friend TimeDiff operator-(const TimeDiff &a, const TimeDiff &b);
00084 
00086   friend TimeStamp& operator+=(TimeStamp&, const TimeDiff&);
00088   friend TimeStamp& operator-=(TimeStamp&, const TimeDiff&);
00089 
00091   friend TimeStamp operator+(const TimeStamp &a, const TimeDiff &msec);
00093   friend TimeStamp operator-(const TimeStamp &a, const TimeDiff &msec);
00094 
00096   friend TimeDiff operator-(const TimeStamp &a, const TimeStamp &b);
00097 
00098   friend bool operator<(const TimeDiff&, const TimeDiff&);
00099   friend bool operator==(const TimeDiff&, const TimeDiff&);
00100 
00101  private:
00102   bool m_isvalid;
00103   long m_sec, m_usec;
00104 };
00105 
00106 inline bool operator>(const TimeDiff &a, const TimeDiff &b) {return b < a;}
00107 inline bool operator<=(const TimeDiff &a, const TimeDiff &b) {return !(b < a);}
00108 inline bool operator>=(const TimeDiff &a, const TimeDiff &b) {return !(a < b);}
00109 inline bool operator!=(const TimeDiff &a, const TimeDiff &b) {return !(b == a);}
00110 
00112 
00117 class TimeStamp {
00118  private:
00119 #ifdef __WIN32__ 
00120   // We roll our own timeval... may only need to be done for mingw32.
00121   struct {
00122     long tv_sec;        /* seconds */
00123     long tv_usec;       /* microseconds */
00124   } _val;
00125 #else
00126   // POSIX, BeOS, ....
00127   struct timeval _val;
00128 #endif
00129   bool _isvalid;
00130   TimeStamp(long sec, long usec, bool isvalid);
00131  public:
00133   TimeStamp() : _isvalid(false) {}
00134   // default copy constructor is fine
00135 
00136   friend bool operator<(const TimeStamp &a, const TimeStamp &b);
00137   friend bool operator==(const TimeStamp &a, const TimeStamp &b);
00138 
00139   friend std::ostream& operator<<(std::ostream& os, const TimeStamp&);
00140   friend std::istream& operator>>(std::istream& is, TimeStamp&);
00141 
00142   bool isValid() const {return _isvalid;}
00144   friend TimeStamp& operator+=(TimeStamp&, const TimeDiff&);
00146   friend TimeStamp& operator-=(TimeStamp&, const TimeDiff&);
00147 
00149   friend TimeStamp operator+(const TimeStamp &a, const TimeDiff &msec);
00151   friend TimeStamp operator-(const TimeStamp &a, const TimeDiff &msec);
00152 
00154   friend TimeDiff operator-(const TimeStamp &a, const TimeStamp &b);    
00155 
00157   static TimeStamp now();
00159   static TimeStamp epochStart();
00160 };
00161 
00163 inline TimeStamp operator+(TimeDiff msec, const TimeStamp &a) {return a + msec;}
00164 
00165 inline bool operator>(const TimeStamp &a, const TimeStamp &b) {return b < a;}
00166 inline bool operator<=(const TimeStamp &a, const TimeStamp &b) {return !(b < a);}
00167 inline bool operator>=(const TimeStamp &a, const TimeStamp &b) {return !(a < b);}
00168 inline bool operator!=(const TimeStamp &a, const TimeStamp &b) {return !(b == a);}
00169 
00170 } // namespace WFMath
00171 
00172 #endif  // WFMATH_TIMESTAMP_H

Generated for WFMath by  doxygen 1.7.1