Logo ROOT  
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
TTimeStamp::AsJulianDate
Double_t AsJulianDate() const
Definition: TTimeStamp.h:139
tm_t
struct tm tm_t
Definition: TTimeStamp.h:55
TTimeStamp::AsLAST
Double_t AsLAST(Double_t Longitude, Double_t UT1Offset=0) const
Return local apparent sidereal time (LAST) in hour-angle, given a longitude in degrees.
Definition: TTimeStamp.cxx:230
kTRUE
const Bool_t kTRUE
Definition: RtypesCore.h:91
TTimeStamp::Set
void Set()
Set Date/Time to current time as reported by the system.
Definition: TTimeStamp.cxx:556
e
#define e(i)
Definition: RSha256.hxx:121
TTimeStamp::operator<
friend Bool_t operator<(const TTimeStamp &lhs, const TTimeStamp &rhs)
Definition: TTimeStamp.h:189
TTimeStamp::TTimeStamp
TTimeStamp(const timespec_t &ts)
Definition: TTimeStamp.h:91
TTimeStamp::MktimeFromUTC
static time_t MktimeFromUTC(tm_t *tmstruct)
Equivalent of standard routine "mktime" but using the assumption that tm struct is filled with UTC,...
Definition: TTimeStamp.cxx:767
TTimeStamp::GetSec
time_t GetSec() const
Definition: TTimeStamp.h:135
TTimeStamp::GetDate
UInt_t GetDate(Bool_t inUTC=kTRUE, Int_t secOffset=0, UInt_t *year=0, UInt_t *month=0, UInt_t *day=0) const
Return date in form of 19971224 (i.e.
Definition: TTimeStamp.cxx:353
TTimeStamp::TTimeStamp
TTimeStamp()
Default ctor.
Definition: TTimeStamp.cxx:95
TTimeStamp::AsDouble
Double_t AsDouble() const
Definition: TTimeStamp.h:138
Int_t
int Int_t
Definition: RtypesCore.h:45
gTimeMutex
R__EXTERN TVirtualMutex * gTimeMutex
Definition: TTimeStamp.h:69
TVirtualMutex
Definition: TVirtualMutex.h:32
TTimeStamp::AsLMST
Double_t AsLMST(Double_t Longitude, Double_t UT1Offset=0) const
Return local mean sidereal time (LMST) in hour-angle, given a longitude in degrees.
Definition: TTimeStamp.cxx:214
TTimeStamp::operator>=
friend Bool_t operator>=(const TTimeStamp &lhs, const TTimeStamp &rhs)
Definition: TTimeStamp.h:204
TBuffer
Definition: TBuffer.h:43
operator<
Bool_t operator<(const TTimeStamp &lhs, const TTimeStamp &rhs)
Definition: TTimeStamp.h:189
TTimeStamp::Add
void Add(const TTimeStamp &offset)
Add "offset" as a delta time.
Definition: TTimeStamp.cxx:534
operator!=
Bool_t operator!=(const TTimeStamp &lhs, const TTimeStamp &rhs)
Definition: TTimeStamp.h:185
Bool_t
bool Bool_t
Definition: RtypesCore.h:63
bool
timespec_t
struct timespec timespec_t
Definition: TTimeStamp.h:54
TTimeStamp::GetWeek
Int_t GetWeek(Bool_t inUTC=kTRUE, Int_t secOffset=0) const
Get the week of the year.
Definition: TTimeStamp.cxx:456
TTimeStamp::operator==
friend Bool_t operator==(const TTimeStamp &lhs, const TTimeStamp &rhs)
Definition: TTimeStamp.h:181
Option_t
const typedef char Option_t
Definition: RtypesCore.h:66
TTimeStamp::NormalizeNanoSec
void NormalizeNanoSec()
Ensure that the fNanoSec field is in range [0,999999999].
Definition: TTimeStamp.cxx:739
operator<=
Bool_t operator<=(const TTimeStamp &lhs, const TTimeStamp &rhs)
Definition: TTimeStamp.h:194
TTimeStamp::Print
void Print(const Option_t *option="") const
Print date and time.
Definition: TTimeStamp.cxx:544
TTimeStamp::GetNanoSec
Int_t GetNanoSec() const
Definition: TTimeStamp.h:136
TTimeStamp::~TTimeStamp
virtual ~TTimeStamp()
Definition: TTimeStamp.h:112
TTimeStamp::TTimeStamp
TTimeStamp(time_t t, Int_t nsec)
Definition: TTimeStamp.h:95
TTimeStamp::GetDayOfWeek
Int_t GetDayOfWeek(Bool_t inUTC=kTRUE, Int_t secOffset=0) const
Method is using Zeller's formula for calculating the day number.
Definition: TTimeStamp.cxx:418
kFALSE
const Bool_t kFALSE
Definition: RtypesCore.h:92
UInt_t
unsigned int UInt_t
Definition: RtypesCore.h:46
TTimeStamp::AsGMST
Double_t AsGMST(Double_t UT1Offset=0) const
Return Greenwich mean sidereal time (GMST) in hour-angle.
Definition: TTimeStamp.cxx:168
TTimeStamp::AsString
const char * AsString(const Option_t *option="") const
Return the date & time as a string.
Definition: TTimeStamp.cxx:271
double
double
Definition: Converters.cxx:921
TTimeStamp::operator<=
friend Bool_t operator<=(const TTimeStamp &lhs, const TTimeStamp &rhs)
Definition: TTimeStamp.h:194
TTimeStamp::SetSec
void SetSec(Int_t sec)
Definition: TTimeStamp.h:130
TTimeStamp::DumpTMStruct
static void DumpTMStruct(const tm_t &tmstruct)
Print out the "tm" structure:
Definition: TTimeStamp.cxx:903
TTimeStamp::GetMonth
Int_t GetMonth(Bool_t inUTC=kTRUE, Int_t secOffset=0) const
Get the month of the year. Valid return values are between 1 and 12.
Definition: TTimeStamp.cxx:438
Double_t
double Double_t
Definition: RtypesCore.h:59
TTimeStamp::SetNanoSec
void SetNanoSec(Int_t nsec)
Definition: TTimeStamp.h:131
operator<<
std::ostream & operator<<(std::ostream &os, const TTimeStamp &ts)
Write time stamp to std::ostream.
Definition: TTimeStamp.cxx:60
ClassDef
#define ClassDef(name, id)
Definition: Rtypes.h:325
TTimeStamp::operator!=
friend Bool_t operator!=(const TTimeStamp &lhs, const TTimeStamp &rhs)
Definition: TTimeStamp.h:185
TTimeStamp::GetTime
UInt_t GetTime(Bool_t inUTC=kTRUE, Int_t secOffset=0, UInt_t *hour=0, UInt_t *min=0, UInt_t *sec=0) const
Return time in form of 123623 (i.e.
Definition: TTimeStamp.cxx:375
TTimeStamp::AsGAST
Double_t AsGAST(Double_t UT1Offset=0) const
Return Greenwich apparent sidereal time (GAST) in hour-angle.
Definition: TTimeStamp.cxx:191
operator>>
TBuffer & operator>>(TBuffer &buf, TTimeStamp &ts)
Read time stamp from TBuffer.
Definition: TTimeStamp.cxx:74
TTimeStamp::operator>
friend Bool_t operator>(const TTimeStamp &lhs, const TTimeStamp &rhs)
Definition: TTimeStamp.h:199
TTimeStamp::GetZoneOffset
static Int_t GetZoneOffset()
Static method returning local (current) time zone offset from UTC.
Definition: TTimeStamp.cxx:504
R__EXTERN
#define R__EXTERN
Definition: DllImport.h:27
TTimeStamp::GetTimeSpec
timespec_t GetTimeSpec() const
Definition: TTimeStamp.h:133
operator>=
Bool_t operator>=(const TTimeStamp &lhs, const TTimeStamp &rhs)
Definition: TTimeStamp.h:204
TTimeStamp::GetDayOfYear
Int_t GetDayOfYear(Bool_t inUTC=kTRUE, Int_t secOffset=0) const
Get the day of the year represented by this time stamp value.
Definition: TTimeStamp.cxx:397
TTimeStamp
Definition: TTimeStamp.h:71
TTimeStamp::IsLeapYear
Bool_t IsLeapYear(Bool_t inUTC=kTRUE, Int_t secOffset=0) const
Is the year a leap year.
Definition: TTimeStamp.cxx:484
operator>
Bool_t operator>(const TTimeStamp &lhs, const TTimeStamp &rhs)
Definition: TTimeStamp.h:199
operator==
Bool_t operator==(const TTimeStamp &lhs, const TTimeStamp &rhs)
Definition: TTimeStamp.h:181
TTimeStamp::Copy
void Copy(TTimeStamp &ts) const
Copy this to ts.
Definition: TTimeStamp.cxx:343
Rtypes.h
TTimeStamp::fSec
Int_t fSec
Definition: TTimeStamp.h:81
int
TTimeStamp::fNanoSec
Int_t fNanoSec
Definition: TTimeStamp.h:82