Time.cc

00001 // Copyright 2008 Michael Marsh, University of Maryland.
00002 //
00003 // This file is part of pydtn.
00004 //
00005 // pydtn is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // pydtn 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
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with pydtn.  If not, see <http://www.gnu.org/licenses/>.
00017 //
00018 // The views and conclusions contained in the software and documentation
00019 // are those of the authors and should not be interpreted as representing
00020 // official policies, either expressed or implied, of the University
00021 // of Maryland.
00022 //
00023 // pydtn extends and embeds the Python interpreter, which is
00024 // Copyright 2001-2006 Python Software Foundation, All Rights Reserved,
00025 // and is released under the PSF License Agreement.
00026 //
00027 // RANLUX random number generation uses the Boost library,
00028 // Copyright 1994-2006 by various authors (details in individual files),
00029 // which is released under the Boost Software License, Version 1.0.
00030 
00031 #include "Time.h"
00032 #include <ostream>
00033 
00034 const long Time::MILLION = 1000000;
00035 
00036 Time::Time( const struct timeval& t )
00037 {
00038    tv.tv_sec  = t.tv_sec;
00039    tv.tv_usec = t.tv_usec;
00040    fix();
00041 }
00042 
00043 Time::Time( long sec, long usec )
00044 {
00045    tv.tv_sec  = sec;
00046    tv.tv_usec = usec;
00047    fix();
00048 }
00049 
00050 Time::Time( const Time& t )
00051 {
00052    tv.tv_sec  = t.tv.tv_sec;
00053    tv.tv_usec = t.tv.tv_usec;
00054 }
00055 
00056 void
00057 Time::operator=( const Time& t )
00058 {
00059    if ( &t == this ) return;
00060    tv.tv_sec  = t.tv.tv_sec;
00061    tv.tv_usec = t.tv.tv_usec;
00062 }
00063 
00064 Time
00065 Time::operator+(const Time& t) const
00066 {
00067    return Time( tv.tv_sec + t.tv.tv_sec , tv.tv_usec + t.tv.tv_usec );
00068 }
00069 
00070 Time
00071 Time::operator-(const Time& t) const
00072 {
00073    return Time( tv.tv_sec - t.tv.tv_sec , tv.tv_usec - t.tv.tv_usec );
00074 }
00075 
00076 Time
00077 Time::operator*(long s) const
00078 {
00079    return Time( tv.tv_sec * s , tv.tv_usec * s );
00080 }
00081 
00082 Time
00083 Time::operator/(long s) const
00084 {
00085    // This requires carrying the remainder of the seconds to the microseconds
00086    return Time( tv.tv_sec / s ,
00087                 ( MILLION * ( tv.tv_sec % s ) + tv.tv_usec ) / s );
00088 }
00089 
00090 Time&
00091 Time::operator+=(const Time& t)
00092 {
00093    tv.tv_sec += t.tv.tv_sec;
00094    tv.tv_usec += t.tv.tv_usec;
00095    fix();
00096    return *this;
00097 }
00098 
00099 Time&
00100 Time::operator-=(const Time& t)
00101 {
00102    tv.tv_sec -= t.tv.tv_sec;
00103    tv.tv_usec -= t.tv.tv_usec;
00104    fix();
00105    return *this;
00106 }
00107 
00108 Time&
00109 Time::operator*=(long s)
00110 {
00111    tv.tv_sec *= s;
00112    tv.tv_usec *= s;
00113    fix();
00114    return *this;
00115 }
00116 
00117 bool
00118 Time::operator==(const Time& t) const
00119 {
00120    if ( tv.tv_sec != t.tv.tv_sec ) return false;
00121    if ( tv.tv_usec != t.tv.tv_usec ) return false;
00122    return true;
00123 }
00124 
00125 bool
00126 Time::operator<(const Time& t) const
00127 {
00128    if ( tv.tv_sec < t.tv.tv_sec ) return true;
00129    if ( tv.tv_sec > t.tv.tv_sec ) return false;
00130    if ( tv.tv_usec < t.tv.tv_usec ) return true;
00131    return false;
00132 }
00133 
00134 bool
00135 Time::operator>(const Time& t) const
00136 {
00137    if ( tv.tv_sec > t.tv.tv_sec ) return true;
00138    if ( tv.tv_sec < t.tv.tv_sec ) return false;
00139    if ( tv.tv_usec > t.tv.tv_usec ) return true;
00140    return false;
00141 }
00142 
00143 bool
00144 Time::operator<=(const Time& t) const
00145 {
00146    return ! operator>(t);
00147 }
00148 
00149 bool
00150 Time::operator>=(const Time& t) const
00151 {
00152    return ! operator<(t);
00153 }
00154 
00155 
00156 void
00157 Time::fix()
00158 {
00159    if ( ( tv.tv_usec > MILLION ) || ( tv.tv_usec < -MILLION ) )
00160    {
00161       // This works regardless of sign
00162       tv.tv_sec += ( tv.tv_usec / MILLION );
00163       tv.tv_usec %= MILLION;
00164    }
00165    if ( tv.tv_sec >= 0 && tv.tv_usec < 0 )
00166    {
00167       tv.tv_usec += MILLION;
00168       tv.tv_sec -= 1;
00169    }
00170    else if ( tv.tv_sec < 0 && tv.tv_usec > 0 )
00171    {
00172       tv.tv_usec -= MILLION;
00173       tv.tv_sec += 1;
00174    }
00175 }
00176 
00177 Time
00178 operator+(const struct timeval& a, const Time& b)
00179 {
00180    return Time( a.tv_sec + b.tv.tv_sec , a.tv_usec + b.tv.tv_usec );
00181 }
00182 
00183 Time
00184 operator-(const struct timeval& a, const Time& b)
00185 {
00186    return Time( a.tv_sec - b.tv.tv_sec , a.tv_usec - b.tv.tv_usec );
00187 }
00188 
00189 Time
00190 operator*(long a, const Time& b)
00191 {
00192    return Time( a * b.tv.tv_sec , a * b.tv.tv_usec );
00193 }
00194 
00195 bool
00196 operator==(const struct timeval& a, const Time& b)
00197 {
00198    if ( a.tv_sec != b.tv.tv_sec ) return false;
00199    if ( a.tv_usec != b.tv.tv_usec ) return false;
00200    return true;
00201 }
00202 
00203 bool
00204 operator<(const struct timeval& a, const Time& b)
00205 {
00206    if ( a.tv_sec < b.tv.tv_sec ) return true;
00207    if ( a.tv_sec > b.tv.tv_sec ) return false;
00208    if ( a.tv_usec < b.tv.tv_usec ) return true;
00209    return false;
00210 }
00211 
00212 bool
00213 operator>(const struct timeval& a, const Time& b)
00214 {
00215    if ( a.tv_sec > b.tv.tv_sec ) return true;
00216    if ( a.tv_sec < b.tv.tv_sec ) return false;
00217    if ( a.tv_usec > b.tv.tv_usec ) return true;
00218    return false;
00219 }
00220 
00221 std::ostream& operator<<(std::ostream& stream, const Time& t)
00222 {
00223    stream << "{" << t.tv.tv_sec << "," << t.tv.tv_usec << "}";
00224    return stream;
00225 }

Generated on Mon Mar 24 11:15:46 2008 for Pydtn Simulator by  doxygen 1.5.4