Ignore:
Timestamp:
12/05/03 16:06:34 (21 years ago)
Author:
tbretz
Message:
*** empty log message ***
Location:
trunk/MagicSoft/Mars/mbase
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/MagicSoft/Mars/mbase/MTime.cc

    r2580 r2607  
    3838//  - removed fTimeStamp[2]
    3939//
     40// Version 3:
     41// ----------
     42//  - removed fDurtaion - we may put it back when it is needed
     43//  - complete rewrite of the data members (old ones completely replaced)
     44//
    4045/////////////////////////////////////////////////////////////////////////////
    4146#include "MTime.h"
    4247
    4348#include <iomanip>
     49#include <sys/time.h> // struct timeval
    4450
    4551#include <TTime.h>
    4652
    4753#include "MLog.h"
     54#include "MAstro.h"
    4855
    4956ClassImp(MTime);
     
    5158using namespace std;
    5259
    53 void MTime::SetTime(const TTime &t)
     60void MTime::GetDate(UShort_t &y, Byte_t &m, Byte_t &d) const
    5461{
    55     SetTime((ULong_t)t);
     62    MAstro::Mjd2Ymd((Long_t)fTime<0?fMjd-1:fMjd, y, m, d);
     63}
     64
     65Bool_t MTime::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)
     66{
     67    if (h>23 || min>59 || s>59 || ms>999 || ns>999999)
     68        return kFALSE;
     69
     70    const Int_t mjd = MAstro::Ymd2Mjd(y, m, d);
     71    if (mjd<0)
     72        return kFALSE;
     73
     74    const ULong_t tm = ((((h*60+min)*60)+s)*1000)+ms;
     75
     76    return Set(mjd, tm, ns);
     77}
     78
     79void MTime::SetSystemTimer(const struct timeval &tv)
     80{
     81    const ULong_t hor = 3600000; // One Hour in milliseconds
     82
     83    const UInt_t mjd = tv.tv_sec/(60*60*24) + 40587;
     84    const Long_t tm  = (tv.tv_usec/1000)%(24*hor);
     85
     86    Set(mjd, tm, tv.tv_usec*1000);
     87}
     88
     89void MTime::Now()
     90{
     91#ifdef __LINUX__
     92    struct timeval tv;
     93    gettimeofday(&tv, NULL);
     94    SetSystemTimer(tv);
     95#else
     96    Reset();
     97#endif
    5698}
    5799
    58100void MTime::Print(Option_t *) const
    59101{
    60     *fLog << GetDescriptor() << ": " << dec << setfill('0');
    61     *fLog << setw(2) << (int)fHour << ":";
    62     *fLog << setw(2) << (int)fMin  << ":";
    63     *fLog << setw(2) << (int)fSec  << ".";
    64     *fLog << setw(9) << fNanoSec << endl;
     102    UShort_t yea, ms;
     103    Byte_t mon, day, h, m, s;
     104
     105    GetDate(yea, mon, day);
     106    GetTime(h, m, s, ms);
     107
     108    *fLog << GetDescriptor() << ": ";
     109    *fLog << Form("%4d/%02d/%02d %02d:%02d:%02d.%03d (+%dns)",
     110                  yea, mon, day, h, m, s, ms, fNanoSec) << endl;
    65111}
  • trunk/MagicSoft/Mars/mbase/MTime.h

    r2580 r2607  
    1414#endif
    1515
    16 class TTime;
     16#ifndef ROOT_TTime
     17#include <TTime.h>
     18#endif
     19
     20struct timeval;
    1721
    1822class MTime : public MParContainer
    1923{
     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
    2031private:
    21     //UInt_t   fTimeStamp[2]; // type of raw event which should be processed by this task
    22     UInt_t   fDuration;     // time of validity
     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)
    2335
    24     Byte_t   fHour;
    25     Byte_t   fMin;
    26     Byte_t   fSec;
    27     UInt_t   fNanoSec;
    28 
    29     /*
    30      UInt_t   fMjd;     // Day in the century        (Day of sun rise)
    31      TTime    fTime;    // Time of Day               (43,200,000<x<43,200,000)
    32      UShort_t fNanoSec; // NanoSec part of TimeOfDay (<1000)
    33      */
     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     }
    3441
    3542public:
     
    3946        fTitle = title;
    4047
    41         SetTime((ULong_t)0);
     48        Reset();
    4249    }
    4350
    4451    MTime(MTime& t) { *this = t; }
    4552
     53    void Reset() { fMjd=0; fTime=0; fNanoSec=0; }
     54
    4655    void operator=(MTime &t)
    4756    {
    48         fDuration = t.fDuration;
    49         fHour     = t.fHour;
    50         fMin      = t.fMin;
    51         fSec      = t.fSec;
    52         fNanoSec  = t.fNanoSec;
     57        fMjd     = t.fMjd;
     58        fTime    = t.fTime;
     59        fNanoSec = t.fNanoSec;
    5360    }
    5461
    5562    void Print(Option_t *t=NULL) const;
    5663
    57     void SetCT1Time(UInt_t t1, UInt_t t0)
     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)
    5891    {
    5992        // int   isecs_since_midday; // seconds passed since midday before sunset (JD of run start)
    6093        // int   isecfrac_200ns;     // fractional part of isecs_since_midday
    6194        // fTime->SetTime(isecfrac_200ns, isecs_since_midday);
    62         fNanoSec  = 200*t1;
    63         fSec      = t0%60;
    64         t0 /= 60;
    65         fMin      = t0%60;
    66         t0 /= 60;
    67         fHour     = (t0+12)%24;
     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;
    68103    }
    69 
    70     void SetTime(ULong_t t)
     104    /*
     105    void SetSystemTime(const TTime &tm)
    71106    {
    72         // t [millisec]
    73         fNanoSec  = (t*1000000)%1000;
    74         t /= 1000;
    75         fSec      = t%60;
    76         t /= 60;
    77         fMin      = t%60;
    78         t /= 60;
    79         fHour     = t%24;
     107        fTime = (ULong_t)tm%(24*hor);
    80108    }
    81 
    82     void SetTime(const TTime &t);
    83 
    84     void SetTime(Double_t t)
    85     {
    86         // t [s]
    87         fNanoSec  = (UInt_t)(fmod(t, 1)*1e9);
    88         fSec      = (Byte_t)fmod(t, 60);
    89         t /= 60;
    90         fMin      = (Byte_t)fmod(t, 60);
    91         t /= 60;
    92         fHour     = (Byte_t)fmod(t, 24);
    93     }
    94 
    95     void SetTime(Byte_t h, Byte_t m, Byte_t s, UInt_t ns)
    96     {
    97         fHour    = h;
    98         fMin     = m;
    99         fSec     = s;
    100         fNanoSec = ns;
    101     }
    102 
    103     void SetDuration(UInt_t t)
    104     {
    105         fDuration = t;
    106     }
    107 
    108     UInt_t GetDuration()
    109     {
    110         return fDuration;
    111     }
    112 
     109     */
    113110    operator double() const //[s]
    114111    {
    115         return fNanoSec/1e9+(fHour*60*60+fMin*60+fSec);
     112        return fMjd*24*3600+((Long_t)fTime+fNanoSec*1e6)*1000;
    116113    }
    117114    double operator()() const //[s]
     
    120117    }
    121118
    122     Byte_t GetHour() const { return fHour; }
    123     Byte_t GetMin() const { return fMin; }
    124     Byte_t GetSec() const { return fSec; }
    125     UInt_t GetNanoSec() const { return fNanoSec; }
     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; }
    126133
    127     ClassDef(MTime, 2)  //A generalized MARS time stamp
     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
    128141};
    129142
    130 inline Double_t operator-(MTime &t1, MTime &t2)
     143inline bool operator<(MTime &t1, MTime &t2)
    131144{
    132     return t1()-t2();
     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;
    133152}
    134 
    135 inline Bool_t operator<(MTime &t1, MTime &t2)
     153inline bool operator>(MTime &t1, MTime &t2)
    136154{
    137     return t1()<t2();
     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;
    138162}
    139 
    140 inline Bool_t operator>(MTime &t1, MTime &t2)
     163inline bool operator<=(MTime &t1, MTime &t2)
    141164{
    142     return t1()>t2();
     165    return !(t1>t2);
    143166}
    144 
    145 inline Bool_t operator<=(MTime &t1, MTime &t2)
     167inline bool operator>=(MTime &t1, MTime &t2)
    146168{
    147     return t1<=t2();
     169    return !(t1<t2);
    148170}
    149 
    150 inline Bool_t operator>=(MTime &t1, MTime &t2)
     171inline bool operator==(MTime &t1, MTime &t2)
    151172{
    152     return t1()>=t2();
     173    return t1.fNanoSec==t2.fNanoSec && t1.fTime==t2.fTime && t1.fMjd==t2.fMjd;
    153174}
    154 
    155 inline Bool_t operator==(MTime &t1, MTime &t2)
     175inline bool operator!=(MTime &t1, MTime &t2)
    156176{
    157     return t1()==t2();
     177    return t1.fNanoSec!=t2.fNanoSec || t1.fTime!=t2.fTime || t1.fMjd!=t2.fMjd;
    158178}
    159 
    160179#endif
Note: See TracChangeset for help on using the changeset viewer.