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#include "Rtypes.h"
16
17#include <ctime>
18
19#if defined (_MSC_VER) && (_MSC_VER < 1900)
20struct timespec {
21 time_t tv_sec; // seconds
22 long tv_nsec; // nanoseconds
23};
24#endif
25
26// For backward compatibility
27typedef struct timespec timespec_t;
28typedef struct tm tm_t;
29
30class TVirtualMutex;
31class TTimeStamp;
32
33std::ostream &operator<<(std::ostream &os, const TTimeStamp &ts);
34TBuffer &operator<<(TBuffer &buf, const TTimeStamp &ts);
36Bool_t operator==(const TTimeStamp &lhs, const TTimeStamp &rhs);
37Bool_t operator!=(const TTimeStamp &lhs, const TTimeStamp &rhs);
38Bool_t operator< (const TTimeStamp &lhs, const TTimeStamp &rhs);
39Bool_t operator<=(const TTimeStamp &lhs, const TTimeStamp &rhs);
40Bool_t operator> (const TTimeStamp &lhs, const TTimeStamp &rhs);
41Bool_t operator>=(const TTimeStamp &lhs, const TTimeStamp &rhs);
42
44
46
47friend Bool_t operator==(const TTimeStamp &lhs, const TTimeStamp &rhs);
48friend Bool_t operator!=(const TTimeStamp &lhs, const TTimeStamp &rhs);
49friend Bool_t operator< (const TTimeStamp &lhs, const TTimeStamp &rhs);
50friend Bool_t operator<=(const TTimeStamp &lhs, const TTimeStamp &rhs);
51friend Bool_t operator> (const TTimeStamp &lhs, const TTimeStamp &rhs);
52friend Bool_t operator>=(const TTimeStamp &lhs, const TTimeStamp &rhs);
53
54private:
55 Int_t fSec; // seconds
56 Int_t fNanoSec; // nanoseconds
57
58 void NormalizeNanoSec();
59
60public:
61 // empty ctor (builds current time with nsec field incremented from static)
62 TTimeStamp();
63
64 // construction from timespec struct
66 fSec(Int_t(ts.tv_sec)), fNanoSec(ts.tv_nsec) { NormalizeNanoSec(); }
67
68 // construction from time_t and separate nsec
69 TTimeStamp(time_t t, Int_t nsec) :
70 fSec(Int_t(t)), fNanoSec(nsec) { NormalizeNanoSec(); }
71
72 // construction from bits and pieces
73 TTimeStamp(UInt_t year, UInt_t month,
74 UInt_t day, UInt_t hour,
75 UInt_t min, UInt_t sec,
76 UInt_t nsec = 0, Bool_t isUTC = kTRUE, Int_t secOffset = 0);
77
78 // compatibility with TDatime
79 TTimeStamp(UInt_t date, UInt_t time, UInt_t nsec,
80 Bool_t isUTC = kTRUE, Int_t secOffset = 0);
81
82 // compatibility with time() and DOS date
83 TTimeStamp(UInt_t tloc, Bool_t isUTC = kTRUE, Int_t secOffset = 0,
84 Bool_t dosDate = kFALSE);
85
86 virtual ~TTimeStamp() { }
87
88 // initialize to current time with nsec field incremented from static
89 void Set();
90
91 // construction from bits and pieces
92 void Set(Int_t year, Int_t month, Int_t day,
93 Int_t hour, Int_t min, Int_t sec,
94 Int_t nsec, Bool_t isUTC, Int_t secOffset);
95
96 // compatibility with TDatime
97 void Set(Int_t date, Int_t time, Int_t nsec,
98 Bool_t isUTC, Int_t secOffset);
99
100 // compatibility with time() and DOS date
101 void Set(UInt_t tloc, Bool_t isUTC, Int_t secOffset, Bool_t dosDate);
102
103 // direct setters
104 void SetSec(Int_t sec) { fSec = sec; }
105 void SetNanoSec(Int_t nsec) { fNanoSec = nsec; }
106
108 { timespec_t value = {fSec,fNanoSec}; return value; }
109 time_t GetSec() const { return fSec; }
110 Int_t GetNanoSec() const { return fNanoSec; }
111
112 Double_t AsDouble() const { return fSec + 1e-9 * fNanoSec; }
113 Double_t AsJulianDate() const { return (AsDouble()/86400.0 + 2440587.5); }
114
115 // return stored time values converted to sidereal time
116 Double_t AsGMST(Double_t UT1Offset = 0 /*milliseconds*/) const; //rval in hours
117 Double_t AsGAST(Double_t UT1Offset = 0 /*milliseconds*/) const; //rval in hours
118 Double_t AsLMST(Double_t Longitude /*degrees*/, Double_t UT1Offset = 0 /*milliseconds*/) const; //rval in hours
119 Double_t AsLAST(Double_t Longitude /*degrees*/, Double_t UT1Offset = 0 /*milliseconds*/) const; //rval in hours
120
121 const char *AsString(const Option_t *option="") const;
122
123 void Copy(TTimeStamp &ts) const;
124 UInt_t GetDate(Bool_t inUTC = kTRUE, Int_t secOffset = 0,
125 UInt_t *year = nullptr, UInt_t *month = nullptr,
126 UInt_t *day = nullptr) const;
127 UInt_t GetTime(Bool_t inUTC = kTRUE, Int_t secOffset = 0,
128 UInt_t *hour = nullptr, UInt_t *min = nullptr,
129 UInt_t *sec = nullptr) const;
130 Int_t GetDayOfYear(Bool_t inUTC = kTRUE, Int_t secOffset = 0) const;
131 Int_t GetDayOfWeek(Bool_t inUTC = kTRUE, Int_t secOffset = 0) const;
132 Int_t GetMonth(Bool_t inUTC = kTRUE, Int_t secOffset = 0) const;
133 Int_t GetWeek(Bool_t inUTC = kTRUE, Int_t secOffset = 0) const;
134 Bool_t IsLeapYear(Bool_t inUTC = kTRUE, Int_t secOffset = 0) const;
135
136 void Add(const TTimeStamp &offset);
137
138 void Print(const Option_t *option="") const;
139
140 operator double() const { return AsDouble(); }
141
142 // Utility functions
143 static Int_t GetZoneOffset();
144 static time_t MktimeFromUTC(tm_t *tmstruct);
145 static void DumpTMStruct(const tm_t &tmstruct);
146 static Int_t GetDayOfYear(Int_t day, Int_t month, Int_t year);
147 static Int_t GetDayOfWeek(Int_t day, Int_t month, Int_t year);
148 static Int_t GetWeek(Int_t day, Int_t month, Int_t year);
149 static Bool_t IsLeapYear(Int_t year);
150
151 ClassDef(TTimeStamp,1) //Encapsulates seconds and ns since EPOCH
152};
153
154
155inline Bool_t operator==(const TTimeStamp &lhs, const TTimeStamp &rhs)
156 { return lhs.fSec == rhs.fSec &&
157 lhs.fNanoSec == rhs.fNanoSec; }
158
159inline Bool_t operator!=(const TTimeStamp &lhs, const TTimeStamp &rhs)
160 { return lhs.fSec != rhs.fSec ||
161 lhs.fNanoSec != rhs.fNanoSec; }
162
163inline Bool_t operator<(const TTimeStamp &lhs, const TTimeStamp &rhs)
164 { return lhs.fSec < rhs.fSec ||
165 (lhs.fSec == rhs.fSec &&
166 lhs.fNanoSec < rhs.fNanoSec); }
167
168inline Bool_t operator<=(const TTimeStamp &lhs, const TTimeStamp &rhs)
169 { return lhs.fSec < rhs.fSec ||
170 (lhs.fSec == rhs.fSec &&
171 lhs.fNanoSec <= rhs.fNanoSec); }
172
173inline Bool_t operator>(const TTimeStamp &lhs, const TTimeStamp &rhs)
174 { return lhs.fSec > rhs.fSec ||
175 (lhs.fSec == rhs.fSec &&
176 lhs.fNanoSec > rhs.fNanoSec); }
177
178inline Bool_t operator>=(const TTimeStamp &lhs, const TTimeStamp &rhs)
179 { return lhs.fSec > rhs.fSec ||
180 (lhs.fSec == rhs.fSec &&
181 lhs.fNanoSec >= rhs.fNanoSec); }
182
183#endif
#define R__EXTERN
Definition DllImport.h:27
#define e(i)
Definition RSha256.hxx:103
bool Bool_t
Definition RtypesCore.h:63
int Int_t
Definition RtypesCore.h:45
unsigned int UInt_t
Definition RtypesCore.h:46
constexpr Bool_t kFALSE
Definition RtypesCore.h:101
double Double_t
Definition RtypesCore.h:59
constexpr Bool_t kTRUE
Definition RtypesCore.h:100
const char Option_t
Definition RtypesCore.h:66
#define ClassDef(name, id)
Definition Rtypes.h:337
TBuffer & operator<<(TBuffer &buf, const Tmpl *obj)
Definition TBuffer.h:399
Option_t Option_t option
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h offset
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
Bool_t operator>=(const TTimeStamp &lhs, const TTimeStamp &rhs)
Definition TTimeStamp.h:178
Bool_t operator<=(const TTimeStamp &lhs, const TTimeStamp &rhs)
Definition TTimeStamp.h:168
struct tm tm_t
Definition TTimeStamp.h:28
Bool_t operator<(const TTimeStamp &lhs, const TTimeStamp &rhs)
Definition TTimeStamp.h:163
TBuffer & operator>>(TBuffer &buf, TTimeStamp &ts)
Read time stamp from TBuffer.
Bool_t operator!=(const TTimeStamp &lhs, const TTimeStamp &rhs)
Definition TTimeStamp.h:159
struct timespec timespec_t
Definition TTimeStamp.h:27
Bool_t operator>(const TTimeStamp &lhs, const TTimeStamp &rhs)
Definition TTimeStamp.h:173
R__EXTERN TVirtualMutex * gTimeMutex
Definition TTimeStamp.h:43
Bool_t operator==(const TTimeStamp &lhs, const TTimeStamp &rhs)
Definition TTimeStamp.h:155
Buffer base class used for serializing objects.
Definition TBuffer.h:43
The TTimeStamp encapsulates seconds and ns since EPOCH.
Definition TTimeStamp.h:45
timespec_t GetTimeSpec() const
Definition TTimeStamp.h:107
Bool_t IsLeapYear(Bool_t inUTC=kTRUE, Int_t secOffset=0) const
Is the year a leap year.
UInt_t GetTime(Bool_t inUTC=kTRUE, Int_t secOffset=0, UInt_t *hour=nullptr, UInt_t *min=nullptr, UInt_t *sec=nullptr) const
Return time in form of 123623 (i.e.
friend Bool_t operator>=(const TTimeStamp &lhs, const TTimeStamp &rhs)
Definition TTimeStamp.h:178
friend Bool_t operator<=(const TTimeStamp &lhs, const TTimeStamp &rhs)
Definition TTimeStamp.h:168
TTimeStamp(const timespec_t &ts)
Definition TTimeStamp.h:65
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:104
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:163
void SetNanoSec(Int_t nsec)
Definition TTimeStamp.h:105
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:109
Double_t AsJulianDate() const
Definition TTimeStamp.h:113
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:159
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:55
void Set()
Set Date/Time to current time as reported by the system.
TTimeStamp(time_t t, Int_t nsec)
Definition TTimeStamp.h:69
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:173
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:110
friend Bool_t operator==(const TTimeStamp &lhs, const TTimeStamp &rhs)
Definition TTimeStamp.h:155
virtual ~TTimeStamp()
Definition TTimeStamp.h:86
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:112
const char * AsString(const Option_t *option="") const
Return the date & time as a string.
Int_t fNanoSec
Definition TTimeStamp.h:56
UInt_t GetDate(Bool_t inUTC=kTRUE, Int_t secOffset=0, UInt_t *year=nullptr, UInt_t *month=nullptr, UInt_t *day=nullptr) const
Return date in form of 19971224 (i.e.
void NormalizeNanoSec()
Ensure that the fNanoSec field is in range [0,999999999].
This class implements a mutex interface.