Function to calculate seconds between 2 dates

Hello ROOTers,

I’m using a Macro in order to calculate the seconds beetween 2 date:

// Extract day, month, year, hh, mm, ss
day = atoi(date.substr(0,2).c_str());
month = atoi(date.substr(2,2).c_str()); 
year = atoi(date.substr(4,2).c_str())+2000;
hh = atoi(time.substr(0,2).c_str());
mm = atoi(time.substr(2,2).c_str());
ss = atoi(time.substr(4,2).c_str());   
 
// Calculate seconds from 01.01.2007 00:00
time_trending = calculate_seconds_between(2007,1,1,0,0,0,year,month,day,hh,mm,ss);
 seconds2007 = time_trending;

But doesn’t work, i see this error:

error: use of undeclared identifier 'calculate_seconds_between'
 
time_trending = calculate_seconds_between(2007,1,1,0,0,0,year,month,day,hh,mm,ss);
                ^

Probabily the problem is linked to the function “caculate_seconds_between”. How can i resolve this? Any advice?

Best Regards.


_ROOT Version: 6.22
_Platform: Xubuntu 20.10
_Compiler: gcc 9.3.0


Hi,

I guess this is not really a ROOT-specific question, so there are a lot of solution out there, see e.g.

No no, it is a root question…

{
  TDatime d0(2007,1,1,0,0,0); // second precision
  TDatime d1(2007,1,1,1,1,1); // second precision
  UInt_t d0_i = d0.Convert();
  UInt_t d1_i = d1.Convert();
  Int_t dt_i = d1_i - d0_i;
  std::cout << dt_i << std::endl;
  TTimeStamp t0(2007,1,1,0,0,0,0); // nanosecond precision
  TTimeStamp t1(2007,1,1,1,1,1,0); // nanosecond precision
  Double_t t0_d = t0.AsDouble();
  Double_t t1_d = t1.AsDouble();
  Double_t dt_d = t1_d - t0_d;
  std::cout << dt_d << std::endl;
}
1 Like

Thanks you so much @Wile_E_Coyote .

@Wile_E_Coyote I have an additional question. This is the source code:

#include "time.h"
#include<stdlib.h>
#include<stdio.h>
#include <Riostream.h>
#include <TFile.h>
#include <TTree.h>

void analysis_miniarray_RUN4()
{
 const char *nomeFile = "RUN4_2.log";
 fstream file(nomeFile, ios::in);
 TFile *f = new TFile("RUN4_2.root","RECREATE");
 // Variables
 Int_t mult, bit[35], nsat, day, month, year, hh, mm, ss, nanosec, first;
 time_t  seconds2007, time_trending;
 Float_t x[35],y[35],z[35],eff[35];
 Int_t nrun,ndet;
 Float_t lat,longitudine;
 Double_t pattern;
 //tprev=409243060; // start RUN4_1  Ok
 Int_t tprev=409484106; // start RUN4_2   Ok
 //tprev=409835868; // start RUN4_3   Ok
 //tprev=410345116 ; // start RUN4_4    Ok
 //tprev=410772578; //  start RUN4_5    Ok
 //tprev=410959665; // start RUN4_6    OK
 

fstream in("RUN4_config.txt",ios::in);
	 in>>nrun>>ndet>>lat>>longitudine;
	 for(Int_t i=0;i<35;i++) {in>>x[i];}
	 for(Int_t i=0;i<35;i++) {in>>y[i];}
	 for(Int_t i=0;i<35;i++) {in>>z[i];}
	 for(Int_t i=0;i<35;i++) {
	 in>>eff[i];
	 cout<<i<<" "<<x[i]<<"  "<<y[i]<<"  "<<z[i]<<"  "<<eff[i]<<endl;
	 }
	 
	 
 
 string date, init, status, time;
 Float_t rate[35], temp, press,weight;
 Float_t rate0,rate1,rate2,rate3,rate4,rate5,rate6,rate7,rate8,rate9,rate10;
 Float_t rate11,rate12,rate13,rate14,rate15,rate16,rate17,rate18,rate19,rate20;
 Float_t rate21,rate22,rate23,rate24,rate25,rate26,rate27,rate28,rate29,rate30;
 Float_t rate31,rate32,rate33,rate34;
 bool D2,D3,D4,D5,D6,D7,D8,D9,D10,D11,D12,D13,D14,D15,D16,D17,D18;
 bool D22,D23,D24,D25,D26,D27,D28,D29,D30,D31,D32,D33,D34;
 string info = "INFO";
 string info1= "FIX";
 
 // Output tree
 TTree *tree = new TTree("tree_arduino","Tree with arduino data");   
 tree->Branch("seconds2007",&seconds2007,"seconds2007/I");
 tree->Branch("nanosec",&nanosec,"nanosec/I");
 tree->Branch("mult",&mult,"mult/I");
 tree->Branch("pattern",&pattern,"pattern/D");
 tree->Branch("weight",&weight,"weight/F");
 tree->Branch("first",&first,"first/I"); 
 tree->Branch("D2",&D2,"D2/B");
 tree->Branch("D3",&D3,"D3/B");
 tree->Branch("D4",&D4,"D4/B");
 tree->Branch("D5",&D5,"D5/B");
 tree->Branch("D6",&D6,"D6/B");
 tree->Branch("D7",&D7,"D7/B");
 tree->Branch("D8",&D8,"D8/B");
 tree->Branch("D9",&D9,"D9/B");
 tree->Branch("D10",&D10,"D10/B");
 tree->Branch("D11",&D11,"D11/B");
 tree->Branch("D12",&D12,"D12/B");
 tree->Branch("D13",&D13,"D13/B");
 tree->Branch("D14",&D14,"D14/B");
 tree->Branch("D15",&D15,"D15/B");
 tree->Branch("D16",&D16,"D16/B");
 tree->Branch("D17",&D17,"D17/B");
 tree->Branch("D18",&D18,"D18/B");
 tree->Branch("D22",&D22,"D22/B");
 tree->Branch("D23",&D23,"D23/B");
 tree->Branch("D24",&D24,"D24/B");
 tree->Branch("D25",&D25,"D25/B");
 tree->Branch("D26",&D26,"D26/B");
 tree->Branch("D27",&D27,"D27/B");
 tree->Branch("D28",&D28,"D28/B");
 tree->Branch("D29",&D29,"D29/B");
 tree->Branch("D30",&D30,"D30/B");
 tree->Branch("D31",&D31,"D31/B");
 tree->Branch("D32",&D32,"D32/B");
 tree->Branch("D33",&D33,"D33/B");
 tree->Branch("D34",&D34,"D34/B");






  
  // Output tree
 TTree *tree_trending = new TTree("tree_trending","tree_trending");   
 tree_trending->Branch("seconds2007",&seconds2007,"seconds2007/I");
 tree_trending->Branch("temp",&temp,"temp/F");
 tree_trending->Branch("press",&press,"press/F");
 tree_trending->Branch("nsat",&nsat,"nsat/I");
 tree_trending->Branch("rate2",&rate2,"rate2/F");
 tree_trending->Branch("rate3",&rate3,"rate3/F");
 tree_trending->Branch("rate4",&rate4,"rate4/F");
 tree_trending->Branch("rate5",&rate5,"rate5/F");
 tree_trending->Branch("rate6",&rate6,"rate6/F"); 
 tree_trending->Branch("rate7",&rate7,"rate7/F");
 tree_trending->Branch("rate8",&rate8,"rate8/F");
 tree_trending->Branch("rate9",&rate9,"rate9/F");
 tree_trending->Branch("rate10",&rate10,"rate10/F");
 tree_trending->Branch("rate11",&rate11,"rate11/F");
 tree_trending->Branch("rate12",&rate12,"rate12/F");
 tree_trending->Branch("rate13",&rate13,"rate13/F");
 tree_trending->Branch("rate14",&rate14,"rate14/F");
 tree_trending->Branch("rate15",&rate15,"rate15/F");
 tree_trending->Branch("rate16",&rate16,"rate16/F");
 tree_trending->Branch("rate17",&rate17,"rate17/F");
 tree_trending->Branch("rate18",&rate18,"rate18/F");
 tree_trending->Branch("rate19",&rate19,"rate19/F");
 tree_trending->Branch("rate20",&rate20,"rate20/F");
 tree_trending->Branch("rate21",&rate21,"rate21/F");
 tree_trending->Branch("rate22",&rate22,"rate22/F");
 tree_trending->Branch("rate23",&rate23,"rate23/F");
 tree_trending->Branch("rate24",&rate24,"rate24/F");
 tree_trending->Branch("rate25",&rate25,"rate25/F");
 tree_trending->Branch("rate26",&rate26,"rate26/F");
 tree_trending->Branch("rate27",&rate27,"rate27/F");
 tree_trending->Branch("rate28",&rate28,"rate28/F");
 tree_trending->Branch("rate29",&rate29,"rate29/F");          
 tree_trending->Branch("rate30",&rate30,"rate30/F");
 tree_trending->Branch("rate31",&rate31,"rate31/F");
 tree_trending->Branch("rate32",&rate32,"rate32/F");
 tree_trending->Branch("rate33",&rate33,"rate33/F");
 tree_trending->Branch("rate34",&rate34,"rate34/F");
 
         
Int_t nevents=0;
 
 string dummy;
 for(int i=0; i<7; i++) {getline(file,dummy);cout << dummy << endl; //skip 7 lines
 }
 
 
 // Loop on the data
 while (file>> init){
 	
   bool data = kFALSE;
   // Reading info lines
   if(init.compare(info)==0) {
        file >> status >> nsat >> date >> time >> temp >> press >> rate[0] >> rate[1] >> rate[2]>>rate[3]>>rate[4]>>rate[5]>>rate[6]>>rate[7]>>rate[8]>>rate[9]>>rate[10]>>
		rate[11]>>rate[12]>>rate[13]>>rate[14]>>rate[15]>>rate[16]>>rate[17]>>rate[18]>>rate[19]>>rate[20]>>
		rate[21]>>rate[22]>>rate[23]>>rate[24]>>rate[25]>>rate[26]>>rate[27]>>rate[28]>>rate[29]>>rate[30]>>
		rate[31]>>rate[32]>>rate[33]>>rate[34];
        press=10.0*press; // pressure in mbar
   }
   else if(init.compare(info1)==0) {
   	 file >> dummy >> dummy;
   	 }
   // Reading data lines
   else { 
        data = kTRUE;
        nevents++;
        //if(nevents>1000) break;
        mult = atoi(init.c_str());
        file >> bit[0]>>bit[1] >> bit[2] >> bit[3] >> bit[4] >> bit[5] >> bit[6] >> bit[7] >> bit[8] >> bit[9] >> bit[10] >> 
		bit[11] >> bit[12] >> bit[13] >> bit[14] >> bit[15] >> bit[16] >> bit[17] >> bit[18] >> bit[19] >> bit[20]>>
		bit[21] >> bit[22] >> bit[23] >> bit[24] >> bit[25] >> bit[26] >> bit[27] >> bit[28] >> bit[29] >> bit[30] >>
		bit[31] >> bit[32] >> bit[33] >> bit[34] >> date >> time;
        pattern=0; 
		for (Int_t k=0; k<=34; k++) pattern=pattern+bit[k]*TMath::Power(2.0,k);

        D2=kFALSE; if(bit[2]>0) D2=kTRUE;
        D3=kFALSE; if(bit[3]>0) D3=kTRUE;
        D4=kFALSE; if(bit[4]>0) D4=kTRUE; 
        D5=kFALSE; if(bit[5]>0) D5=kTRUE;
        D6=kFALSE; if(bit[6]>0) D6=kTRUE;
        D7=kFALSE; if(bit[7]>0) D7=kTRUE; 
        D8=kFALSE; if(bit[8]>0) D8=kTRUE;
        D9=kFALSE; if(bit[9]>0) D9=kTRUE;
        D10=kFALSE; if(bit[10]>0) D10=kTRUE; 
        D11=kFALSE; if(bit[11]>0) D11=kTRUE;
        D12=kFALSE; if(bit[12]>0) D12=kTRUE;
        D13=kFALSE; if(bit[13]>0) D13=kTRUE; 
        D14=kFALSE; if(bit[14]>0) D14=kTRUE; 
        D15=kFALSE; if(bit[15]>0) D15=kTRUE;
        D16=kFALSE; if(bit[16]>0) D16=kTRUE;
        D17=kFALSE; if(bit[17]>0) D17=kTRUE; 
        D18=kFALSE; if(bit[18]>0) D18=kTRUE; 
        D22=kFALSE; if(bit[22]>0) D22=kTRUE;
        D23=kFALSE; if(bit[23]>0) D23=kTRUE;
        D24=kFALSE; if(bit[24]>0) D24=kTRUE; 
        D25=kFALSE; if(bit[25]>0) D25=kTRUE;
        D26=kFALSE; if(bit[26]>0) D26=kTRUE;
        D27=kFALSE; if(bit[27]>0) D27=kTRUE;        
        D28=kFALSE; if(bit[28]>0) D28=kTRUE;
        D29=kFALSE; if(bit[29]>0) D29=kTRUE;
        D30=kFALSE; if(bit[30]>0) D30=kTRUE;
        D31=kFALSE; if(bit[31]>0) D31=kTRUE;
        D32=kFALSE; if(bit[32]>0) D32=kTRUE;
        D33=kFALSE; if(bit[33]>0) D33=kTRUE;        
        D34=kFALSE; if(bit[34]>0) D34=kTRUE;

		        
        
// Evaluates the first hit channel
        first=0;
        Int_t found=0;
		for (Int_t k=0; k<=34; k++) {
		if(bit[k]>0 && found==0) first=k;
	    if(bit[k]>0) found=1;
		}
		
   }
   

    
   // Extract day, month, year, hh, mm, ss
   day = atoi(date.substr(0,2).c_str());
   month = atoi(date.substr(2,2).c_str()); 
   year = atoi(date.substr(4,2).c_str())+2000;
   hh = atoi(time.substr(0,2).c_str());
   mm = atoi(time.substr(2,2).c_str());
   ss = atoi(time.substr(4,2).c_str());   
   // Calculate seconds from 01.01.2007 00:00
   time_trending = calculate_seconds_between(2007,1,1,0,0,0,year,month,day,hh,mm,ss);
   seconds2007 = time_trending;
   
   
   
   
       
   rate0=rate[0];
   rate1=rate[1];   
   rate2=rate[2];
   rate3=rate[3];
   rate4=rate[4];
   rate5=rate[5];
   rate6=rate[6];
   rate7=rate[7];
   rate8=rate[8];
   rate9=rate[9];
   rate10=rate[10];
   rate11=rate[11];
   rate12=rate[12];
   rate13=rate[13];
   rate14=rate[14];
   rate15=rate[15];
   rate16=rate[16];
   rate17=rate[17];
   rate18=rate[18];
   rate19=rate[19];
   rate20=rate[20];
   rate21=rate[21];
   rate22=rate[22];
   rate23=rate[23];
   rate24=rate[24];
   rate25=rate[25];
   rate26=rate[26];
   rate27=rate[27];
   rate28=rate[28];
   rate29=rate[29];
   rate30=rate[30];
   rate31=rate[31];
   rate32=rate[32];
   rate33=rate[33];
   rate34=rate[34];
            
   if(temp*press>0. && !data && time_trending>tprev+1000 ) { 
      tree_trending->Fill();
      tprev=time_trending;
      cout<<"Fill"<<endl;
    }   



//   if(temp*press>0. && !data) tree_trending->Fill();
   if(data) { //nanosec info present only in data lines
      if (!(nevents % 100)) cout << "\rEvents analyzed: " << nevents << flush;
            nanosec = atoi(time.substr(7,7).c_str())*100;
            weight=1.0;
            for(Int_t j=2; j<=34; j++) {
            	if(j==19 || j==20 || j==21) continue;
            	if(bit[j]) weight=weight/eff[j];
             }   
			   
            tree->Fill();
            
            }
 	  
 } 

   
   tree->Write(); 
   tree_trending->Write();
   f->Close();
 
}


// Function to calculate seconds between 2 dates
time_t calculate_seconds_between(
    const int Y1, const int M1, const int D1, const int H1, const int m1, const int S1, // YY/MM/DD HH:mm:SS
    const int Y2, const int M2, const int D2, const int H2, const int m2, const int S2
)
{
    time_t raw;
    time(&raw);

    struct tm t1 = *gmtime(&raw), t2 = t1;

    t1.tm_year = Y1 - 1900;
    t1.tm_mon = M1 - 1;
    t1.tm_mday = D1;
    t1.tm_hour = H1;
    t1.tm_min = m1;
    t1.tm_sec = S1;

    t2.tm_year = Y2 - 1900;
    t2.tm_mon = M2 - 1;
    t2.tm_mday = D2;
    t2.tm_hour = H2;
    t2.tm_min = m2;
    t2.tm_sec = S2;

    time_t tt1, tt2;
    tt1 = mktime(&t1);
    tt2 = mktime(&t2);

    return (tt2 - tt1);
}

The solution proposed by you don’t work for this case. I wanto to understand why i recive only this error:

error: use of undeclared identifier 'calculate_seconds_between'
   time_trending = calculate_seconds_between(2007,1,1,0,0,0,year,month,day,hh,mm,ss);

Search for “declaring functions”
https://www.cplusplus.com/doc/tutorial/functions/

@dastudillo Thanks for the reply, but i have already tried such solution (put the function before the void) . The output in this case is:

0 0  1.4013e-45  -8.47399e+34  3.98702e+12
1 0  0  4.59135e-41  4.55814e-41
2 0  1.13449e-41  5.61222e+15  1.4013e-45
3 0  9.18341e-41  4.55814e-41  4.59135e-41
4 2.83866e+15  1.77554e-09  1.4013e-45  -nan
5 4.55814e-41  3.07963e-41  0  0
6 0  3.93202e+12  0  5.60125e+15
7 0  4.55814e-41  0  4.55814e-41
8 -nan  -8.47369e+34  1.54498e-09  3.93808e+12
9 -nan  4.59135e-41  3.07963e-41  4.55814e-41
10 -nan  5.62915e+15  3.93808e+12  1.52732e-09
11 -nan  4.55814e-41  4.55814e-41  3.07963e-41
12 2.83887e+15  5.40347e+15  -8.47359e+34  1.4013e-45
13 4.55814e-41  4.55814e-41  4.59135e-41  0
14 0  0  5.62915e+15  0
15 0  0  4.55814e-41  0
16 4.43161e+21  6.59236e+12  0  1.4013e-45
17 1.52742e-43  4.55814e-41  0  0
18 1.80768e-43  2.04005e-08  1.4013e-45  1.52732e-09
19 0  3.07963e-41  0  3.07963e-41
20 0  1.53872e-09  1.48436e-09  0
21 0  3.07963e-41  3.07963e-41  0
22 0  1.12104e-44  1.62264e-09  0
23 3.36312e-44  0  3.07963e-41  0
24 6.9689e+11  1.68156e-44  -8.47363e+34  1.52732e-09
25 4.55814e-41  0  4.59135e-41  3.07963e-41
26 1.72192e-41  2.86932e-09  1.4013e-45  1.27312e+15
27 0  3.07963e-41  0  4.55814e-41
28 2.02822e-19  1.45979e-08  1.68156e-44  -8.47363e+34
29 4.6308e+27  3.07963e-41  0  4.59135e-41
30 1.14696e+27  1.44846e-08  -8.47383e+34  -8.47539e+34
31 1.14824e+27  3.07963e-41  4.59135e-41  4.59135e-41
32 2.63682e-09  1.68156e-44  -8.47399e+34  0
33 3.52632e+09  0  4.59135e-41  0
34 1.31382e+19  1.13449e-41  5.61222e+15  -8.47383e+34
=~=~=~=~=~=~=~=~=~=~=~= PuTTY log 2019.12.19 18:14:29 =~=~=~=~=~=~=~=~=~=~=~=
Performing initial syncronization...
FIX NOT VALID
Fix: 2019-12-19 17:14:30
# of Sat: 4
2 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 191219 171431.3368934
2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 191219 171431.4457277
Events analyzed: 63700

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.