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

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