CMS 3D CMS Logo

Public Member Functions | Static Public Member Functions | Private Attributes | Static Private Attributes | Friends

Tm Class Reference

#include <Tm.h>

List of all members.

Public Member Functions

struct tm c_tm () const
uint64_t cmsNanoSeconds () const
void dumpTm ()
uint64_t epoch () const
int isNull () const
uint64_t microsTime () const
bool operator!= (const Tm &t) const
const Tm operator+ (int seconds)
Tmoperator+= (int seconds)
Tmoperator-= (int seconds)
bool operator< (const Tm &t) const
bool operator<= (const Tm &t) const
bool operator== (const Tm &t) const
void setNull ()
void setToCmsNanoTime (uint64_t nanos)
void setToCurrentGMTime ()
void setToCurrentLocalTime ()
void setToGMTime (time_t t)
void setToLocalTime (time_t t)
void setToMicrosTime (uint64_t micros)
void setToString (const std::string s) throw (std::runtime_error)
std::string str () const
 Tm (struct tm *initTm)
 Tm ()
 Tm (uint64_t micros)
uint64_t unixTime () const
virtual ~Tm ()

Static Public Member Functions

static Tm negInfinity ()
static Tm plusInfinity ()

Private Attributes

struct tm m_tm

Static Private Attributes

static const uint64_t NEG_INF_MICROS = 0
static const uint64_t PLUS_INF_MICROS = (uint64_t)INT_MAX * 1000000

Friends

std::ostream & operator<< (std::ostream &out, const Tm &t)

Detailed Description

Definition at line 14 of file Tm.h.


Constructor & Destructor Documentation

Tm::Tm ( )

Definition at line 24 of file Tm.cc.

Referenced by negInfinity(), and plusInfinity().

{
  this->setNull();
}
Tm::Tm ( struct tm *  initTm)

Definition at line 32 of file Tm.cc.

{
  m_tm = *initTm;
}
Tm::Tm ( uint64_t  micros)

Definition at line 37 of file Tm.cc.

{
  this->setNull();
  if (micros > PLUS_INF_MICROS) {
    //micros = PLUS_INF_MICROS;
    this->setToCmsNanoTime(micros);
  } else {
    this->setToMicrosTime(micros);
  }
}
Tm::~Tm ( ) [virtual]

Definition at line 50 of file Tm.cc.

{
}

Member Function Documentation

struct tm Tm::c_tm ( ) const [read]

Definition at line 56 of file Tm.cc.

Referenced by str(), and DateHandler::tmToDate().

{
  return m_tm;
}
uint64_t Tm::cmsNanoSeconds ( ) const

Definition at line 116 of file Tm.cc.

Referenced by popcon::EcalLaserHandler::getNewObjects(), and popcon::EcalLaser_weekly_Handler::getNewObjects().

{
  return microsTime()/1000000 << 32;
}
void Tm::dumpTm ( )

Definition at line 244 of file Tm.cc.

References gather_cfg::cout.

{
  cout << "=== dumpTm() ===" << endl;
  cout << "tm_year  " << m_tm.tm_year << endl;
  cout << "tm_mon   " << m_tm.tm_mon << endl;
  cout << "tm_mday  " << m_tm.tm_mday << endl;
  cout << "tm_hour  " << m_tm.tm_hour << endl;
  cout << "tm_min   " << m_tm.tm_min << endl;
  cout << "tm_sec   " << m_tm.tm_sec << endl;
  cout << "tm_yday  " << m_tm.tm_yday << endl;
  cout << "tm_wday  " << m_tm.tm_wday << endl;
  cout << "tm_isdst " << m_tm.tm_isdst << endl;
  cout << "================" << endl;
}
uint64_t Tm::epoch ( ) const [inline]

Definition at line 67 of file Tm.h.

References unixTime().

{ return unixTime(); };
int Tm::isNull ( ) const

Definition at line 63 of file Tm.cc.

Referenced by DateHandler::tmToDate().

{
  if (m_tm.tm_year == 0
      && m_tm.tm_mon == 0
      && m_tm.tm_mday == 0 ) {
    return 1;
  } else { return 0; }
}
uint64_t Tm::microsTime ( ) const

Definition at line 126 of file Tm.cc.

References query::result, alignCSCRings::s, and x.

Referenced by popcon::EcalLaserHandler::getNewObjects(), DataReducer< T >::getReducedDataList(), RunDCSHVDat::nowMicroseconds(), RunDCSMagnetDat::nowMicroseconds(), RunDCSLVDat::nowMicroseconds(), operator+=(), operator-=(), operator<(), DataReducer< T >::MyData< U >::operator<(), operator<=(), RunDCSHVDat::setStatusForBarrel(), RunDCSLVDat::setStatusForBarrel(), RunDCSHVDat::setStatusForEndcaps(), and RunDCSLVDat::setStatusForEndcaps().

{
  uint64_t result = 0;
  /*  
  result += (uint64_t)ceil((m_tm.tm_year - 70 ) * 365.25) * 24 * 3600;
  result += (m_tm.tm_yday) * 24 * 3600;
  result += m_tm.tm_hour * 3600;
  result += m_tm.tm_min * 60;
  result += m_tm.tm_sec;
  return (uint64_t) (result * 1000000);
  */

  struct tm time_struct;
  time_struct.tm_year=1970-1900;
  time_struct.tm_mon=0;
  time_struct.tm_mday=1;
  time_struct.tm_sec=0;
  time_struct.tm_min=0;
  time_struct.tm_hour=0;
  time_struct.tm_isdst=0;
  
  time_t t1970=mktime(&time_struct);
  tm s = m_tm;
  time_t t_this=mktime(&s);

  double x= difftime(t_this,t1970); 
  result =(uint64_t) x*1000000;

  return result; 

}
static Tm Tm::negInfinity ( ) [inline, static]

Definition at line 53 of file Tm.h.

References NEG_INF_MICROS, and Tm().

    {
      return Tm(NEG_INF_MICROS);
    };
bool Tm::operator!= ( const Tm t) const [inline]

Definition at line 122 of file Tm.h.

{ return !(t == *this); }
const Tm Tm::operator+ ( int  seconds) [inline]

Definition at line 134 of file Tm.h.

References run_regression::ret, and seconds().

                                  {
    Tm ret = *this;
    ret += seconds;
    return ret;
  }
Tm& Tm::operator+= ( int  seconds) [inline]

Definition at line 129 of file Tm.h.

References microsTime(), and setToMicrosTime().

                              {
    setToMicrosTime(microsTime() + seconds * 1e6);
    return *this;
  }
Tm& Tm::operator-= ( int  seconds) [inline]

Definition at line 124 of file Tm.h.

References microsTime(), and setToMicrosTime().

                              {
    setToMicrosTime(microsTime() - seconds * 1e6);
    return *this;
  }
bool Tm::operator< ( const Tm t) const [inline]

Definition at line 100 of file Tm.h.

References microsTime().

    {
      return microsTime() < t.microsTime();
    }
bool Tm::operator<= ( const Tm t) const [inline]

Definition at line 105 of file Tm.h.

References microsTime().

    {
      return microsTime() <= t.microsTime();
    }
bool Tm::operator== ( const Tm t) const [inline]

Definition at line 110 of file Tm.h.

References m_tm.

    {   return (m_tm.tm_hour  == t.m_tm.tm_hour &&
                m_tm.tm_isdst == t.m_tm.tm_isdst &&
                m_tm.tm_mday  == t.m_tm.tm_mday &&
                m_tm.tm_min   == t.m_tm.tm_min &&
                m_tm.tm_mon   == t.m_tm.tm_mon &&
                m_tm.tm_sec   == t.m_tm.tm_sec &&
                m_tm.tm_wday  == t.m_tm.tm_wday &&
                m_tm.tm_yday  == t.m_tm.tm_yday &&
                m_tm.tm_year  == t.m_tm.tm_year); 
    }
static Tm Tm::plusInfinity ( ) [inline, static]

Definition at line 47 of file Tm.h.

References PLUS_INF_MICROS, and Tm().

Referenced by LMFSeqDat::LMFSeqDat().

    {
      return Tm(PLUS_INF_MICROS);
    };
void Tm::setNull ( )

Definition at line 74 of file Tm.cc.

Referenced by LMFSextuple::LMFSextuple().

{
  m_tm.tm_hour  = 0;
  m_tm.tm_isdst = 0;
  m_tm.tm_mday  = 0;
  m_tm.tm_min   = 0;
  m_tm.tm_mon   = 0;
  m_tm.tm_sec   = 0;
  m_tm.tm_wday  = 0;
  m_tm.tm_yday  = 0;
  m_tm.tm_year  = 0;
}
void Tm::setToCmsNanoTime ( uint64_t  nanos)

Definition at line 158 of file Tm.cc.

                                        {
  setToMicrosTime((nanos >> 32) * 1000000);
}
void Tm::setToCurrentGMTime ( )
void Tm::setToCurrentLocalTime ( )

Definition at line 171 of file Tm.cc.

References NULL, lumiQTWidget::t, and cond::rpcobgas::time.

Referenced by popcon::EcalLaserHandler::getNewObjects().

{
  time_t t = time(NULL);
  m_tm = *localtime( &t );
}
void Tm::setToGMTime ( time_t  t)

Definition at line 188 of file Tm.cc.

Referenced by str().

{
  m_tm = *gmtime( &t );
}
void Tm::setToLocalTime ( time_t  t)

Definition at line 183 of file Tm.cc.

{
  m_tm = *localtime( &t );
}
void Tm::setToMicrosTime ( uint64_t  micros)

Definition at line 162 of file Tm.cc.

References lumiQTWidget::t.

Referenced by operator+=(), and operator-=().

{
  time_t t = micros / 1000000;
  if (t >= INT_MAX) {
    t = INT_MAX;
  }
  m_tm = *gmtime(&t);
}
void Tm::setToString ( const std::string  s) throw (std::runtime_error)

Definition at line 193 of file Tm.cc.

References alignCSCRings::e, lumiQueryAPI::msg, and alignCSCRings::s.

Referenced by LMFCorrCoefDat::fetchAfter(), LMFCorrCoefDat::fetchBetween(), RunDCSHVDat::fillTheMapByTime(), LMFRunIOV::getDBInsertionTime(), LMFLmrSubIOV::getIOVIDsLaterThan(), popcon::EcalLaserHandler::getNewObjects(), LMFLmrSubIOV::getParameters(), LMFSeqDat::getSequenceStart(), LMFSeqDat::getSequenceStop(), LMFRunIOV::getSubRunEnd(), and LMFRunIOV::getSubRunStart().

{
  sscanf(s.c_str(), "%04d-%02d-%02d %02d:%02d:%02d", 
         &m_tm.tm_year, &m_tm.tm_mon, &m_tm.tm_mday,
         &m_tm.tm_hour, &m_tm.tm_min, &m_tm.tm_sec);

  try {
    if (m_tm.tm_year > 9999 || m_tm.tm_year < 1900) {
      throw(std::runtime_error("Year out of bounds"));
    } else if (m_tm.tm_mon > 12 || m_tm.tm_mon < 1) {
      throw(std::runtime_error("Month out of bounds"));
    } else if (m_tm.tm_mday > 31 || m_tm.tm_mday < 1) {
      throw(std::runtime_error("Day out of bounds"));
    } else if (m_tm.tm_hour > 23 || m_tm.tm_mday < 0) {
      throw(std::runtime_error("Hour out of bounds"));
    } else if (m_tm.tm_min > 59 || m_tm.tm_min < 0) {
      throw(std::runtime_error("Minute out of bounds"));
    } else if (m_tm.tm_sec > 59 || m_tm.tm_sec < 0) {
      throw(std::runtime_error("Day out of bounds"));
    }

    if (m_tm.tm_year >= 2038) {
      // take into account UNIX time limits
      m_tm.tm_year = 2038;
      if (m_tm.tm_mon > 1) {
        m_tm.tm_mon = 1;
      }
      if (m_tm.tm_mday > 19) {
        m_tm.tm_mday = 19;
      }
      if (m_tm.tm_hour > 3) {
        m_tm.tm_hour = 3;
      }
      if (m_tm.tm_min > 14) {
        m_tm.tm_min = 14;
      }
      if (m_tm.tm_sec > 7) {
        m_tm.tm_sec = 7;
      }
    }
    m_tm.tm_year -= 1900;
    m_tm.tm_mon -= 1;
  } catch (std::runtime_error &e) {
    this->setNull();
    string msg("Tm::setToString():  ");
    msg.append(e.what());
    throw(std::runtime_error(msg));
  }
}
string Tm::str ( ) const

"One hour shif" fix Create a temporary dummy object that is in GMT and use it to generate the output. This is to avoid the "one hour shift" related to the Summer time and the value of m_tm.tm_isdst, see [1]. It guaranties that the output is always in GMT / UTC. [1] https://hypernews.cern.ch/HyperNews/CMS/get/ecalDB/66.html

Definition at line 89 of file Tm.cc.

References c_tm(), setToGMTime(), and AlCaHLTBitMon_QueryRunRegistry::string.

Referenced by LMFCorrCoefDat::fetchBetween(), LMFSeqDat::fetchByRunNumber(), RunDCSHVDat::fillTheMapByTime(), LMFCorrCoefDat::getCorrections(), popcon::EcalLaserHandler::getNewObjects(), LMFSeqDat::getParameters(), LMFRunIOV::getParameters(), DataReducer< T >::getReducedDataList(), LMFRunIOV::initialize(), LMFSeqDat::LMFSeqDat(), operator<<(), EcalTPGDBApp::printIOV(), LMFUnique::sequencePostfix(), LMFSeqDat::setSequenceStart(), LMFSeqDat::setSequenceStop(), LMFRunIOV::setSubRunEnd(), and LMFRunIOV::setSubRunStart().

{
  if (this->isNull()) {
    return "";
  }

  char timebuf[20] = "";
  if (this->microsTime() >= PLUS_INF_MICROS) {
    sprintf(timebuf, "9999-12-12 23:59:59");
  } else {
    Tm dummy_Tm;
    dummy_Tm.setToGMTime(this->microsTime()/1000000);
    struct tm dummy_tm = dummy_Tm.c_tm();
    strftime(timebuf, 20, "%Y-%m-%d %H:%M:%S", &dummy_tm);
  }
  return string(timebuf);
}
uint64_t Tm::unixTime ( ) const

Definition at line 121 of file Tm.cc.

Referenced by epoch().

{
  return microsTime()/1000000;
}

Friends And Related Function Documentation

std::ostream& operator<< ( std::ostream &  out,
const Tm t 
) [friend]

GO: the maximum UNIX time is restricted to INT_MAX, corresponding to 2038-01-19 03:14:07. Take it into account in str()

Definition at line 18 of file Tm.cc.

                                                     {
  out << t.str();
  return out;
}

Member Data Documentation

struct tm Tm::m_tm [private]

Definition at line 143 of file Tm.h.

Referenced by operator==().

const uint64_t Tm::NEG_INF_MICROS = 0 [static, private]

Definition at line 15 of file Tm.h.

Referenced by negInfinity().

const uint64_t Tm::PLUS_INF_MICROS = (uint64_t)INT_MAX * 1000000 [static, private]

Definition at line 17 of file Tm.h.

Referenced by plusInfinity().