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

Last change on this file since 4563 was 4563, checked in by tbretz, 20 years ago
*** empty log message ***
File size: 5.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
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 ULong_t GetTime24() const
38 {
39 return (Long_t)fTime<0 ? (Long_t)fTime+kDay : (ULong_t)fTime;
40 }
41 void Init(const char *name, const char *title)
42 {
43 fName = name ? name : "MTime";
44 fTitle = title ? title : "Generalized time stamp";
45 }
46
47public:
48 MTime(const char *name=NULL, const char *title=NULL)
49 {
50 Init(name, title);
51 Clear();
52 }
53 MTime(const struct timeval &tm)
54 {
55 Init(NULL, NULL);
56 Set(tm);
57 }
58 MTime(Double_t mjd);
59 MTime(const MTime& t) : fMjd(t.fMjd), fTime(t.fTime), fNanoSec(t.fNanoSec)
60 {
61 Init(NULL, NULL);
62 }
63
64 void operator=(const MTime &t)
65 {
66 fMjd = t.fMjd;
67 fTime = t.fTime;
68 fNanoSec = t.fNanoSec;
69 }
70
71 void Clear(const Option_t *o="") { fMjd=0; fTime=0; fNanoSec=0; }
72
73 void Print(Option_t *t=NULL) const;
74
75 void Now();
76
77 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);
78 void Set(const struct timeval &tv);
79 Bool_t SetString(const char *str);
80 Bool_t SetSqlDateTime(const char *str);
81 Bool_t SetSqlTimeStamp(const char *str);
82 void SetCT1Time(UInt_t mjd, UInt_t t1, UInt_t t0);
83 Bool_t UpdMagicTime(Byte_t h, Byte_t m, Byte_t s, UInt_t ns);
84 Bool_t SetMjd(UInt_t mjd, ULong_t ms, UInt_t ns=0);
85 void SetMjd(Double_t m);
86 Double_t GetMjd() const;
87 Double_t GetGmst() const;
88 TString GetString() const;
89 TString GetStringFmt(const char *fmt=0) const;
90 TString GetSqlDateTime() const;
91 TString GetSqlTimeStamp() const;
92 TString GetFileName() const;
93 void GetDate(UShort_t &y, Byte_t &m, Byte_t &d) const;
94 void GetDateOfSunrise(UShort_t &y, Byte_t &m, Byte_t &d) const;
95 TTime GetRootTime() const;
96 Double_t GetAxisTime() const;
97 Long_t GetTime() const { return (Long_t)fTime; } // [ms] Time of Day returned in the range [-11h, 13h)
98 void GetTime(Byte_t &h, Byte_t &m, Byte_t &s, UShort_t &ms) const;
99 void GetTime(Byte_t &h, Byte_t &m, Byte_t &s) const
100 {
101 UShort_t ms;
102 GetTime(h, m, s, ms);
103 }
104
105 UInt_t Year() const { UShort_t y; Byte_t m, d; GetDate(y,m,d); return y; }
106 UInt_t Month() const { UShort_t y; Byte_t m, d; GetDate(y,m,d); return m; }
107 UInt_t Day() const { UShort_t y; Byte_t m, d; GetDate(y,m,d); return d; }
108 UInt_t Hour() const { Byte_t h, m, s; GetTime(h,m,s); return h; }
109 UInt_t Min() const { Byte_t h, m, s; GetTime(h,m,s); return m; }
110 UInt_t Sec() const { Byte_t h, m, s; GetTime(h,m,s); return s; }
111
112 istream &ReadBinary(istream &fin);
113
114 operator double() const //[s]
115 {
116 return ((Double_t)fMjd*kDay+(Long_t)fTime+fNanoSec/1e6)/1000;
117 }
118 double operator()() const //[s]
119 {
120 return operator double();
121 }
122
123 bool operator<(const MTime &t) const
124 {
125 if (fMjd<t.fMjd)
126 return true;
127 if (fMjd==t.fMjd && fTime<t.fTime)
128 return true;
129 if (fMjd==t.fMjd && fTime==t.fTime && fNanoSec<t.fNanoSec)
130 return true;
131 return false;
132 }
133 bool operator>(const MTime &t) const
134 {
135 if (fMjd>t.fMjd)
136 return true;
137 if (fMjd==t.fMjd && fTime>t.fTime)
138 return true;
139 if (fMjd==t.fMjd && fTime==t.fTime && fNanoSec>t.fNanoSec)
140 return true;
141 return false;
142 }
143
144 bool operator<=(const MTime &t) const
145 {
146 return !operator>(t);
147 }
148
149 bool operator>=(const MTime &t) const
150 {
151 return !operator<(t);
152 }
153
154 bool operator==(const MTime &t) const
155 {
156 return fNanoSec==t.fNanoSec && fTime==t.fTime && fMjd==t.fMjd;
157 }
158
159 bool operator!=(const MTime &t) const
160 {
161 return fNanoSec!=t.fNanoSec || fTime!=t.fTime || fMjd!=t.fMjd;
162 }
163
164 bool operator!() const
165 {
166 return fNanoSec==0 && (ULong_t)fTime==0 && fMjd==0;
167 }
168
169 ClassDef(MTime, 3) //A generalized MARS time stamp
170};
171
172inline ostream &operator<<(ostream &out, const MTime &t)
173{
174 out << t.GetString();
175 return out;
176}
177
178inline istream &operator>>(istream &in, MTime &t)
179{
180 TString date, time;
181 date.ReadToDelim(in, ' ');
182 time.ReadToDelim(in, ' ');
183 t.SetString(Form("%s %s", date.Data(), time.Data()));
184 return in;
185}
186
187#endif
Note: See TracBrowser for help on using the repository browser.