| 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 |
|
|---|
| 24 | struct timeval;
|
|---|
| 25 |
|
|---|
| 26 | class MTime : public MParContainer
|
|---|
| 27 | {
|
|---|
| 28 | private:
|
|---|
| 29 | static const UInt_t kHour; // [ms] one hour
|
|---|
| 30 | static const UInt_t kDay; // [ms] one day
|
|---|
| 31 |
|
|---|
| 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 | return (Long_t)fTime<0 ? (Long_t)fTime+kDay : (ULong_t)fTime;
|
|---|
| 39 | }
|
|---|
| 40 | void Init(const char *name, const char *title)
|
|---|
| 41 | {
|
|---|
| 42 | fName = name ? name : "MTime";
|
|---|
| 43 | fTitle = title ? title : "Generalized time stamp";
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 | public:
|
|---|
| 47 | MTime(const char *name=NULL, const char *title=NULL)
|
|---|
| 48 | {
|
|---|
| 49 | Init(name, title);
|
|---|
| 50 | Reset();
|
|---|
| 51 | }
|
|---|
| 52 | MTime(const struct timeval &tm)
|
|---|
| 53 | {
|
|---|
| 54 | Init(NULL, NULL);
|
|---|
| 55 | Set(tm);
|
|---|
| 56 | }
|
|---|
| 57 | MTime(const MTime& t) : fMjd(t.fMjd), fTime(t.fTime), fNanoSec(t.fNanoSec)
|
|---|
| 58 | {
|
|---|
| 59 | Init(NULL, NULL);
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | void operator=(const MTime &t)
|
|---|
| 63 | {
|
|---|
| 64 | fMjd = t.fMjd;
|
|---|
| 65 | fTime = t.fTime;
|
|---|
| 66 | fNanoSec = t.fNanoSec;
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | void Reset() { fMjd=0; fTime=0; fNanoSec=0; }
|
|---|
| 70 |
|
|---|
| 71 | void Print(Option_t *t=NULL) const;
|
|---|
| 72 |
|
|---|
| 73 | void Now();
|
|---|
| 74 |
|
|---|
| 75 | Bool_t Set(UInt_t mjd, ULong_t ms, UInt_t ns=0);
|
|---|
| 76 | 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);
|
|---|
| 77 | void Set(const struct timeval &tv);
|
|---|
| 78 | void SetCT1Time(UInt_t mjd, UInt_t t1, UInt_t t0);
|
|---|
| 79 | void SetMjd(Double_t m);
|
|---|
| 80 | Double_t GetMjd() const;
|
|---|
| 81 | TString GetString() const;
|
|---|
| 82 | TString GetFileName() const;
|
|---|
| 83 | void GetDate(UShort_t &y, Byte_t &m, Byte_t &d) const;
|
|---|
| 84 | TTime GetRootTime() const;
|
|---|
| 85 | Double_t GetAxisTime() const;
|
|---|
| 86 | Long_t GetTime() const { return (Long_t)fTime; } // [ms] Time of Day returned in the range [-11h, 13h)
|
|---|
| 87 | void GetTime(Byte_t &h, Byte_t &m, Byte_t &s, UShort_t &ms) const;
|
|---|
| 88 | void GetTime(Byte_t &h, Byte_t &m, Byte_t &s) const
|
|---|
| 89 | {
|
|---|
| 90 | UShort_t ms;
|
|---|
| 91 | GetTime(h, m, s, ms);
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | UInt_t Year() const { UShort_t y; Byte_t m, d; GetDate(y,m,d); return y; }
|
|---|
| 95 | UInt_t Month() const { UShort_t y; Byte_t m, d; GetDate(y,m,d); return m; }
|
|---|
| 96 | UInt_t Day() const { UShort_t y; Byte_t m, d; GetDate(y,m,d); return d; }
|
|---|
| 97 | UInt_t Hour() const { Byte_t h, m, s; GetTime(h,m,s); return h; }
|
|---|
| 98 | UInt_t Min() const { Byte_t h, m, s; GetTime(h,m,s); return m; }
|
|---|
| 99 | UInt_t Sec() const { Byte_t h, m, s; GetTime(h,m,s); return s; }
|
|---|
| 100 |
|
|---|
| 101 | istream &ReadBinary(istream &fin);
|
|---|
| 102 |
|
|---|
| 103 | operator double() const //[s]
|
|---|
| 104 | {
|
|---|
| 105 | return ((Double_t)fMjd*kDay+(Long_t)fTime+fNanoSec/1e6)/1000;
|
|---|
| 106 | }
|
|---|
| 107 | double operator()() const //[s]
|
|---|
| 108 | {
|
|---|
| 109 | return operator double();
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | bool operator<(const MTime &t) const
|
|---|
| 113 | {
|
|---|
| 114 | if (fMjd<t.fMjd)
|
|---|
| 115 | return true;
|
|---|
| 116 | if (fMjd==t.fMjd && fTime<t.fTime)
|
|---|
| 117 | return true;
|
|---|
| 118 | if (fMjd==t.fMjd && fTime==t.fTime && fNanoSec<t.fNanoSec)
|
|---|
| 119 | return true;
|
|---|
| 120 | return false;
|
|---|
| 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 |
|
|---|
| 133 | bool operator<=(const MTime &t) const
|
|---|
| 134 | {
|
|---|
| 135 | return !operator>(t);
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | bool operator>=(const MTime &t) const
|
|---|
| 139 | {
|
|---|
| 140 | return !operator<(t);
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | bool operator==(const MTime &t) const
|
|---|
| 144 | {
|
|---|
| 145 | return fNanoSec==t.fNanoSec && fTime==t.fTime && fMjd==t.fMjd;
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 | bool operator!=(const MTime &t) const
|
|---|
| 149 | {
|
|---|
| 150 | return fNanoSec!=t.fNanoSec || fTime!=t.fTime || fMjd!=t.fMjd;
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 | bool operator!() const
|
|---|
| 154 | {
|
|---|
| 155 | return fNanoSec==0 && (ULong_t)fTime==0 && fMjd==0;
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | ClassDef(MTime, 3) //A generalized MARS time stamp
|
|---|
| 159 | };
|
|---|
| 160 |
|
|---|
| 161 | inline ostream &operator<<(ostream &out, const MTime &t)
|
|---|
| 162 | {
|
|---|
| 163 | out << t.GetString();
|
|---|
| 164 | return out;
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 167 | #endif
|
|---|