Changeset 3148 for trunk/MagicSoft/Mars/mbase
- Timestamp:
- 02/14/04 11:48:43 (21 years ago)
- Location:
- trunk/MagicSoft/Mars/mbase
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/MagicSoft/Mars/mbase/MTime.cc
r3138 r3148 29 29 // A generalized MARS time stamp. 30 30 // 31 // 32 // We do not use floating point values here, because of several reasons: 33 // - having the times stored in integers only is more accurate and 34 // more reliable in comparison conditions 35 // - storing only integers gives similar bit-pattern for similar times 36 // which makes compression (eg gzip algorithm in TFile) more 37 // successfull 38 // 39 // Note, that there are many conversion function converting the day time 40 // into a readable string. Also a direct interface to SQL time strings 41 // is available. 42 // 43 // If you are using MTime containers as axis lables in root histograms 44 // use GetAxisTime(). Make sure that you use the correct TimeFormat 45 // on your TAxis (see GetAxisTime()) 46 // 47 // 31 48 // WARNING: Be carefull changing this class. It is also used in the 32 49 // MAGIC drive software cosy! … … 34 51 // Remarke: If you encounter strange behaviour, check the casting. 35 52 // Note, that on Linux machines ULong_t and UInt_t is the same. 53 // 36 54 // 37 55 // Version 1: … … 52 70 53 71 #include <iomanip> 72 73 #include <time.h> // struct tm 54 74 #include <sys/time.h> // struct timeval 55 75 … … 65 85 const UInt_t MTime::kHour = 3600000; // [ms] one hour 66 86 const UInt_t MTime::kDay = MTime::kHour*24; // [ms] one day 87 88 // -------------------------------------------------------------------------- 89 // 90 // Constructor. Calls SetMjd(d) for d>0 in all other cases the time 91 // is set to the current UTC time. 92 // 93 MTime::MTime(Double_t d) 94 { 95 Init(0, 0); 96 if (d<=0) 97 Now(); 98 else 99 SetMjd(d); 100 } 67 101 68 102 // -------------------------------------------------------------------------- … … 192 226 193 227 Set(mjd, tm, ms*1000); 228 } 229 230 // -------------------------------------------------------------------------- 231 // 232 // Return contents as a TString of the form: 233 // "dd.mm.yyyy hh:mm:ss.fff" 234 // 235 Bool_t MTime::SetString(const char *str) 236 { 237 if (!str) 238 return kFALSE; 239 240 UInt_t y, mon, d, h, m, s, ms; 241 const Int_t n = sscanf(str, "%02u.%02u.%04u %02u:%02u:%02u.%03u", 242 &d, &mon, &y, &h, &m, &s, &ms); 243 244 return n==7 ? Set(y, mon, d, h, m, s, ms) : kFALSE; 245 } 246 247 // -------------------------------------------------------------------------- 248 // 249 // Return contents as a TString of the form: 250 // "yyyy-mm-dd hh:mm:ss" 251 // 252 Bool_t MTime::SetSqlDateTime(const char *str) 253 { 254 if (!str) 255 return kFALSE; 256 257 UInt_t y, mon, d, h, m, s; 258 const Int_t n = sscanf(str, "%04u-%02u-%02u %02u:%02u:%02u", 259 &y, &mon, &d, &h, &m, &s); 260 261 return n==6 ? Set(y, mon, d, h, m, s) : kFALSE; 262 } 263 264 // -------------------------------------------------------------------------- 265 // 266 // Return contents as a TString of the form: 267 // "yyyymmddhhmmss" 268 // 269 Bool_t MTime::SetSqlTimeStamp(const char *str) 270 { 271 if (!str) 272 return kFALSE; 273 274 UInt_t y, mon, d, h, m, s; 275 const Int_t n = sscanf(str, "%04u%02u%02u%02u%02u%02u", 276 &y, &mon, &d, &h, &m, &s); 277 278 return n==6 ? Set(y, mon, d, h, m, s) : kFALSE; 194 279 } 195 280 … … 255 340 // 256 341 // Return contents as a TString of the form: 257 // " yyyy/mm/ddhh:mm:ss.fff"342 // "dd.mm.yyyy hh:mm:ss.fff" 258 343 // 259 344 TString MTime::GetString() const … … 265 350 GetTime(h, m, s, ms); 266 351 267 return TString(Form("%4d/%02d/%02d %02d:%02d:%02d.%03d", y, mon, d, h, m, s, ms)); 352 return TString(Form("%02d.%02d.%04d %02d:%02d:%02d.%03d", d, mon, y, h, m, s, ms)); 353 } 354 355 // -------------------------------------------------------------------------- 356 // 357 // Return contents as a string format'd with strftime: 358 // Here is a short summary of the most important formats. For more 359 // information see the man page (or any other description) of 360 // strftime... 361 // 362 // %a The abbreviated weekday name according to the current locale. 363 // %A The full weekday name according to the current locale. 364 // %b The abbreviated month name according to the current locale. 365 // %B The full month name according to the current locale. 366 // %c The preferred date and time representation for the current locale. 367 // %d The day of the month as a decimal number (range 01 to 31). 368 // %e Like %d, the day of the month as a decimal number, 369 // but a leading zero is replaced by a space. 370 // %H The hour as a decimal number using a 24-hour clock (range 00 to 23) 371 // %k The hour (24-hour clock) as a decimal number (range 0 to 23); 372 // single digits are preceded by a blank. 373 // %m The month as a decimal number (range 01 to 12). 374 // %M The minute as a decimal number (range 00 to 59). 375 // %R The time in 24-hour notation (%H:%M). For a 376 // version including the seconds, see %T below. 377 // %S The second as a decimal number (range 00 to 61). 378 // %T The time in 24-hour notation (%H:%M:%S). 379 // %x The preferred date representation for the current 380 // locale without the time. 381 // %X The preferred time representation for the current 382 // locale without the date. 383 // %y The year as a decimal number without a century (range 00 to 99). 384 // %Y The year as a decimal number including the century. 385 // %+ The date and time in date(1) format. 386 // 387 // The default is: Tuesday 16.February 2004 12:17:22 388 // 389 // The maximum size of the return string is 128 (incl. NULL) 390 // 391 TString MTime::GetStringFmt(const char *fmt) const 392 { 393 if (!fmt) 394 fmt = "%A %e.%B %Y %H:%M:%S"; 395 396 UShort_t y, ms; 397 Byte_t mon, d, h, m, s; 398 399 GetDate(y, mon, d); 400 GetTime(h, m, s, ms); 401 402 struct tm time; 403 time.tm_sec = s; 404 time.tm_min = m; 405 time.tm_hour = h; 406 time.tm_mday = d; 407 time.tm_mon = mon-1; 408 time.tm_year = y-1900; 409 time.tm_isdst = 0; 410 411 // recalculate tm_yday and tm_wday 412 mktime(&time); 413 414 char ret[128]; 415 return TString(strftime(ret, 127, fmt, &time) ? ret : ""); 416 } 417 418 // -------------------------------------------------------------------------- 419 // 420 // Return contents as a TString of the form: 421 // "yyyy-mm-dd hh:mm:ss" 422 // 423 TString MTime::GetSqlDateTime() const 424 { 425 return GetStringFmt("%Y-%m-%d %H:%M:%S"); 426 } 427 428 // -------------------------------------------------------------------------- 429 // 430 // Return contents as a TString of the form: 431 // "yyyymmddhhmmss" 432 // 433 TString MTime::GetSqlTimeStamp() const 434 { 435 return GetStringFmt("%Y%m%d%H%M%S"); 268 436 } 269 437 … … 275 443 TString MTime::GetFileName() const 276 444 { 277 UShort_t y; 278 Byte_t mon, d, h, m, s; 279 280 GetDate(y, mon, d); 281 GetTime(h, m, s); 282 283 return TString(Form("%04d%02d%02d_%02d%02d%02d", y, mon, d, h, m, s)); 445 return GetStringFmt("%Y%m%d_%H%M%S"); 284 446 } 285 447 -
trunk/MagicSoft/Mars/mbase/MTime.h
r3138 r3148 56 56 Set(tm); 57 57 } 58 MTime(Double_t d); 58 59 MTime(const MTime& t) : fMjd(t.fMjd), fTime(t.fTime), fNanoSec(t.fNanoSec) 59 60 { … … 77 78 Bool_t Set(UShort_t y, Byte_t m, Byte_t d, Byte_t h=13, Byte_t min=0, Byte_t s=0, UShort_t ms=0, UInt_t ns=0); 78 79 void Set(const struct timeval &tv); 80 Bool_t SetString(const char *str); 81 Bool_t SetSqlDateTime(const char *str); 82 Bool_t SetSqlTimeStamp(const char *str); 79 83 void SetCT1Time(UInt_t mjd, UInt_t t1, UInt_t t0); 80 84 Bool_t UpdMagicTime(Byte_t h, Byte_t m, Byte_t s, UInt_t ns); … … 82 86 Double_t GetMjd() const; 83 87 TString GetString() const; 88 TString GetStringFmt(const char *fmt=0) const; 89 TString GetSqlDateTime() const; 90 TString GetSqlTimeStamp() const; 84 91 TString GetFileName() const; 85 92 void GetDate(UShort_t &y, Byte_t &m, Byte_t &d) const;
Note:
See TracChangeset
for help on using the changeset viewer.