Ignore:
Timestamp:
12/07/03 12:52:29 (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

    r2607 r2612  
    2929// A generalized MARS time stamp
    3030//
     31// WARNING: Be carefull changing this class. It is also used in the
     32//          MAGIC drive software cosy!
    3133//
    3234// Version 1:
     
    5860using namespace std;
    5961
     62const UInt_t MTime::kHour = 3600000;         // [ms] one hour
     63const UInt_t MTime::kDay  = MTime::kHour*24; // [ms] one day
     64
     65// --------------------------------------------------------------------------
     66//
     67// Return date as year(y), month(m), day(d)
     68//
    6069void MTime::GetDate(UShort_t &y, Byte_t &m, Byte_t &d) const
    6170{
     
    6372}
    6473
     74// --------------------------------------------------------------------------
     75//
     76// Return the time in the range [0h, 24h) = [0h0m0.000s - 23h59m59.999s]
     77//
     78void MTime::GetTime(Byte_t &h, Byte_t &m, Byte_t &s, UShort_t &ms) const
     79{
     80    Long_t tm = GetTime24();
     81    ms  = tm%1000;            // [ms]
     82    tm /= 1000;               // [s]
     83    s   = tm%60;              // [s]
     84    tm /= 60;                 // [m]
     85    m   = tm%60;              // [m]
     86    tm /= 60;                 // [h]
     87    h   = tm;                 // [h]
     88}
     89
     90// --------------------------------------------------------------------------
     91//
     92//  Return time as MJD (=JD-24000000.5)
     93//
     94Double_t MTime::GetMjd() const
     95{
     96    return fMjd+(Double_t)(fNanoSec/1e6+(Long_t)fTime)/kDay;
     97}
     98
     99// --------------------------------------------------------------------------
     100//
     101// Return a time which is expressed in milliseconds since 01/01/1995 0:00h
     102// This is compatible with root's definition used in gSystem->Now()
     103// and TTime.
     104// Note, gSystem->Now() returns local time, such that it may differ
     105// from GetRootTime() (if you previously called MTime::Now())
     106//
     107TTime MTime::GetRootTime() const
     108{
     109    return (ULong_t)((GetMjd()-49718)*kDay);
     110}
     111
     112// --------------------------------------------------------------------------
     113//
     114// Return a time which is expressed in seconds since 01/01/1995 0:00h
     115// This is compatible with root's definition used in TAxis.
     116// Note, a TAxis always displayes (automatically) given times in
     117// local time (while here we return UTC) such, that you may encounter
     118// strange offsets. You can get rid of this by calling:
     119//    TAxis::SetTimeFormat("[your-format] %F1995-01-01 00:00:00");
     120//
     121Double_t MTime::GetAxisTime() const
     122{
     123    return (GetMjd()-49718)*kDay/1000;
     124}
     125
     126// --------------------------------------------------------------------------
     127//
     128// Set a time expressed in MJD, Time of Day (eg. 23:12.779h expressed
     129// in milliseconds) and a nanosecond part.
     130//
     131Bool_t MTime::Set(UInt_t mjd, ULong_t ms, UInt_t ns=0)
     132{
     133    // [d]  mjd  (eg. 52320)
     134    // [ms] time (eg. 17h expressed in ms)
     135    // [ns] time (ns part of time)
     136
     137    if (ms>kDay-1 || ns>999999)
     138        return kFALSE;
     139
     140    const Bool_t am = ms < kHour*13; // day of sunrise?
     141
     142    fMjd     = am ? mjd : mjd + 1;
     143    fTime    = (Long_t)(am ? ms  : ms-kDay);
     144    fNanoSec = ns;
     145
     146    return kTRUE;
     147}
     148
     149// --------------------------------------------------------------------------
     150//
     151// Set MTime to given MJD (eg. 52080.0915449892)
     152//
     153void MTime::SetMjd(Double_t m)
     154{
     155    const UInt_t   mjd  = (UInt_t)TMath::Floor(m);
     156    const Double_t frac = fmod(m, 1)*kDay; // [ms] Fraction of day
     157    const UInt_t   ns   = (UInt_t)fmod(frac*1e6, 1000000);
     158
     159    Set(mjd, (ULong_t)TMath::Floor(frac), ns);
     160}
     161
     162// --------------------------------------------------------------------------
     163//
     164// Set MTime to given time and date
     165//
    65166Bool_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)
    66167{
     
    77178}
    78179
    79 void 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 
     180// --------------------------------------------------------------------------
     181//
     182// Set MTime to time expressed in a 'struct timeval'
     183//
     184void MTime::Set(const struct timeval &tv)
     185{
     186    const UInt_t mjd = 1000*tv.tv_sec/kDay + 40587;
     187    const Long_t tm  = tv.tv_sec%(24*3600)*1000 + tv.tv_usec/1000;
     188    const UInt_t ms  = tv.tv_usec%1000;
     189
     190    Set(mjd, tm, ms*1000);
     191}
     192
     193// --------------------------------------------------------------------------
     194//
     195// Set MTime to time expressed as in CT1 PreProc files
     196//
     197void MTime::SetCT1Time(UInt_t mjd, UInt_t t1, UInt_t t0)
     198{
     199    // int   isecs_since_midday; // seconds passed since midday before sunset (JD of run start)
     200    // int   isecfrac_200ns;     // fractional part of isecs_since_midday
     201    // fTime->SetTime(isecfrac_200ns, isecs_since_midday);
     202    fNanoSec         = (200*t1)%1000000;
     203    const ULong_t ms = (200*t1)/1000000 + t0+12*kHour;
     204
     205    fTime = (Long_t)(ms<13*kHour ? ms : ms-kDay);
     206
     207    fMjd = mjd+1;
     208}
     209
     210// --------------------------------------------------------------------------
     211//
     212// Set the time to the current system time. The timezone is ignored.
     213// If everything is set correctly you'll get UTC.
     214//
    89215void MTime::Now()
    90216{
    91217#ifdef __LINUX__
    92218    struct timeval tv;
    93     gettimeofday(&tv, NULL);
    94     SetSystemTimer(tv);
     219    if (gettimeofday(&tv, NULL)<0)
     220        Reset();
     221    else
     222        Set(tv);
    95223#else
    96224    Reset();
     
    98226}
    99227
     228// --------------------------------------------------------------------------
     229//
     230// Return contents as a TString of the form:
     231//   "[yy]yy/mm/dd [h]h:mm:ss.fff"
     232//
     233TString MTime::GetString() const
     234{
     235    UShort_t y, ms;
     236    Byte_t mon, d, h, m, s;
     237
     238    GetDate(y, mon, d);
     239    GetTime(h, m, s, ms);
     240
     241    return TString(Form("%2d/%02d/%02d %d:%02d:%02d.%03d", y, mon, d, h, m, s, ms));
     242}
     243
     244// --------------------------------------------------------------------------
     245//
     246// Return contents as a TString of the form:
     247//   "yyyymmdd_hhmmss"
     248//
     249TString MTime::GetFileName() const
     250{
     251    UShort_t y;
     252    Byte_t mon, d, h, m, s;
     253
     254    GetDate(y, mon, d);
     255    GetTime(h, m, s);
     256
     257    return TString(Form("%04d%02d%02d_%02d%02d%02d", y, mon, d, h, m, s));
     258}
     259
     260// --------------------------------------------------------------------------
     261//
     262// Print MTime as string
     263//
    100264void MTime::Print(Option_t *) const
    101265{
     
    107271
    108272    *fLog << GetDescriptor() << ": ";
    109     *fLog << Form("%4d/%02d/%02d %02d:%02d:%02d.%03d (+%dns)",
    110                   yea, mon, day, h, m, s, ms, fNanoSec) << endl;
     273    *fLog << GetString() << Form(" (+%dns)", fNanoSec) << endl;
    111274}
  • trunk/MagicSoft/Mars/mbase/MTime.h

    r2607 r2612  
    3030
    3131private:
     32    static const UInt_t kHour; // [ms] one hour
     33    static const UInt_t kDay;  // [ms] one day
     34
    3235     UInt_t fMjd;     // [d]  Day in the century        (Day of sun rise)
    3336     TTime  fTime;    // [ms] Time of Day               (-11h<=x<13h)
     
    3639     ULong_t GetTime24() const
    3740     {
    38          const ULong_t hor = 3600000; // One Hour in milliseconds
    39          return (Long_t)fTime<0 ? (Long_t)fTime+24*hor : (ULong_t)fTime;
     41         return (Long_t)fTime<0 ? (Long_t)fTime+kDay : (ULong_t)fTime;
     42     }
     43     void Init(const char *name, const char *title)
     44     {
     45        fName  = name  ? name  : "MTime";
     46        fTitle = title ? title : "Generalized time stamp";
    4047     }
    4148
     
    4350    MTime(const char *name=NULL, const char *title=NULL)
    4451    {
    45         fName = name ? name : ClassName();
    46         fTitle = title;
    47 
     52        Init(name, title);
    4853        Reset();
    4954    }
     55    MTime(const struct timeval &tm)
     56    {
     57        Init(NULL, NULL);
     58        Set(tm);
     59    }
     60    MTime(const MTime& t) : fMjd(t.fMjd), fTime(t.fTime), fNanoSec(t.fNanoSec)
     61    {
     62        Init(NULL, NULL);
     63    }
    5064
    51     MTime(MTime& t) { *this = t; }
    52 
    53     void Reset() { fMjd=0; fTime=0; fNanoSec=0; }
    54 
    55     void operator=(MTime &t)
     65    void operator=(const MTime &t)
    5666    {
    5767        fMjd     = t.fMjd;
     
    6070    }
    6171
     72    void Reset() { fMjd=0; fTime=0; fNanoSec=0; }
     73
    6274    void Print(Option_t *t=NULL) const;
    6375
    6476    void Now();
    6577
    66     Double_t GetMjd() const
     78    Bool_t   Set(UInt_t mjd, ULong_t ms, UInt_t ns=0);
     79    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);
     80    void     Set(const struct timeval &tv);
     81    void     SetCT1Time(UInt_t mjd, UInt_t t1, UInt_t t0);
     82    void     SetMjd(Double_t m);
     83    Double_t GetMjd() const;
     84    TString  GetString() const;
     85    TString  GetFileName() const;
     86    void     GetDate(UShort_t &y, Byte_t &m, Byte_t &d) const;
     87    TTime    GetRootTime() const;
     88    Double_t GetAxisTime() const;
     89    Long_t   GetTime() const { return (Long_t)fTime; } // [ms] Time of Day returned in the range [-11h, 13h)
     90    void     GetTime(Byte_t &h, Byte_t &m, Byte_t &s, UShort_t &ms) const;
     91    void     GetTime(Byte_t &h, Byte_t &m, Byte_t &s) const
    6792    {
    68         return fMjd+((Long_t)fTime+fNanoSec/1e6)/1000;
     93        UShort_t ms;
     94        GetTime(h, m, s, ms);
    6995    }
    7096
    71     void SetSystemTimer(const struct timeval &tv);
     97    UInt_t Year() const  { UShort_t y; Byte_t m, d; GetDate(y,m,d); return y; }
     98    UInt_t Month() const { UShort_t y; Byte_t m, d; GetDate(y,m,d); return m; }
     99    UInt_t Day() const   { UShort_t y; Byte_t m, d; GetDate(y,m,d); return d; }
     100    UInt_t Hour() const  { Byte_t h, m, s; GetTime(h,m,s); return h; }
     101    UInt_t Min() const   { Byte_t h, m, s; GetTime(h,m,s); return m; }
     102    UInt_t Sec() const   { Byte_t h, m, s; GetTime(h,m,s); return s; }
    72103
    73     Bool_t Set(UInt_t mjd, ULong_t ms, UInt_t ns=0)
     104    operator double() const   //[s]
    74105    {
    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;
     106        return ((Double_t)fMjd*kDay+(Long_t)fTime+fNanoSec/1e6)/1000;
    113107    }
    114108    double operator()() const //[s]
    115109    {
    116110        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);
    138111    }
    139112
     
    151124    return false;
    152125}
     126
    153127inline bool operator>(MTime &t1, MTime &t2)
    154128{
     
    161135    return false;
    162136}
     137
    163138inline bool operator<=(MTime &t1, MTime &t2)
    164139{
    165140    return !(t1>t2);
    166141}
     142
    167143inline bool operator>=(MTime &t1, MTime &t2)
    168144{
    169145    return !(t1<t2);
    170146}
     147
    171148inline bool operator==(MTime &t1, MTime &t2)
    172149{
    173150    return t1.fNanoSec==t2.fNanoSec && t1.fTime==t2.fTime && t1.fMjd==t2.fMjd;
    174151}
     152
    175153inline bool operator!=(MTime &t1, MTime &t2)
    176154{
    177155    return t1.fNanoSec!=t2.fNanoSec || t1.fTime!=t2.fTime || t1.fMjd!=t2.fMjd;
    178156}
     157
     158inline ostream &operator<<(ostream &out, MTime &t)
     159{
     160    out << t.GetString();
     161    return out;
     162}
     163
    179164#endif
Note: See TracChangeset for help on using the changeset viewer.