/*! \file \version $Id: TAGtimestamp.cxx,v 1.2 2003/05/25 12:33:11 mueller Exp $ \brief Implementation of TATOFtimestamp. */ #include #include #include #include #include "TBuffer.h" #include "TATOFtimestamp.h" /*! \class TATOFtimestamp TATOFtimestamp.h "TATOFtimestamp.h" \brief Time representation. ** */ ClassImp(TATOFtimestamp); //------------------------------------------+----------------------------------- //! Default constructor. TATOFtimestamp::TATOFtimestamp() : fiSec(-1), fiUSec(0) {} //------------------------------------------+----------------------------------- //! Construct from Unix time \a i_time . TATOFtimestamp::TATOFtimestamp(Int_t i_time) : fiSec(i_time), fiUSec(0) {} //------------------------------------------+----------------------------------- //! Destructor. TATOFtimestamp::~TATOFtimestamp() {} //------------------------------------------+----------------------------------- //! Convert a \c double to a timestamp. void TATOFtimestamp::FromDouble(Double_t d_time) { Double_t d_sec = floor(d_time); Double_t d_usec = floor(1.e6*(d_time - d_sec)); if (d_usec == 1.e6) { d_sec += 1.; d_usec = 0.; } fiSec = (Int_t) d_sec; fiUSec = (Int_t) d_usec; return; } //------------------------------------------+----------------------------------- //! Set to current system time. void TATOFtimestamp::SetCurrent() { struct timeval tv; struct timezone tz; gettimeofday(&tv, &tz); fiSec = (Int_t) tv.tv_sec; fiUSec = (Int_t) tv.tv_usec; return; } //------------------------------------------+----------------------------------- //! Returns time difference between current time and timestamp Double_t TATOFtimestamp::Age() const { if (!Valid()) return 0.; TATOFtimestamp current; current.SetCurrent(); return current - *this; } //------------------------------------------+----------------------------------- //! Add \a d_deltatime seconds to the timestamp. TATOFtimestamp& TATOFtimestamp::operator+(Double_t d_deltatime) { FromDouble(ToDouble() + d_deltatime); return *this; } //------------------------------------------+----------------------------------- //! Subtract \a d_deltatime seconds from the timestamp. TATOFtimestamp& TATOFtimestamp::operator-(Double_t d_deltatime) { FromDouble(ToDouble() - d_deltatime); return *this; } //------------------------------------------+----------------------------------- //! Custom streamer. void TATOFtimestamp::Streamer(TBuffer& R__b) { UInt_t R__s, R__c; if (R__b.IsReading()) { Version_t R__v = R__b.ReadVersion(&R__s, &R__c); if (R__v) { } R__b >> fiSec; R__b >> fiUSec; } else { R__c = R__b.WriteVersion(TATOFtimestamp::IsA(), kFALSE); R__b << fiSec; R__b << fiUSec; } return; } //------------------------------------------+----------------------------------- /*! \relates TATOFtimestamp Returns the time elapsed from timestamp \rhs to timestamp \lhs in seconds. */ Double_t operator-(const TATOFtimestamp& lhs, const TATOFtimestamp& rhs) { return lhs.ToDouble() - rhs.ToDouble(); }