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

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