Logo ROOT   master
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
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
void SetNanoSec(Int_t nsec)
Definition: TTimeStamp.h:131
Bool_t IsLeapYear(Bool_t inUTC=kTRUE, Int_t secOffset=0) const
Is the year a leap year.
Definition: TTimeStamp.cxx:484
Int_t GetWeek(Bool_t inUTC=kTRUE, Int_t secOffset=0) const
Get the week of the year.
Definition: TTimeStamp.cxx:456
TBuffer & operator>>(TBuffer &buf, TTimeStamp &ts)
Read time stamp from TBuffer.
Definition: TTimeStamp.cxx:74
double
Definition: Converters.cxx:921
friend Bool_t operator<(const TTimeStamp &lhs, const TTimeStamp &rhs)
Definition: TTimeStamp.h:189
friend Bool_t operator!=(const TTimeStamp &lhs, const TTimeStamp &rhs)
Definition: TTimeStamp.h:185
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
unsigned int UInt_t
Definition: CPyCppyy.h:44
const char Option_t
Definition: RtypesCore.h:64
Bool_t operator==(const TTimeStamp &lhs, const TTimeStamp &rhs)
Definition: TTimeStamp.h:181
Double_t AsDouble() const
Definition: TTimeStamp.h:138
int Int_t
Definition: CPyCppyy.h:43
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
TTimeStamp(time_t t, Int_t nsec)
Definition: TTimeStamp.h:95
Buffer base class used for serializing objects.
Definition: TBuffer.h:42
This class implements a mutex interface.
Definition: TVirtualMutex.h:32
friend Bool_t operator<=(const TTimeStamp &lhs, const TTimeStamp &rhs)
Definition: TTimeStamp.h:194
struct tm tm_t
Definition: TTimeStamp.h:55
bool Bool_t
Definition: RtypesCore.h:61
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
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
void NormalizeNanoSec()
Ensure that the fNanoSec field is in range [0,999999999].
Definition: TTimeStamp.cxx:739
Int_t fSec
Definition: TTimeStamp.h:81
friend Bool_t operator>=(const TTimeStamp &lhs, const TTimeStamp &rhs)
Definition: TTimeStamp.h:204
Int_t GetDayOfWeek(Bool_t inUTC=kTRUE, Int_t secOffset=0) const
Method is using Zeller&#39;s formula for calculating the day number.
Definition: TTimeStamp.cxx:418
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 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
Int_t fNanoSec
Definition: TTimeStamp.h:82
#define ClassDef(name, id)
Definition: Rtypes.h:322
void Add(const TTimeStamp &offset)
Add "offset" as a delta time.
Definition: TTimeStamp.cxx:534
void Print(const Option_t *option="") const
Print date and time.
Definition: TTimeStamp.cxx:544
const char * AsString(const Option_t *option="") const
Return the date & time as a string.
Definition: TTimeStamp.cxx:271
void Set()
Set Date/Time to current time as reported by the system.
Definition: TTimeStamp.cxx:556
virtual ~TTimeStamp()
Definition: TTimeStamp.h:112
TTimeStamp()
Default ctor.
Definition: TTimeStamp.cxx:95
timespec_t GetTimeSpec() const
Definition: TTimeStamp.h:133
void SetSec(Int_t sec)
Definition: TTimeStamp.h:130
static void DumpTMStruct(const tm_t &tmstruct)
Print out the "tm" structure: tmstruct.tm_year = year; // years since 1900 tmstruct.tm_mon = month-1; // months since Jan [0,11] tmstruct.tm_mday = day; // day of the month [1,31] tmstruct.tm_hour = hour; // hours since midnight [0,23] tmstruct.tm_min = min; // minutes after the hour [0,59] tmstruct.tm_sec = sec; // seconds after the minute [0,59] tmstruct.tm_wday // day of week [0,6] tmstruct.tm_yday // days in year [0,365] tmstruct.tm_isdst // DST [-1/0/1] (unknown,false,true).
Definition: TTimeStamp.cxx:903
std::ostream & operator<<(std::ostream &os, const TTimeStamp &ts)
Write time stamp to std::ostream.
Definition: TTimeStamp.cxx:60
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
friend Bool_t operator==(const TTimeStamp &lhs, const TTimeStamp &rhs)
Definition: TTimeStamp.h:181
const Bool_t kFALSE
Definition: RtypesCore.h:90
double Double_t
Definition: RtypesCore.h:57
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
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
void Copy(TTimeStamp &ts) const
Copy this to ts.
Definition: TTimeStamp.cxx:343
Double_t AsGAST(Double_t UT1Offset=0) const
Return Greenwich apparent sidereal time (GAST) in hour-angle.
Definition: TTimeStamp.cxx:191
static Int_t GetZoneOffset()
Static method returning local (current) time zone offset from UTC.
Definition: TTimeStamp.cxx:504
const Bool_t kTRUE
Definition: RtypesCore.h:89
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
Int_t GetNanoSec() const
Definition: TTimeStamp.h:136
Double_t AsGMST(Double_t UT1Offset=0) const
Return Greenwich mean sidereal time (GMST) in hour-angle.
Definition: TTimeStamp.cxx:168
friend Bool_t operator>(const TTimeStamp &lhs, const TTimeStamp &rhs)
Definition: TTimeStamp.h:199