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

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