CMS 3D CMS Logo

Tm.h
Go to the documentation of this file.
1 
2 #ifndef TM_HH
3 #define TM_HH
4 
5 #include <stdexcept>
6 #include <string>
7 #include <iostream>
8 #include <time.h>
9 #include <stdint.h>
10 #include <limits.h>
11 
12 // Wrapper class for time.h tm struct
13 class Tm {
14  static const uint64_t NEG_INF_MICROS = 0;
15  // GO: maximum UNIX time
16  static const uint64_t PLUS_INF_MICROS = (uint64_t)INT_MAX * 1000000;
17 
18  public:
19  // Default constructor makes a null Tm
20  Tm();
21 
22  // Initialized constructor
23  Tm(struct tm * initTm);
24 
25  Tm(uint64_t micros);
26 
27  // Destructor
28  virtual ~Tm();
29 
30  /*
31  * Return a pointer to the tm data structure.
32  */
33  struct tm c_tm() const;
34 
35  /*
36  * Returns true if this Tm is null
37  */
38  int isNull() const;
39 
40  /*
41  * Sets this Tm to null
42  */
43  void setNull();
44 
45 
46  static Tm plusInfinity()
47  {
48  return Tm(PLUS_INF_MICROS);
49  };
50 
51 
52  static Tm negInfinity()
53  {
54  return Tm(NEG_INF_MICROS);
55  };
56 
57  /*
58  * String representation of Tm in YYYY-MM-DD hh:mm:ss format
59  */
60  std::string str() const;
61 
62  /*
63  * return number of microseconds since Jan 1 1970 and the epoch
64  */
65  uint64_t unixTime() const;
66  uint64_t epoch() const { return unixTime(); };
67  uint64_t microsTime() const;
68 
69  /*
70  * return the number of nanoseconds packed as a CMS time
71  */
72  uint64_t cmsNanoSeconds() const;
73 
74  /*
75  * Set self to current time
76  */
77  void setToCurrentLocalTime();
78  void setToCurrentGMTime();
79 
80  /*
81  * Set using time_t
82  */
83  void setToLocalTime( time_t t);
84  void setToGMTime( time_t t );
85 
86  /*
87  * Set using microseconds and CMS times
88  */
89  void setToMicrosTime(uint64_t micros);
90  void setToCmsNanoTime(uint64_t nanos);
91 
92  /*
93  * Set to string of format YYYY-MM-DD HH:MM:SS
94  */
95  void setToString(const std::string s) noexcept(false);
96 
97  void dumpTm();
98 
99  inline bool operator<(const Tm &t) const
100  {
101  return microsTime() < t.microsTime();
102  }
103 
104  inline bool operator<=(const Tm &t) const
105  {
106  return microsTime() <= t.microsTime();
107  }
108 
109  inline bool operator==(const Tm &t) const
110  { return (m_tm.tm_hour == t.m_tm.tm_hour &&
111  m_tm.tm_isdst == t.m_tm.tm_isdst &&
112  m_tm.tm_mday == t.m_tm.tm_mday &&
113  m_tm.tm_min == t.m_tm.tm_min &&
114  m_tm.tm_mon == t.m_tm.tm_mon &&
115  m_tm.tm_sec == t.m_tm.tm_sec &&
116  m_tm.tm_wday == t.m_tm.tm_wday &&
117  m_tm.tm_yday == t.m_tm.tm_yday &&
118  m_tm.tm_year == t.m_tm.tm_year);
119  }
120 
121  inline bool operator!=(const Tm &t) const { return !(t == *this); }
122 
124  setToMicrosTime(microsTime() - seconds * 1e6);
125  return *this;
126  }
127 
129  setToMicrosTime(microsTime() + seconds * 1e6);
130  return *this;
131  }
132 
133  const Tm operator+(int seconds) {
134  Tm ret = *this;
135  ret += seconds;
136  return ret;
137  }
138 
139  friend std::ostream& operator<< (std::ostream &out, const Tm &t);
140 
141  private:
142  struct tm m_tm;
143 };
144 
145 #endif
static const uint64_t NEG_INF_MICROS
Definition: Tm.h:14
double seconds()
#define noexcept
virtual ~Tm()
Definition: Tm.cc:50
void setToCurrentLocalTime()
Definition: Tm.cc:171
Tm & operator-=(int seconds)
Definition: Tm.h:123
friend std::ostream & operator<<(std::ostream &out, const Tm &t)
Definition: Tm.cc:18
void setToCurrentGMTime()
Definition: Tm.cc:177
struct tm c_tm() const
Definition: Tm.cc:56
uint64_t microsTime() const
Definition: Tm.cc:126
void setToMicrosTime(uint64_t micros)
Definition: Tm.cc:162
void setToCmsNanoTime(uint64_t nanos)
Definition: Tm.cc:158
uint64_t unixTime() const
Definition: Tm.cc:121
bool operator!=(const Tm &t) const
Definition: Tm.h:121
Tm & operator+=(int seconds)
Definition: Tm.h:128
bool operator<(const Tm &t) const
Definition: Tm.h:99
void setToGMTime(time_t t)
Definition: Tm.cc:188
uint64_t cmsNanoSeconds() const
Definition: Tm.cc:116
bool operator==(const Tm &t) const
Definition: Tm.h:109
void setToString(const std::string s) noexcept(false)
Definition: Tm.cc:193
unsigned long long uint64_t
Definition: Time.h:15
void setToLocalTime(time_t t)
Definition: Tm.cc:183
uint64_t epoch() const
Definition: Tm.h:66
std::string str() const
Definition: Tm.cc:89
int isNull() const
Definition: Tm.cc:63
const Tm operator+(int seconds)
Definition: Tm.h:133
static const uint64_t PLUS_INF_MICROS
Definition: Tm.h:16
void dumpTm()
Definition: Tm.cc:244
bool operator<=(const Tm &t) const
Definition: Tm.h:104
Definition: Tm.h:13
Tm()
Definition: Tm.cc:24
static Tm plusInfinity()
Definition: Tm.h:46
void setNull()
Definition: Tm.cc:74
static Tm negInfinity()
Definition: Tm.h:52
struct tm m_tm
Definition: Tm.h:142