KaliVeda  1.13/01
Heavy-Ion Analysis Toolkit
KVDatime.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2 $Id: KVDatime.cpp,v 1.7 2009/01/15 16:10:13 franklan Exp $
3 $Revision: 1.7 $
4 $Date: 2009/01/15 16:10:13 $
5 $Author: franklan $
6 *******************************************************************************/
7 
8 #include "KVDatime.h"
9 #include "KVList.h"
10 #include "KVString.h"
11 #include "TObjArray.h"
12 #include "TObjString.h"
13 #include "KVError.h"
14 
16 
19 
20 
24 
26 {
27  //called by all ctors
28  //sets up static list of months' names if not already done
29  if (!fmonths) {
30  fmonths = new KVList;
31  fmonths->Add(new TObjString("JAN"));
32  fmonths->Add(new TObjString("FEB"));
33  fmonths->Add(new TObjString("MAR"));
34  fmonths->Add(new TObjString("APR"));
35  fmonths->Add(new TObjString("MAY"));
36  fmonths->Add(new TObjString("JUN"));
37  fmonths->Add(new TObjString("JUL"));
38  fmonths->Add(new TObjString("AUG"));
39  fmonths->Add(new TObjString("SEP"));
40  fmonths->Add(new TObjString("OCT"));
41  fmonths->Add(new TObjString("NOV"));
42  fmonths->Add(new TObjString("DEC"));
43  }
44  ndatime++;
45  fStr = "";
46 }
47 
48 
49 
51 
53  : TDatime()
54 {
55  init();
56 }
57 
58 
59 
101 
103  : TDatime()
104 {
105  //if f = KVDatime::kGANACQ:
106  //
107  //Decodes GANIL acquisition (INDRA) run-sheet format date into a TDatime
108  //
109  //Format of date string is: `29-SEP-2005 09:42:17.00`
110  //
111  //if f = KVDatime::kGANACQ2010:
112  //
113  //Decodes GANIL acquisition runfile format date into a TDatime
114  //
115  //Format of date string is: `27Nov10_02h07m40s`
116  //
117  //if f = KVDatime::kGANACQNarval:
118  //
119  //Decodes GANIL Narval acquisition runfile format date into a TDatime
120  //
121  //Format of date string is: `12-04-18_17h09m41s`
122  //
123  //if f = KVDatime::kSQL:
124  //
125  //Decodes SQL format date into a TDatime (i.e. same format as returned
126  //by TDatime::AsSQLString(): `2007-05-02 14:52:18`)
127  //
128  //if f = KVDatime::kSRB:
129  //
130  //Decodes SRB format date into a TDatime
131  //
132  //Format of date string is: `2008-12-19-15.21`
133  //
134  //if f = KVDatime::kIRODS:
135  //
136  //Decodes IRODS format date into a TDatime
137  //
138  //Format of date string is: `2008-12-19.15:21`
139  //
140  //if f = KVDatime::kDMY:
141  //
142  //Decodes DMY format date into a TDatime
143  //
144  //Format of date string is: `19/12/2008`
145 
146  init();
147  switch (f) {
148  case kGANACQ:
149  SetGanacqDate(DateString);
150  break;
151  case kGANACQ2010:
152  SetGanacq2010Date(DateString);
153  break;
154  case kGANACQNarval:
155  SetGanacqNarvalDate(DateString);
156  break;
157  case kSQL:
158  SetSQLDate(DateString);
159  break;
160  case kSRB:
161  SetSRBDate(DateString);
162  break;
163  case kIRODS:
164  SetIRODSDate(DateString);
165  break;
166  case kDMY:
167  SetDMYDate(DateString);
168  break;
169  default:
170  Error(KV__ERROR(KVDatime), "Unknown date format");
171  }
172 }
173 
174 
175 
178 
180 {
181  // copy assignment operator
182  return dynamic_cast<KVDatime&>(TDatime::operator=(d));
183 }
184 
185 
186 
188 
189 KVDatime::~KVDatime()
190 {
191  ndatime--;
192  if (!ndatime) {
193  delete fmonths;
194  fmonths = 0;
195  }
196 }
197 
198 
199 
203 
204 void KVDatime::SetSQLDate(const Char_t* DateString)
205 {
206  //Decodes SQL format date into a TDatime (i.e. same format as returned
207  //by TDatime::AsSQLString(): "2007-05-02 14:52:18")
208  Int_t Y, M, D, h, m, s;
209  sscanf(DateString, "%4d-%02d-%02d %02d:%02d:%02d",
210  &Y, &M, &D, &h, &m, &s);
211  Set(Y, M, D, h, m, s);
212 }
213 
214 
215 
220 
221 void KVDatime::SetSRBDate(const Char_t* DateString)
222 {
223  //Decodes SRB format date into a TDatime
224  //Format of date string is:
225  // 2008-12-19-15.21
226 
227  Int_t Y, M, D, h, m, s;
228  sscanf(DateString, "%4d-%02d-%02d-%02d.%02d",
229  &Y, &M, &D, &h, &m);
230  s = 0;
231  Set(Y, M, D, h, m, s);
232 }
233 
234 
235 
240 
241 void KVDatime::SetIRODSDate(const Char_t* DateString)
242 {
243  //Decodes IRODS format date into a TDatime
244  //Format of date string is:
245  // 2008-12-19.15:21
246 
247  Int_t Y, M, D, h, m, s;
248  sscanf(DateString, "%4d-%02d-%02d.%02d:%02d",
249  &Y, &M, &D, &h, &m);
250  s = 0;
251  Set(Y, M, D, h, m, s);
252 }
253 
254 
255 
258 
259 void KVDatime::SetDMYDate(const Char_t* DMYString)
260 {
261  // Set date from string in format "DD/MM/YYYY"
262  Int_t Y, M, D;
263  sscanf(DMYString, "%2d/%2d/%4d", &D, &M, &Y);
264  Set(Y, M, D, 0, 0, 0);
265 }
266 
267 
268 
272 
273 void KVDatime::SetGanacq2010Date(const Char_t* GanacqDateString)
274 {
275  // Decodes dates in new (2010) format of GANIL acquisition
276  // run files, e.g. run_0058.dat.27Nov10_02h07m40s
277  Int_t Y, D, H, M, S;
278  Char_t Month[5];
279  TString tmp(GanacqDateString);
280  tmp.ToUpper();
281  tmp.ReplaceAll("_", " ");
282  if (sscanf(tmp.Data(), "%02d%3s%02d %02dH%02dM%02dS", &D, Month, &Y, &H, &M, &S) == 6) {
284  Int_t month = 0;
285  if (mm) month = fmonths->IndexOf(mm) + 1;
286  Y += 2000;
287  Set(Y, month, D, H, M, S);
288  }
289 }
290 
291 
292 
296 
297 void KVDatime::SetGanacqNarvalDate(const Char_t* GanacqDateString)
298 {
299  // Decodes dates in format of GANIL Narval acquisition
300  // run files, e.g. run_0058.dat.27-11-10_02h07m40s
301  Int_t Y, D, H, M, S, Month;
302  TString tmp(GanacqDateString);
303  tmp.ToUpper();
304  tmp.ReplaceAll("_", " ");
305  if (sscanf(tmp.Data(), "%02d-%02d-%02d %02dH%02dM%02dS", &D, &Month, &Y, &H, &M, &S) == 6) {
306  Y += 2000;
307  Set(Y, Month, D, H, M, S);
308  }
309 }
310 
311 
312 
322 
323 void KVDatime::SetGanacqDate(const Char_t* GanacqDateString)
324 {
325  //Decodes GANIL acquisition (INDRA) run-sheet format date into a TDatime
326  //Format of date string is:
327  // 29-SEP-2005 09:42:17.00
328  // or 29-SEP-2005 09:42:17
329  // or 29-SEP-05 09:42:17.00
330  // or 29-SEP-05 09:42:17
331  //
332  //If format is not respected, we set to current time & date
333 
334  KVString tmp(GanacqDateString);
335  TObjArray* toks = tmp.Tokenize("-:. ");
336  if (toks->GetEntries() < 6 || toks->GetEntries() > 7) {
337  // if format is correct, there should be 6 or 7 elements in toks
338  delete toks;
339  Error("SetGanacqDate", "Format is incorrect: %s (should be like \"29-SEP-2005 09:42:17.00\" or \"29-SEP-05 09:42:17\")",
340  GanacqDateString);
341  Set();
342  return;
343  }
344  KV__TOBJSTRING_TO_INT(toks, 0, day)
345  TObject* mm =
346  fmonths->FindObject(((TObjString*) toks->At(1))->String().Data());
347  Int_t month = 0;
348  if (mm)
349  month = fmonths->IndexOf(mm) + 1;
350  KV__TOBJSTRING_TO_INT(toks, 2, year)
351  // year may be written in shortened form: 97 instead of 1997
352  if (year < 100) {
353 // Warning("SetGanacqDate",
354 // "Ambiguous value for year: %d. Assuming this means: %d",
355 // year, (year < 82 ? year + 2000 : year + 1900));
356  (year < 82 ? year += 2000 : year += 1900);
357  }
358  KV__TOBJSTRING_TO_INT(toks, 3, hour)
359  KV__TOBJSTRING_TO_INT(toks, 4, min)
360  KV__TOBJSTRING_TO_INT(toks, 5, sec)
361  delete toks;
362  Set(year, month, day, hour, min, sec);
363 }
364 
365 
366 
370 
372 {
373  //Return date and time string with format "29-SEP-2005 09:42:17.00"
374  //Copy the string immediately if you want to reuse/keep it
375  return Form("%d-%s-%4d %02d:%02d:%02d.00",
376  GetDay(),
377  ((TObjString*) fmonths->At(GetMonth() - 1))->String().
378  Data(), GetYear(), GetHour(), GetMinute(), GetSecond());
379 }
380 
381 
382 
384 
386 {
387  return Form("%02d/%02d/%4d",
388  GetDay(),
389  GetMonth(), GetYear());
390 }
391 
392 
393 
400 
402 {
403  // Returns date & time as a string in required format:
404  // fmt = kCTIME (default) : ctime format e.g. Thu Apr 10 10:48:34 2008
405  // fmt = kSQL : SQL format e.g. 1997-01-15 20:16:28
406  // fmt = kGANACQ : GANIL acquisition format e.g. 29-SEP-2005 09:42:17.00
407  // fmt = kDMY : DD/MM/YYYY
408  switch (fmt) {
409  case kCTIME:
410  fStr = AsString();
411  break;
412  case kSQL:
413  fStr = AsSQLString();
414  break;
415  case kGANACQ:
417  break;
418  case kDMY:
419  fStr = AsDMYDateString();
420  break;
421  default:
422  fStr = "";
423  }
424  return fStr.Data();
425 }
426 
427 
428 
432 
434 {
435  // Static method, returns kTRUE if 'date' is in format of GANIL acquisition
436  // e.g. 29-SEP-2005 09:42:17.00
437 
438  KVString tmp(date);
439  TObjArray* toks = tmp.Tokenize("-:. ");
440  if (toks->GetEntries() < 6 || toks->GetEntries() > 7) {
441  // if format is correct, there should be 6 or 7 elements in toks
442  delete toks;
443  return kFALSE;
444  }
445  delete toks;
446  return kTRUE;
447 }
448 
449 
450 
454 
456 {
457  // Static method, returns kTRUE if 'date' is in new (2010) format of GANIL acquisition
458  // run files, e.g. run_0058.dat.27Nov10_02h07m40s
459 
460  Int_t Y, D, H, M, S;
461  Char_t Month[5];
462  TString tmp(date);
463  tmp.ToUpper();
464  tmp.ReplaceAll("_", " ");
465  if (sscanf(tmp.Data(), "%02d%3s%02d %02dH%02dM%02dS", &D, Month, &Y, &H, &M, &S) == 6)
466  return kTRUE;
467  return kFALSE;
468 }
469 
470 
471 
475 
477 {
478  // Static method, returns kTRUE if 'date' is in format of GANIL Narval acquisition
479  // run files, e.g. run_0058.dat.27-11-10_02h07m40s
480 
481  Int_t Y, D, H, M, S, Month;
482  TString tmp(date);
483  tmp.ToUpper();
484  tmp.ReplaceAll("_", " ");
485  if (sscanf(tmp.Data(), "%02d-%02d-%02d %02dH%02dM%02dS", &D, &Month, &Y, &H, &M, &S) == 6)
486  return kTRUE;
487  return kFALSE;
488 }
489 
490 
491 
495 
497 {
498  // Static method, returns kTRUE if 'date' is in SQL format
499  // e.g. 2007-05-02 14:52:18
500 
501  Int_t Y, M, D, h, m, s;
502  if (sscanf(date, "%4d-%02d-%02d %02d:%02d:%02d",
503  &Y, &M, &D, &h, &m, &s) != 6) return kFALSE;
504  return kTRUE;
505 }
506 
507 
508 
512 
514 {
515  // Static method, returns kTRUE if 'date' is in SRB format
516  // e.g. 2008-12-19-15.21
517 
518  Int_t Y, M, D, h, m;
519  if (sscanf(date, "%4d-%02d-%02d-%02d.%02d",
520  &Y, &M, &D, &h, &m) != 5) return kFALSE;
521  return kTRUE;
522 }
523 
524 
525 
529 
531 {
532  // Static method, returns kTRUE if 'date' is in IRODS format
533  // e.g. 2008-12-19.15:21
534 
535  Int_t Y, M, D, h, m;
536  if (sscanf(date, "%4d-%02d-%02d.%02d:%02d",
537  &Y, &M, &D, &h, &m) != 5) return kFALSE;
538  return kTRUE;
539 }
540 
541 
542 
544 
546 {
547  return ((TObjString*) fmonths->At(m - 1))->String().Data();
548 }
549 
550 
551 
553 
555 {
556 
557  Double_t total_ins = 0;
558 
559  Double_t year_ins = GetYear();
560  year_ins -= ref_year;
561 
562  year_ins *= 365.25; //1y=365.25 d
563  year_ins *= 24; //1d=24 h
564  year_ins *= 3660; //1h=60x60=3660 s
565 
566  total_ins += year_ins;
567 
568  //nombre de jours depuis le debut de l annee
569  Double_t month_ins = GetMonth() - 1;
570  month_ins *= 365.25 / 12; //1month = 30.437d (365.25/12) approximatif
571  month_ins += GetDay(); //-> nombre de jours
572  month_ins *= 24; //1d=24 h
573  month_ins *= 3660; //1h=60x60=3660 s
574 
575  total_ins += month_ins; //nombre de jours totals en second
576 
577  total_ins += GetHour() * 3600;
578  total_ins += GetMinute() * 60;
579  total_ins += GetSecond();
580 
581  return total_ins;
582 
583 
584 }
585 
586 
587 
589 
591 {
592  Int_t year_ref = from.GetYear();
593  if (year_ref > this->GetYear()) year_ref = this->GetYear();
594 
595  Double_t sum = from.GetNumberOfSeconds(year_ref);
596  return this->GetNumberOfSeconds(year_ref) - sum;
597 }
598 
599 
int Int_t
Defines macros for standard error messages.
#define KV__ERROR(method)
Definition: KVError.h:30
KVIonRangeTableMaterial * M
ClassImp(KVPartitionList) void KVPartitionList
Initialisation.
#define KV__TOBJSTRING_TO_INT(arr, index, var)
Definition: KVString.h:17
#define d(i)
#define f(i)
char Char_t
const Bool_t kFALSE
bool Bool_t
double Double_t
const Bool_t kTRUE
char * Form(const char *fmt,...)
Extension of TDatime to handle various useful date formats.
Definition: KVDatime.h:32
static const Char_t * Month(Int_t m)
Definition: KVDatime.cpp:545
Double_t GetNumberOfSeconds(Int_t ref_year=0)
Definition: KVDatime.cpp:554
void SetDMYDate(const Char_t *DMYString)
Set date from string in format "DD/MM/YYYY".
Definition: KVDatime.cpp:259
void SetIRODSDate(const Char_t *IRODSDateString)
Definition: KVDatime.cpp:241
EKVDateFormat
Definition: KVDatime.h:40
@ kSRB
Definition: KVDatime.h:44
@ kGANACQ2010
Definition: KVDatime.h:47
@ kIRODS
Definition: KVDatime.h:45
@ kGANACQ
Definition: KVDatime.h:42
@ kDMY
Definition: KVDatime.h:46
@ kSQL
Definition: KVDatime.h:43
@ kCTIME
Definition: KVDatime.h:41
@ kGANACQNarval
Definition: KVDatime.h:48
static Bool_t IsGANACQFormat(const Char_t *date)
Definition: KVDatime.cpp:433
static Bool_t IsSRBFormat(const Char_t *date)
Definition: KVDatime.cpp:513
TString fStr
internal buffer used by String method
Definition: KVDatime.h:36
KVDatime()
Definition: KVDatime.cpp:52
const Char_t * String(EKVDateFormat fmt=kCTIME)
Definition: KVDatime.cpp:401
static Bool_t IsSQLFormat(const Char_t *date)
Definition: KVDatime.cpp:496
static Bool_t IsGANACQ2010Format(const Char_t *date)
Definition: KVDatime.cpp:455
void init()
Definition: KVDatime.cpp:25
void SetSQLDate(const Char_t *SQLDateString)
Definition: KVDatime.cpp:204
void SetGanacqNarvalDate(const Char_t *GanacqDateString)
Definition: KVDatime.cpp:297
const Char_t * AsDMYDateString() const
Definition: KVDatime.cpp:385
KVDatime & operator=(const KVDatime &)
copy assignment operator
Definition: KVDatime.cpp:179
const Char_t * AsGanacqDateString() const
Definition: KVDatime.cpp:371
void SetSRBDate(const Char_t *SRBDateString)
Definition: KVDatime.cpp:221
static Int_t ndatime
counter
Definition: KVDatime.h:34
static Bool_t IsGANACQNarvalFormat(const Char_t *date)
Definition: KVDatime.cpp:476
static KVList * fmonths
list of months
Definition: KVDatime.h:33
static Bool_t IsIRODSFormat(const Char_t *date)
Definition: KVDatime.cpp:530
void SetGanacq2010Date(const Char_t *GanacqDateString)
Definition: KVDatime.cpp:273
Double_t GetDureeInSeconds(KVDatime from)
Definition: KVDatime.cpp:590
void SetGanacqDate(const Char_t *GanacqDateString)
Definition: KVDatime.cpp:323
Extended TList class which owns its objects by default.
Definition: KVList.h:27
virtual TObject * At(Int_t idx) const
virtual void Add(TObject *obj)
virtual TObject * FindObject(const char *name) const
Extension of ROOT TString class which allows backwards compatibility with ROOT v3....
Definition: KVString.h:72
Int_t GetMonth() const
Int_t GetDay() const
Int_t GetHour() const
Int_t GetSecond() const
const char * AsSQLString() const
Int_t GetYear() const
Int_t GetMinute() const
void Set()
TDatime & operator=(const TDatime &d)
const char * AsString() const
Int_t GetEntries() const
TObject * At(Int_t idx) const
virtual Int_t IndexOf(const TObject *obj) const
void ToUpper()
TObjArray * Tokenize(const TString &delim) const
const char * Data() const
TString & ReplaceAll(const char *s1, const char *s2)
TH1 * h
const long double s
Definition: KVUnits.h:94
const long double mm
Definition: KVUnits.h:69
const long double m
Definition: KVUnits.h:70
void Error(const char *location, const char *va_(fmt),...)
RooArgSet S(Args_t &&... args)
constexpr Double_t H()