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

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