Changeset 2612 for trunk/MagicSoft/Mars/mbase
- Timestamp:
- 12/07/03 12:52:29 (21 years ago)
- Location:
- trunk/MagicSoft/Mars/mbase
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/MagicSoft/Mars/mbase/MTime.cc
r2607 r2612 29 29 // A generalized MARS time stamp 30 30 // 31 // WARNING: Be carefull changing this class. It is also used in the 32 // MAGIC drive software cosy! 31 33 // 32 34 // Version 1: … … 58 60 using namespace std; 59 61 62 const UInt_t MTime::kHour = 3600000; // [ms] one hour 63 const UInt_t MTime::kDay = MTime::kHour*24; // [ms] one day 64 65 // -------------------------------------------------------------------------- 66 // 67 // Return date as year(y), month(m), day(d) 68 // 60 69 void MTime::GetDate(UShort_t &y, Byte_t &m, Byte_t &d) const 61 70 { … … 63 72 } 64 73 74 // -------------------------------------------------------------------------- 75 // 76 // Return the time in the range [0h, 24h) = [0h0m0.000s - 23h59m59.999s] 77 // 78 void MTime::GetTime(Byte_t &h, Byte_t &m, Byte_t &s, UShort_t &ms) const 79 { 80 Long_t tm = GetTime24(); 81 ms = tm%1000; // [ms] 82 tm /= 1000; // [s] 83 s = tm%60; // [s] 84 tm /= 60; // [m] 85 m = tm%60; // [m] 86 tm /= 60; // [h] 87 h = tm; // [h] 88 } 89 90 // -------------------------------------------------------------------------- 91 // 92 // Return time as MJD (=JD-24000000.5) 93 // 94 Double_t MTime::GetMjd() const 95 { 96 return fMjd+(Double_t)(fNanoSec/1e6+(Long_t)fTime)/kDay; 97 } 98 99 // -------------------------------------------------------------------------- 100 // 101 // Return a time which is expressed in milliseconds since 01/01/1995 0:00h 102 // This is compatible with root's definition used in gSystem->Now() 103 // and TTime. 104 // Note, gSystem->Now() returns local time, such that it may differ 105 // from GetRootTime() (if you previously called MTime::Now()) 106 // 107 TTime MTime::GetRootTime() const 108 { 109 return (ULong_t)((GetMjd()-49718)*kDay); 110 } 111 112 // -------------------------------------------------------------------------- 113 // 114 // Return a time which is expressed in seconds since 01/01/1995 0:00h 115 // This is compatible with root's definition used in TAxis. 116 // Note, a TAxis always displayes (automatically) given times in 117 // local time (while here we return UTC) such, that you may encounter 118 // strange offsets. You can get rid of this by calling: 119 // TAxis::SetTimeFormat("[your-format] %F1995-01-01 00:00:00"); 120 // 121 Double_t MTime::GetAxisTime() const 122 { 123 return (GetMjd()-49718)*kDay/1000; 124 } 125 126 // -------------------------------------------------------------------------- 127 // 128 // Set a time expressed in MJD, Time of Day (eg. 23:12.779h expressed 129 // in milliseconds) and a nanosecond part. 130 // 131 Bool_t MTime::Set(UInt_t mjd, ULong_t ms, UInt_t ns=0) 132 { 133 // [d] mjd (eg. 52320) 134 // [ms] time (eg. 17h expressed in ms) 135 // [ns] time (ns part of time) 136 137 if (ms>kDay-1 || ns>999999) 138 return kFALSE; 139 140 const Bool_t am = ms < kHour*13; // day of sunrise? 141 142 fMjd = am ? mjd : mjd + 1; 143 fTime = (Long_t)(am ? ms : ms-kDay); 144 fNanoSec = ns; 145 146 return kTRUE; 147 } 148 149 // -------------------------------------------------------------------------- 150 // 151 // Set MTime to given MJD (eg. 52080.0915449892) 152 // 153 void MTime::SetMjd(Double_t m) 154 { 155 const UInt_t mjd = (UInt_t)TMath::Floor(m); 156 const Double_t frac = fmod(m, 1)*kDay; // [ms] Fraction of day 157 const UInt_t ns = (UInt_t)fmod(frac*1e6, 1000000); 158 159 Set(mjd, (ULong_t)TMath::Floor(frac), ns); 160 } 161 162 // -------------------------------------------------------------------------- 163 // 164 // Set MTime to given time and date 165 // 65 166 Bool_t MTime::Set(UShort_t y, Byte_t m, Byte_t d, Byte_t h, Byte_t min, Byte_t s, UShort_t ms, UInt_t ns) 66 167 { … … 77 178 } 78 179 79 void MTime::SetSystemTimer(const struct timeval &tv) 80 { 81 const ULong_t hor = 3600000; // One Hour in milliseconds 82 83 const UInt_t mjd = tv.tv_sec/(60*60*24) + 40587; 84 const Long_t tm = (tv.tv_usec/1000)%(24*hor); 85 86 Set(mjd, tm, tv.tv_usec*1000); 87 } 88 180 // -------------------------------------------------------------------------- 181 // 182 // Set MTime to time expressed in a 'struct timeval' 183 // 184 void MTime::Set(const struct timeval &tv) 185 { 186 const UInt_t mjd = 1000*tv.tv_sec/kDay + 40587; 187 const Long_t tm = tv.tv_sec%(24*3600)*1000 + tv.tv_usec/1000; 188 const UInt_t ms = tv.tv_usec%1000; 189 190 Set(mjd, tm, ms*1000); 191 } 192 193 // -------------------------------------------------------------------------- 194 // 195 // Set MTime to time expressed as in CT1 PreProc files 196 // 197 void MTime::SetCT1Time(UInt_t mjd, UInt_t t1, UInt_t t0) 198 { 199 // int isecs_since_midday; // seconds passed since midday before sunset (JD of run start) 200 // int isecfrac_200ns; // fractional part of isecs_since_midday 201 // fTime->SetTime(isecfrac_200ns, isecs_since_midday); 202 fNanoSec = (200*t1)%1000000; 203 const ULong_t ms = (200*t1)/1000000 + t0+12*kHour; 204 205 fTime = (Long_t)(ms<13*kHour ? ms : ms-kDay); 206 207 fMjd = mjd+1; 208 } 209 210 // -------------------------------------------------------------------------- 211 // 212 // Set the time to the current system time. The timezone is ignored. 213 // If everything is set correctly you'll get UTC. 214 // 89 215 void MTime::Now() 90 216 { 91 217 #ifdef __LINUX__ 92 218 struct timeval tv; 93 gettimeofday(&tv, NULL); 94 SetSystemTimer(tv); 219 if (gettimeofday(&tv, NULL)<0) 220 Reset(); 221 else 222 Set(tv); 95 223 #else 96 224 Reset(); … … 98 226 } 99 227 228 // -------------------------------------------------------------------------- 229 // 230 // Return contents as a TString of the form: 231 // "[yy]yy/mm/dd [h]h:mm:ss.fff" 232 // 233 TString MTime::GetString() const 234 { 235 UShort_t y, ms; 236 Byte_t mon, d, h, m, s; 237 238 GetDate(y, mon, d); 239 GetTime(h, m, s, ms); 240 241 return TString(Form("%2d/%02d/%02d %d:%02d:%02d.%03d", y, mon, d, h, m, s, ms)); 242 } 243 244 // -------------------------------------------------------------------------- 245 // 246 // Return contents as a TString of the form: 247 // "yyyymmdd_hhmmss" 248 // 249 TString MTime::GetFileName() const 250 { 251 UShort_t y; 252 Byte_t mon, d, h, m, s; 253 254 GetDate(y, mon, d); 255 GetTime(h, m, s); 256 257 return TString(Form("%04d%02d%02d_%02d%02d%02d", y, mon, d, h, m, s)); 258 } 259 260 // -------------------------------------------------------------------------- 261 // 262 // Print MTime as string 263 // 100 264 void MTime::Print(Option_t *) const 101 265 { … … 107 271 108 272 *fLog << GetDescriptor() << ": "; 109 *fLog << Form("%4d/%02d/%02d %02d:%02d:%02d.%03d (+%dns)", 110 yea, mon, day, h, m, s, ms, fNanoSec) << endl; 273 *fLog << GetString() << Form(" (+%dns)", fNanoSec) << endl; 111 274 } -
trunk/MagicSoft/Mars/mbase/MTime.h
r2607 r2612 30 30 31 31 private: 32 static const UInt_t kHour; // [ms] one hour 33 static const UInt_t kDay; // [ms] one day 34 32 35 UInt_t fMjd; // [d] Day in the century (Day of sun rise) 33 36 TTime fTime; // [ms] Time of Day (-11h<=x<13h) … … 36 39 ULong_t GetTime24() const 37 40 { 38 const ULong_t hor = 3600000; // One Hour in milliseconds 39 return (Long_t)fTime<0 ? (Long_t)fTime+24*hor : (ULong_t)fTime; 41 return (Long_t)fTime<0 ? (Long_t)fTime+kDay : (ULong_t)fTime; 42 } 43 void Init(const char *name, const char *title) 44 { 45 fName = name ? name : "MTime"; 46 fTitle = title ? title : "Generalized time stamp"; 40 47 } 41 48 … … 43 50 MTime(const char *name=NULL, const char *title=NULL) 44 51 { 45 fName = name ? name : ClassName(); 46 fTitle = title; 47 52 Init(name, title); 48 53 Reset(); 49 54 } 55 MTime(const struct timeval &tm) 56 { 57 Init(NULL, NULL); 58 Set(tm); 59 } 60 MTime(const MTime& t) : fMjd(t.fMjd), fTime(t.fTime), fNanoSec(t.fNanoSec) 61 { 62 Init(NULL, NULL); 63 } 50 64 51 MTime(MTime& t) { *this = t; } 52 53 void Reset() { fMjd=0; fTime=0; fNanoSec=0; } 54 55 void operator=(MTime &t) 65 void operator=(const MTime &t) 56 66 { 57 67 fMjd = t.fMjd; … … 60 70 } 61 71 72 void Reset() { fMjd=0; fTime=0; fNanoSec=0; } 73 62 74 void Print(Option_t *t=NULL) const; 63 75 64 76 void Now(); 65 77 66 Double_t GetMjd() const 78 Bool_t Set(UInt_t mjd, ULong_t ms, UInt_t ns=0); 79 Bool_t Set(UShort_t y, Byte_t m, Byte_t d, Byte_t h, Byte_t min, Byte_t s, UShort_t ms, UInt_t ns=0); 80 void Set(const struct timeval &tv); 81 void SetCT1Time(UInt_t mjd, UInt_t t1, UInt_t t0); 82 void SetMjd(Double_t m); 83 Double_t GetMjd() const; 84 TString GetString() const; 85 TString GetFileName() const; 86 void GetDate(UShort_t &y, Byte_t &m, Byte_t &d) const; 87 TTime GetRootTime() const; 88 Double_t GetAxisTime() const; 89 Long_t GetTime() const { return (Long_t)fTime; } // [ms] Time of Day returned in the range [-11h, 13h) 90 void GetTime(Byte_t &h, Byte_t &m, Byte_t &s, UShort_t &ms) const; 91 void GetTime(Byte_t &h, Byte_t &m, Byte_t &s) const 67 92 { 68 return fMjd+((Long_t)fTime+fNanoSec/1e6)/1000; 93 UShort_t ms; 94 GetTime(h, m, s, ms); 69 95 } 70 96 71 void SetSystemTimer(const struct timeval &tv); 97 UInt_t Year() const { UShort_t y; Byte_t m, d; GetDate(y,m,d); return y; } 98 UInt_t Month() const { UShort_t y; Byte_t m, d; GetDate(y,m,d); return m; } 99 UInt_t Day() const { UShort_t y; Byte_t m, d; GetDate(y,m,d); return d; } 100 UInt_t Hour() const { Byte_t h, m, s; GetTime(h,m,s); return h; } 101 UInt_t Min() const { Byte_t h, m, s; GetTime(h,m,s); return m; } 102 UInt_t Sec() const { Byte_t h, m, s; GetTime(h,m,s); return s; } 72 103 73 Bool_t Set(UInt_t mjd, ULong_t ms, UInt_t ns=0)104 operator double() const //[s] 74 105 { 75 const ULong_t hor = 3600000; // One Hour in milliseconds 76 if (ms>24*hor-1 || ns>999999) 77 return kFALSE; 78 79 const Bool_t am = ms < hor*13; 80 81 fMjd = am ? mjd : mjd + 1; 82 fTime = (Long_t)(am ? ms : ms - hor*24); 83 fNanoSec = ns%1000000; 84 85 return kTRUE; 86 } 87 88 Bool_t Set(UShort_t y, Byte_t m, Byte_t d, Byte_t h, Byte_t min, Byte_t s, UShort_t ms, UInt_t ns=0); 89 90 void SetCT1Time(UInt_t mjd, UInt_t t1, UInt_t t0) 91 { 92 // int isecs_since_midday; // seconds passed since midday before sunset (JD of run start) 93 // int isecfrac_200ns; // fractional part of isecs_since_midday 94 // fTime->SetTime(isecfrac_200ns, isecs_since_midday); 95 const ULong_t hor = 3600000; // One Hour in milliseconds 96 97 fNanoSec = (200*t1)%1000000; 98 const ULong_t ms = (200*t1)/1000000 + t0+12*hor; 99 100 fTime = (Long_t)(ms<13*hor ? ms : ms-24*hor); 101 102 fMjd = mjd+1; 103 } 104 /* 105 void SetSystemTime(const TTime &tm) 106 { 107 fTime = (ULong_t)tm%(24*hor); 108 } 109 */ 110 operator double() const //[s] 111 { 112 return fMjd*24*3600+((Long_t)fTime+fNanoSec*1e6)*1000; 106 return ((Double_t)fMjd*kDay+(Long_t)fTime+fNanoSec/1e6)/1000; 113 107 } 114 108 double operator()() const //[s] 115 109 { 116 110 return operator double(); 117 }118 119 void GetDate(UShort_t &y, Byte_t &m, Byte_t &d) const;120 void GetTime(Byte_t &h, Byte_t &m, Byte_t &s, UShort_t &ms) const // Time of Day returned in the range [0h, 24h)121 {122 Long_t tm = GetTime24();123 ms = tm%1000;124 tm /= 1000;125 s = tm%60;126 tm /= 60;127 m = tm%60;128 tm /= 60;129 h = tm;130 }131 Long_t GetTime() const // Time of Day returned in the range [-11h, 13h)132 { return (Long_t)fTime; }133 134 void GetTime(Byte_t &h, Byte_t &m, Byte_t &s) const135 {136 UShort_t ms;137 GetTime(h, m, s, ms);138 111 } 139 112 … … 151 124 return false; 152 125 } 126 153 127 inline bool operator>(MTime &t1, MTime &t2) 154 128 { … … 161 135 return false; 162 136 } 137 163 138 inline bool operator<=(MTime &t1, MTime &t2) 164 139 { 165 140 return !(t1>t2); 166 141 } 142 167 143 inline bool operator>=(MTime &t1, MTime &t2) 168 144 { 169 145 return !(t1<t2); 170 146 } 147 171 148 inline bool operator==(MTime &t1, MTime &t2) 172 149 { 173 150 return t1.fNanoSec==t2.fNanoSec && t1.fTime==t2.fTime && t1.fMjd==t2.fMjd; 174 151 } 152 175 153 inline bool operator!=(MTime &t1, MTime &t2) 176 154 { 177 155 return t1.fNanoSec!=t2.fNanoSec || t1.fTime!=t2.fTime || t1.fMjd!=t2.fMjd; 178 156 } 157 158 inline ostream &operator<<(ostream &out, MTime &t) 159 { 160 out << t.GetString(); 161 return out; 162 } 163 179 164 #endif
Note:
See TracChangeset
for help on using the changeset viewer.