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

Last change on this file since 7450 was 7450, checked in by tbretz, 19 years ago
*** empty log message ***
File size: 6.9 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 MTime(const struct timeval &tm)
59 {
60 Init(NULL, NULL);
61 Set(tm);
62 }
63 MTime(Double_t mjd);
64 MTime(const MTime& t) : fMjd(t.fMjd), fTime(t.fTime), fNanoSec(t.fNanoSec)
65 {
66 Init(NULL, NULL);
67 }
68
69 void operator=(const MTime &t)
70 {
71 fMjd = t.fMjd;
72 fTime = t.fTime;
73 fNanoSec = t.fNanoSec;
74 }
75
76 void Clear(const Option_t *o="") { fMjd=0; fTime=0; fNanoSec=0; }
77
78 void Print(Option_t *t=NULL) const;
79
80 void Now();
81
82 // Setter functions
83 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);
84 void Set(const struct timeval &tv);
85 Bool_t SetString(const char *str);
86 Bool_t SetSqlDateTime(const char *str);
87 Bool_t SetSqlTimeStamp(const char *str);
88 void SetCT1Time(UInt_t mjd, UInt_t t1, UInt_t t0);
89 Bool_t SetStringFmt(const char *time, const char *fmt, const char *loc=0);
90 Bool_t UpdMagicTime(Byte_t h, Byte_t m, Byte_t s, UInt_t ns);
91 Bool_t SetMjd(UInt_t mjd, ULong_t ms, UInt_t ns=0);
92 void SetMjd(Double_t m);
93 void SetAxisTime(Double_t time);
94 void SetEaster(Short_t year=0);
95
96 // Getter functions
97 Double_t GetMjd() const;
98 Double_t GetGmst() const;
99 TString GetString() const;
100 TString GetStringFmt(const char *fmt=0, const char *loc=0) const;
101 TString GetSqlDateTime() const;
102 TString GetSqlTimeStamp() const;
103 TString GetFileName() const;
104 void GetDate(UShort_t &y, Byte_t &m, Byte_t &d) const;
105 void GetDateOfSunrise(UShort_t &y, Byte_t &m, Byte_t &d) const;
106 TTime GetRootTime() const;
107 Double_t GetAxisTime() const;
108 Double_t GetMoonPhase() const;
109 Double_t GetMoonPeriod() const;
110 Int_t GetMagicPeriod() const;
111 Long_t GetTime() const { return (Long_t)fTime; } // [ms] Time of Day returned in the range [-11h, 13h)
112 void GetTime(Byte_t &h, Byte_t &m, Byte_t &s, UShort_t &ms) const;
113 void GetTime(Byte_t &h, Byte_t &m, Byte_t &s) const
114 {
115 UShort_t ms;
116 GetTime(h, m, s, ms);
117 }
118
119 UInt_t Year() const { UShort_t y; Byte_t m, d; GetDate(y,m,d); return y; }
120 UInt_t Month() const { UShort_t y; Byte_t m, d; GetDate(y,m,d); return m; }
121 UInt_t Day() const { UShort_t y; Byte_t m, d; GetDate(y,m,d); return d; }
122 Byte_t WeekDay() const { return TMath::Nint(TMath::Floor(GetMjd()+3))%7; } // Return Day of the week: Sun=0, Mon=1, ..., Sat=6
123 UInt_t Hour() const { Byte_t h, m, s; GetTime(h,m,s); return h; }
124 UInt_t Min() const { Byte_t h, m, s; GetTime(h,m,s); return m; }
125 UInt_t Sec() const { Byte_t h, m, s; GetTime(h,m,s); return s; }
126 UInt_t DayOfYear() const;
127 Int_t Week() const;
128
129 Bool_t IsMidnight() const { return (Long_t)fTime==0 && fNanoSec==0; }
130 Bool_t IsLeapYear() const;
131
132 // I/O functions
133 istream &ReadBinary(istream &fin);
134
135 void AsciiRead(istream &fin);
136 Bool_t AsciiWrite(ostream &out) const;
137
138 // Conversion functions
139 operator double() const; //[s]
140 double operator()() const; //[s]
141
142 // Calculation functions
143 void AddMilliSeconds(UInt_t ms);
144 void Minus1ns();
145 void Plus1ns();
146 void SetMean(const MTime &t0, const MTime &t1);
147 void SetMean(Double_t t0, Double_t t1);
148 /*
149 MTime operator-(const MTime &tm1);
150 void operator-=(const MTime &t);
151 MTime operator+(const MTime &t1);
152 void operator+=(const MTime &t);
153 */
154
155 // Base comparison operators
156 bool operator<(const MTime &t) const;
157 bool operator>(const MTime &t) const;
158
159 // Derived comparison operators
160 bool operator<=(const MTime &t) const;
161 bool operator>=(const MTime &t) const;
162 bool operator==(const MTime &t) const;
163 bool operator!=(const MTime &t) const;
164 bool operator!() const;
165
166 static MTime GetEaster(Short_t year=-1);
167
168 ClassDef(MTime, 3) //A generalized MARS time stamp
169};
170
171inline ostream &operator<<(ostream &out, const MTime &t)
172{
173 out << t.GetString();
174 return out;
175}
176
177inline istream &operator>>(istream &in, MTime &t)
178{
179 TString date, time;
180 date.ReadToDelim(in, ' ');
181 time.ReadToDelim(in, ' ');
182 t.SetString(Form("%s %s", date.Data(), time.Data()));
183 return in;
184}
185
186inline MTime::operator double() const //[s]
187{
188 return ((Double_t)fMjd*kDay+(Long_t)fTime+fNanoSec/1e6)/1000;
189}
190
191inline double MTime::operator()() const //[s]
192{
193 return operator double();
194}
195
196inline bool MTime::operator<(const MTime &t) const
197{
198 if (fMjd<t.fMjd)
199 return true;
200 if (fMjd==t.fMjd && fTime<t.fTime)
201 return true;
202 if (fMjd==t.fMjd && fTime==t.fTime && fNanoSec<t.fNanoSec)
203 return true;
204 return false;
205}
206
207inline bool MTime::operator>(const MTime &t) const
208{
209 if (fMjd>t.fMjd)
210 return true;
211 if (fMjd==t.fMjd && fTime>t.fTime)
212 return true;
213 if (fMjd==t.fMjd && fTime==t.fTime && fNanoSec>t.fNanoSec)
214 return true;
215 return false;
216}
217
218inline bool MTime::operator<=(const MTime &t) const
219{
220 return !operator>(t);
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 fNanoSec==t.fNanoSec && fTime==t.fTime && fMjd==t.fMjd;
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
239{
240 return fNanoSec==0 && (ULong_t)fTime==0 && fMjd==0;
241}
242
243#endif
Note: See TracBrowser for help on using the repository browser.