source: branches/Mars_use_drstimefiles/mbase/MTime.h@ 18454

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