Logo ROOT   6.12/07
Reference Guide
TTimeStamp.h
Go to the documentation of this file.
1 // @(#)root/base:$Id$
2 // Author: R. Hatcher 30/9/2001
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2002, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 #ifndef ROOT_TTimeStamp
13 #define ROOT_TTimeStamp
14 
15 //////////////////////////////////////////////////////////////////////////
16 //
17 // The TTimeStamp encapsulates seconds and ns since EPOCH
18 //
19 // This extends (and isolates) struct timespec
20 // struct timespec
21 // {
22 // time_t tv_sec; /* seconds */
23 // long tv_nsec; /* nanoseconds */
24 // }
25 // time_t seconds is relative to Jan 1, 1970 00:00:00 UTC
26 //
27 // No accounting of leap seconds is made.
28 //
29 // Due to ROOT/CINT limitations TTimeStamp does not explicitly
30 // hold a timespec struct; attempting to do so means the Streamer
31 // must be hand written. Instead we have chosen to simply contain
32 // similar fields within the private area of this class.
33 //
34 // NOTE: the use of time_t (and its default implementation as a 32 int)
35 // implies overflow conditions occurs somewhere around
36 // Jan 18, 19:14:07, 2038.
37 // If this experiment is still going when it becomes significant
38 // someone will have to deal with it.
39 //
40 //////////////////////////////////////////////////////////////////////////
41 
42 #include "Rtypes.h"
43 
44 #include <ctime>
45 
46 #if defined (_MSC_VER) && (_MSC_VER < 1900)
47 struct timespec {
48  time_t tv_sec; // seconds
49  long tv_nsec; // nanoseconds
50 };
51 #endif
52 
53 // For backward compatibility
54 typedef struct timespec timespec_t;
55 typedef struct tm tm_t;
56 
57 class TVirtualMutex;
58 class TTimeStamp;
59 std::ostream &operator<<(std::ostream &os, const TTimeStamp &ts);
60 TBuffer &operator<<(TBuffer &buf, const TTimeStamp &ts);
62 Bool_t operator==(const TTimeStamp &lhs, const TTimeStamp &rhs);
63 Bool_t operator!=(const TTimeStamp &lhs, const TTimeStamp &rhs);
64 Bool_t operator< (const TTimeStamp &lhs, const TTimeStamp &rhs);
65 Bool_t operator<=(const TTimeStamp &lhs, const TTimeStamp &rhs);
66 Bool_t operator> (const TTimeStamp &lhs, const TTimeStamp &rhs);
67 Bool_t operator>=(const TTimeStamp &lhs, const TTimeStamp &rhs);
68 
70 
71 class TTimeStamp {
72 
73 friend Bool_t operator==(const TTimeStamp &lhs, const TTimeStamp &rhs);
74 friend Bool_t operator!=(const TTimeStamp &lhs, const TTimeStamp &rhs);
75 friend Bool_t operator< (const TTimeStamp &lhs, const TTimeStamp &rhs);
76 friend Bool_t operator<=(const TTimeStamp &lhs, const TTimeStamp &rhs);
77 friend Bool_t operator> (const TTimeStamp &lhs, const TTimeStamp &rhs);
78 friend Bool_t operator>=(const TTimeStamp &lhs, const TTimeStamp &rhs);
79 
80 private:
81  Int_t fSec; // seconds
82  Int_t fNanoSec; // nanoseconds
83 
84  void NormalizeNanoSec();
85 
86 public:
87  // empty ctor (builds current time with nsec field incremented from static)
88  TTimeStamp();
89 
90  // construction from timespec struct
91  TTimeStamp(const timespec_t &ts) :
92  fSec(Int_t(ts.tv_sec)), fNanoSec(ts.tv_nsec) { NormalizeNanoSec(); }
93 
94  // construction from time_t and separate nsec
95  TTimeStamp(time_t t, Int_t nsec) :
96  fSec(Int_t(t)), fNanoSec(nsec) { NormalizeNanoSec(); }
97 
98  // construction from bits and pieces
99  TTimeStamp(UInt_t year, UInt_t month,
100  UInt_t day, UInt_t hour,
101  UInt_t min, UInt_t sec,
102  UInt_t nsec = 0, Bool_t isUTC = kTRUE, Int_t secOffset = 0);
103 
104  // compatibility with TDatime
105  TTimeStamp(UInt_t date, UInt_t time, UInt_t nsec,
106  Bool_t isUTC = kTRUE, Int_t secOffset = 0);
107 
108  // compatability with time() and DOS date
109  TTimeStamp(UInt_t tloc, Bool_t isUTC = kTRUE, Int_t secOffset = 0,
110  Bool_t dosDate = kFALSE);
111 
112  virtual ~TTimeStamp() { }
113 
114  // initialize to current time with nsec field incremented from static
115  void Set();
116 
117  // construction from bits and pieces
118  void Set(Int_t year, Int_t month, Int_t day,
119  Int_t hour, Int_t min, Int_t sec,
120  Int_t nsec, Bool_t isUTC, Int_t secOffset);
121 
122  // compatibility with TDatime
123  void Set(Int_t date, Int_t time, Int_t nsec,
124  Bool_t isUTC, Int_t secOffset);
125 
126  // compatability with time() and DOS date
127  void Set(UInt_t tloc, Bool_t isUTC, Int_t secOffset, Bool_t dosDate);
128 
129  // direct setters
130  void SetSec(Int_t sec) { fSec = sec; }
131  void SetNanoSec(Int_t nsec) { fNanoSec = nsec; }
132 
134  { timespec_t value = {fSec,fNanoSec}; return value; }
135  time_t GetSec() const { return fSec; }
136  Int_t GetNanoSec() const { return fNanoSec; }
137 
138  Double_t AsDouble() const { return fSec + 1e-9 * fNanoSec; }
139  Double_t AsJulianDate() const { return (AsDouble()/86400.0 + 2440587.5); }
140 
141  // return stored time values converted to sidereal time
142  Double_t AsGMST(Double_t UT1Offset = 0 /*milliseconds*/) const; //rval in hours
143  Double_t AsGAST(Double_t UT1Offset = 0 /*milliseconds*/) const; //rval in hours
144  Double_t AsLMST(Double_t Longitude /*degrees*/, Double_t UT1Offset = 0 /*milliseconds*/) const; //rval in hours
145  Double_t AsLAST(Double_t Longitude /*degrees*/, Double_t UT1Offset = 0 /*milliseconds*/) const; //rval in hours
146 
147  const char *AsString(const Option_t *option="") const;
148 
149  void Copy(TTimeStamp &ts) const;
150  UInt_t GetDate(Bool_t inUTC = kTRUE, Int_t secOffset = 0,
151  UInt_t *year = 0, UInt_t *month = 0,
152  UInt_t *day = 0) const;
153  UInt_t GetTime(Bool_t inUTC = kTRUE, Int_t secOffset = 0,
154  UInt_t *hour = 0, UInt_t *min = 0,
155  UInt_t *sec = 0) const;
156  Int_t GetDayOfYear(Bool_t inUTC = kTRUE, Int_t secOffset = 0) const;
157  Int_t GetDayOfWeek(Bool_t inUTC = kTRUE, Int_t secOffset = 0) const;
158  Int_t GetMonth(Bool_t inUTC = kTRUE, Int_t secOffset = 0) const;
159  Int_t GetWeek(Bool_t inUTC = kTRUE, Int_t secOffset = 0) const;
160  Bool_t IsLeapYear(Bool_t inUTC = kTRUE, Int_t secOffset = 0) const;
161 
162  void Add(const TTimeStamp &offset);
163 
164  void Print(const Option_t *option="") const;
165 
166  operator double() const { return AsDouble(); }
167 
168  // Utility functions
169  static Int_t GetZoneOffset();
170  static time_t MktimeFromUTC(tm_t *tmstruct);
171  static void DumpTMStruct(const tm_t &tmstruct);
172  static Int_t GetDayOfYear(Int_t day, Int_t month, Int_t year);
173  static Int_t GetDayOfWeek(Int_t day, Int_t month, Int_t year);
174  static Int_t GetWeek(Int_t day, Int_t month, Int_t year);
175  static Bool_t IsLeapYear(Int_t year);
176 
177  ClassDef(TTimeStamp,1) //Encapsulates seconds and ns since EPOCH
178 };
179 
180 
181 inline Bool_t operator==(const TTimeStamp &lhs, const TTimeStamp &rhs)
182  { return lhs.fSec == rhs.fSec &&
183  lhs.fNanoSec == rhs.fNanoSec; }
184 
185 inline Bool_t operator!=(const TTimeStamp &lhs, const TTimeStamp &rhs)
186  { return lhs.fSec != rhs.fSec ||
187  lhs.fNanoSec != rhs.fNanoSec; }
188 
189 inline Bool_t operator<(const TTimeStamp &lhs, const TTimeStamp &rhs)
190  { return lhs.fSec < rhs.fSec ||
191  (lhs.fSec == rhs.fSec &&
192  lhs.fNanoSec < rhs.fNanoSec); }
193 
194 inline Bool_t operator<=(const TTimeStamp &lhs, const TTimeStamp &rhs)
195  { return lhs.fSec < rhs.fSec ||
196  (lhs.fSec == rhs.fSec &&
197  lhs.fNanoSec <= rhs.fNanoSec); }
198 
199 inline Bool_t operator>(const TTimeStamp &lhs, const TTimeStamp &rhs)
200  { return lhs.fSec > rhs.fSec ||
201  (lhs.fSec == rhs.fSec &&
202  lhs.fNanoSec > rhs.fNanoSec); }
203 
204 inline Bool_t operator>=(const TTimeStamp &lhs, const TTimeStamp &rhs)
205  { return lhs.fSec > rhs.fSec ||
206  (lhs.fSec == rhs.fSec &&
207  lhs.fNanoSec >= rhs.fNanoSec); }
208 
209 #endif
void SetNanoSec(Int_t nsec)
Definition: TTimeStamp.h:131
TBuffer & operator>>(TBuffer &buf, TTimeStamp &ts)
Read time stamp from TBuffer.
Definition: TTimeStamp.cxx:74
const char Option_t
Definition: RtypesCore.h:62
Bool_t operator==(const TTimeStamp &lhs, const TTimeStamp &rhs)
Definition: TTimeStamp.h:181
Double_t AsDouble() const
Definition: TTimeStamp.h:138
TTimeStamp(time_t t, Int_t nsec)
Definition: TTimeStamp.h:95
Buffer base class used for serializing objects.
Definition: TBuffer.h:40
This class implements a mutex interface.
Definition: TVirtualMutex.h:34
struct tm tm_t
Definition: TTimeStamp.h:55
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
time_t GetSec() const
Definition: TTimeStamp.h:135
TTimeStamp(const timespec_t &ts)
Definition: TTimeStamp.h:91
Double_t AsJulianDate() const
Definition: TTimeStamp.h:139
Int_t fSec
Definition: TTimeStamp.h:81
Bool_t operator<=(const TTimeStamp &lhs, const TTimeStamp &rhs)
Definition: TTimeStamp.h:194
struct timespec timespec_t
Definition: TTimeStamp.h:54
R__EXTERN TVirtualMutex * gTimeMutex
Definition: TTimeStamp.h:69
Int_t fNanoSec
Definition: TTimeStamp.h:82
#define ClassDef(name, id)
Definition: Rtypes.h:320
static Bool_t IsLeapYear(Int_t year)
virtual ~TTimeStamp()
Definition: TTimeStamp.h:112
timespec_t GetTimeSpec() const
Definition: TTimeStamp.h:133
void SetSec(Int_t sec)
Definition: TTimeStamp.h:130
std::ostream & operator<<(std::ostream &os, const TTimeStamp &ts)
Write time stamp to std::ostream.
Definition: TTimeStamp.cxx:60
unsigned int UInt_t
Definition: RtypesCore.h:42
Bool_t operator>(const TTimeStamp &lhs, const TTimeStamp &rhs)
Definition: TTimeStamp.h:199
Bool_t operator<(const TTimeStamp &lhs, const TTimeStamp &rhs)
Definition: TTimeStamp.h:189
Bool_t operator!=(const TTimeStamp &lhs, const TTimeStamp &rhs)
Definition: TTimeStamp.h:185
const Bool_t kFALSE
Definition: RtypesCore.h:88
void Add(THist< DIMENSIONS, PRECISION_TO, STAT_TO... > &to, const THist< DIMENSIONS, PRECISION_FROM, STAT_FROM... > &from)
Add two histograms.
Definition: THist.hxx:308
void Copy(void *source, void *dest)
void Print(std::ostream &os, const OptionType &opt)
double Double_t
Definition: RtypesCore.h:55
The TTimeStamp encapsulates seconds and ns since EPOCH.
Definition: TTimeStamp.h:71
you should not use this method at all Int_t Int_t Double_t Double_t Double_t e
Definition: TRolke.cxx:630
#define R__EXTERN
Definition: DllImport.h:27
Bool_t operator>=(const TTimeStamp &lhs, const TTimeStamp &rhs)
Definition: TTimeStamp.h:204
const Bool_t kTRUE
Definition: RtypesCore.h:87
Int_t GetNanoSec() const
Definition: TTimeStamp.h:136