00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "BaBar/BaBar.hh"
00021
00022
00023
00024
00025 #include "BdbTime/BdbDuration.hh"
00026
00027
00028
00029
00030 extern "C" {
00031 }
00032
00033
00034
00035
00036 #include <iostream>
00037 #include <iomanip>
00038 using std::cout;
00039 using std::ostream;
00040 using std::setw;
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054 BdbDuration::BdbDuration( )
00055 : _sec( 0 ), _nsec( 0 )
00056 {
00057 }
00058
00059 BdbDuration::BdbDuration( const BdbDuration & d )
00060 : _sec( d._sec ), _nsec( d._nsec )
00061 {
00062 }
00063
00064 BdbDuration::BdbDuration( d_ULong sec, d_ULong nsec )
00065 {
00066 if ( nsec >= BdbTimeConst::nsecInASec )
00067 {
00068
00069 d_ULong extraSec = nsec / BdbTimeConst::nsecInASec;
00070 d_ULong remainNsec = nsec % BdbTimeConst::nsecInASec;
00071 sec += extraSec;
00072 nsec = remainNsec;
00073 }
00074
00075 _sec = sec;
00076 _nsec = nsec;
00077 }
00078
00079
00080
00081
00082
00083
00084
00085 BdbDuration BdbDuration::operator - ( const BdbDuration & d1 ) const
00086 {
00087
00088
00089 d_ULong d2Sec;
00090 d_ULong d2Nsec;
00091 d_ULong d1Sec;
00092 d_ULong d1Nsec;
00093
00094 if ( *this > d1 )
00095 {
00096 d2Sec = _sec;
00097 d2Nsec = _nsec;
00098 d1Sec = d1._sec;
00099 d1Nsec = d1._nsec;
00100 }
00101 else
00102 {
00103 d2Sec = d1._sec;
00104 d2Nsec = d1._nsec;
00105 d1Sec = _sec;
00106 d1Nsec = _nsec;
00107 }
00108
00109 if ( d2Nsec < d1Nsec )
00110 {
00111
00112 d2Nsec += BdbTimeConst::nsecInASec;
00113 d2Sec--;
00114 }
00115
00116 d_ULong sec = d2Sec - d1Sec;
00117 d_ULong nsec = d2Nsec - d1Nsec;
00118
00119 BdbDuration diff( sec, nsec );
00120
00121 return diff;
00122 }
00123
00124
00125 BdbDuration BdbDuration::operator + ( const BdbDuration & d1 ) const
00126 {
00127 d_ULong totalSec = _sec + d1._sec;
00128 d_ULong totalNsec = _nsec + d1._nsec;
00129
00130 if ( totalNsec >= BdbTimeConst::nsecInASec )
00131 {
00132
00133 d_ULong extraSec = totalNsec / BdbTimeConst::nsecInASec;
00134 d_ULong remainNsec = totalNsec % BdbTimeConst::nsecInASec;
00135 totalSec += extraSec;
00136 totalNsec = remainNsec;
00137 }
00138
00139 BdbDuration sum( totalSec, totalNsec );
00140
00141 return sum;
00142 }
00143
00144
00145 BdbDuration & BdbDuration::operator = ( const BdbDuration & d1 )
00146 {
00147 if ( this == &d1 )
00148 {
00149 return *this;
00150 }
00151
00152 _sec = d1._sec;
00153 _nsec = d1._nsec;
00154
00155 return *this;
00156 }
00157
00158
00159 BdbDuration & BdbDuration::operator += ( const BdbDuration & d1 )
00160 {
00161 d_ULong totalSec = _sec + d1._sec;
00162 d_ULong totalNsec = _nsec + d1._nsec;
00163
00164 if ( totalNsec >= BdbTimeConst::nsecInASec )
00165 {
00166
00167 d_ULong extraSec = totalNsec / BdbTimeConst::nsecInASec;
00168 d_ULong remainNsec = totalNsec % BdbTimeConst::nsecInASec;
00169 totalSec += extraSec;
00170 totalNsec = remainNsec;
00171 }
00172
00173 _sec = totalSec;
00174 _nsec = totalNsec;
00175
00176 return *this;
00177 }
00178
00179
00180
00181
00182
00183
00184 ostream & operator << ( ostream & os, const BdbDuration & d )
00185 {
00186 if ( d._nsec == 0 )
00187 {
00188 os << d._sec << " sec";
00189 return os;
00190 }
00191 else
00192 {
00193 cout.fill( '0' );
00194 os << d._sec << "." << setw(9) << d._nsec << " sec";
00195 cout.fill( ' ' );
00196 return os;
00197 }
00198 }
00199
00200
00201
00202
00203
00204
00205
00206
00207