00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include "TSystemClock.hh"
00017
00018 TSystemClock::TSystemClock()
00019 : theAllocatedTime( -1 ), theLastUpdateTime( -1 ), theTime( -1 )
00020 {
00021 theAllocatedTime = (Tint)time( 0 );
00022 theTime = theAllocatedTime;
00023 }
00024
00025 TSystemClock::TSystemClock( const TSystemClock& right )
00026 : theAllocatedTime( -1 ),
00027 theLastUpdateTime( -1 ),
00028 theTime( -1 )
00029 {
00030 theAllocatedTime = (Tint)time( 0 );
00031 theLastUpdateTime = right.theLastUpdateTime;
00032 theTime = theAllocatedTime;
00033 }
00034
00035 TSystemClock::~TSystemClock()
00036 {;}
00037
00038 const TSystemClock& TSystemClock::operator=( const TSystemClock& right )
00039 {
00040 theAllocatedTime = (Tint)time( 0 );
00041 theLastUpdateTime = right.theLastUpdateTime;
00042 theTime = theAllocatedTime;
00043 return *this;
00044 }
00045
00046 Tstring TSystemClock::WhatTimeIsItNow()
00047 {
00048 Ttime_t now = (Ttime_t)Update();
00049
00050 struct tm* tp = localtime( &now );
00051 Tint year = tp -> tm_year + 1900;
00052 Tint month = tp -> tm_mon + 1;
00053 Tint day = tp -> tm_mday;
00054 Tint hour = tp -> tm_hour;
00055 Tint min = tp -> tm_min;
00056 Tint sec = tp -> tm_sec;
00057
00058 static const Tsize_t buflen = 128;
00059 static Tchar cbuf[ buflen ];
00060 Tostrstream bufstream( cbuf, buflen );
00061
00062 bufstream << year;
00063 bufstream << "/";
00064 bufstream << setfill( '0' ) << setiosflags( Tios::right ) << setw( 2 ) << month;
00065 bufstream << "/";
00066 bufstream << setfill( '0' ) << setiosflags( Tios::right ) << setw( 2 ) << day;
00067 bufstream << " ";
00068 bufstream << setfill( '0' ) << setiosflags( Tios::right ) << setw( 2 ) << hour;
00069 bufstream << ":";
00070 bufstream << setfill( '0' ) << setiosflags( Tios::right ) << setw( 2 ) << min;
00071 bufstream << ":";
00072 bufstream << setfill( '0' ) << setiosflags( Tios::right ) << setw( 2 ) << sec;
00073 bufstream << Tflush;
00074
00075 Tstring s = bufstream.str();
00076 return s;
00077 }
00078
00079 Tstring TSystemClock::WhatTimeIsIt( Tint second )
00080 {
00081 if ( second < 0 ) {
00082 return WhatTimeIsItNow();
00083 } else {
00084 struct tm* tp = localtime( (Ttime_t*)(&second) );
00085 Tint year = tp -> tm_year + 1900;
00086 Tint month = tp -> tm_mon + 1;
00087 Tint day = tp -> tm_mday;
00088 Tint hour = tp -> tm_hour;
00089 Tint min = tp -> tm_min;
00090 Tint sec = tp -> tm_sec;
00091
00092 static const Tsize_t buflen = 128;
00093 static Tchar cbuf[ buflen ];
00094 Tostrstream bufstream( cbuf, buflen );
00095
00096 bufstream << year;
00097 bufstream << "/";
00098 bufstream << setfill( '0' ) << setiosflags( Tios::right ) << setw( 2 ) << month;
00099 bufstream << "/";
00100 bufstream << setfill( '0' ) << setiosflags( Tios::right ) << setw( 2 ) << day;
00101 bufstream << " ";
00102 bufstream << setfill( '0' ) << setiosflags( Tios::right ) << setw( 2 ) << hour;
00103 bufstream << ":";
00104 bufstream << setfill( '0' ) << setiosflags( Tios::right ) << setw( 2 ) << min;
00105 bufstream << ":";
00106 bufstream << setfill( '0' ) << setiosflags( Tios::right ) << setw( 2 ) << sec;
00107 bufstream << Tflush;
00108
00109 Tstring s = bufstream.str();
00110 return s;
00111 }
00112 }
00113
00114 Tstring TSystemClock::WhenAllocated()
00115 {
00116 return WhatTimeIsIt( theAllocatedTime );
00117 }
00118
00119 Tstring TSystemClock::WhenUpdate()
00120 {
00121 return WhatTimeIsIt( theLastUpdateTime );
00122 }
00123
00124 Tint TSystemClock::GetElapsedTime( const Tstring& unit )
00125 {
00126 Update();
00127 Tint diff = theTime - theAllocatedTime;
00128 if ( unit == Thour ) {
00129 return diff/60/24;
00130 } else if ( unit == Tmin ) {
00131 return diff/60;
00132 } else {
00133 return diff;
00134 }
00135 }
00136
00137 Tint TSystemClock::GetElapsedTimeOfLastUpdate( const Tstring& unit )
00138 {
00139 Update();
00140 Tint diff = theTime - theLastUpdateTime;
00141 if ( unit == Thour ) {
00142 return diff/60/24;
00143 } else if ( unit == Tmin ) {
00144 return diff/60;
00145 } else {
00146 return diff;
00147 }
00148 }
00149
00150 Tint TSystemClock::GetYear( Tint second )
00151 {
00152 struct tm* tp = 0;
00153 if ( second < 0 ) {
00154 Ttime_t now = (Ttime_t)Update();
00155 tp = localtime( &now );
00156 } else {
00157 tp = localtime( (Ttime_t*)(&second) );
00158 }
00159 return tp -> tm_year + 1900;
00160 }
00161
00162 Tint TSystemClock::GetMonth( Tint second )
00163 {
00164 struct tm* tp = 0;
00165 if ( second < 0 ) {
00166 Ttime_t now = (Ttime_t)Update();
00167 tp = localtime( &now );
00168 } else {
00169 tp = localtime( (Ttime_t*)(&second) );
00170 }
00171 return tp -> tm_mon + 1;
00172 }
00173
00174 Tint TSystemClock::GetDay( Tint second )
00175 {
00176 struct tm* tp = 0;
00177 if ( second < 0 ) {
00178 Ttime_t now = (Ttime_t)Update();
00179 tp = localtime( &now );
00180 } else {
00181 tp = localtime( (Ttime_t*)(&second) );
00182 }
00183 return tp -> tm_mday;
00184 }
00185
00186 Tint TSystemClock::GetHour( Tint second )
00187 {
00188 struct tm* tp = 0;
00189 if ( second < 0 ) {
00190 Ttime_t now = (Ttime_t)Update();
00191 tp = localtime( &now );
00192 } else {
00193 tp = localtime( (Ttime_t*)(&second) );
00194 }
00195 return tp -> tm_hour;
00196 }
00197
00198 Tint TSystemClock::GetMinute( Tint second )
00199 {
00200 struct tm* tp = 0;
00201 if ( second < 0 ) {
00202 Ttime_t now = (Ttime_t)Update();
00203 tp = localtime( &now );
00204 } else {
00205 tp = localtime( (Ttime_t*)(&second) );
00206 }
00207 return tp -> tm_min;
00208 }
00209
00210 Tint TSystemClock::GetSecond( Tint second )
00211 {
00212 struct tm* tp = 0;
00213 if ( second < 0 ) {
00214 Ttime_t now = (Ttime_t)Update();
00215 tp = localtime( &now );
00216 } else {
00217 tp = localtime( (Ttime_t*)(&second) );
00218 }
00219 return tp -> tm_sec;
00220 }
00221
00222 Tint TSystemClock::Update()
00223 {
00224 theLastUpdateTime = theTime;
00225 return theTime = (Tint)time( 0 );
00226 }
00227
00228 Tostream& operator<<( Tostream& tos, const TSystemClock& right )
00229 {
00230 TSystemClock copy( right );
00231 tos << copy.WhatTimeIsItNow() << Tflush;
00232 return tos;
00233 }
00234
00235 #ifdef __CLDAQ_ROOT_DLL
00236 ClassImp(TSystemClock)
00237 #endif