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

Last change on this file since 2607 was 2607, checked in by tbretz, 21 years ago
*** empty log message ***
File size: 4.9 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 ROOT_TTime
17#include <TTime.h>
18#endif
19
20struct timeval;
21
22class MTime : public MParContainer
23{
24 friend bool operator==(MTime &t1, MTime &t2);
25 friend bool operator!=(MTime &t1, MTime &t2);
26 friend bool operator< (MTime &t1, MTime &t2);
27 friend bool operator> (MTime &t1, MTime &t2);
28 //friend bool operator<=(MTime &t1, MTime &t2);
29 //friend bool operator>=(MTime &t1, MTime &t2);
30
31private:
32 UInt_t fMjd; // [d] Day in the century (Day of sun rise)
33 TTime fTime; // [ms] Time of Day (-11h<=x<13h)
34 UInt_t fNanoSec; // [ns] NanoSec part of TimeOfDay (<1000000)
35
36 ULong_t GetTime24() const
37 {
38 const ULong_t hor = 3600000; // One Hour in milliseconds
39 return (Long_t)fTime<0 ? (Long_t)fTime+24*hor : (ULong_t)fTime;
40 }
41
42public:
43 MTime(const char *name=NULL, const char *title=NULL)
44 {
45 fName = name ? name : ClassName();
46 fTitle = title;
47
48 Reset();
49 }
50
51 MTime(MTime& t) { *this = t; }
52
53 void Reset() { fMjd=0; fTime=0; fNanoSec=0; }
54
55 void operator=(MTime &t)
56 {
57 fMjd = t.fMjd;
58 fTime = t.fTime;
59 fNanoSec = t.fNanoSec;
60 }
61
62 void Print(Option_t *t=NULL) const;
63
64 void Now();
65
66 Double_t GetMjd() const
67 {
68 return fMjd+((Long_t)fTime+fNanoSec/1e6)/1000;
69 }
70
71 void SetSystemTimer(const struct timeval &tv);
72
73 Bool_t Set(UInt_t mjd, ULong_t ms, UInt_t ns=0)
74 {
75 const ULong_t hor = 3600000; // One Hour in milliseconds
76 if (ms>24*hor-1 || ns>999999)
77 return kFALSE;
78
79 const Bool_t am = ms < hor*13;
80
81 fMjd = am ? mjd : mjd + 1;
82 fTime = (Long_t)(am ? ms : ms - hor*24);
83 fNanoSec = ns%1000000;
84
85 return kTRUE;
86 }
87
88 Bool_t Set(UShort_t y, Byte_t m, Byte_t d, Byte_t h, Byte_t min, Byte_t s, UShort_t ms, UInt_t ns=0);
89
90 void SetCT1Time(UInt_t mjd, UInt_t t1, UInt_t t0)
91 {
92 // int isecs_since_midday; // seconds passed since midday before sunset (JD of run start)
93 // int isecfrac_200ns; // fractional part of isecs_since_midday
94 // fTime->SetTime(isecfrac_200ns, isecs_since_midday);
95 const ULong_t hor = 3600000; // One Hour in milliseconds
96
97 fNanoSec = (200*t1)%1000000;
98 const ULong_t ms = (200*t1)/1000000 + t0+12*hor;
99
100 fTime = (Long_t)(ms<13*hor ? ms : ms-24*hor);
101
102 fMjd = mjd+1;
103 }
104 /*
105 void SetSystemTime(const TTime &tm)
106 {
107 fTime = (ULong_t)tm%(24*hor);
108 }
109 */
110 operator double() const //[s]
111 {
112 return fMjd*24*3600+((Long_t)fTime+fNanoSec*1e6)*1000;
113 }
114 double operator()() const //[s]
115 {
116 return operator double();
117 }
118
119 void GetDate(UShort_t &y, Byte_t &m, Byte_t &d) const;
120 void GetTime(Byte_t &h, Byte_t &m, Byte_t &s, UShort_t &ms) const // Time of Day returned in the range [0h, 24h)
121 {
122 Long_t tm = GetTime24();
123 ms = tm%1000;
124 tm /= 1000;
125 s = tm%60;
126 tm /= 60;
127 m = tm%60;
128 tm /= 60;
129 h = tm;
130 }
131 Long_t GetTime() const // Time of Day returned in the range [-11h, 13h)
132 { return (Long_t)fTime; }
133
134 void GetTime(Byte_t &h, Byte_t &m, Byte_t &s) const
135 {
136 UShort_t ms;
137 GetTime(h, m, s, ms);
138 }
139
140 ClassDef(MTime, 3) //A generalized MARS time stamp
141};
142
143inline bool operator<(MTime &t1, MTime &t2)
144{
145 if (t1.fMjd<t2.fMjd)
146 return true;
147 if (t1.fMjd==t2.fMjd && t1.fTime<t2.fTime)
148 return true;
149 if (t1.fMjd==t2.fMjd && t1.fTime==t2.fTime && t1.fNanoSec<t2.fNanoSec)
150 return true;
151 return false;
152}
153inline bool operator>(MTime &t1, MTime &t2)
154{
155 if (t1.fMjd>t2.fMjd)
156 return true;
157 if (t1.fMjd==t2.fMjd && t1.fTime>t2.fTime)
158 return true;
159 if (t1.fMjd==t2.fMjd && t1.fTime==t2.fTime && t1.fNanoSec>t2.fNanoSec)
160 return true;
161 return false;
162}
163inline bool operator<=(MTime &t1, MTime &t2)
164{
165 return !(t1>t2);
166}
167inline bool operator>=(MTime &t1, MTime &t2)
168{
169 return !(t1<t2);
170}
171inline bool operator==(MTime &t1, MTime &t2)
172{
173 return t1.fNanoSec==t2.fNanoSec && t1.fTime==t2.fTime && t1.fMjd==t2.fMjd;
174}
175inline bool operator!=(MTime &t1, MTime &t2)
176{
177 return t1.fNanoSec!=t2.fNanoSec || t1.fTime!=t2.fTime || t1.fMjd!=t2.fMjd;
178}
179#endif
Note: See TracBrowser for help on using the repository browser.