KaliVeda
Toolkit for HIC analysis
KVDatime.cpp
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 #include "KVError.h"
8 #include "KVDatime.h"
9 #include "KVString.h"
10 
12 
13 const std::vector<std::string> KVDatime::fNumToMonth = {"JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"};
14 std::unordered_map<std::string,int> KVDatime::fMonthToNum;
15 
16 
19 
21 {
22  // set up static map of month names to month numbers if needed
23  if(fMonthToNum.empty())
24  {
25  int mth=1;
26  for(auto M : fNumToMonth)
27  fMonthToNum[M]=mth++;
28  }
29 }
30 
31 
32 
34 
35 std::optional<int> KVDatime::get_month_from_name(const char* Month)
36 {
37  if(fMonthToNum.find(Month) == std::end(fMonthToNum))
38  {
39  KVError::Error(this,"get_month_from_name",
40  "Unrecognised month in date (%s)", Month);
41  return {};
42  }
43  return fMonthToNum[Month];
44 }
45 
46 
47 
49 
51  : TDatime()
52 {
53 }
54 
55 
56 
98 
100  : KVDatime()
101 {
102  //if f = KVDatime::kGANACQ:
103  //
104  //Decodes GANIL acquisition (INDRA) run-sheet format date into a TDatime
105  //
106  //Format of date string is: `29-SEP-2005 09:42:17.00`
107  //
108  //if f = KVDatime::kGANACQ2010:
109  //
110  //Decodes GANIL acquisition runfile format date into a TDatime
111  //
112  //Format of date string is: `27Nov10_02h07m40s`
113  //
114  //if f = KVDatime::kGANACQNarval:
115  //
116  //Decodes GANIL Narval acquisition runfile format date into a TDatime
117  //
118  //Format of date string is: `12-04-18_17h09m41s`
119  //
120  //if f = KVDatime::kSQL:
121  //
122  //Decodes SQL format date into a TDatime (i.e. same format as returned
123  //by TDatime::AsSQLString(): `2007-05-02 14:52:18`)
124  //
125  //if f = KVDatime::kSRB:
126  //
127  //Decodes SRB format date into a TDatime
128  //
129  //Format of date string is: `2008-12-19-15.21`
130  //
131  //if f = KVDatime::kIRODS:
132  //
133  //Decodes IRODS format date into a TDatime
134  //
135  //Format of date string is: `2008-12-19.15:21`
136  //
137  //if f = KVDatime::kDMY:
138  //
139  //Decodes DMY format date into a TDatime
140  //
141  //Format of date string is: `19/12/2008`
142 
143  switch (f) {
144  case kGANACQ:
145  SetGanacqDate(DateString);
146  break;
147  case kGANACQ2010:
148  SetGanacq2010Date(DateString);
149  break;
150  case kGANACQNarval:
151  SetGanacqNarvalDate(DateString);
152  break;
153  case kSQL:
154  SetSQLDate(DateString);
155  break;
156  case kSRB:
157  SetSRBDate(DateString);
158  break;
159  case kIRODS:
160  SetIRODSDate(DateString);
161  break;
162  case kDMY:
163  SetDMYDate(DateString);
164  break;
165  default:
166  {
167  KVError::Error(this, "KVDatime", "Unknown date format");
168  }
169  }
170 }
171 
172 
174  : TDatime(d)
175 {}
176 
178  : TDatime(d)
179 {}
180 
182  : TDatime(tloc,dosDate)
183 {}
184 
186  : TDatime(date,time)
187 {}
188 
189 KVDatime::KVDatime(Int_t year, Int_t month, Int_t day, Int_t hour, Int_t min, Int_t sec)
190  : TDatime(year,month,day,hour,min,sec)
191 {}
192 
193 
196 
198 {
199  // copy assignment operator
200  return dynamic_cast<KVDatime&>(TDatime::operator=(d));
201 }
202 
203 
204 
208 
209 void KVDatime::SetSQLDate(const Char_t* DateString)
210 {
211  //Decodes SQL format date into a TDatime (i.e. same format as returned
212  //by TDatime::AsSQLString(): "2007-05-02 14:52:18")
213  Int_t Y, M, D, h, m, s;
214  sscanf(DateString, "%4d-%02d-%02d %02d:%02d:%02d",
215  &Y, &M, &D, &h, &m, &s);
216  Set(Y, M, D, h, m, s);
217 }
218 
219 
220 
225 
226 void KVDatime::SetSRBDate(const Char_t* DateString)
227 {
228  //Decodes SRB format date into a TDatime
229  //Format of date string is:
230  // 2008-12-19-15.21
231 
232  Int_t Y, M, D, h, m, s;
233  sscanf(DateString, "%4d-%02d-%02d-%02d.%02d",
234  &Y, &M, &D, &h, &m);
235  s = 0;
236  Set(Y, M, D, h, m, s);
237 }
238 
239 
240 
245 
246 void KVDatime::SetIRODSDate(const Char_t* DateString)
247 {
248  //Decodes IRODS format date into a TDatime
249  //Format of date string is:
250  // 2008-12-19.15:21
251 
252  Int_t Y, M, D, h, m, s;
253  sscanf(DateString, "%4d-%02d-%02d.%02d:%02d",
254  &Y, &M, &D, &h, &m);
255  s = 0;
256  Set(Y, M, D, h, m, s);
257 }
258 
259 
260 
263 
264 void KVDatime::SetDMYDate(const Char_t* DMYString)
265 {
266  // Set date from string in format "DD/MM/YYYY"
267  Int_t Y, M, D;
268  sscanf(DMYString, "%2d/%2d/%4d", &D, &M, &Y);
269  Set(Y, M, D, 0, 0, 0);
270 }
271 
272 
273 
277 
278 void KVDatime::SetGanacq2010Date(const Char_t* GanacqDateString)
279 {
280  // Decodes dates in new (2010) format of GANIL acquisition
281  // run files, e.g. run_0058.dat.27Nov10_02h07m40s
282 
283  init_month_map();
284  Int_t Y, D, H, M, S;
285  Char_t Month[5];
286  TString tmp(GanacqDateString);
287  tmp.ToUpper();
288  tmp.ReplaceAll("_", " ");
289  if (sscanf(tmp.Data(), "%02d%3s%02d %02dH%02dM%02dS", &D, Month, &Y, &H, &M, &S) == 6)
290  {
291  if(auto imonth = get_month_from_name(Month))
292  {
293  Y += 2000;
294  Set(Y, *imonth, D, H, M, S);
295  }
296  }
297 }
298 
299 
300 
304 
305 void KVDatime::SetGanacqNarvalDate(const Char_t* GanacqDateString)
306 {
307  // Decodes dates in format of GANIL Narval acquisition
308  // run files, e.g. run_0058.dat.27-11-10_02h07m40s
309  Int_t Y, D, H, M, S, Month;
310  TString tmp(GanacqDateString);
311  tmp.ToUpper();
312  tmp.ReplaceAll("_", " ");
313  if (sscanf(tmp.Data(), "%02d-%02d-%02d %02dH%02dM%02dS", &D, &Month, &Y, &H, &M, &S) == 6) {
314  Y += 2000;
315  Set(Y, Month, D, H, M, S);
316  }
317 }
318 
319 
320 
330 
331 void KVDatime::SetGanacqDate(const Char_t* GanacqDateString)
332 {
333  //Decodes GANIL acquisition (INDRA) run-sheet format date into a TDatime
334  //Format of date string is:
335  // 29-SEP-2005 09:42:17.00
336  // or 29-SEP-2005 09:42:17
337  // or 29-SEP-05 09:42:17.00
338  // or 29-SEP-05 09:42:17
339  //
340  //If format is not respected, we set to current time & date
341 
342  if (!IsGANACQFormat(GanacqDateString))
343  {
344  // if format is correct, there should be 6 or 7 elements in toks
345  KVError::Error(this, "SetGanacqDate",
346  "Format is incorrect: %s (should be like \"29-SEP-2005 09:42:17.00\" or \"29-SEP-05 09:42:17\")",
347  GanacqDateString);
348  Set();
349  return;
350  }
351  KVString tmp(GanacqDateString);
352  tmp.Begin("-:. ");
353  auto day = tmp.Next().Atoi();
354  auto month = get_month_from_name(tmp.Next(true).Data());
355  if(!month){
356  Set();
357  return;
358  }
359  auto year = tmp.Next().Atoi();
360  // year may be written in shortened form: 97 instead of 1997
361  if (year < 100) {
362  (year < 82 ? year += 2000 : year += 1900);
363  }
364  auto hour = tmp.Next().Atoi();
365  auto min = tmp.Next().Atoi();
366  auto sec = tmp.Next().Atoi();
367  Set(year, *month, day, hour, min, sec);
368 }
369 
370 
371 
375 
377 {
378  //Return date and time string with format "29-SEP-2005 09:42:17.00"
379  //Copy the string immediately if you want to reuse/keep it
380  return Form("%d-%s-%4d %02d:%02d:%02d.00",
381  GetDay(), fNumToMonth[GetMonth()-1].c_str(), GetYear(), GetHour(), GetMinute(), GetSecond());
382 }
383 
384 
385 
387 
389 {
390  return Form("%02d/%02d/%4d",
391  GetDay(),
392  GetMonth(), GetYear());
393 }
394 
395 
396 
403 
405 {
406  // Returns date & time as a string in required format:
407  // fmt = kCTIME (default) : ctime format e.g. Thu Apr 10 10:48:34 2008
408  // fmt = kSQL : SQL format e.g. 1997-01-15 20:16:28
409  // fmt = kGANACQ : GANIL acquisition format e.g. 29-SEP-2005 09:42:17.00
410  // fmt = kDMY : DD/MM/YYYY
411  switch (fmt) {
412  case kCTIME:
413  fStr = AsString();
414  break;
415  case kSQL:
416  fStr = AsSQLString();
417  break;
418  case kGANACQ:
420  break;
421  case kDMY:
422  fStr = AsDMYDateString();
423  break;
424  default:
425  fStr = "";
426  }
427  return fStr.Data();
428 }
429 
430 
431 
435 
437 {
438  // Static method, returns kTRUE if 'date' is in format of GANIL acquisition
439  // e.g. 29-SEP-2005 09:42:17.00
440 
441  KVString tmp(date);
442  auto nelem = tmp.GetNValues("-:. ");
443  // if format is correct, there should be 6 or 7 elements in toks
444  return (nelem > 5 && nelem < 8);
445 }
446 
447 
448 
452 
454 {
455  // Static method, returns kTRUE if 'date' is in new (2010) format of GANIL acquisition
456  // run files, e.g. run_0058.dat.27Nov10_02h07m40s
457 
458  Int_t Y, D, H, M, S;
459  Char_t Month[5];
460  TString tmp(date);
461  tmp.ToUpper();
462  tmp.ReplaceAll("_", " ");
463  if (sscanf(tmp.Data(), "%02d%3s%02d %02dH%02dM%02dS", &D, Month, &Y, &H, &M, &S) == 6)
464  return kTRUE;
465  return kFALSE;
466 }
467 
468 
469 
473 
475 {
476  // Static method, returns kTRUE if 'date' is in format of GANIL Narval acquisition
477  // run files, e.g. run_0058.dat.27-11-10_02h07m40s
478 
479  Int_t Y, D, H, M, S, Month;
480  TString tmp(date);
481  tmp.ToUpper();
482  tmp.ReplaceAll("_", " ");
483  if (sscanf(tmp.Data(), "%02d-%02d-%02d %02dH%02dM%02dS", &D, &Month, &Y, &H, &M, &S) == 6)
484  return kTRUE;
485  return kFALSE;
486 }
487 
488 
489 
493 
495 {
496  // Static method, returns kTRUE if 'date' is in SQL format
497  // e.g. 2007-05-02 14:52:18
498 
499  Int_t Y, M, D, h, m, s;
500  if (sscanf(date, "%4d-%02d-%02d %02d:%02d:%02d",
501  &Y, &M, &D, &h, &m, &s) != 6) return kFALSE;
502  return kTRUE;
503 }
504 
505 
506 
510 
512 {
513  // Static method, returns kTRUE if 'date' is in SRB format
514  // e.g. 2008-12-19-15.21
515 
516  Int_t Y, M, D, h, m;
517  if (sscanf(date, "%4d-%02d-%02d-%02d.%02d",
518  &Y, &M, &D, &h, &m) != 5) return kFALSE;
519  return kTRUE;
520 }
521 
522 
523 
527 
529 {
530  // Static method, returns kTRUE if 'date' is in IRODS format
531  // e.g. 2008-12-19.15:21
532 
533  Int_t Y, M, D, h, m;
534  if (sscanf(date, "%4d-%02d-%02d.%02d:%02d",
535  &Y, &M, &D, &h, &m) != 5) return kFALSE;
536  return kTRUE;
537 }
538 
539 
540 
542 
544 {
545  return fNumToMonth[m-1].c_str();
546 }
547 
548 
549 
551 
553 {
554 
555  Double_t total_ins = 0;
556 
557  Double_t year_ins = GetYear();
558  year_ins -= ref_year;
559 
560  year_ins *= 365.25; //1y=365.25 d
561  year_ins *= 24; //1d=24 h
562  year_ins *= 3660; //1h=60x60=3660 s
563 
564  total_ins += year_ins;
565 
566  //nombre de jours depuis le debut de l annee
567  Double_t month_ins = GetMonth() - 1;
568  month_ins *= 365.25 / 12; //1month = 30.437d (365.25/12) approximatif
569  month_ins += GetDay(); //-> nombre de jours
570  month_ins *= 24; //1d=24 h
571  month_ins *= 3660; //1h=60x60=3660 s
572 
573  total_ins += month_ins; //nombre de jours totals en second
574 
575  total_ins += GetHour() * 3600;
576  total_ins += GetMinute() * 60;
577  total_ins += GetSecond();
578 
579  return total_ins;
580 
581 
582 }
583 
584 
585 
587 
589 {
590  Int_t year_ref = from.GetYear();
591  if (year_ref > this->GetYear()) year_ref = this->GetYear();
592 
593  Double_t sum = from.GetNumberOfSeconds(year_ref);
594  return this->GetNumberOfSeconds(year_ref) - sum;
595 }
596 
597 
int Int_t
unsigned int UInt_t
#define d(i)
#define f(i)
bool Bool_t
char Char_t
constexpr Bool_t kFALSE
double Double_t
constexpr Bool_t kTRUE
char * Form(const char *fmt,...)
Extension of TDatime to handle various useful date formats.
Definition: KVDatime.h:35
static const Char_t * Month(Int_t m)
Definition: KVDatime.cpp:543
Double_t GetNumberOfSeconds(Int_t ref_year=0)
Definition: KVDatime.cpp:552
void SetDMYDate(const Char_t *DMYString)
Set date from string in format "DD/MM/YYYY".
Definition: KVDatime.cpp:264
void SetIRODSDate(const Char_t *IRODSDateString)
Definition: KVDatime.cpp:246
EKVDateFormat
Definition: KVDatime.h:45
@ kSRB
Definition: KVDatime.h:49
@ kGANACQ2010
Definition: KVDatime.h:52
@ kIRODS
Definition: KVDatime.h:50
@ kGANACQ
Definition: KVDatime.h:47
@ kDMY
Definition: KVDatime.h:51
@ kSQL
Definition: KVDatime.h:48
@ kCTIME
Definition: KVDatime.h:46
@ kGANACQNarval
Definition: KVDatime.h:53
static std::unordered_map< std::string, int > fMonthToNum
map month name to number
Definition: KVDatime.h:37
static Bool_t IsGANACQFormat(const Char_t *date)
Definition: KVDatime.cpp:436
static const std::vector< std::string > fNumToMonth
map month number to name
Definition: KVDatime.h:36
static Bool_t IsSRBFormat(const Char_t *date)
Definition: KVDatime.cpp:511
TString fStr
internal buffer used by String method
Definition: KVDatime.h:38
KVDatime()
Definition: KVDatime.cpp:50
const Char_t * String(EKVDateFormat fmt=kCTIME)
Definition: KVDatime.cpp:404
void init_month_map()
set up static map of month names to month numbers if needed
Definition: KVDatime.cpp:20
static Bool_t IsSQLFormat(const Char_t *date)
Definition: KVDatime.cpp:494
static Bool_t IsGANACQ2010Format(const Char_t *date)
Definition: KVDatime.cpp:453
void SetSQLDate(const Char_t *SQLDateString)
Definition: KVDatime.cpp:209
void SetGanacqNarvalDate(const Char_t *GanacqDateString)
Definition: KVDatime.cpp:305
const Char_t * AsDMYDateString() const
Definition: KVDatime.cpp:388
KVDatime & operator=(const KVDatime &)
copy assignment operator
Definition: KVDatime.cpp:197
const Char_t * AsGanacqDateString() const
Definition: KVDatime.cpp:376
std::optional< int > get_month_from_name(const char *)
Definition: KVDatime.cpp:35
void SetSRBDate(const Char_t *SRBDateString)
Definition: KVDatime.cpp:226
static Bool_t IsGANACQNarvalFormat(const Char_t *date)
Definition: KVDatime.cpp:474
static Bool_t IsIRODSFormat(const Char_t *date)
Definition: KVDatime.cpp:528
void SetGanacq2010Date(const Char_t *GanacqDateString)
Definition: KVDatime.cpp:278
Double_t GetDureeInSeconds(KVDatime from)
Definition: KVDatime.cpp:588
void SetGanacqDate(const Char_t *GanacqDateString)
Definition: KVDatime.cpp:331
Extension of ROOT TString class which allows backwards compatibility with ROOT v3....
Definition: KVString.h:73
void Begin(TString delim) const
Definition: KVString.cpp:565
KVString Next(Bool_t strip_whitespace=kFALSE) const
Definition: KVString.cpp:695
Int_t GetNValues(TString delim) const
Definition: KVString.cpp:886
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 Atoi() const
const char * Data() const
void ToUpper()
TString & ReplaceAll(const char *s1, const char *s2)
TH1 * h
void Error(UserClass p, const char *location, const char *va_(fmt),...)
Definition: KVError.h:116
RooArgSet S(Args_t &&... args)
double min(double x, double y)
constexpr Double_t H()
TMarker m
ClassImp(TPyArg)