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

Last change on this file since 7432 was 7432, checked in by tbretz, 19 years ago
*** empty log message ***
File size: 6.8 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
32private:
33 UInt_t fMjd; // [d] Day in the century (Day of sun rise)
34 TTime fTime; // [ms] Time of Day (-11h<=x<13h)
35 UInt_t fNanoSec; // [ns] NanoSec part of TimeOfDay (<1000000)
36
37
38 ULong_t GetTime24() const
39 {
40 return (Long_t)fTime<0 ? (Long_t)fTime+kDay : (ULong_t)fTime;
41 }
42 void Init(const char *name, const char *title)
43 {
44 fName = name ? name : "MTime";
45 fTitle = title ? title : "Generalized time stamp";
46 }
47
48public:
49 MTime(const char *name=NULL, const char *title=NULL)
50 {
51 Init(name, title);
52 Clear();
53 }
54 MTime(const struct timeval &tm)
55 {
56 Init(NULL, NULL);
57 Set(tm);
58 }
59 MTime(Double_t mjd);
60 MTime(const MTime& t) : fMjd(t.fMjd), fTime(t.fTime), fNanoSec(t.fNanoSec)
61 {
62 Init(NULL, NULL);
63 }
64
65 void operator=(const MTime &t)
66 {
67 fMjd = t.fMjd;
68 fTime = t.fTime;
69 fNanoSec = t.fNanoSec;
70 }
71
72 void Clear(const Option_t *o="") { fMjd=0; fTime=0; fNanoSec=0; }
73
74 void Print(Option_t *t=NULL) const;
75
76 void Now();
77
78 // Setter functions
79 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);
80 void Set(const struct timeval &tv);
81 Bool_t SetString(const char *str);
82 Bool_t SetSqlDateTime(const char *str);
83 Bool_t SetSqlTimeStamp(const char *str);
84 void SetCT1Time(UInt_t mjd, UInt_t t1, UInt_t t0);
85 Bool_t SetStringFmt(const char *time, const char *fmt, const char *loc=0);
86 Bool_t UpdMagicTime(Byte_t h, Byte_t m, Byte_t s, UInt_t ns);
87 Bool_t SetMjd(UInt_t mjd, ULong_t ms, UInt_t ns=0);
88 void SetMjd(Double_t m);
89 void SetAxisTime(Double_t time);
90 void SetEaster(Short_t year=0);
91
92 // Getter functions
93 Double_t GetMjd() const;
94 Double_t GetGmst() const;
95 TString GetString() const;
96 TString GetStringFmt(const char *fmt=0, const char *loc=0) const;
97 TString GetSqlDateTime() const;
98 TString GetSqlTimeStamp() const;
99 TString GetFileName() const;
100 void GetDate(UShort_t &y, Byte_t &m, Byte_t &d) const;
101 void GetDateOfSunrise(UShort_t &y, Byte_t &m, Byte_t &d) const;
102 TTime GetRootTime() const;
103 Double_t GetAxisTime() const;
104 Double_t GetMoonPhase() const;
105 Double_t GetMoonPeriod() const;
106 Int_t GetMagicPeriod() const;
107 Long_t GetTime() const { return (Long_t)fTime; } // [ms] Time of Day returned in the range [-11h, 13h)
108 void GetTime(Byte_t &h, Byte_t &m, Byte_t &s, UShort_t &ms) const;
109 void GetTime(Byte_t &h, Byte_t &m, Byte_t &s) const
110 {
111 UShort_t ms;
112 GetTime(h, m, s, ms);
113 }
114
115 UInt_t Year() const { UShort_t y; Byte_t m, d; GetDate(y,m,d); return y; }
116 UInt_t Month() const { UShort_t y; Byte_t m, d; GetDate(y,m,d); return m; }
117 UInt_t Day() const { UShort_t y; Byte_t m, d; GetDate(y,m,d); return d; }
118 Byte_t WeekDay() const { return TMath::Nint(TMath::Floor(GetMjd()+3))%7; } // Return Day of the week: Sun=0, Mon=1, ..., Sat=6
119 UInt_t Hour() const { Byte_t h, m, s; GetTime(h,m,s); return h; }
120 UInt_t Min() const { Byte_t h, m, s; GetTime(h,m,s); return m; }
121 UInt_t Sec() const { Byte_t h, m, s; GetTime(h,m,s); return s; }
122
123 Bool_t IsMidnight() const { return (Long_t)fTime==0 && fNanoSec==0; }
124
125 // I/O functions
126 istream &ReadBinary(istream &fin);
127
128 void AsciiRead(istream &fin);
129 Bool_t AsciiWrite(ostream &out) const;
130
131 // Conversion functions
132 operator double() const; //[s]
133 double operator()() const; //[s]
134
135 // Calculation functions
136 void AddMilliSeconds(UInt_t ms);
137 void Minus1ns();
138 void Plus1ns();
139 void SetMean(const MTime &t0, const MTime &t1);
140 void SetMean(Double_t t0, Double_t t1);
141 /*
142 MTime operator-(const MTime &tm1);
143 void operator-=(const MTime &t);
144 MTime operator+(const MTime &t1);
145 void operator+=(const MTime &t);
146 */
147
148 // Base comparison operators
149 bool operator<(const MTime &t) const;
150 bool operator>(const MTime &t) const;
151
152 // Derived comparison operators
153 bool operator<=(const MTime &t) const;
154 bool operator>=(const MTime &t) const;
155 bool operator==(const MTime &t) const;
156 bool operator!=(const MTime &t) const;
157 bool operator!() const;
158
159 static MTime GetEaster(Short_t year=-1);
160
161 ClassDef(MTime, 3) //A generalized MARS time stamp
162};
163
164inline ostream &operator<<(ostream &out, const MTime &t)
165{
166 out << t.GetString();
167 return out;
168}
169
170inline istream &operator>>(istream &in, MTime &t)
171{
172 TString date, time;
173 date.ReadToDelim(in, ' ');
174 time.ReadToDelim(in, ' ');
175 t.SetString(Form("%s %s", date.Data(), time.Data()));
176 return in;
177}
178
179inline MTime::operator double() const //[s]
180{
181 return ((Double_t)fMjd*kDay+(Long_t)fTime+fNanoSec/1e6)/1000;
182}
183
184inline double MTime::operator()() const //[s]
185{
186 return operator double();
187}
188
189inline bool MTime::operator<(const MTime &t) const
190{
191 if (fMjd<t.fMjd)
192 return true;
193 if (fMjd==t.fMjd && fTime<t.fTime)
194 return true;
195 if (fMjd==t.fMjd && fTime==t.fTime && fNanoSec<t.fNanoSec)
196 return true;
197 return false;
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 return !operator>(t);
214}
215
216inline bool MTime::operator>=(const MTime &t) const
217{
218 return !operator<(t);
219}
220
221inline bool MTime::operator==(const MTime &t) const
222{
223 return fNanoSec==t.fNanoSec && fTime==t.fTime && fMjd==t.fMjd;
224}
225
226inline bool MTime::operator!=(const MTime &t) const
227{
228 return fNanoSec!=t.fNanoSec || fTime!=t.fTime || fMjd!=t.fMjd;
229}
230
231inline bool MTime::operator!() const
232{
233 return fNanoSec==0 && (ULong_t)fTime==0 && fMjd==0;
234}
235
236#endif
Note: See TracBrowser for help on using the repository browser.