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

Last change on this file since 3361 was 3148, checked in by tbretz, 21 years ago
*** empty log message ***
File size: 5.0 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 d);
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(UInt_t mjd, ULong_t ms, UInt_t ns=0);
78 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);
79 void Set(const struct timeval &tv);
80 Bool_t SetString(const char *str);
81 Bool_t SetSqlDateTime(const char *str);
82 Bool_t SetSqlTimeStamp(const char *str);
83 void SetCT1Time(UInt_t mjd, UInt_t t1, UInt_t t0);
84 Bool_t UpdMagicTime(Byte_t h, Byte_t m, Byte_t s, UInt_t ns);
85 void SetMjd(Double_t m);
86 Double_t GetMjd() const;
87 TString GetString() const;
88 TString GetStringFmt(const char *fmt=0) const;
89 TString GetSqlDateTime() const;
90 TString GetSqlTimeStamp() const;
91 TString GetFileName() const;
92 void GetDate(UShort_t &y, Byte_t &m, Byte_t &d) const;
93 TTime GetRootTime() const;
94 Double_t GetAxisTime() const;
95 Long_t GetTime() const { return (Long_t)fTime; } // [ms] Time of Day returned in the range [-11h, 13h)
96 void GetTime(Byte_t &h, Byte_t &m, Byte_t &s, UShort_t &ms) const;
97 void GetTime(Byte_t &h, Byte_t &m, Byte_t &s) const
98 {
99 UShort_t ms;
100 GetTime(h, m, s, ms);
101 }
102
103 UInt_t Year() const { UShort_t y; Byte_t m, d; GetDate(y,m,d); return y; }
104 UInt_t Month() const { UShort_t y; Byte_t m, d; GetDate(y,m,d); return m; }
105 UInt_t Day() const { UShort_t y; Byte_t m, d; GetDate(y,m,d); return d; }
106 UInt_t Hour() const { Byte_t h, m, s; GetTime(h,m,s); return h; }
107 UInt_t Min() const { Byte_t h, m, s; GetTime(h,m,s); return m; }
108 UInt_t Sec() const { Byte_t h, m, s; GetTime(h,m,s); return s; }
109
110 istream &ReadBinary(istream &fin);
111
112 operator double() const //[s]
113 {
114 return ((Double_t)fMjd*kDay+(Long_t)fTime+fNanoSec/1e6)/1000;
115 }
116 double operator()() const //[s]
117 {
118 return operator double();
119 }
120
121 bool operator<(const MTime &t) const
122 {
123 if (fMjd<t.fMjd)
124 return true;
125 if (fMjd==t.fMjd && fTime<t.fTime)
126 return true;
127 if (fMjd==t.fMjd && fTime==t.fTime && fNanoSec<t.fNanoSec)
128 return true;
129 return false;
130 }
131 bool operator>(const MTime &t) const
132 {
133 if (fMjd>t.fMjd)
134 return true;
135 if (fMjd==t.fMjd && fTime>t.fTime)
136 return true;
137 if (fMjd==t.fMjd && fTime==t.fTime && fNanoSec>t.fNanoSec)
138 return true;
139 return false;
140 }
141
142 bool operator<=(const MTime &t) const
143 {
144 return !operator>(t);
145 }
146
147 bool operator>=(const MTime &t) const
148 {
149 return !operator<(t);
150 }
151
152 bool operator==(const MTime &t) const
153 {
154 return fNanoSec==t.fNanoSec && fTime==t.fTime && fMjd==t.fMjd;
155 }
156
157 bool operator!=(const MTime &t) const
158 {
159 return fNanoSec!=t.fNanoSec || fTime!=t.fTime || fMjd!=t.fMjd;
160 }
161
162 bool operator!() const
163 {
164 return fNanoSec==0 && (ULong_t)fTime==0 && fMjd==0;
165 }
166
167 ClassDef(MTime, 3) //A generalized MARS time stamp
168};
169
170inline ostream &operator<<(ostream &out, const MTime &t)
171{
172 out << t.GetString();
173 return out;
174}
175
176#endif
Note: See TracBrowser for help on using the repository browser.