TTimeStamp.h

Go to the documentation of this file.
00001 // @(#)root/base:$Id: TTimeStamp.h 32404 2010-02-25 12:06:10Z rdm $
00002 // Author: R. Hatcher   30/9/2001
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2002, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 #ifndef ROOT_TTimeStamp
00013 #define ROOT_TTimeStamp
00014 
00015 //////////////////////////////////////////////////////////////////////////
00016 //
00017 // The TTimeStamp encapsulates seconds and ns since EPOCH
00018 //
00019 // This extends (and isolates) struct timespec
00020 //    struct timespec
00021 //       {
00022 //          time_t   tv_sec;   /* seconds */
00023 //          long     tv_nsec;  /* nanoseconds */
00024 //       }
00025 //    time_t seconds is relative to Jan 1, 1970 00:00:00 UTC
00026 //
00027 // No accounting of leap seconds is made.
00028 //
00029 // Due to ROOT/CINT limitations TTimeStamp does not explicitly
00030 // hold a timespec struct; attempting to do so means the Streamer
00031 // must be hand written.  Instead we have chosen to simply contain
00032 // similar fields within the private area of this class.
00033 //
00034 // NOTE: the use of time_t (and its default implementation as a 32 int)
00035 //       implies overflow conditions occurs somewhere around
00036 //       Jan 18, 19:14:07, 2038.
00037 //       If this experiment is still going when it becomes significant
00038 //       someone will have to deal with it.
00039 //
00040 //////////////////////////////////////////////////////////////////////////
00041 
00042 #ifndef ROOT_Rtypes
00043 #include "Rtypes.h"
00044 #endif
00045 #ifndef ROOT_Riosfwd
00046 #include "Riosfwd.h"
00047 #endif
00048 
00049 #include <time.h>
00050 #if !defined(__CINT__) && (defined(R__MACOSX) || defined(R__OBSD))
00051 #include <sys/time.h>
00052 #endif
00053 #if defined(__CINT__) || defined(R__WIN32)
00054 // Explicit definition of timespec 'cause "rootcint" won't look in
00055 // appropriate <time.h>. time_t appears to be defined as "typedef long time_t;"
00056 // in CINT version of <time.h>.  This isn't required by the standard:
00057 // to be compatible w/ std functions it must be at least 32-bits long,
00058 // but it might be longer to avoid the year-2037 cutoff.
00059 struct timespec
00060 {
00061    time_t   tv_sec;             // seconds
00062    long     tv_nsec;            // nanoseconds
00063 };
00064 #endif
00065 #if defined(__CINT__)
00066 struct tm
00067 {
00068    int tm_sec;                   // Seconds.     [0-60] (1 leap second)
00069    int tm_min;                   // Minutes.     [0-59]
00070    int tm_hour;                  // Hours.       [0-23]
00071    int tm_mday;                  // Day.         [1-31]
00072    int tm_mon;                   // Month.       [0-11]
00073    int tm_year;                  // Year - 1900.
00074    int tm_wday;                  // Day of week. [0-6]
00075    int tm_yday;                  // Days in year.[0-365]
00076    int tm_isdst;                 // DST.         [-1/0/1]
00077 };
00078 #endif
00079 
00080 // define this typedef so that CINT doesn't barf at using these
00081 // for function return values
00082 typedef struct timespec timespec_t;
00083 typedef struct tm       tm_t;
00084 
00085 class TVirtualMutex;
00086 class TTimeStamp;
00087 ostream &operator<<(ostream &os,  const TTimeStamp &ts);
00088 TBuffer &operator<<(TBuffer &buf, const TTimeStamp &ts);
00089 TBuffer &operator>>(TBuffer &buf, TTimeStamp &ts);
00090 Bool_t operator==(const TTimeStamp &lhs, const TTimeStamp &rhs);
00091 Bool_t operator!=(const TTimeStamp &lhs, const TTimeStamp &rhs);
00092 Bool_t operator< (const TTimeStamp &lhs, const TTimeStamp &rhs);
00093 Bool_t operator<=(const TTimeStamp &lhs, const TTimeStamp &rhs);
00094 Bool_t operator> (const TTimeStamp &lhs, const TTimeStamp &rhs);
00095 Bool_t operator>=(const TTimeStamp &lhs, const TTimeStamp &rhs);
00096 
00097 R__EXTERN TVirtualMutex *gTimeMutex;
00098 
00099 class TTimeStamp {
00100 
00101 friend Bool_t operator==(const TTimeStamp &lhs, const TTimeStamp &rhs);
00102 friend Bool_t operator!=(const TTimeStamp &lhs, const TTimeStamp &rhs);
00103 friend Bool_t operator< (const TTimeStamp &lhs, const TTimeStamp &rhs);
00104 friend Bool_t operator<=(const TTimeStamp &lhs, const TTimeStamp &rhs);
00105 friend Bool_t operator> (const TTimeStamp &lhs, const TTimeStamp &rhs);
00106 friend Bool_t operator>=(const TTimeStamp &lhs, const TTimeStamp &rhs);
00107 
00108 private:
00109    Int_t  fSec;      // seconds
00110    Int_t  fNanoSec;  // nanoseconds
00111 
00112    void   NormalizeNanoSec();
00113 
00114 public:
00115    // empty ctor (builds current time with nsec field incremented from static)
00116    TTimeStamp();
00117 
00118    // construction from timespec struct
00119    TTimeStamp(const timespec_t &ts) :
00120       fSec(Int_t(ts.tv_sec)), fNanoSec(ts.tv_nsec) { NormalizeNanoSec(); }
00121 
00122    // construction from time_t and separate nsec
00123    TTimeStamp(time_t t, Int_t nsec) :
00124       fSec(Int_t(t)), fNanoSec(nsec) { NormalizeNanoSec(); }
00125 
00126    // construction from bits and pieces
00127    TTimeStamp(UInt_t year, UInt_t month,
00128               UInt_t day,  UInt_t hour,
00129               UInt_t min,  UInt_t sec,
00130               UInt_t nsec = 0, Bool_t isUTC = kTRUE, Int_t secOffset = 0);
00131 
00132    // compatibility with TDatime
00133    TTimeStamp(UInt_t date, UInt_t time, UInt_t nsec,
00134               Bool_t isUTC = kTRUE, Int_t secOffset = 0);
00135 
00136    // compatability with time() and DOS date
00137    TTimeStamp(UInt_t tloc, Bool_t isUTC = kTRUE, Int_t secOffset = 0,
00138               Bool_t dosDate = kFALSE);
00139 
00140    virtual ~TTimeStamp() { }
00141 
00142    // initialize to current time with nsec field incremented from static
00143    void Set();
00144 
00145    // construction from bits and pieces
00146    void Set(Int_t year, Int_t month,  Int_t day,
00147             Int_t hour, Int_t min,    Int_t sec,
00148             Int_t nsec, Bool_t isUTC, Int_t secOffset);
00149 
00150    // compatibility with TDatime
00151    void Set(Int_t date,   Int_t time, Int_t nsec,
00152             Bool_t isUTC, Int_t secOffset);
00153 
00154    // compatability with time() and DOS date
00155    void Set(UInt_t tloc, Bool_t isUTC, Int_t secOffset, Bool_t dosDate);
00156 
00157    // direct setters
00158    void SetSec(Int_t sec) { fSec = sec; }
00159    void SetNanoSec(Int_t nsec) { fNanoSec = nsec; }
00160 
00161    timespec_t   GetTimeSpec() const
00162       { timespec_t value = {fSec,fNanoSec}; return value; }
00163    time_t       GetSec() const { return fSec; }
00164    Int_t        GetNanoSec() const { return fNanoSec; }
00165 
00166    Double_t     AsDouble() const { return fSec + 1e-9 * fNanoSec; }
00167    Double_t     AsJulianDate() const { return (AsDouble()/86400.0 + 2440587.5); }
00168    const char  *AsString(const Option_t *option="") const;
00169 
00170    void         Copy(TTimeStamp &ts) const;
00171    UInt_t       GetDate(Bool_t inUTC = kTRUE, Int_t secOffset = 0,
00172                         UInt_t *year = 0, UInt_t *month = 0,
00173                         UInt_t *day = 0) const;
00174    UInt_t       GetTime(Bool_t inUTC = kTRUE, Int_t secOffset = 0,
00175                         UInt_t *hour = 0, UInt_t *min = 0,
00176                         UInt_t *sec = 0) const;
00177    Int_t        GetDayOfYear(Bool_t inUTC = kTRUE, Int_t secOffset = 0) const;
00178    Int_t        GetDayOfWeek(Bool_t inUTC = kTRUE, Int_t secOffset = 0) const;
00179    Int_t        GetMonth(Bool_t inUTC = kTRUE, Int_t secOffset = 0) const;
00180    Int_t        GetWeek(Bool_t inUTC = kTRUE, Int_t secOffset = 0) const;
00181    Bool_t       IsLeapYear(Bool_t inUTC = kTRUE, Int_t secOffset = 0) const;
00182 
00183    void         Add(const TTimeStamp &offset);
00184 
00185    void         Print(const Option_t *option="") const;
00186 
00187    operator double() const { return AsDouble(); }
00188 
00189    // Utility functions
00190    static Int_t   GetZoneOffset();
00191    static time_t  MktimeFromUTC(tm_t *tmstruct);
00192    static void    DumpTMStruct(const tm_t &tmstruct);
00193    static Int_t   GetDayOfYear(Int_t day, Int_t month, Int_t year);
00194    static Int_t   GetDayOfWeek(Int_t day, Int_t month, Int_t year);
00195    static Int_t   GetWeek(Int_t day, Int_t month, Int_t year);
00196    static Bool_t  IsLeapYear(Int_t year);
00197 
00198    ClassDef(TTimeStamp,1)  //Encapsulates seconds and ns since EPOCH
00199 };
00200 
00201 
00202 inline Bool_t operator==(const TTimeStamp &lhs, const TTimeStamp &rhs)
00203    { return lhs.fSec == rhs.fSec &&
00204             lhs.fNanoSec == rhs.fNanoSec; }
00205 
00206 inline Bool_t operator!=(const TTimeStamp &lhs, const TTimeStamp &rhs)
00207    { return lhs.fSec != rhs.fSec ||
00208             lhs.fNanoSec != rhs.fNanoSec; }
00209 
00210 inline Bool_t operator<(const TTimeStamp &lhs, const TTimeStamp &rhs)
00211    { return lhs.fSec < rhs.fSec ||
00212              (lhs.fSec == rhs.fSec &&
00213               lhs.fNanoSec < rhs.fNanoSec); }
00214 
00215 inline Bool_t operator<=(const TTimeStamp &lhs, const TTimeStamp &rhs)
00216    { return lhs.fSec < rhs.fSec ||
00217              (lhs.fSec == rhs.fSec &&
00218               lhs.fNanoSec <= rhs.fNanoSec); }
00219 
00220 inline Bool_t operator>(const TTimeStamp &lhs, const TTimeStamp &rhs)
00221    { return lhs.fSec > rhs.fSec ||
00222              (lhs.fSec == rhs.fSec &&
00223               lhs.fNanoSec > rhs.fNanoSec); }
00224 
00225 inline Bool_t operator>=(const TTimeStamp &lhs, const TTimeStamp &rhs)
00226    { return lhs.fSec > rhs.fSec ||
00227              (lhs.fSec == rhs.fSec &&
00228               lhs.fNanoSec >= rhs.fNanoSec); }
00229 
00230 #endif

Generated on Tue Jul 5 14:10:32 2011 for ROOT_528-00b_version by  doxygen 1.5.1