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

Last change on this file since 4450 was 3967, checked in by tbretz, 21 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 TTime GetRootTime() const;
95 Double_t GetAxisTime() const;
96 Long_t GetTime() const { return (Long_t)fTime; } // [ms] Time of Day returned in the range [-11h, 13h)
97 void GetTime(Byte_t &h, Byte_t &m, Byte_t &s, UShort_t &ms) const;
98 void GetTime(Byte_t &h, Byte_t &m, Byte_t &s) const
99 {
100 UShort_t ms;
101 GetTime(h, m, s, ms);
102 }
103
104 UInt_t Year() const { UShort_t y; Byte_t m, d; GetDate(y,m,d); return y; }
105 UInt_t Month() const { UShort_t y; Byte_t m, d; GetDate(y,m,d); return m; }
106 UInt_t Day() const { UShort_t y; Byte_t m, d; GetDate(y,m,d); return d; }
107 UInt_t Hour() const { Byte_t h, m, s; GetTime(h,m,s); return h; }
108 UInt_t Min() const { Byte_t h, m, s; GetTime(h,m,s); return m; }
109 UInt_t Sec() const { Byte_t h, m, s; GetTime(h,m,s); return s; }
110
111 istream &ReadBinary(istream &fin);
112
113 operator double() const //[s]
114 {
115 return ((Double_t)fMjd*kDay+(Long_t)fTime+fNanoSec/1e6)/1000;
116 }
117 double operator()() const //[s]
118 {
119 return operator double();
120 }
121
122 bool operator<(const MTime &t) const
123 {
124 if (fMjd<t.fMjd)
125 return true;
126 if (fMjd==t.fMjd && fTime<t.fTime)
127 return true;
128 if (fMjd==t.fMjd && fTime==t.fTime && fNanoSec<t.fNanoSec)
129 return true;
130 return false;
131 }
132 bool operator>(const MTime &t) const
133 {
134 if (fMjd>t.fMjd)
135 return true;
136 if (fMjd==t.fMjd && fTime>t.fTime)
137 return true;
138 if (fMjd==t.fMjd && fTime==t.fTime && fNanoSec>t.fNanoSec)
139 return true;
140 return false;
141 }
142
143 bool operator<=(const MTime &t) const
144 {
145 return !operator>(t);
146 }
147
148 bool operator>=(const MTime &t) const
149 {
150 return !operator<(t);
151 }
152
153 bool operator==(const MTime &t) const
154 {
155 return fNanoSec==t.fNanoSec && fTime==t.fTime && fMjd==t.fMjd;
156 }
157
158 bool operator!=(const MTime &t) const
159 {
160 return fNanoSec!=t.fNanoSec || fTime!=t.fTime || fMjd!=t.fMjd;
161 }
162
163 bool operator!() const
164 {
165 return fNanoSec==0 && (ULong_t)fTime==0 && fMjd==0;
166 }
167
168 ClassDef(MTime, 3) //A generalized MARS time stamp
169};
170
171inline ostream &operator<<(ostream &out, const MTime &t)
172{
173 out << t.GetString();
174 return out;
175}
176
177inline istream &operator>>(istream &in, MTime &t)
178{
179 TString date, time;
180 date.ReadToDelim(in, ' ');
181 time.ReadToDelim(in, ' ');
182 t.SetString(Form("%s %s", date.Data(), time.Data()));
183 return in;
184}
185
186#endif
Note: See TracBrowser for help on using the repository browser.