EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ValTimeStamp.h
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file ValTimeStamp.h
1 #ifndef VALTIMESTAMP_H
2 #define VALTIMESTAMP_H
3 
4 #include "TTimeStamp.h"
5 
6 #include <iosfwd>
7 #include <iostream>
8 using namespace std;
9 
10 
11 
12 class ValTimeStamp;
13 
14 std::ostream& operator<<(std::ostream& os, const ValTimeStamp& vldts);
15 
17 {
18 
19  friend Bool_t operator==(const ValTimeStamp& lhs, const ValTimeStamp& rhs);
20  friend Bool_t operator!=(const ValTimeStamp& lhs, const ValTimeStamp& rhs);
21  friend Bool_t operator< (const ValTimeStamp& lhs, const ValTimeStamp& rhs);
22  friend Bool_t operator<=(const ValTimeStamp& lhs, const ValTimeStamp& rhs);
23  friend Bool_t operator> (const ValTimeStamp& lhs, const ValTimeStamp& rhs);
24  friend Bool_t operator>=(const ValTimeStamp& lhs, const ValTimeStamp& rhs);
25 
26  friend ValTimeStamp operator- (const ValTimeStamp& lhs, const ValTimeStamp& rhs);
27 
28  public:
29 
31  static ValTimeStamp GetBOT();
32 
36  static ValTimeStamp GetEOT();
37 
40  static ValTimeStamp GetNBOT();
41 
43  ValTimeStamp();
44 
46  ValTimeStamp(const ValTimeStamp& source)
47  : fSec(source.fSec), fNanoSec(source.fNanoSec) { }
48 
51  if (this != &source) {fSec = source.fSec; fNanoSec = source.fNanoSec;}
52  return *this;
53  }
54 
56  ValTimeStamp(const timespec_t& ts)
57  : fSec(ts.tv_sec), fNanoSec(ts.tv_nsec)
58  { NormalizeNanoSec(); }
59 
61  ValTimeStamp(const time_t& t, const Int_t nsec)
62  : fSec(t), fNanoSec(nsec)
63  { NormalizeNanoSec(); }
64 
77  ValTimeStamp(UInt_t year, UInt_t month,
78  UInt_t day, UInt_t hour,
79  UInt_t min, UInt_t sec,
80  UInt_t nsec=0,
81  Bool_t isUTC=kTRUE, Int_t secOffset=0);
82 
85  ValTimeStamp(UInt_t date, UInt_t time, UInt_t nsec,
86  Bool_t isUTC=kTRUE, Int_t secOffset=0);
87 
95  ValTimeStamp(Double_t seconds)
96  : fSec((Int_t)seconds), fNanoSec((Int_t)((seconds-fSec)*1.0e9))
97  { NormalizeNanoSec(); }
98 
99 
100  virtual ~ValTimeStamp();
101 
109  operator double() const { return fSec + 1.0e-9 * fNanoSec; }
110 
111 
113  timespec_t GetTimeSpec() const
114  { timespec_t value = {fSec,fNanoSec}; return value; }
115 
117  time_t GetSec(void) const { return fSec;}
119  Int_t GetNanoSec(void) const { return fNanoSec; }
120 
122  Double_t GetSeconds(void) const { return fSec+(fNanoSec/1.0e9); }
123 
156  const char* AsString(Option_t* option="") const;
157  void Copy(ValTimeStamp& vldts) const;
158 
161  Int_t GetDate(Bool_t inUTC=kTRUE, Int_t secOffset=0,
162  UInt_t* year=0, UInt_t* month=0,
163  UInt_t* day=0) const;
164 
167  Int_t GetTime(Bool_t inUTC=kTRUE, Int_t secOffset=0,
168  UInt_t* hour=0, UInt_t* min=0,
169  UInt_t* sec=0) const;
170 
171  void Add(const ValTimeStamp& offset);
172  void Add(Double_t seconds);
173 
174  void Print(Option_t* option="") const;
175 
176  // Utility functions
177 
180  static Int_t GetZoneOffset();
192  static time_t MktimeFromUTC(tm_t* tmstruct);
194  static Bool_t IsLeapYear(Int_t year);
196  static void DumpTMStruct(const tm_t& tmstruct);
197 
198  private:
199 
200  void Set();
201  void Set(Int_t year, Int_t month, Int_t day,
202  Int_t hour, Int_t min, Int_t sec,
203  Int_t nsec, Bool_t isUTC, Int_t secOffset);
204  void Set(Int_t date, Int_t time, Int_t nsec,
205  Bool_t isUTC, Int_t secOffset);
206  void NormalizeNanoSec();
207 
208  // Data members:
209  // similar fields to struct timespec
210  // use ROOT versions to "know" that they are platform consistent
211  // 32-bit integers to avoid IO confusion.
212  Int_t fSec;
213  Int_t fNanoSec;
214 
215  ClassDef(ValTimeStamp,2)
216 };
217 
218 #ifndef __CINT__
219 
220 inline Bool_t operator==(const ValTimeStamp& lhs, const ValTimeStamp& rhs)
221 {
222  return lhs.fSec == rhs.fSec &&
223  lhs.fNanoSec == rhs.fNanoSec;
224 }
225 
226 inline Bool_t operator!=(const ValTimeStamp& lhs, const ValTimeStamp& rhs)
227 {
228  return lhs.fSec != rhs.fSec ||
229  lhs.fNanoSec != rhs.fNanoSec;
230 }
231 
232 inline Bool_t operator<(const ValTimeStamp& lhs, const ValTimeStamp& rhs)
233 {
234  return lhs.fSec < rhs.fSec ||
235  ( lhs.fSec == rhs.fSec &&
236  lhs.fNanoSec < rhs.fNanoSec );
237 }
238 
239 inline Bool_t operator<=(const ValTimeStamp& lhs, const ValTimeStamp& rhs)
240 {
241  return lhs.fSec < rhs.fSec ||
242  ( lhs.fSec == rhs.fSec &&
243  lhs.fNanoSec <= rhs.fNanoSec );
244 }
245 
246 inline Bool_t operator>(const ValTimeStamp& lhs, const ValTimeStamp& rhs)
247 {
248  return lhs.fSec > rhs.fSec ||
249  ( lhs.fSec == rhs.fSec &&
250  lhs.fNanoSec > rhs.fNanoSec );
251 }
252 
253 inline Bool_t operator>=(const ValTimeStamp& lhs, const ValTimeStamp& rhs)
254 {
255  return lhs.fSec > rhs.fSec ||
256  ( lhs.fSec == rhs.fSec &&
257  lhs.fNanoSec >= rhs.fNanoSec );
258 }
259 
260 inline ValTimeStamp operator-(const ValTimeStamp& lhs, const ValTimeStamp& rhs)
261 {
262  return ValTimeStamp(lhs.GetSec() - rhs.GetSec(),
263  lhs.GetNanoSec() - rhs.GetNanoSec());
264 }
265 
266 #endif /* __CINT__ */
267 #endif // VADTIMESTAMP_H