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 <ctime>
9 #include <cstdint>
10 #include <climits>
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  static Tm plusInfinity() { return Tm(PLUS_INF_MICROS); };
46 
47  static Tm negInfinity() { return Tm(NEG_INF_MICROS); };
48 
49  /*
50  * String representation of Tm in YYYY-MM-DD hh:mm:ss format
51  */
52  std::string str() const;
53 
54  /*
55  * return number of microseconds since Jan 1 1970 and the epoch
56  */
57  uint64_t unixTime() const;
58  uint64_t epoch() const { return unixTime(); };
59  uint64_t microsTime() const;
60 
61  /*
62  * return the number of nanoseconds packed as a CMS time
63  */
64  uint64_t cmsNanoSeconds() const;
65 
66  /*
67  * Set self to current time
68  */
69  void setToCurrentLocalTime();
70  void setToCurrentGMTime();
71 
72  /*
73  * Set using time_t
74  */
75  void setToLocalTime(time_t t);
76  void setToGMTime(time_t t);
77 
78  /*
79  * Set using microseconds and CMS times
80  */
81  void setToMicrosTime(uint64_t micros);
82  void setToCmsNanoTime(uint64_t nanos);
83 
84  /*
85  * Set to string of format YYYY-MM-DD HH:MM:SS
86  */
87  void setToString(const std::string s) noexcept(false);
88 
89  void dumpTm();
90 
91  inline bool operator<(const Tm &t) const { return microsTime() < t.microsTime(); }
92 
93  inline bool operator<=(const Tm &t) const { return microsTime() <= t.microsTime(); }
94 
95  inline bool operator==(const Tm &t) const {
96  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 &&
97  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 &&
98  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);
99  }
100 
101  inline bool operator!=(const Tm &t) const { return !(t == *this); }
102 
105  return *this;
106  }
107 
110  return *this;
111  }
112 
113  const Tm operator+(int seconds) {
114  Tm ret = *this;
115  ret += seconds;
116  return ret;
117  }
118 
119  friend std::ostream &operator<<(std::ostream &out, const Tm &t);
120 
121 private:
122  struct tm m_tm;
123 };
124 
125 #endif
uint64_t cmsNanoSeconds() const
Definition: Tm.cc:92
bool operator!=(const Tm &t) const
Definition: Tm.h:101
static const uint64_t NEG_INF_MICROS
Definition: Tm.h:14
double seconds()
bool operator<=(const Tm &t) const
Definition: Tm.h:93
uint64_t epoch() const
Definition: Tm.h:58
ret
prodAgent to be discontinued
bool operator==(const Tm &t) const
Definition: Tm.h:95
virtual ~Tm()
Definition: Tm.cc:40
void setToCurrentLocalTime()
Definition: Tm.cc:136
Tm & operator-=(int seconds)
Definition: Tm.h:103
struct tm c_tm() const
Definition: Tm.cc:42
friend std::ostream & operator<<(std::ostream &out, const Tm &t)
Definition: Tm.cc:18
void setToCurrentGMTime()
Definition: Tm.cc:141
bool operator<(const Tm &t) const
Definition: Tm.h:91
uint64_t unixTime() const
Definition: Tm.cc:94
void setToMicrosTime(uint64_t micros)
Definition: Tm.cc:128
void setToCmsNanoTime(uint64_t nanos)
Definition: Tm.cc:126
Tm & operator+=(int seconds)
Definition: Tm.h:108
void setToGMTime(time_t t)
Definition: Tm.cc:148
int isNull() const
Definition: Tm.cc:46
void setToString(const std::string s) noexcept(false)
Definition: Tm.cc:150
unsigned long long uint64_t
Definition: Time.h:13
void setToLocalTime(time_t t)
Definition: Tm.cc:146
uint64_t microsTime() const
Definition: Tm.cc:96
const Tm operator+(int seconds)
Definition: Tm.h:113
static const uint64_t PLUS_INF_MICROS
Definition: Tm.h:16
void dumpTm()
Definition: Tm.cc:204
std::string str() const
Definition: Tm.cc:66
Definition: Tm.h:13
Tm()
Definition: Tm.cc:24
static Tm plusInfinity()
Definition: Tm.h:45
void setNull()
Definition: Tm.cc:54
static Tm negInfinity()
Definition: Tm.h:47
struct tm m_tm
Definition: Tm.h:122