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

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