ROOT logo

From $ROOTSYS/tutorials/graphs/timeonaxis3.C

// This example compare what the system time function gmtime and localtime give
// with what gives TGaxis. It can be used as referenced test to check if TGaxis
// is working properly. 
// The original code was developped by Philippe Gras (CEA Saclay. IRFU/SEDI)
//Author: Philippe Gras, Bertrand Bellenot, Olivier Couet

#if defined(G__WIN32) && defined(__CINT__) && !defined(__MAKECINT__)
{
   // timeonaxis3.C has to be run in compiled mode on Windows.
   // the following code does it.
   TString dir = gSystem->DirName(gInterpreter->GetCurrentMacroName());
   gSystem->CompileMacro(dir+"/timeonaxis3.C");
   timeonaxis3();
}
#else

#include "TAxis.h"
#include "TGaxis.h"
#include "TCanvas.h"
#include "TString.h"
#include "TLine.h"
#include "TLatex.h"
#include <time.h>
#include <stdio.h>

TString stime(time_t* t, bool utc = false, bool display_time_zone = true) {
   struct tm* tt;
   if (utc) tt = gmtime(t);
   else     tt = localtime(t);
   char buf[256];
   if (display_time_zone) strftime(buf, sizeof(buf), "%H:%M:%S %Z", tt);
   else                   strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", tt);
   return TString(buf);
}


void timeonaxis3() {
   double f = 1.8;

   TCanvas* c = new TCanvas;

   TLatex* tex1 = new TLatex;
   tex1->SetNDC();
   tex1->SetTextFont(102);
   tex1->SetTextSize(0.07*f);

   TLatex* tex3 = new TLatex;
   tex3->SetNDC();
   tex3->SetTextFont(102);
   tex3->SetTextSize(0.07*f);
   tex3->SetTextColor(kBlue+2);

   TLatex* tex2 = new TLatex;
   tex2->SetNDC();
   tex2->SetTextFont(102);
   tex2->SetTextSize(0.07*f);
   tex2->SetTextColor(kOrange+3);

   time_t offset[] = {0,                   0, 1325376000, 1341100800};
   time_t t[]      = {1331150400, 1336417200,          0, 36000};

   c->SetTopMargin(0); c->SetBottomMargin(0);
   c->SetLeftMargin(0); c->SetRightMargin(0);
   c->Divide(2, 4, -1, -1);
   TLine l;
   l.DrawLine(0.5, 0, 0.5, 1.);

   for(int i = 0; i < 4; ++i){
      for(int gmt = 0; gmt < 2; ++gmt){
         const char* opt = (gmt ? "gmt" : "local");
         TVirtualPad* p = c->cd(2*i + gmt + 1);
         p->SetTopMargin(0); p->SetBottomMargin(0);
         p->SetLeftMargin(0); p->SetRightMargin(0);
         p->SetFillStyle(4000);

         TGaxis* ga = new TGaxis (.4, .25, 5., .25, t[i], t[i] + 1,  1, "t");
         ga->SetTimeFormat("TGaxis label: #color[2]{%Y-%m-%d %H:%M:%S}");
         ga->SetLabelFont(102);
         ga->SetLabelColor(kBlue+2);

         ga->SetTimeOffset(offset[i], opt);
         ga->SetLabelOffset(0.04*f);
         ga->SetLabelSize(0.07*f);
         ga->SetLineColor(0);
         ga->Draw();

         // Get offset string of axis time format: there is not acccessor
         // to time format in TGaxis.
         // Assumes TAxis use the same format.
         TAxis a(10, 0, 1600000000);
         a.SetTimeOffset(offset[i], opt);
         const char* offsettimeformat = a.GetTimeFormat();

         char buf[256];
         if (offset[i] < t[i]) {
            sprintf(buf, "#splitline{%s, %s}{offset: %ld, option %s}",
                    stime(t+i).Data(), stime(t+i, true).Data(), offset[i], opt);
         } else {
            int h = t[i] / 3600;
            int m = (t[i] - 3600 * h) / 60 ;
            int s = (t[i] - h * 3600 - m * 60);
            sprintf(buf, "#splitline{%d h %d m %d s}{offset: %s, option %s}",
                    h, m, s, stime(offset + i, gmt).Data(), opt);
         }
         tex1->DrawLatex(.01, .75, buf);
         tex2->DrawLatex(.01, .50, offsettimeformat);
         time_t t_ = t[i] + offset[i];
         sprintf(buf, "Expecting:    #color[2]{%s}", stime(&t_, gmt, false).Data());
         tex3->DrawLatex(.01, .24, buf);
         if(i > 0) l.DrawLine(0, 0.95, 1, 0.95);
      }
   }
}
#endif
 timeonaxis3.C:1
 timeonaxis3.C:2
 timeonaxis3.C:3
 timeonaxis3.C:4
 timeonaxis3.C:5
 timeonaxis3.C:6
 timeonaxis3.C:7
 timeonaxis3.C:8
 timeonaxis3.C:9
 timeonaxis3.C:10
 timeonaxis3.C:11
 timeonaxis3.C:12
 timeonaxis3.C:13
 timeonaxis3.C:14
 timeonaxis3.C:15
 timeonaxis3.C:16
 timeonaxis3.C:17
 timeonaxis3.C:18
 timeonaxis3.C:19
 timeonaxis3.C:20
 timeonaxis3.C:21
 timeonaxis3.C:22
 timeonaxis3.C:23
 timeonaxis3.C:24
 timeonaxis3.C:25
 timeonaxis3.C:26
 timeonaxis3.C:27
 timeonaxis3.C:28
 timeonaxis3.C:29
 timeonaxis3.C:30
 timeonaxis3.C:31
 timeonaxis3.C:32
 timeonaxis3.C:33
 timeonaxis3.C:34
 timeonaxis3.C:35
 timeonaxis3.C:36
 timeonaxis3.C:37
 timeonaxis3.C:38
 timeonaxis3.C:39
 timeonaxis3.C:40
 timeonaxis3.C:41
 timeonaxis3.C:42
 timeonaxis3.C:43
 timeonaxis3.C:44
 timeonaxis3.C:45
 timeonaxis3.C:46
 timeonaxis3.C:47
 timeonaxis3.C:48
 timeonaxis3.C:49
 timeonaxis3.C:50
 timeonaxis3.C:51
 timeonaxis3.C:52
 timeonaxis3.C:53
 timeonaxis3.C:54
 timeonaxis3.C:55
 timeonaxis3.C:56
 timeonaxis3.C:57
 timeonaxis3.C:58
 timeonaxis3.C:59
 timeonaxis3.C:60
 timeonaxis3.C:61
 timeonaxis3.C:62
 timeonaxis3.C:63
 timeonaxis3.C:64
 timeonaxis3.C:65
 timeonaxis3.C:66
 timeonaxis3.C:67
 timeonaxis3.C:68
 timeonaxis3.C:69
 timeonaxis3.C:70
 timeonaxis3.C:71
 timeonaxis3.C:72
 timeonaxis3.C:73
 timeonaxis3.C:74
 timeonaxis3.C:75
 timeonaxis3.C:76
 timeonaxis3.C:77
 timeonaxis3.C:78
 timeonaxis3.C:79
 timeonaxis3.C:80
 timeonaxis3.C:81
 timeonaxis3.C:82
 timeonaxis3.C:83
 timeonaxis3.C:84
 timeonaxis3.C:85
 timeonaxis3.C:86
 timeonaxis3.C:87
 timeonaxis3.C:88
 timeonaxis3.C:89
 timeonaxis3.C:90
 timeonaxis3.C:91
 timeonaxis3.C:92
 timeonaxis3.C:93
 timeonaxis3.C:94
 timeonaxis3.C:95
 timeonaxis3.C:96
 timeonaxis3.C:97
 timeonaxis3.C:98
 timeonaxis3.C:99
 timeonaxis3.C:100
 timeonaxis3.C:101
 timeonaxis3.C:102
 timeonaxis3.C:103
 timeonaxis3.C:104
 timeonaxis3.C:105
 timeonaxis3.C:106
 timeonaxis3.C:107
 timeonaxis3.C:108
 timeonaxis3.C:109
 timeonaxis3.C:110
 timeonaxis3.C:111
 timeonaxis3.C:112
 timeonaxis3.C:113
 timeonaxis3.C:114
 timeonaxis3.C:115