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

Last change on this file since 8881 was 8765, 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 void SetCorsikaTime(Float_t time);
97 Bool_t SetStringFmt(const char *time, const char *fmt, const char *loc=0);
98 Bool_t UpdMagicTime(Byte_t h, Byte_t m, Byte_t s, UInt_t ns);
99 Bool_t SetMjd(UInt_t mjd, ULong_t ms, UInt_t ns=0);
100 void SetMjd(Double_t m);
101 void SetAxisTime(Double_t time);
102 void SetUnixTime(Long64_t sec, ULong64_t usec=0);
103 void SetEaster(Short_t year=0);
104
105 // Getter functions
106 Double_t GetJD() const { return GetMjd()+2400000.5; }
107 Double_t GetMjd() const;
108 Double_t GetGmst() const;
109 TString GetString() const;
110 TString GetStringFmt(const char *fmt=0, const char *loc=0) const;
111 TString GetSqlDateTime() const;
112 TString GetSqlTimeStamp() const;
113 TString GetFileName() const;
114 void GetDate(UShort_t &y, Byte_t &m, Byte_t &d) const;
115 void GetDateOfSunrise(UShort_t &y, Byte_t &m, Byte_t &d) const;
116 TTime GetRootTime() const;
117 TDatime GetRootDatime() const;
118 Double_t GetAxisTime() const;
119 Double_t GetMoonPhase() const;
120 Double_t GetMoonPeriod() const;
121 Int_t GetMagicPeriod() const;
122 Long_t GetTime() const { return (Long_t)fTime; } // [ms] Time of Day returned in the range [-11h, 13h)
123 void GetTime(Byte_t &h, Byte_t &m, Byte_t &s, UShort_t &ms) const;
124 void GetTime(Byte_t &h, Byte_t &m, Byte_t &s) const
125 {
126 UShort_t ms;
127 GetTime(h, m, s, ms);
128 }
129
130 UInt_t Year() const { UShort_t y; Byte_t m, d; GetDate(y,m,d); return y; }
131 UInt_t Month() const { UShort_t y; Byte_t m, d; GetDate(y,m,d); return m; }
132 UInt_t Day() const { UShort_t y; Byte_t m, d; GetDate(y,m,d); return d; }
133 Byte_t WeekDay() const { return TMath::Nint(TMath::Floor(GetMjd()+3))%7; } // Return Day of the week: Sun=0, Mon=1, ..., Sat=6
134 UInt_t Hour() const { Byte_t h, m, s; GetTime(h,m,s); return h; }
135 UInt_t Min() const { Byte_t h, m, s; GetTime(h,m,s); return m; }
136 UInt_t Sec() const { Byte_t h, m, s; GetTime(h,m,s); return s; }
137 UInt_t MilliSec() const { return (GetTime()+kDay)%1000; }
138 UInt_t MicroSec() const { return fNanoSec/1000+MilliSec()*1000; }
139 UInt_t NanoSec() const { return fNanoSec+MilliSec()*1000000; }
140 Int_t Week() const { Short_t y; return Week(y); }
141 Int_t Week(Short_t &year) const;
142 UInt_t DayOfYear() const;
143
144 Bool_t IsMidnight() const { return (Long_t)fTime==0 && fNanoSec==0; }
145 Bool_t IsLeapYear() const;
146
147 // I/O functions
148 istream &ReadBinary(istream &fin);
149
150 void AsciiRead(istream &fin);
151 Bool_t AsciiWrite(ostream &out) const;
152
153 // Conversion functions
154 operator double() const; //[s]
155 double operator()() const; //[s]
156
157 Double_t AsDouble() const { return GetMjd(); }
158 //const char *AsString() const { return GetString(); }
159 //const char *AsSQLString() const { return GetSqldateTime(); }
160
161 // Calculation functions
162 void AddMilliSeconds(UInt_t ms);
163 void Minus1ns();
164 void Plus1ns();
165 void SetMean(const MTime &t0, const MTime &t1);
166 void SetMean(Double_t t0, Double_t t1);
167 /*
168 MTime operator-(const MTime &tm1);
169 void operator-=(const MTime &t);
170 MTime operator+(const MTime &t1);
171 void operator+=(const MTime &t);
172 */
173
174 // Base comparison operators
175 bool operator<(const MTime &t) const;
176 bool operator>(const MTime &t) const;
177
178 // Derived comparison operators
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 MTime &t) const;
183 bool operator!() const;
184
185 static MTime GetEaster(Short_t year=-1);
186 static Int_t GetMjdWeek1(Short_t year);
187
188 ClassDef(MTime, 3) //A generalized MARS time stamp
189};
190
191inline ostream &operator<<(ostream &out, const MTime &t)
192{
193 out << t.GetString();
194 return out;
195}
196
197inline istream &operator>>(istream &in, MTime &t)
198{
199 TString date, time;
200 date.ReadToDelim(in, ' ');
201 time.ReadToDelim(in, ' ');
202 t.SetString(Form("%s %s", date.Data(), time.Data()));
203 return in;
204}
205
206inline MTime::operator double() const //[s]
207{
208 return ((Double_t)fMjd*kDay+(Long_t)fTime+fNanoSec/1e6)/1000;
209}
210
211inline double MTime::operator()() const //[s]
212{
213 return operator double();
214}
215
216inline bool MTime::operator<(const MTime &t) const
217{
218 if (fMjd<t.fMjd)
219 return true;
220 if (fMjd==t.fMjd && fTime<t.fTime)
221 return true;
222 if (fMjd==t.fMjd && fTime==t.fTime && fNanoSec<t.fNanoSec)
223 return true;
224 return false;
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 return !operator>(t);
241}
242
243inline bool MTime::operator>=(const MTime &t) const
244{
245 return !operator<(t);
246}
247
248inline bool MTime::operator==(const MTime &t) const
249{
250 return fNanoSec==t.fNanoSec && fTime==t.fTime && fMjd==t.fMjd;
251}
252
253inline bool MTime::operator!=(const MTime &t) const
254{
255 return fNanoSec!=t.fNanoSec || fTime!=t.fTime || fMjd!=t.fMjd;
256}
257
258inline bool MTime::operator!() const
259{
260 return fNanoSec==0 && (ULong_t)fTime==0 && fMjd==0;
261}
262
263#endif
Note: See TracBrowser for help on using the repository browser.