Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-03-17 11:15:07

0001 
0002 #ifndef TM_HH
0003 #define TM_HH
0004 
0005 #include <stdexcept>
0006 #include <string>
0007 #include <iostream>
0008 #include <ctime>
0009 #include <cstdint>
0010 #include <climits>
0011 
0012 // Wrapper class for time.h tm struct
0013 class Tm {
0014   static const uint64_t NEG_INF_MICROS = 0;
0015   // GO: maximum UNIX time
0016   static const uint64_t PLUS_INF_MICROS = (uint64_t)INT_MAX * 1000000;
0017 
0018 public:
0019   // Default constructor makes a null Tm
0020   Tm();
0021 
0022   // Initialized constructor
0023   Tm(struct tm *initTm);
0024 
0025   Tm(uint64_t micros);
0026 
0027   // Destructor
0028   virtual ~Tm();
0029 
0030   /*
0031    *  Return a pointer to the tm data structure.
0032    */
0033   struct tm c_tm() const;
0034 
0035   /*
0036    *  Returns true if this Tm is null
0037    */
0038   int isNull() const;
0039 
0040   /*
0041    *  Sets this Tm to null
0042    */
0043   void setNull();
0044 
0045   static Tm plusInfinity() { return Tm(PLUS_INF_MICROS); };
0046 
0047   static Tm negInfinity() { return Tm(NEG_INF_MICROS); };
0048 
0049   /*
0050    *  String representation of Tm in YYYY-MM-DD hh:mm:ss format
0051    */
0052   std::string str() const;
0053 
0054   /*
0055    *  return number of microseconds since Jan 1 1970 and the epoch
0056    */
0057   uint64_t unixTime() const;
0058   uint64_t epoch() const { return unixTime(); };
0059   uint64_t microsTime() const;
0060 
0061   /*
0062    *  return the number of nanoseconds packed as a CMS time
0063    */
0064   uint64_t cmsNanoSeconds() const;
0065 
0066   /*
0067    *  Set self to current time
0068    */
0069   void setToCurrentLocalTime();
0070   void setToCurrentGMTime();
0071 
0072   /*
0073    *  Set using time_t
0074    */
0075   void setToLocalTime(time_t t);
0076   void setToGMTime(time_t t);
0077 
0078   /*
0079    *  Set using microseconds and CMS times
0080    */
0081   void setToMicrosTime(uint64_t micros);
0082   void setToCmsNanoTime(uint64_t nanos);
0083 
0084   /*
0085    *  Set to string of format YYYY-MM-DD HH:MM:SS
0086    */
0087   void setToString(const std::string s) noexcept(false);
0088 
0089   void dumpTm();
0090 
0091   inline bool operator<(const Tm &t) const { return microsTime() < t.microsTime(); }
0092 
0093   inline bool operator<=(const Tm &t) const { return microsTime() <= t.microsTime(); }
0094 
0095   inline bool operator==(const Tm &t) const {
0096     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 &&
0097             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 &&
0098             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);
0099   }
0100 
0101   inline bool operator!=(const Tm &t) const { return !(t == *this); }
0102 
0103   Tm &operator-=(int seconds) {
0104     setToMicrosTime(microsTime() - seconds * 1e6);
0105     return *this;
0106   }
0107 
0108   Tm &operator+=(int seconds) {
0109     setToMicrosTime(microsTime() + seconds * 1e6);
0110     return *this;
0111   }
0112 
0113   const Tm operator+(int seconds) {
0114     Tm ret = *this;
0115     ret += seconds;
0116     return ret;
0117   }
0118 
0119   friend std::ostream &operator<<(std::ostream &out, const Tm &t);
0120 
0121 private:
0122   struct tm m_tm;
0123 };
0124 
0125 #endif