| Classes | Job Modules | Data Objects | Services | Algorithms | Tools | Packages | Directories | Tracs |

In This Package:

TimeStamp.h

Go to the documentation of this file.
00001 #ifndef TIMESTAMP_HH
00002 #define TIMESTAMP_HH
00003 
00071 
00072 #include <ctime>
00073 
00074 // make the TimeStamp cout'able
00075 #include <iosfwd>
00076 
00077 
00078 class TimeStamp;
00079 std::ostream& operator<<(std::ostream& os, const TimeStamp& vldts);
00080 
00081 class TimeStamp {
00082 
00083     friend bool operator==(const TimeStamp &lhs, const TimeStamp &rhs);
00084     friend bool operator!=(const TimeStamp &lhs, const TimeStamp &rhs);
00085     friend bool operator< (const TimeStamp &lhs, const TimeStamp &rhs);
00086     friend bool operator<=(const TimeStamp &lhs, const TimeStamp &rhs);
00087     friend bool operator> (const TimeStamp &lhs, const TimeStamp &rhs);
00088     friend bool operator>=(const TimeStamp &lhs, const TimeStamp &rhs);
00089 
00090     friend TimeStamp operator- (const TimeStamp &lhs, const TimeStamp &rhs);
00091 
00092 public:
00093 
00095     static TimeStamp GetBOT();
00096 
00100     static TimeStamp GetEOT();
00101 
00104     static TimeStamp GetNBOT();
00105 
00107     TimeStamp();
00108 
00110     TimeStamp(const TimeStamp &source)
00111         { mSec = source.mSec; mNanoSec = source.mNanoSec; }
00113     TimeStamp& operator=(const TimeStamp &source)
00114         { if (this != &source) {mSec = source.mSec; mNanoSec = source.mNanoSec;}
00115             return *this; }
00116 
00118     TimeStamp(const timespec &ts)
00119         { mSec = ts.tv_sec; mNanoSec = ts.tv_nsec; NormalizeNanoSec(); }
00120 
00122     TimeStamp(const time_t &t, const int nsec)
00123         { mSec = t; mNanoSec = 0; mNanoSec = nsec; NormalizeNanoSec(); }
00124 
00137     TimeStamp(unsigned int year, unsigned int month,
00138               unsigned int day,  unsigned int hour,
00139               unsigned int min,  unsigned int sec,
00140               unsigned int nsec=0, 
00141               bool isUTC=true, int secOffset=0);
00142 
00145     TimeStamp(unsigned int date, unsigned int time, unsigned int nsec, 
00146               bool isUTC=true, int secOffset=0);
00147 
00148     // Create a TimeStamp using double precision floating point
00149     // seconds from the EPOCH.
00150     //
00151     // \warning This will truncate precision to no better than about 1
00152     // microsecond.  Do not use this constructor for timestamps that
00153     // are expected to be more precise!
00154     //
00155     TimeStamp(double seconds)
00156         { mSec = (int)seconds; mNanoSec = (int)((seconds-mSec)*1.0e9); 
00157             NormalizeNanoSec();}
00158   
00159 
00160     virtual ~TimeStamp();
00161 
00162     // Implicitly convert a TimeStamp to a double.
00163     //
00164     // \warning This will truncate precision to no better than about 1
00165     // microsecond.  Do not compare/subtract TimeStamps that have
00166     // been converted to doubles if you require the full nanosecond
00167     // precision!
00168   
00169     operator double() const { return mSec + 1.0e-9 * mNanoSec; }
00170   
00171   
00173     timespec     GetTimeSpec() const { timespec value = {mSec,mNanoSec}; return value; }
00174   
00176     time_t         GetSec(void) const { return mSec;}
00178     int          GetNanoSec(void) const { return mNanoSec; }
00179   
00181     double GetSeconds(void) const { return mSec+(mNanoSec/1.0e9); }
00182   
00183     //    Return the date & time as a string.
00184     //   
00185     //    Result is pointer to a statically allocated string.
00186     //    User should copy this into their own buffer before calling
00187     //    this method again.  This is somewhat mitigated
00188     //    by use of a circular buffer of strings.
00189     //   
00190     //    Option "l" returns it in local zone format
00191     //    (can be applied to default or compact format).
00192     //   
00193     //    Default format is RFC822 compliant:
00194     //      "Mon, 02 Jan 2001 18:11:12 +0000 (GMT) +999999999 nsec"
00195     //      "Mon, 02 Jan 2001 10:11:12 -0800 (PST) +999999999 nsec"
00196     //   
00197     //    Option "c" compact is (almost) ISO 8601 compliant:
00198     //      "2001-01-02 18:11:12.9999999999Z"
00199     //      "2001-01-02 10:11:12.9999999999-0800"  if PST
00200     //         * uses "-" as date separator as specified in ISO 8601
00201     //         * uses "." rather than preferred "," for decimal separator
00202     //         * -HHMM is the difference between local and UTC (if behind, + if ahead).
00203     //      The "-HHMM" is replaced with "Z" if given as UTC.
00204     //      To be strictly conforming it should use "T" instead of the
00205     //      blank separating the date and time.
00206     //   
00207     //    Option "2" returns as {sec,nsec} integers.
00208     //   
00209     //    Option "s" returns "2001-01-02 18:11:12" with an implied UTC,
00210     //    overrides "l" option.
00211     //   
00212     //    Internally uses a circular list of buffers to avoid problems
00213     //    using AsString multiple times in a single statement.
00214     //    
00215     const char    *AsString(const char* option="") const;
00216     void           Copy(TimeStamp &vldts) const;
00217 
00220     int          GetDate(bool inUTC=true, int secOffset=0,
00221                          unsigned int *year=0, unsigned int *month=0,
00222                          unsigned int *day=0) const;
00223 
00226     int          GetTime(bool inUTC=true, int secOffset=0,
00227                          unsigned int* hour=0, unsigned int* min=0, 
00228                          unsigned int* sec=0) const;
00229 
00230     void           Add(const TimeStamp& offset);
00231     void           Add(double seconds);
00232     void           Subtract(const TimeStamp& offset);
00233     void           Subtract(double seconds);
00234 
00235     void           Print(const char* option="") const;
00236   
00237     bool          IsNull() const { return (mSec==0)&&(mNanoSec==0); };
00238   
00239     // Utility functions
00240   
00243     static int   GetZoneOffset();
00244  
00245     // Equivalent of standard routine "mktime" but
00246     // using the assumption that tm struct is filled with UTC, not local, time.
00247     //
00248     // This version *ISN'T* configured to handle every possible
00249     // weirdness of out-of-range values in the case of normalizing
00250     // the tm struct.
00251     //
00252     // This version *DOESN'T* correctly handle values that can't be
00253     // fit into a time_t (i.e. beyond year 2038-01-18 19:14:07, or
00254     // before the start of Epoch). */
00255   
00256     static time_t  MktimeFromUTC(tm* tmstruct);
00257   
00259     static bool  IsLeapYear(int year);
00260   
00262     static void    DumpTMStruct(const tm& tmstruct);
00263 
00264 private:
00265 
00266     void           Set();
00267     void           Set(int year, int month, int day,
00268                        int hour, int min, int sec, 
00269                        int nsec, bool isUTC, int secOffset);
00270     void           Set(int date, int time, int nsec,
00271                        bool isUTC, int secOffset);
00272     void           NormalizeNanoSec();
00273 
00274     // Data members:
00275     // similar fields to struct timespec
00276     // use ROOT versions to "know" that they are platform consistent
00277     // 32-bit integers to avoid IO confusion.
00278     int  mSec;
00279     int  mNanoSec;
00280 
00281 };
00282 
00283 
00284 //=============================================================================
00285 // Implementation details -- inlines need to be hidden from CINT
00286 //=============================================================================
00287 
00288 inline bool operator==(const TimeStamp &lhs, const TimeStamp &rhs)
00289 { return lhs.mSec  == rhs.mSec && 
00290         lhs.mNanoSec == rhs.mNanoSec; }
00291 
00292 inline bool operator!=(const TimeStamp &lhs, const TimeStamp &rhs)
00293 { return lhs.mSec  != rhs.mSec ||
00294         lhs.mNanoSec != rhs.mNanoSec; }
00295 
00296 inline bool operator<(const TimeStamp &lhs, const TimeStamp &rhs)
00297 { return lhs.mSec  < rhs.mSec ||
00298         ( lhs.mSec  == rhs.mSec &&
00299           lhs.mNanoSec <  rhs.mNanoSec   ); }
00300 
00301 inline bool operator<=(const TimeStamp &lhs, const TimeStamp &rhs)
00302 { return lhs.mSec  < rhs.mSec ||
00303         ( lhs.mSec  == rhs.mSec &&
00304           lhs.mNanoSec <= rhs.mNanoSec   ); }
00305 
00306 inline bool operator>(const TimeStamp &lhs, const TimeStamp &rhs)
00307 { return lhs.mSec  > rhs.mSec ||
00308         ( lhs.mSec  == rhs.mSec &&
00309           lhs.mNanoSec >  rhs.mNanoSec   ); }
00310 
00311 inline bool operator>=(const TimeStamp &lhs, const TimeStamp &rhs)
00312 { return lhs.mSec  > rhs.mSec ||
00313         ( lhs.mSec  == rhs.mSec &&
00314           lhs.mNanoSec >= rhs.mNanoSec   ); }
00315 
00316 inline TimeStamp operator-(const TimeStamp& lhs, const TimeStamp& rhs)
00317 {
00318     return TimeStamp(lhs.GetSec()     - rhs.GetSec(),
00319                      lhs.GetNanoSec() - rhs.GetNanoSec());
00320 }
00321 
00322 #endif // TIMESTAMP_HH
| Classes | Job Modules | Data Objects | Services | Algorithms | Tools | Packages | Directories | Tracs |

Generated on Mon Apr 11 20:15:31 2011 for Context by doxygen 1.4.7