source: trunk/MagicSoft/Mars/mbase/MTime.h@ 8324

Last change on this file since 8324 was 8066, checked in by tbretz, 18 years ago
*** empty log message ***
File size: 7.2 KB
Line 
1#ifndef MARS_MTime
2#define MARS_MTime
3
4/////////////////////////////////////////////////////////////////////////////
5// //
6// MTime //
7// //
8// A generalized MARS time stamp //
9// //
10/////////////////////////////////////////////////////////////////////////////
11
12#ifndef MARS_MParContainer
13#include "MParContainer.h"
14#endif
15
16#ifndef _CPP_IOSFWD
17#include <iosfwd>
18#endif
19
20#ifndef ROOT_TTime
21#include <TTime.h>
22#endif
23
24struct timeval;
25
26class MTime : public MParContainer
27{
28public:
29 static const UInt_t kHour; // [ms] one hour
30 static const UInt_t kDay; // [ms] one day
31
32 enum {
33 kSunday, kMonday, kTuesday, kWednesday, kThursday, kFriday, kSaturday
34 };
35
36private:
37 UInt_t fMjd; // [d] Day in the century (Day of sun rise)
38 TTime fTime; // [ms] Time of Day (-11h<=x<13h)
39 UInt_t fNanoSec; // [ns] NanoSec part of TimeOfDay (<1000000)
40
41
42 ULong_t GetTime24() const
43 {
44 return (Long_t)fTime<0 ? (Long_t)fTime+kDay : (ULong_t)fTime;
45 }
46 void Init(const char *name, const char *title)
47 {
48 fName = name ? name : "MTime";
49 fTitle = title ? title : "Generalized time stamp";
50 }
51
52public:
53 MTime(const char *name=NULL, const char *title=NULL)
54 {
55 Init(name, title);
56 Clear();
57
58 SetSqlDateTime(name);
59 }
60 MTime(const struct timeval &tm)
61 {
62 Init(NULL, NULL);
63 Set(tm);
64 }
65 MTime(Double_t mjd);
66 MTime(const MTime& t) : MParContainer(), fMjd(t.fMjd), fTime(t.fTime), fNanoSec(t.fNanoSec)
67 {
68 Init(NULL, NULL);
69 }
70
71 void operator=(const MTime &t)
72 {
73 fMjd = t.fMjd;
74 fTime = t.fTime;
75 fNanoSec = t.fNanoSec;
76 }
77
78 void Clear(const Option_t *o="") { fMjd=0; fTime=0; fNanoSec=0; }
79
80 void Print(Option_t *t=NULL) const;
81
82 void Now();
83
84 // Setter functions
85 Bool_t Set(UShort_t y, Byte_t m, Byte_t d, Byte_t h=13, Byte_t min=0, Byte_t s=0, UShort_t ms=0, UInt_t ns=0);
86 void Set(const struct timeval &tv);
87 Bool_t SetString(const char *str);
88 Bool_t SetSqlDateTime(const char *str);
89 Bool_t SetSqlTimeStamp(const char *str);
90 void SetCT1Time(UInt_t mjd, UInt_t t1, UInt_t t0);
91 Bool_t SetStringFmt(const char *time, const char *fmt, const char *loc=0);
92 Bool_t UpdMagicTime(Byte_t h, Byte_t m, Byte_t s, UInt_t ns);
93 Bool_t SetMjd(UInt_t mjd, ULong_t ms, UInt_t ns=0);
94 void SetMjd(Double_t m);
95 void SetAxisTime(Double_t time);
96 void SetEaster(Short_t year=0);
97
98 // Getter functions
99 Double_t GetJD() const { return GetMjd()+2400000.5; }
100 Double_t GetMjd() const;
101 Double_t GetGmst() const;
102 TString GetString() const;
103 TString GetStringFmt(const char *fmt=0, const char *loc=0) const;
104 TString GetSqlDateTime() const;
105 TString GetSqlTimeStamp() const;
106 TString GetFileName() const;
107 void GetDate(UShort_t &y, Byte_t &m, Byte_t &d) const;
108 void GetDateOfSunrise(UShort_t &y, Byte_t &m, Byte_t &d) const;
109 TTime GetRootTime() const;
110 Double_t GetAxisTime() const;
111 Double_t GetMoonPhase() const;
112 Double_t GetMoonPeriod() const;
113 Int_t GetMagicPeriod() const;
114 Long_t GetTime() const { return (Long_t)fTime; } // [ms] Time of Day returned in the range [-11h, 13h)
115 void GetTime(Byte_t &h, Byte_t &m, Byte_t &s, UShort_t &ms) const;
116 void GetTime(Byte_t &h, Byte_t &m, Byte_t &s) const
117 {
118 UShort_t ms;
119 GetTime(h, m, s, ms);
120 }
121
122 UInt_t Year() const { UShort_t y; Byte_t m, d; GetDate(y,m,d); return y; }
123 UInt_t Month() const { UShort_t y; Byte_t m, d; GetDate(y,m,d); return m; }
124 UInt_t Day() const { UShort_t y; Byte_t m, d; GetDate(y,m,d); return d; }
125 Byte_t WeekDay() const { return TMath::Nint(TMath::Floor(GetMjd()+3))%7; } // Return Day of the week: Sun=0, Mon=1, ..., Sat=6
126 UInt_t Hour() const { Byte_t h, m, s; GetTime(h,m,s); return h; }
127 UInt_t Min() const { Byte_t h, m, s; GetTime(h,m,s); return m; }
128 UInt_t Sec() const { Byte_t h, m, s; GetTime(h,m,s); return s; }
129 UInt_t DayOfYear() const;
130 Int_t Week() const { Short_t y; return Week(y); }
131 Int_t Week(Short_t &year) const;
132
133 Bool_t IsMidnight() const { return (Long_t)fTime==0 && fNanoSec==0; }
134 Bool_t IsLeapYear() const;
135
136 // I/O functions
137 istream &ReadBinary(istream &fin);
138
139 void AsciiRead(istream &fin);
140 Bool_t AsciiWrite(ostream &out) const;
141
142 // Conversion functions
143 operator double() const; //[s]
144 double operator()() const; //[s]
145
146 // Calculation functions
147 void AddMilliSeconds(UInt_t ms);
148 void Minus1ns();
149 void Plus1ns();
150 void SetMean(const MTime &t0, const MTime &t1);
151 void SetMean(Double_t t0, Double_t t1);
152 /*
153 MTime operator-(const MTime &tm1);
154 void operator-=(const MTime &t);
155 MTime operator+(const MTime &t1);
156 void operator+=(const MTime &t);
157 */
158
159 // Base comparison operators
160 bool operator<(const MTime &t) const;
161 bool operator>(const MTime &t) const;
162
163 // Derived comparison operators
164 bool operator<=(const MTime &t) const;
165 bool operator>=(const MTime &t) const;
166 bool operator==(const MTime &t) const;
167 bool operator!=(const MTime &t) const;
168 bool operator!() const;
169
170 static MTime GetEaster(Short_t year=-1);
171 static Int_t GetMjdWeek1(Short_t year);
172
173 ClassDef(MTime, 3) //A generalized MARS time stamp
174};
175
176inline ostream &operator<<(ostream &out, const MTime &t)
177{
178 out << t.GetString();
179 return out;
180}
181
182inline istream &operator>>(istream &in, MTime &t)
183{
184 TString date, time;
185 date.ReadToDelim(in, ' ');
186 time.ReadToDelim(in, ' ');
187 t.SetString(Form("%s %s", date.Data(), time.Data()));
188 return in;
189}
190
191inline MTime::operator double() const //[s]
192{
193 return ((Double_t)fMjd*kDay+(Long_t)fTime+fNanoSec/1e6)/1000;
194}
195
196inline double MTime::operator()() const //[s]
197{
198 return operator double();
199}
200
201inline bool MTime::operator<(const MTime &t) const
202{
203 if (fMjd<t.fMjd)
204 return true;
205 if (fMjd==t.fMjd && fTime<t.fTime)
206 return true;
207 if (fMjd==t.fMjd && fTime==t.fTime && fNanoSec<t.fNanoSec)
208 return true;
209 return false;
210}
211
212inline bool MTime::operator>(const MTime &t) const
213{
214 if (fMjd>t.fMjd)
215 return true;
216 if (fMjd==t.fMjd && fTime>t.fTime)
217 return true;
218 if (fMjd==t.fMjd && fTime==t.fTime && fNanoSec>t.fNanoSec)
219 return true;
220 return false;
221}
222
223inline bool MTime::operator<=(const MTime &t) const
224{
225 return !operator>(t);
226}
227
228inline bool MTime::operator>=(const MTime &t) const
229{
230 return !operator<(t);
231}
232
233inline bool MTime::operator==(const MTime &t) const
234{
235 return fNanoSec==t.fNanoSec && fTime==t.fTime && fMjd==t.fMjd;
236}
237
238inline bool MTime::operator!=(const MTime &t) const
239{
240 return fNanoSec!=t.fNanoSec || fTime!=t.fTime || fMjd!=t.fMjd;
241}
242
243inline bool MTime::operator!() const
244{
245 return fNanoSec==0 && (ULong_t)fTime==0 && fMjd==0;
246}
247
248#endif
Note: See TracBrowser for help on using the repository browser.