ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 #ifndef ROOT_Rtypes
43 #include "Rtypes.h"
44 #endif
45 #ifndef ROOT_Riosfwd
46 #include "Riosfwd.h"
47 #endif
48 
49 #include <ctime>
50 
51 #if defined (_MSC_VER) && (_MSC_VER < 1900)
52 struct timespec {
53  time_t tv_sec; // seconds
54  long tv_nsec; // nanoseconds
55 };
56 #endif
57 
58 // For backward compatibility
59 typedef struct timespec timespec_t;
60 typedef struct tm tm_t;
61 
62 class TVirtualMutex;
63 class TTimeStamp;
64 std::ostream &operator<<(std::ostream &os, const TTimeStamp &ts);
65 TBuffer &operator<<(TBuffer &buf, const TTimeStamp &ts);
67 Bool_t operator==(const TTimeStamp &lhs, const TTimeStamp &rhs);
68 Bool_t operator!=(const TTimeStamp &lhs, const TTimeStamp &rhs);
69 Bool_t operator< (const TTimeStamp &lhs, const TTimeStamp &rhs);
70 Bool_t operator<=(const TTimeStamp &lhs, const TTimeStamp &rhs);
71 Bool_t operator> (const TTimeStamp &lhs, const TTimeStamp &rhs);
72 Bool_t operator>=(const TTimeStamp &lhs, const TTimeStamp &rhs);
73 
75 
76 class TTimeStamp {
77 
78 friend Bool_t operator==(const TTimeStamp &lhs, const TTimeStamp &rhs);
79 friend Bool_t operator!=(const TTimeStamp &lhs, const TTimeStamp &rhs);
80 friend Bool_t operator< (const TTimeStamp &lhs, const TTimeStamp &rhs);
81 friend Bool_t operator<=(const TTimeStamp &lhs, const TTimeStamp &rhs);
82 friend Bool_t operator> (const TTimeStamp &lhs, const TTimeStamp &rhs);
83 friend Bool_t operator>=(const TTimeStamp &lhs, const TTimeStamp &rhs);
84 
85 private:
86  Int_t fSec; // seconds
87  Int_t fNanoSec; // nanoseconds
88 
89  void NormalizeNanoSec();
90 
91 public:
92  // empty ctor (builds current time with nsec field incremented from static)
93  TTimeStamp();
94 
95  // construction from timespec struct
96  TTimeStamp(const timespec_t &ts) :
97  fSec(Int_t(ts.tv_sec)), fNanoSec(ts.tv_nsec) { NormalizeNanoSec(); }
98 
99  // construction from time_t and separate nsec
100  TTimeStamp(time_t t, Int_t nsec) :
101  fSec(Int_t(t)), fNanoSec(nsec) { NormalizeNanoSec(); }
102 
103  // construction from bits and pieces
104  TTimeStamp(UInt_t year, UInt_t month,
105  UInt_t day, UInt_t hour,
106  UInt_t min, UInt_t sec,
107  UInt_t nsec = 0, Bool_t isUTC = kTRUE, Int_t secOffset = 0);
108 
109  // compatibility with TDatime
110  TTimeStamp(UInt_t date, UInt_t time, UInt_t nsec,
111  Bool_t isUTC = kTRUE, Int_t secOffset = 0);
112 
113  // compatability with time() and DOS date
114  TTimeStamp(UInt_t tloc, Bool_t isUTC = kTRUE, Int_t secOffset = 0,
115  Bool_t dosDate = kFALSE);
116 
117  virtual ~TTimeStamp() { }
118 
119  // initialize to current time with nsec field incremented from static
120  void Set();
121 
122  // construction from bits and pieces
123  void Set(Int_t year, Int_t month, Int_t day,
124  Int_t hour, Int_t min, Int_t sec,
125  Int_t nsec, Bool_t isUTC, Int_t secOffset);
126 
127  // compatibility with TDatime
128  void Set(Int_t date, Int_t time, Int_t nsec,
129  Bool_t isUTC, Int_t secOffset);
130 
131  // compatability with time() and DOS date
132  void Set(UInt_t tloc, Bool_t isUTC, Int_t secOffset, Bool_t dosDate);
133 
134  // direct setters
135  void SetSec(Int_t sec) { fSec = sec; }
136  void SetNanoSec(Int_t nsec) { fNanoSec = nsec; }
137 
139  { timespec_t value = {fSec,fNanoSec}; return value; }
140  time_t GetSec() const { return fSec; }
141  Int_t GetNanoSec() const { return fNanoSec; }
142 
143  Double_t AsDouble() const { return fSec + 1e-9 * fNanoSec; }
144  Double_t AsJulianDate() const { return (AsDouble()/86400.0 + 2440587.5); }
145 
146  // return stored time values converted to sidereal time
147  Double_t AsGMST(Double_t UT1Offset = 0 /*milliseconds*/) const; //rval in hours
148  Double_t AsGAST(Double_t UT1Offset = 0 /*milliseconds*/) const; //rval in hours
149  Double_t AsLMST(Double_t Longitude /*degrees*/, Double_t UT1Offset = 0 /*milliseconds*/) const; //rval in hours
150  Double_t AsLAST(Double_t Longitude /*degrees*/, Double_t UT1Offset = 0 /*milliseconds*/) const; //rval in hours
151 
152  const char *AsString(const Option_t *option="") const;
153 
154  void Copy(TTimeStamp &ts) const;
155  UInt_t GetDate(Bool_t inUTC = kTRUE, Int_t secOffset = 0,
156  UInt_t *year = 0, UInt_t *month = 0,
157  UInt_t *day = 0) const;
158  UInt_t GetTime(Bool_t inUTC = kTRUE, Int_t secOffset = 0,
159  UInt_t *hour = 0, UInt_t *min = 0,
160  UInt_t *sec = 0) const;
161  Int_t GetDayOfYear(Bool_t inUTC = kTRUE, Int_t secOffset = 0) const;
162  Int_t GetDayOfWeek(Bool_t inUTC = kTRUE, Int_t secOffset = 0) const;
163  Int_t GetMonth(Bool_t inUTC = kTRUE, Int_t secOffset = 0) const;
164  Int_t GetWeek(Bool_t inUTC = kTRUE, Int_t secOffset = 0) const;
165  Bool_t IsLeapYear(Bool_t inUTC = kTRUE, Int_t secOffset = 0) const;
166 
167  void Add(const TTimeStamp &offset);
168 
169  void Print(const Option_t *option="") const;
170 
171  operator double() const { return AsDouble(); }
172 
173  // Utility functions
174  static Int_t GetZoneOffset();
175  static time_t MktimeFromUTC(tm_t *tmstruct);
176  static void DumpTMStruct(const tm_t &tmstruct);
177  static Int_t GetDayOfYear(Int_t day, Int_t month, Int_t year);
178  static Int_t GetDayOfWeek(Int_t day, Int_t month, Int_t year);
179  static Int_t GetWeek(Int_t day, Int_t month, Int_t year);
180  static Bool_t IsLeapYear(Int_t year);
181 
182  ClassDef(TTimeStamp,1) //Encapsulates seconds and ns since EPOCH
183 };
184 
185 
186 inline Bool_t operator==(const TTimeStamp &lhs, const TTimeStamp &rhs)
187  { return lhs.fSec == rhs.fSec &&
188  lhs.fNanoSec == rhs.fNanoSec; }
189 
190 inline Bool_t operator!=(const TTimeStamp &lhs, const TTimeStamp &rhs)
191  { return 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 inline Bool_t operator>=(const TTimeStamp &lhs, const TTimeStamp &rhs)
210  { return lhs.fSec > rhs.fSec ||
211  (lhs.fSec == rhs.fSec &&
212  lhs.fNanoSec >= rhs.fNanoSec); }
213 
214 #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:765
void SetNanoSec(Int_t nsec)
Definition: TTimeStamp.h:136
TBuffer & operator>>(TBuffer &buf, TTimeStamp &ts)
Read time stamp from TBuffer.
Definition: TTimeStamp.cxx:73
friend Bool_t operator<(const TTimeStamp &lhs, const TTimeStamp &rhs)
Definition: TTimeStamp.h:194
static Vc_ALWAYS_INLINE int_v min(const int_v &x, const int_v &y)
Definition: vector.h:433
friend Bool_t operator!=(const TTimeStamp &lhs, const TTimeStamp &rhs)
Definition: TTimeStamp.h:190
const char * AsString(const Option_t *option="") const
Return the date & time as a string.
Definition: TTimeStamp.cxx:269
Double_t AsGAST(Double_t UT1Offset=0) const
Return Greenwich apparent sidereal time (GAST) in hour-angle.
Definition: TTimeStamp.cxx:190
const char Option_t
Definition: RtypesCore.h:62
tuple offset
Definition: tree.py:93
Bool_t operator==(const TTimeStamp &lhs, const TTimeStamp &rhs)
Definition: TTimeStamp.h:186
TTimeStamp(time_t t, Int_t nsec)
Definition: TTimeStamp.h:100
Buffer base class used for serializing objects.
Definition: TBuffer.h:42
This class implements a mutex interface.
Definition: TVirtualMutex.h:34
friend Bool_t operator<=(const TTimeStamp &lhs, const TTimeStamp &rhs)
Definition: TTimeStamp.h:199
struct tm tm_t
Definition: TTimeStamp.h:60
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:212
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
Double_t AsJulianDate() const
Definition: TTimeStamp.h:144
const Bool_t kFALSE
Definition: Rtypes.h:92
TTimeStamp(const timespec_t &ts)
Definition: TTimeStamp.h:96
void NormalizeNanoSec()
Ensure that the fNanoSec field is in range [0,999999999].
Definition: TTimeStamp.cxx:737
Int_t fSec
Definition: TTimeStamp.h:86
friend Bool_t operator>=(const TTimeStamp &lhs, const TTimeStamp &rhs)
Definition: TTimeStamp.h:209
Bool_t operator<=(const TTimeStamp &lhs, const TTimeStamp &rhs)
Definition: TTimeStamp.h:199
struct timespec timespec_t
Definition: TTimeStamp.h:59
R__EXTERN TVirtualMutex * gTimeMutex
Definition: TTimeStamp.h:74
Int_t fNanoSec
Definition: TTimeStamp.h:87
#define ClassDef(name, id)
Definition: Rtypes.h:254
void Add(const TTimeStamp &offset)
Add "offset" as a delta time.
Definition: TTimeStamp.cxx:532
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:395
void Copy(TTimeStamp &ts) const
Copy this to ts.
Definition: TTimeStamp.cxx:341
void Set()
Set Date/Time to current time as reported by the system.
Definition: TTimeStamp.cxx:554
virtual ~TTimeStamp()
Definition: TTimeStamp.h:117
timespec_t GetTimeSpec() const
Definition: TTimeStamp.h:138
TTimeStamp()
Default ctor.
Definition: TTimeStamp.cxx:94
void Print(const Option_t *option="") const
Print date and time.
Definition: TTimeStamp.cxx:542
time_t GetSec() const
Definition: TTimeStamp.h:140
TThread * t[5]
Definition: threadsh1.C:13
void SetSec(Int_t sec)
Definition: TTimeStamp.h:135
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:901
std::ostream & operator<<(std::ostream &os, const TTimeStamp &ts)
Write time stamp to std::ostream.
Definition: TTimeStamp.cxx:59
Int_t GetNanoSec() const
Definition: TTimeStamp.h:141
Int_t GetWeek(Bool_t inUTC=kTRUE, Int_t secOffset=0) const
Get the week of the year.
Definition: TTimeStamp.cxx:454
unsigned int UInt_t
Definition: RtypesCore.h:42
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:373
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:228
Bool_t operator>(const TTimeStamp &lhs, const TTimeStamp &rhs)
Definition: TTimeStamp.h:204
Bool_t operator<(const TTimeStamp &lhs, const TTimeStamp &rhs)
Definition: TTimeStamp.h:194
Bool_t operator!=(const TTimeStamp &lhs, const TTimeStamp &rhs)
Definition: TTimeStamp.h:190
friend Bool_t operator==(const TTimeStamp &lhs, const TTimeStamp &rhs)
Definition: TTimeStamp.h:186
double Double_t
Definition: RtypesCore.h:55
The TTimeStamp encapsulates seconds and ns since EPOCH.
Definition: TTimeStamp.h:76
Bool_t IsLeapYear(Bool_t inUTC=kTRUE, Int_t secOffset=0) const
Is the year a leap year.
Definition: TTimeStamp.cxx:482
Double_t AsGMST(Double_t UT1Offset=0) const
Return Greenwich mean sidereal time (GMST) in hour-angle.
Definition: TTimeStamp.cxx:167
Double_t AsDouble() const
Definition: TTimeStamp.h:143
#define R__EXTERN
Definition: DllImport.h:27
Bool_t operator>=(const TTimeStamp &lhs, const TTimeStamp &rhs)
Definition: TTimeStamp.h:209
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:351
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:416
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:436
const Bool_t kTRUE
Definition: Rtypes.h:91
static Int_t GetZoneOffset()
Static method returning local (current) time zone offset from UTC.
Definition: TTimeStamp.cxx:502
float value
Definition: math.cpp:443
friend Bool_t operator>(const TTimeStamp &lhs, const TTimeStamp &rhs)
Definition: TTimeStamp.h:204