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

Timer.cxx

00001 /* $Header: /home/hopper/src/cvs/C++/UniEvent/Timer.cxx,v 1.6 2002/08/29 00:58:05 hopper Exp $ */
00002 
00003 // For a log, see ChangeLog
00004 
00005 #ifdef __GNUG__
00006 #  pragma implementation "Timer.h"
00007 #endif
00008 
00009 #include "UniEvent/Timer.h"
00010 #include <cmath>
00011 #include <iostream>
00012 #include <ctime>
00013 
00014 namespace strmod {
00015 namespace unievent {
00016 
00017 const UNEVT_ClassIdent Timer::identifier(12UL);
00018 
00019 bool operator <(const Timer::interval_t &a, const Timer::interval_t &b)
00020 {
00021    Timer::interval_t an(a);
00022    an.normalize();
00023    Timer::interval_t bn(b);
00024    bn.normalize();
00025    if (an.seconds != bn.seconds)
00026    {
00027       return an.seconds < bn.seconds;
00028    }
00029    else
00030    {
00031       return an.nanoseconds < bn.nanoseconds;
00032    }
00033 }
00034 
00035 bool operator ==(const Timer::interval_t &a, const Timer::interval_t &b)
00036 {
00037    Timer::interval_t an(a);
00038    an.normalize();
00039    Timer::interval_t bn(b);
00040    bn.normalize();
00041    if (an.seconds != bn.seconds)
00042    {
00043       return an.seconds < bn.seconds;
00044    }
00045    else
00046    {
00047       return an.nanoseconds < bn.nanoseconds;
00048    }
00049 }
00050 
00051 //--
00052 
00053 bool operator <(const Timer::absolute_t &a, const Timer::absolute_t &b)
00054 {
00055    double seconds = ::difftime(a.time, b.time);
00056    if (seconds == 0)
00057    {
00058       const Timer::interval_t &ainterval = a;
00059       const Timer::interval_t &binterval = b;
00060       return ainterval <= binterval;
00061    }
00062    else if (seconds < 0)  // a.time < b.time
00063    {
00064       seconds = -seconds;
00065       double disecs;
00066       unsigned long insecs;
00067       insecs = static_cast<unsigned long>(::floor(modf(seconds, &disecs) * 1000000000 + 0.5));
00068       unsigned long isecs = static_cast<unsigned long>(disecs);
00069       // Add the interval between a.time and b.time to binterval.
00070       // Essentially represent both a and b as intervals from a.time.
00071       Timer::interval_t binterval(b);
00072       binterval.normalize();
00073       binterval.seconds += isecs;
00074       binterval.nanoseconds += insecs;
00075       const Timer::interval_t &ainterval = a;
00076       return ainterval < binterval;
00077    }
00078    else // a.time >= b.time
00079    {
00080       double disecs;
00081       unsigned long insecs;
00082       insecs = static_cast<unsigned long>(::floor(modf(seconds, &disecs) * 1000000000 + 0.5));
00083       unsigned long isecs = static_cast<unsigned long>(disecs);
00084       // Add the interval between a.time and b.time to ainterval.
00085       // Essentially represent both a and b as intervals from b.time.
00086       Timer::interval_t ainterval(a);
00087       ainterval.normalize();
00088       ainterval.seconds += isecs;
00089       ainterval.nanoseconds += insecs;
00090       const Timer::interval_t &binterval = b;
00091       return ainterval < binterval;
00092    }
00093 }
00094 
00095 const Timer::interval_t operator -(const Timer::absolute_t &a,
00096                                    const Timer::absolute_t &b)
00097 {
00098    typedef Timer::interval_t interval_t;
00099    typedef Timer::absolute_t absolute_t;
00100    double timetdiff = ::difftime(a.time, b.time);
00101    const interval_t &ai = a;
00102    const interval_t &bi = b;
00103 
00104    if (timetdiff < 0)
00105    {
00106       timetdiff = -timetdiff;
00107       interval_t tdiffint =
00108          interval_t(static_cast<unsigned long>(::floor(timetdiff)),
00109                     static_cast<lcore::U4Byte>(timetdiff - floor(timetdiff)) *
00110                     1000000000U);
00111       if (ai < tdiffint)
00112       {
00113          return interval_t(0, 0);
00114       }
00115       else
00116       {
00117          return (ai - tdiffint) - bi;
00118       }
00119    }
00120    else if (timetdiff == 0)
00121    {
00122       return ai - bi;
00123    }
00124    else
00125    {
00126       interval_t tdiffint =
00127          interval_t(static_cast<unsigned long>(::floor(timetdiff)),
00128                     static_cast<lcore::U4Byte>(timetdiff - floor(timetdiff)) *
00129                     1000000000U);
00130       if (ai < bi)
00131       {
00132          return tdiffint - (bi - ai);
00133       }
00134       else
00135       {
00136          return tdiffint + (ai - bi);
00137       }
00138    }
00139 }
00140 
00141 bool operator ==(const Timer::absolute_t &a, const Timer::absolute_t &b)
00142 {
00143    double seconds = ::difftime(a.time, b.time);
00144    if (seconds == 0)
00145    {
00146       const Timer::interval_t &ainterval = a;
00147       const Timer::interval_t &binterval = b;
00148       return ainterval == binterval;
00149    }
00150    else if (seconds < 0)  // a.time < b.time
00151    {
00152       seconds = -seconds;
00153       double disecs;
00154       unsigned long insecs;
00155       insecs = static_cast<unsigned long>(::floor(modf(seconds, &disecs) * 1000000000 + 0.5));
00156       unsigned long isecs = static_cast<unsigned long>(disecs);
00157       // Add the interval between a.time and b.time to binterval.
00158       // Essentially represent both a and b as intervals from a.time.
00159       Timer::interval_t binterval(b);
00160       binterval.normalize();
00161       binterval.seconds += isecs;
00162       binterval.nanoseconds += insecs;
00163       const Timer::interval_t &ainterval = a;
00164       return ainterval == binterval;
00165    }
00166    else // a.time >= b.time
00167    {
00168       double disecs;
00169       unsigned long insecs;
00170       insecs = static_cast<unsigned long>(::floor(modf(seconds, &disecs) * 1000000000 + 0.5));
00171       unsigned long isecs = static_cast<unsigned long>(disecs);
00172       // Add the interval between a.time and b.time to ainterval.
00173       // Essentially represent both a and b as intervals from b.time.
00174       Timer::interval_t ainterval(a);
00175       ainterval.normalize();
00176       ainterval.seconds += isecs;
00177       ainterval.nanoseconds += insecs;
00178       const Timer::interval_t &binterval = b;
00179       return ainterval == binterval;
00180    }
00181 }
00182 
00183 const Timer::interval_t
00184 operator -(const Timer::interval_t &a, const Timer::interval_t &b)
00185 {
00186    Timer::interval_t an = a;
00187    an.normalize();
00188    Timer::interval_t bn = b;
00189    bn.normalize();
00190    if (an.seconds  > bn.seconds)
00191    {
00192       unsigned long secdiff = an.seconds - bn.seconds;
00193       if (an.nanoseconds >= bn.nanoseconds)
00194       {
00195          return Timer::interval_t(secdiff, an.nanoseconds - bn.nanoseconds);
00196       }
00197       else
00198       {
00199          return Timer::interval_t(secdiff - 1,
00200                                   (an.nanoseconds + 1000000000U) -
00201                                   bn.nanoseconds);
00202       }
00203    }
00204    else if (an.seconds == bn.seconds)
00205    {
00206       if (an.nanoseconds >= bn.nanoseconds)
00207       {
00208          return Timer::interval_t(0, an.nanoseconds - bn.nanoseconds);
00209       }
00210       else
00211       {
00212          return Timer::interval_t(0, bn.nanoseconds - an.nanoseconds);
00213       }
00214    }
00215    else // an.seconds < bn.seconds
00216    {
00217       unsigned long secdiff = bn.seconds - an.seconds;
00218       if (bn.nanoseconds >= an.nanoseconds)
00219       {
00220          return Timer::interval_t(secdiff, bn.nanoseconds - an.nanoseconds);
00221       }
00222       else
00223       {
00224          return Timer::interval_t(secdiff - 1,
00225                                   (bn.nanoseconds + 1000000000U) -
00226                                   an.nanoseconds);
00227       }
00228    }
00229 }
00230 
00231 ::std::ostream &operator <<(::std::ostream &os, const Timer::absolute_t &time)
00232 {
00233    char buf[32];
00234    ::ctime_r(&(time.time), buf);
00235    buf[24] = '\0';
00236    os << "(" << buf << " + "
00237       << Timer::interval_t(time.seconds, time.nanoseconds) << ")";
00238    return os;
00239 }
00240 
00241 ::std::ostream &operator <<(::std::ostream &os, const Timer::interval_t &intval)
00242 {
00243    double seconds = intval.seconds + double(intval.nanoseconds) / 1000000000U;
00244    os << "(" <<  seconds << " seconds)";
00245    return os;
00246 }
00247 
00248 };  // namespace unievent
00249 };  // namespace strmod

Generated on Wed Jan 29 00:32:45 2003 for libNet by doxygen1.3-rc1