Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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)
47struct timespec {
48 time_t tv_sec; // seconds
49 long tv_nsec; // nanoseconds
50};
51#endif
52
53// For backward compatibility
54typedef struct timespec timespec_t;
55typedef struct tm tm_t;
56
57class TVirtualMutex;
58class TTimeStamp;
59std::ostream &operator<<(std::ostream &os, const TTimeStamp &ts);
60TBuffer &operator<<(TBuffer &buf, const TTimeStamp &ts);
62Bool_t operator==(const TTimeStamp &lhs, const TTimeStamp &rhs);
63Bool_t operator!=(const TTimeStamp &lhs, const TTimeStamp &rhs);
64Bool_t operator< (const TTimeStamp &lhs, const TTimeStamp &rhs);
65Bool_t operator<=(const TTimeStamp &lhs, const TTimeStamp &rhs);
66Bool_t operator> (const TTimeStamp &lhs, const TTimeStamp &rhs);
67Bool_t operator>=(const TTimeStamp &lhs, const TTimeStamp &rhs);
68
70
72
73friend Bool_t operator==(const TTimeStamp &lhs, const TTimeStamp &rhs);
74friend Bool_t operator!=(const TTimeStamp &lhs, const TTimeStamp &rhs);
75friend Bool_t operator< (const TTimeStamp &lhs, const TTimeStamp &rhs);
76friend Bool_t operator<=(const TTimeStamp &lhs, const TTimeStamp &rhs);
77friend Bool_t operator> (const TTimeStamp &lhs, const TTimeStamp &rhs);
78friend Bool_t operator>=(const TTimeStamp &lhs, const TTimeStamp &rhs);
79
80private:
81 Int_t fSec; // seconds
82 Int_t fNanoSec; // nanoseconds
83
84 void NormalizeNanoSec();
85
86public:
87 // empty ctor (builds current time with nsec field incremented from static)
88 TTimeStamp();
89
90 // construction from timespec struct
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
181inline Bool_t operator==(const TTimeStamp &lhs, const TTimeStamp &rhs)
182 { return lhs.fSec == rhs.fSec &&
183 lhs.fNanoSec == rhs.fNanoSec; }
184
185inline Bool_t operator!=(const TTimeStamp &lhs, const TTimeStamp &rhs)
186 { return lhs.fSec != rhs.fSec ||
187 lhs.fNanoSec != rhs.fNanoSec; }
188
189inline 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
194inline 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
199inline 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
204inline 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
double
#define R__EXTERN
Definition DllImport.h:27
#define e(i)
Definition RSha256.hxx:103
int Int_t
Definition RtypesCore.h:45
unsigned int UInt_t
Definition RtypesCore.h:46
const Bool_t kFALSE
Definition RtypesCore.h:92
bool Bool_t
Definition RtypesCore.h:63
double Double_t
Definition RtypesCore.h:59
const Bool_t kTRUE
Definition RtypesCore.h:91
const char Option_t
Definition RtypesCore.h:66
#define ClassDef(name, id)
Definition Rtypes.h:325
TBuffer & operator<<(TBuffer &buf, const Tmpl *obj)
Definition TBuffer.h:399
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
struct tm tm_t
Definition TTimeStamp.h:55
Bool_t operator<(const TTimeStamp &lhs, const TTimeStamp &rhs)
Definition TTimeStamp.h:189
TBuffer & operator>>(TBuffer &buf, TTimeStamp &ts)
Read time stamp from TBuffer.
Bool_t operator!=(const TTimeStamp &lhs, const TTimeStamp &rhs)
Definition TTimeStamp.h:185
struct timespec timespec_t
Definition TTimeStamp.h:54
Bool_t operator>(const TTimeStamp &lhs, const TTimeStamp &rhs)
Definition TTimeStamp.h:199
R__EXTERN TVirtualMutex * gTimeMutex
Definition TTimeStamp.h:69
Bool_t operator==(const TTimeStamp &lhs, const TTimeStamp &rhs)
Definition TTimeStamp.h:181
Buffer base class used for serializing objects.
Definition TBuffer.h:43
The TTimeStamp encapsulates seconds and ns since EPOCH.
Definition TTimeStamp.h:71
timespec_t GetTimeSpec() const
Definition TTimeStamp.h:133
Bool_t IsLeapYear(Bool_t inUTC=kTRUE, Int_t secOffset=0) const
Is the year a leap year.
friend Bool_t operator>=(const TTimeStamp &lhs, const TTimeStamp &rhs)
Definition TTimeStamp.h:204
friend Bool_t operator<=(const TTimeStamp &lhs, const TTimeStamp &rhs)
Definition TTimeStamp.h:194
TTimeStamp(const timespec_t &ts)
Definition TTimeStamp.h:91
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.
void SetSec(Int_t sec)
Definition TTimeStamp.h:130
Int_t GetDayOfYear(Bool_t inUTC=kTRUE, Int_t secOffset=0) const
Get the day of the year represented by this time stamp value.
friend Bool_t operator<(const TTimeStamp &lhs, const TTimeStamp &rhs)
Definition TTimeStamp.h:189
void SetNanoSec(Int_t nsec)
Definition TTimeStamp.h:131
static void DumpTMStruct(const tm_t &tmstruct)
Print out the "tm" structure:
Int_t GetWeek(Bool_t inUTC=kTRUE, Int_t secOffset=0) const
Get the week of the year.
TTimeStamp()
Default ctor.
time_t GetSec() const
Definition TTimeStamp.h:135
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.
Double_t AsJulianDate() const
Definition TTimeStamp.h:139
void Add(const TTimeStamp &offset)
Add "offset" as a delta time.
void Copy(TTimeStamp &ts) const
Copy this to ts.
Int_t GetDayOfWeek(Bool_t inUTC=kTRUE, Int_t secOffset=0) const
Method is using Zeller's formula for calculating the day number.
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.
friend Bool_t operator!=(const TTimeStamp &lhs, const TTimeStamp &rhs)
Definition TTimeStamp.h:185
Double_t AsGMST(Double_t UT1Offset=0) const
Return Greenwich mean sidereal time (GMST) in hour-angle.
void Print(const Option_t *option="") const
Print date and time.
Int_t fSec
Definition TTimeStamp.h:81
void Set()
Set Date/Time to current time as reported by the system.
TTimeStamp(time_t t, Int_t nsec)
Definition TTimeStamp.h:95
Double_t AsGAST(Double_t UT1Offset=0) const
Return Greenwich apparent sidereal time (GAST) in hour-angle.
friend Bool_t operator>(const TTimeStamp &lhs, const TTimeStamp &rhs)
Definition TTimeStamp.h:199
static Int_t GetZoneOffset()
Static method returning local (current) time zone offset from UTC.
static time_t MktimeFromUTC(tm_t *tmstruct)
Equivalent of standard routine "mktime" but using the assumption that tm struct is filled with UTC,...
Int_t GetNanoSec() const
Definition TTimeStamp.h:136
friend Bool_t operator==(const TTimeStamp &lhs, const TTimeStamp &rhs)
Definition TTimeStamp.h:181
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.
virtual ~TTimeStamp()
Definition TTimeStamp.h:112
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.
Double_t AsDouble() const
Definition TTimeStamp.h:138
const char * AsString(const Option_t *option="") const
Return the date & time as a string.
Int_t fNanoSec
Definition TTimeStamp.h:82
void NormalizeNanoSec()
Ensure that the fNanoSec field is in range [0,999999999].
This class implements a mutex interface.