StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
TUnixTime.cxx
1 /***************************************************************************
2  *
3  * $Id: TUnixTime.cxx,v 1.4 2013/02/20 01:59:13 perev Exp $
4  *
5  ***************************************************************************
6  *
7  * Description:
8  *
9  ***************************************************************************
10  **************************************************************************/
11 #include <ctime>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <assert.h>
15 #include "TUnixTime.h"
16 #include "TDatime.h"
17 
18 
19 ClassImp(TUnixTime)
20 //______________________________________________________________________________
21 TUnixTime::TUnixTime(ULong_t utime)
22 {
23  fUTime = utime;
24  if (!fUTime) fUTime = time(0);
25 }
26 //______________________________________________________________________________
27 TUnixTime::TUnixTime(Int_t date,Int_t time,int gmt)
28 {
29  if (gmt) SetGTime(date,time);
30  else SetLTime(date,time);
31 }
32 //______________________________________________________________________________
33 TUnixTime::TUnixTime(const TDatime &tdt,int gmt)
34 {
35  if (gmt) {SetGTime(tdt.GetDate(),tdt.GetTime());}
36  else {SetLTime(tdt.GetDate(),tdt.GetTime());}
37 }
38 //______________________________________________________________________________
39 void TUnixTime::SetGTime(const struct tm *tm_gmt)
40 {
41  int dif;
42  time_t ugmt;
43  struct tm tm_test;
44  tm_test = *tm_gmt;
45  tm_test.tm_isdst=-1;
46  ugmt = mktime(&tm_test)-timezone;
47  for (int iter=0;iter<24;iter++) {
48  tm_test = *gmtime(&ugmt);
49  tm_test.tm_isdst=-1;
50  dif = tm_test.tm_year - tm_gmt->tm_year;
51  if (dif) goto UPD;
52  dif = tm_test.tm_mon - tm_gmt->tm_mon ;
53  if (dif) goto UPD;
54  dif = tm_test.tm_mday - tm_gmt->tm_mday;
55  if (dif) goto UPD;
56  dif = tm_test.tm_hour - tm_gmt->tm_hour;
57  if (!dif) goto OK;
58 UPD: if (dif<0) ugmt+=3600;
59  else ugmt-=3600;
60  }
61  assert(0);
62 OK: fUTime = ugmt;
63 
64 }
65 //______________________________________________________________________________
66 void TUnixTime::SetLTime(const struct tm *lt)
67 {
68  struct tm ltt;
69  ltt = *lt;
70  fUTime = mktime(&ltt);
71 }
72 //______________________________________________________________________________
73 static void DateTime2tm(struct tm *gt,Int_t idate, Int_t itime)
74 {
75  time_t ul = time(0);
76  *gt = *localtime(&ul);
77  gt->tm_isdst =-1;
78  if (idate < 19000000) idate +=19000000;
79  if (idate < 19500000) idate += 1000000;
80  gt->tm_year= ((idate/10000)-1900);
81  gt->tm_mon = ((idate/100)%100) - 1;
82  gt->tm_mday= ((idate )%100);
83  gt->tm_hour= itime/10000;
84  gt->tm_min = (itime/100 )%100;
85  gt->tm_sec = (itime )%100;
86 }
87 
88 //______________________________________________________________________________
89 static void tm2DateTime(Int_t &idate, Int_t &itime,const struct tm *gt)
90 {
91  idate = (gt->tm_year+1900)*10000 + (gt->tm_mon+1)*100 + gt->tm_mday;
92  itime = gt->tm_hour*10000 + gt->tm_min*100 + gt->tm_sec;
93 }
94 
95 //______________________________________________________________________________
96 void TUnixTime::SetGTime(Int_t idate, Int_t itime)
97 {
98  struct tm gt;
99  DateTime2tm(&gt,idate,itime);
100  SetGTime(&gt);
101 
102 }
103 //______________________________________________________________________________
104 void TUnixTime::SetLTime(Int_t idate, Int_t itime)
105 {
106  struct tm gt;
107  DateTime2tm(&gt,idate,itime);
108  SetLTime(&gt);
109 
110 }
111 //______________________________________________________________________________
112 void TUnixTime::GetGTime(Int_t &idate, Int_t &itime)
113 {
114  std::time_t utime = fUTime;
115  std::tm *gt = gmtime(&utime);
116  tm2DateTime(idate, itime, gt);
117 
118 }
119 //______________________________________________________________________________
120 void TUnixTime::GetLTime(Int_t &idate, Int_t &itime)
121 {
122  std::time_t utime = fUTime;
123  std::tm *gt = localtime(&utime);
124  tm2DateTime(idate, itime, gt);
125 }
126 //______________________________________________________________________________
127 TString TUnixTime::GetLString()
128 {
129  std::time_t utime = fUTime;
130  return TString(ctime(&utime));
131 }
132 //______________________________________________________________________________
133 TString TUnixTime::GetGString()
134 {
135  std::time_t utime = fUTime;
136  return TString(asctime(gmtime(&utime)));
137 }
138 //______________________________________________________________________________
139 void TUnixTime::SetLTime(const TDatime &loc)
140 {
141  SetLTime(loc.GetDate(), loc.GetTime());
142 }
143 //______________________________________________________________________________
144 void TUnixTime::SetGTime(const TDatime &gmt)
145 {
146  SetGTime(gmt.GetDate(), gmt.GetTime());
147 }
148 //______________________________________________________________________________
149 ULong_t TUnixTime::Convert(const TDatime &dt,int gmt)
150 {
151  TUnixTime ux;
152  if (gmt) {ux.SetGTime(dt);}
153  else {ux.SetLTime(dt);}
154  return ux.GetUTime();
155 }