CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_10/src/OnlineDB/EcalCondDB/interface/Tm.h

Go to the documentation of this file.
00001 // $Id: Tm.h,v 1.6 2011/05/21 06:19:34 organtin Exp $
00002 
00003 #ifndef TM_HH
00004 #define TM_HH
00005 
00006 #include <stdexcept>
00007 #include <string>
00008 #include <iostream>
00009 #include <time.h>
00010 #include <stdint.h>
00011 #include <limits.h>
00012 
00013 // Wrapper class for time.h tm struct
00014 class Tm {
00015   static const uint64_t NEG_INF_MICROS = 0;
00016   // GO: maximum UNIX time
00017   static const uint64_t PLUS_INF_MICROS = (uint64_t)INT_MAX * 1000000;
00018 
00019  public:
00020   // Default constructor makes a null Tm
00021   Tm();
00022 
00023   // Initialized constructor
00024   Tm(struct tm * initTm);
00025 
00026   Tm(uint64_t micros);
00027 
00028   // Destructor
00029   virtual ~Tm();
00030 
00031   /*
00032    *  Return a pointer to the tm data structure.
00033    */
00034   struct tm c_tm() const;
00035 
00036   /*
00037    *  Returns true if this Tm is null
00038    */
00039   int isNull() const;
00040 
00041   /*
00042    *  Sets this Tm to null
00043    */
00044   void setNull();
00045 
00046 
00047   static Tm plusInfinity()
00048     {
00049       return Tm(PLUS_INF_MICROS);
00050     };
00051 
00052 
00053   static Tm negInfinity()
00054     {
00055       return Tm(NEG_INF_MICROS);
00056     };
00057 
00058   /*
00059    *  String representation of Tm in YYYY-MM-DD hh:mm:ss format
00060    */
00061   std::string str() const;
00062 
00063   /*
00064    *  return number of microseconds since Jan 1 1970 and the epoch
00065    */
00066   uint64_t unixTime() const;
00067   uint64_t epoch() const { return unixTime(); };
00068   uint64_t microsTime() const;
00069 
00070   /*
00071    *  return the number of nanoseconds packed as a CMS time
00072    */
00073   uint64_t cmsNanoSeconds() const;
00074 
00075   /*
00076    *  Set self to current time
00077    */
00078   void setToCurrentLocalTime();
00079   void setToCurrentGMTime();
00080 
00081   /*
00082    *  Set using time_t
00083    */
00084   void setToLocalTime( time_t t);
00085   void setToGMTime( time_t t );
00086 
00087   /*
00088    *  Set using microseconds and CMS times
00089    */
00090   void setToMicrosTime(uint64_t micros);
00091   void setToCmsNanoTime(uint64_t nanos);
00092 
00093   /*
00094    *  Set to string of format YYYY-MM-DD HH:MM:SS
00095    */
00096   void setToString(const std::string s) throw(std::runtime_error);
00097 
00098   void dumpTm();
00099 
00100   inline bool operator<(const Tm &t) const 
00101     {
00102       return microsTime() < t.microsTime();
00103     }
00104 
00105   inline bool operator<=(const Tm &t) const 
00106     {
00107       return microsTime() <= t.microsTime();
00108     }
00109 
00110   inline bool operator==(const Tm &t) const
00111     {   return (m_tm.tm_hour  == t.m_tm.tm_hour &&
00112                 m_tm.tm_isdst == t.m_tm.tm_isdst &&
00113                 m_tm.tm_mday  == t.m_tm.tm_mday &&
00114                 m_tm.tm_min   == t.m_tm.tm_min &&
00115                 m_tm.tm_mon   == t.m_tm.tm_mon &&
00116                 m_tm.tm_sec   == t.m_tm.tm_sec &&
00117                 m_tm.tm_wday  == t.m_tm.tm_wday &&
00118                 m_tm.tm_yday  == t.m_tm.tm_yday &&
00119                 m_tm.tm_year  == t.m_tm.tm_year); 
00120     }
00121                                     
00122   inline bool operator!=(const Tm &t) const { return !(t == *this); }
00123 
00124   Tm& operator-=(int seconds) {
00125     setToMicrosTime(microsTime() - seconds * 1e6);
00126     return *this;
00127   }
00128 
00129   Tm& operator+=(int seconds) {
00130     setToMicrosTime(microsTime() + seconds * 1e6);
00131     return *this;
00132   }
00133 
00134   const Tm operator+(int seconds) {
00135     Tm ret = *this;
00136     ret += seconds;
00137     return ret;
00138   }
00139 
00140   friend std::ostream& operator<< (std::ostream &out, const Tm &t);
00141 
00142  private:
00143   struct tm m_tm;
00144 };
00145 
00146 #endif