00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #ifndef ROOT_TTimeStamp
00013 #define ROOT_TTimeStamp
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 #ifndef ROOT_Rtypes
00043 #include "Rtypes.h"
00044 #endif
00045 #ifndef ROOT_Riosfwd
00046 #include "Riosfwd.h"
00047 #endif
00048
00049 #include <time.h>
00050 #if !defined(__CINT__) && (defined(R__MACOSX) || defined(R__OBSD))
00051 #include <sys/time.h>
00052 #endif
00053 #if defined(__CINT__) || defined(R__WIN32)
00054
00055
00056
00057
00058
00059 struct timespec
00060 {
00061 time_t tv_sec;
00062 long tv_nsec;
00063 };
00064 #endif
00065 #if defined(__CINT__)
00066 struct tm
00067 {
00068 int tm_sec;
00069 int tm_min;
00070 int tm_hour;
00071 int tm_mday;
00072 int tm_mon;
00073 int tm_year;
00074 int tm_wday;
00075 int tm_yday;
00076 int tm_isdst;
00077 };
00078 #endif
00079
00080
00081
00082 typedef struct timespec timespec_t;
00083 typedef struct tm tm_t;
00084
00085 class TVirtualMutex;
00086 class TTimeStamp;
00087 ostream &operator<<(ostream &os, const TTimeStamp &ts);
00088 TBuffer &operator<<(TBuffer &buf, const TTimeStamp &ts);
00089 TBuffer &operator>>(TBuffer &buf, TTimeStamp &ts);
00090 Bool_t operator==(const TTimeStamp &lhs, const TTimeStamp &rhs);
00091 Bool_t operator!=(const TTimeStamp &lhs, const TTimeStamp &rhs);
00092 Bool_t operator< (const TTimeStamp &lhs, const TTimeStamp &rhs);
00093 Bool_t operator<=(const TTimeStamp &lhs, const TTimeStamp &rhs);
00094 Bool_t operator> (const TTimeStamp &lhs, const TTimeStamp &rhs);
00095 Bool_t operator>=(const TTimeStamp &lhs, const TTimeStamp &rhs);
00096
00097 R__EXTERN TVirtualMutex *gTimeMutex;
00098
00099 class TTimeStamp {
00100
00101 friend Bool_t operator==(const TTimeStamp &lhs, const TTimeStamp &rhs);
00102 friend Bool_t operator!=(const TTimeStamp &lhs, const TTimeStamp &rhs);
00103 friend Bool_t operator< (const TTimeStamp &lhs, const TTimeStamp &rhs);
00104 friend Bool_t operator<=(const TTimeStamp &lhs, const TTimeStamp &rhs);
00105 friend Bool_t operator> (const TTimeStamp &lhs, const TTimeStamp &rhs);
00106 friend Bool_t operator>=(const TTimeStamp &lhs, const TTimeStamp &rhs);
00107
00108 private:
00109 Int_t fSec;
00110 Int_t fNanoSec;
00111
00112 void NormalizeNanoSec();
00113
00114 public:
00115
00116 TTimeStamp();
00117
00118
00119 TTimeStamp(const timespec_t &ts) :
00120 fSec(Int_t(ts.tv_sec)), fNanoSec(ts.tv_nsec) { NormalizeNanoSec(); }
00121
00122
00123 TTimeStamp(time_t t, Int_t nsec) :
00124 fSec(Int_t(t)), fNanoSec(nsec) { NormalizeNanoSec(); }
00125
00126
00127 TTimeStamp(UInt_t year, UInt_t month,
00128 UInt_t day, UInt_t hour,
00129 UInt_t min, UInt_t sec,
00130 UInt_t nsec = 0, Bool_t isUTC = kTRUE, Int_t secOffset = 0);
00131
00132
00133 TTimeStamp(UInt_t date, UInt_t time, UInt_t nsec,
00134 Bool_t isUTC = kTRUE, Int_t secOffset = 0);
00135
00136
00137 TTimeStamp(UInt_t tloc, Bool_t isUTC = kTRUE, Int_t secOffset = 0,
00138 Bool_t dosDate = kFALSE);
00139
00140 virtual ~TTimeStamp() { }
00141
00142
00143 void Set();
00144
00145
00146 void Set(Int_t year, Int_t month, Int_t day,
00147 Int_t hour, Int_t min, Int_t sec,
00148 Int_t nsec, Bool_t isUTC, Int_t secOffset);
00149
00150
00151 void Set(Int_t date, Int_t time, Int_t nsec,
00152 Bool_t isUTC, Int_t secOffset);
00153
00154
00155 void Set(UInt_t tloc, Bool_t isUTC, Int_t secOffset, Bool_t dosDate);
00156
00157
00158 void SetSec(Int_t sec) { fSec = sec; }
00159 void SetNanoSec(Int_t nsec) { fNanoSec = nsec; }
00160
00161 timespec_t GetTimeSpec() const
00162 { timespec_t value = {fSec,fNanoSec}; return value; }
00163 time_t GetSec() const { return fSec; }
00164 Int_t GetNanoSec() const { return fNanoSec; }
00165
00166 Double_t AsDouble() const { return fSec + 1e-9 * fNanoSec; }
00167 Double_t AsJulianDate() const { return (AsDouble()/86400.0 + 2440587.5); }
00168 const char *AsString(const Option_t *option="") const;
00169
00170 void Copy(TTimeStamp &ts) const;
00171 UInt_t GetDate(Bool_t inUTC = kTRUE, Int_t secOffset = 0,
00172 UInt_t *year = 0, UInt_t *month = 0,
00173 UInt_t *day = 0) const;
00174 UInt_t GetTime(Bool_t inUTC = kTRUE, Int_t secOffset = 0,
00175 UInt_t *hour = 0, UInt_t *min = 0,
00176 UInt_t *sec = 0) const;
00177 Int_t GetDayOfYear(Bool_t inUTC = kTRUE, Int_t secOffset = 0) const;
00178 Int_t GetDayOfWeek(Bool_t inUTC = kTRUE, Int_t secOffset = 0) const;
00179 Int_t GetMonth(Bool_t inUTC = kTRUE, Int_t secOffset = 0) const;
00180 Int_t GetWeek(Bool_t inUTC = kTRUE, Int_t secOffset = 0) const;
00181 Bool_t IsLeapYear(Bool_t inUTC = kTRUE, Int_t secOffset = 0) const;
00182
00183 void Add(const TTimeStamp &offset);
00184
00185 void Print(const Option_t *option="") const;
00186
00187 operator double() const { return AsDouble(); }
00188
00189
00190 static Int_t GetZoneOffset();
00191 static time_t MktimeFromUTC(tm_t *tmstruct);
00192 static void DumpTMStruct(const tm_t &tmstruct);
00193 static Int_t GetDayOfYear(Int_t day, Int_t month, Int_t year);
00194 static Int_t GetDayOfWeek(Int_t day, Int_t month, Int_t year);
00195 static Int_t GetWeek(Int_t day, Int_t month, Int_t year);
00196 static Bool_t IsLeapYear(Int_t year);
00197
00198 ClassDef(TTimeStamp,1)
00199 };
00200
00201
00202 inline Bool_t operator==(const TTimeStamp &lhs, const TTimeStamp &rhs)
00203 { return lhs.fSec == rhs.fSec &&
00204 lhs.fNanoSec == rhs.fNanoSec; }
00205
00206 inline Bool_t operator!=(const TTimeStamp &lhs, const TTimeStamp &rhs)
00207 { return lhs.fSec != rhs.fSec ||
00208 lhs.fNanoSec != rhs.fNanoSec; }
00209
00210 inline Bool_t operator<(const TTimeStamp &lhs, const TTimeStamp &rhs)
00211 { return lhs.fSec < rhs.fSec ||
00212 (lhs.fSec == rhs.fSec &&
00213 lhs.fNanoSec < rhs.fNanoSec); }
00214
00215 inline Bool_t operator<=(const TTimeStamp &lhs, const TTimeStamp &rhs)
00216 { return lhs.fSec < rhs.fSec ||
00217 (lhs.fSec == rhs.fSec &&
00218 lhs.fNanoSec <= rhs.fNanoSec); }
00219
00220 inline Bool_t operator>(const TTimeStamp &lhs, const TTimeStamp &rhs)
00221 { return lhs.fSec > rhs.fSec ||
00222 (lhs.fSec == rhs.fSec &&
00223 lhs.fNanoSec > rhs.fNanoSec); }
00224
00225 inline Bool_t operator>=(const TTimeStamp &lhs, const TTimeStamp &rhs)
00226 { return lhs.fSec > rhs.fSec ||
00227 (lhs.fSec == rhs.fSec &&
00228 lhs.fNanoSec >= rhs.fNanoSec); }
00229
00230 #endif