CMS 3D CMS Logo

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