source: trunk/Mars/mbase/MTime.h@ 14888

Last change on this file since 14888 was 14446, checked in by tbretz, 12 years ago
Added some missing std::
File size: 8.3 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
24#include <TMath.h>
25
26struct timeval;
27
28class MTime : public MParContainer
29{
30public:
31 static const UInt_t kHour; // [ms] one hour
32 static const UInt_t kDay; // [ms] one day
33 static const UInt_t kDaySec; // [s] one day
34
35 enum {
36 kSunday, kMonday, kTuesday, kWednesday, kThursday, kFriday, kSaturday
37 };
38
39private:
40 UInt_t fMjd; // [d] Day in the century (Day of sun rise)
41 TTime fTime; // [ms] Time of Day (-11h<=x<13h)
42 UInt_t fNanoSec; // [ns] NanoSec part of TimeOfDay (<1000000)
43
44
45 ULong_t GetTime24() const
46 {
47 return (Long_t)fTime<0 ? (Long_t)fTime+kDay : (ULong_t)fTime;
48 }
49 void Init(const char *name, const char *title)
50 {
51 fName = name ? name : "MTime";
52 fTitle = title ? title : "Generalized time stamp";
53 }
54
55public:
56 MTime(const char *name=NULL, const char *title=NULL)
57 {
58 Init(name, title);
59 Clear();
60
61 SetSqlDateTime(name);
62 }
63 MTime(const struct timeval &tm)
64 {
65 Init(NULL, NULL);
66 Set(tm);
67 }
68 MTime(Double_t mjd);
69 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);
70 MTime(const MTime& t) : MParContainer(), fMjd(t.fMjd), fTime(t.fTime), fNanoSec(t.fNanoSec)
71 {
72 Init(NULL, NULL);
73 }
74
75 //static Int_t Hour() { return 3600; }
76 //static Int_t Day() { return 3600;*24 }
77
78 void operator=(const MTime &t)
79 {
80 fMjd = t.fMjd;
81 fTime = t.fTime;
82 fNanoSec = t.fNanoSec;
83 }
84
85 void Clear(const Option_t *) { Clear(); }
86 void Clear() { fMjd=0; fTime=0; fNanoSec=0; }
87
88 void Print(Option_t *t=NULL) const;
89
90 void Now();
91
92 // Setter functions
93 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);
94 void Set(const struct timeval &tv);
95 Bool_t SetString(const char *str);
96 Bool_t SetSqlDateTime(TString str);
97 Bool_t SetSqlTimeStamp(const char *str);
98 void SetCT1Time(UInt_t mjd, UInt_t t1, UInt_t t0);
99 void SetCorsikaTime(Float_t time);
100 Bool_t SetStringFmt(const char *time, const char *fmt, const char *loc=0);
101 Bool_t SetBinary(const UInt_t t[6]);
102 Bool_t UpdMagicTime(Byte_t h, Byte_t m, Byte_t s, UInt_t ns);
103 Bool_t SetMjd(UInt_t mjd, ULong_t ms, UInt_t ns=0);
104 void SetMjd(Double_t m);
105 void SetAxisTime(Double_t time);
106 void SetMoonPeriod(Double_t p);
107 void SetMagicPeriodStart(Int_t p);
108 void SetUnixTime(Long64_t sec, ULong64_t usec=0);
109 void SetEaster(Short_t year=0);
110
111 // Getter functions
112 Double_t GetJD() const { return GetMjd()+2400000.5; }
113 Double_t GetMjd() const;
114 Double_t GetGmst() const;
115 TString GetString() const;
116 TString GetStringFmt(const char *fmt=0, const char *loc=0) const;
117 TString GetSqlDateTime() const;
118 TString GetSqlTimeStamp() const;
119 TString GetFileName() const;
120 void GetBinary(UInt_t t[6]) const;
121 void GetDate(UShort_t &y, Byte_t &m, Byte_t &d) const;
122 MTime GetDateOfSunrise() const;
123 void GetDateOfSunrise(UShort_t &y, Byte_t &m, Byte_t &d) const;
124 TTime GetRootTime() const;
125 TDatime GetRootDatime() const;
126 Double_t GetAxisTime() const;
127 Double_t GetMoonPhase() const;
128 Double_t GetMoonPeriod() const;
129 Int_t GetMagicPeriod() const;
130 UInt_t GetNightAsInt() const;
131 Long_t GetTime() const { return (Long_t)fTime; } // [ms] Time of Day returned in the range [-11h, 13h)
132 void GetTime(Byte_t &h, Byte_t &m, Byte_t &s, UShort_t &ms) const;
133 void GetTime(Byte_t &h, Byte_t &m, Byte_t &s) const
134 {
135 UShort_t ms;
136 GetTime(h, m, s, ms);
137 }
138
139 UInt_t Year() const { UShort_t y; Byte_t m, d; GetDate(y,m,d); return y; }
140 UInt_t Month() const { UShort_t y; Byte_t m, d; GetDate(y,m,d); return m; }
141 UInt_t Day() const { UShort_t y; Byte_t m, d; GetDate(y,m,d); return d; }
142 Byte_t WeekDay() const; // { return TMath::FloorNint(GetMjd()+3)%7; } // Return Day of the week: Sun=0, Mon=1, ..., Sat=6
143 UInt_t Hour() const { Byte_t h, m, s; GetTime(h,m,s); return h; }
144 UInt_t Min() const { Byte_t h, m, s; GetTime(h,m,s); return m; }
145 UInt_t Sec() const { Byte_t h, m, s; GetTime(h,m,s); return s; }
146 UInt_t MilliSec() const { return (GetTime()+kDay)%1000; }
147 UInt_t MicroSec() const { return fNanoSec/1000+MilliSec()*1000; }
148 UInt_t NanoSec() const { return fNanoSec+MilliSec()*1000000; }
149 Int_t Week() const { Short_t y; return Week(y); }
150 Int_t Week(Short_t &year) const;
151 UInt_t DayOfYear() const;
152
153 Bool_t IsMidnight() const { return (Long_t)fTime==0 && fNanoSec==0; }
154 Bool_t IsLeapYear() const;
155
156 // I/O functions
157 std::istream &ReadBinary(std::istream &fin);
158 std::ostream &WriteBinary(std::ostream &out) const;
159
160 void AsciiRead(std::istream &fin);
161 Bool_t AsciiWrite(std::ostream &out) const;
162
163 // Conversion functions
164 operator double() const; //[s]
165 double operator()() const; //[s]
166
167 Double_t AsDouble() const { return GetMjd(); }
168 //const char *AsString() const { return GetString(); }
169 //const char *AsSQLString() const { return GetSqldateTime(); }
170
171 // Calculation functions
172 void AddMilliSeconds(UInt_t ms);
173 void Minus1ns();
174 void Plus1ns();
175 void SetMean(const MTime &t0, const MTime &t1);
176 void SetMean(Double_t t0, Double_t t1);
177 /*
178 MTime operator-(const MTime &tm1);
179 void operator-=(const MTime &t);
180 MTime operator+(const MTime &t1);
181 void operator+=(const MTime &t);
182 */
183
184 // Base comparison operators
185 bool operator<(const MTime &t) const;
186 bool operator>(const MTime &t) const;
187
188 // Derived comparison operators
189 bool operator<=(const MTime &t) const;
190 bool operator>=(const MTime &t) const;
191 bool operator==(const MTime &t) const;
192 bool operator!=(const MTime &t) const;
193 bool operator!() const;
194
195 static MTime GetEaster(Short_t year=-1);
196 static Int_t GetMjdWeek1(Short_t year);
197
198 ClassDef(MTime, 3) //A generalized MARS time stamp
199};
200
201inline std::ostream &operator<<(std::ostream &out, const MTime &t)
202{
203 out << t.GetString();
204 return out;
205}
206
207inline std::istream &operator>>(std::istream &in, MTime &t)
208{
209 TString date, time;
210 date.ReadToDelim(in, ' ');
211 time.ReadToDelim(in, ' ');
212 t.SetString(Form("%s %s", date.Data(), time.Data()));
213 return in;
214}
215
216inline MTime::operator double() const //[s]
217{
218 return ((Double_t)fMjd*kDay+(Long_t)fTime+fNanoSec/1e6)/1000;
219}
220
221inline double MTime::operator()() const //[s]
222{
223 return operator double();
224}
225
226inline bool MTime::operator<(const MTime &t) const
227{
228 if (fMjd<t.fMjd)
229 return true;
230 if (fMjd==t.fMjd && fTime<t.fTime)
231 return true;
232 if (fMjd==t.fMjd && fTime==t.fTime && fNanoSec<t.fNanoSec)
233 return true;
234 return false;
235}
236
237inline bool MTime::operator>(const MTime &t) const
238{
239 if (fMjd>t.fMjd)
240 return true;
241 if (fMjd==t.fMjd && fTime>t.fTime)
242 return true;
243 if (fMjd==t.fMjd && fTime==t.fTime && fNanoSec>t.fNanoSec)
244 return true;
245 return false;
246}
247
248inline bool MTime::operator<=(const MTime &t) const
249{
250 return !operator>(t);
251}
252
253inline bool MTime::operator>=(const MTime &t) const
254{
255 return !operator<(t);
256}
257
258inline bool MTime::operator==(const MTime &t) const
259{
260 return fNanoSec==t.fNanoSec && fTime==t.fTime && fMjd==t.fMjd;
261}
262
263inline bool MTime::operator!=(const MTime &t) const
264{
265 return fNanoSec!=t.fNanoSec || fTime!=t.fTime || fMjd!=t.fMjd;
266}
267
268inline bool MTime::operator!() const
269{
270 return fNanoSec==0 && (ULong_t)fTime==0 && fMjd==0;
271}
272
273#endif
Note: See TracBrowser for help on using the repository browser.