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

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