CMS 3D CMS Logo

Tm.cc
Go to the documentation of this file.
1 #include <ctime>
2 #include <iostream>
3 #include <string>
4 #include <stdexcept>
5 #include <cmath>
6 #include <cstdio>
7 
9 
10 using namespace std;
11 
18 std::ostream &operator<<(std::ostream &out, const Tm &t) {
19  out << t.str();
20  return out;
21 }
22 
23 // Default Constructor
24 Tm::Tm() { this->setNull(); }
25 
26 // Initialized Constructor
27 Tm::Tm(struct tm *initTm) { m_tm = *initTm; }
28 
29 Tm::Tm(uint64_t micros) {
30  this->setNull();
31  if (micros > PLUS_INF_MICROS) {
32  //micros = PLUS_INF_MICROS;
33  this->setToCmsNanoTime(micros);
34  } else {
35  this->setToMicrosTime(micros);
36  }
37 }
38 
39 // Destructor
40 Tm::~Tm() {}
41 
42 struct tm Tm::c_tm() const {
43  return m_tm;
44 }
45 
46 int Tm::isNull() const {
47  if (m_tm.tm_year == 0 && m_tm.tm_mon == 0 && m_tm.tm_mday == 0) {
48  return 1;
49  } else {
50  return 0;
51  }
52 }
53 
54 void Tm::setNull() {
55  m_tm.tm_hour = 0;
56  m_tm.tm_isdst = 0;
57  m_tm.tm_mday = 0;
58  m_tm.tm_min = 0;
59  m_tm.tm_mon = 0;
60  m_tm.tm_sec = 0;
61  m_tm.tm_wday = 0;
62  m_tm.tm_yday = 0;
63  m_tm.tm_year = 0;
64 }
65 
66 string Tm::str() const {
67  if (this->isNull()) {
68  return "";
69  }
70 
80  char timebuf[20] = "";
81  if (this->microsTime() >= PLUS_INF_MICROS) {
82  sprintf(timebuf, "9999-12-12 23:59:59");
83  } else {
84  Tm dummy_Tm;
85  dummy_Tm.setToGMTime(this->microsTime() / 1000000);
86  struct tm dummy_tm = dummy_Tm.c_tm();
87  strftime(timebuf, 20, "%Y-%m-%d %H:%M:%S", &dummy_tm);
88  }
89  return string(timebuf);
90 }
91 
92 uint64_t Tm::cmsNanoSeconds() const { return microsTime() / 1000000 << 32; }
93 
94 uint64_t Tm::unixTime() const { return microsTime() / 1000000; }
95 
97  uint64_t result = 0;
98  /*
99  result += (uint64_t)ceil((m_tm.tm_year - 70 ) * 365.25) * 24 * 3600;
100  result += (m_tm.tm_yday) * 24 * 3600;
101  result += m_tm.tm_hour * 3600;
102  result += m_tm.tm_min * 60;
103  result += m_tm.tm_sec;
104  return (uint64_t) (result * 1000000);
105  */
106 
107  struct tm time_struct;
108  time_struct.tm_year = 1970 - 1900;
109  time_struct.tm_mon = 0;
110  time_struct.tm_mday = 1;
111  time_struct.tm_sec = 0;
112  time_struct.tm_min = 0;
113  time_struct.tm_hour = 0;
114  time_struct.tm_isdst = 0;
115 
116  time_t t1970 = mktime(&time_struct);
117  tm s = m_tm;
118  time_t t_this = mktime(&s);
119 
120  double x = difftime(t_this, t1970);
121  result = (uint64_t)x * 1000000;
122 
123  return result;
124 }
125 
126 void Tm::setToCmsNanoTime(uint64_t nanos) { setToMicrosTime((nanos >> 32) * 1000000); }
127 
129  time_t t = micros / 1000000;
130  if (t >= INT_MAX) {
131  t = INT_MAX;
132  }
133  m_tm = *gmtime(&t);
134 }
135 
137  time_t t = time(nullptr);
138  m_tm = *localtime(&t);
139 }
140 
142  time_t t = time(nullptr);
143  m_tm = *gmtime(&t);
144 }
145 
146 void Tm::setToLocalTime(time_t t) { m_tm = *localtime(&t); }
147 
148 void Tm::setToGMTime(time_t t) { m_tm = *gmtime(&t); }
149 
150 void Tm::setToString(const string s) noexcept(false) {
151  sscanf(s.c_str(),
152  "%04d-%02d-%02d %02d:%02d:%02d",
153  &m_tm.tm_year,
154  &m_tm.tm_mon,
155  &m_tm.tm_mday,
156  &m_tm.tm_hour,
157  &m_tm.tm_min,
158  &m_tm.tm_sec);
159 
160  try {
161  if (m_tm.tm_year > 9999 || m_tm.tm_year < 1900) {
162  throw(std::runtime_error("Year out of bounds"));
163  } else if (m_tm.tm_mon > 12 || m_tm.tm_mon < 1) {
164  throw(std::runtime_error("Month out of bounds"));
165  } else if (m_tm.tm_mday > 31 || m_tm.tm_mday < 1) {
166  throw(std::runtime_error("Day out of bounds"));
167  } else if (m_tm.tm_hour > 23 || m_tm.tm_mday < 0) {
168  throw(std::runtime_error("Hour out of bounds"));
169  } else if (m_tm.tm_min > 59 || m_tm.tm_min < 0) {
170  throw(std::runtime_error("Minute out of bounds"));
171  } else if (m_tm.tm_sec > 59 || m_tm.tm_sec < 0) {
172  throw(std::runtime_error("Day out of bounds"));
173  }
174 
175  if (m_tm.tm_year >= 2038) {
176  // take into account UNIX time limits
177  m_tm.tm_year = 2038;
178  if (m_tm.tm_mon > 1) {
179  m_tm.tm_mon = 1;
180  }
181  if (m_tm.tm_mday > 19) {
182  m_tm.tm_mday = 19;
183  }
184  if (m_tm.tm_hour > 3) {
185  m_tm.tm_hour = 3;
186  }
187  if (m_tm.tm_min > 14) {
188  m_tm.tm_min = 14;
189  }
190  if (m_tm.tm_sec > 7) {
191  m_tm.tm_sec = 7;
192  }
193  }
194  m_tm.tm_year -= 1900;
195  m_tm.tm_mon -= 1;
196  } catch (std::runtime_error &e) {
197  this->setNull();
198  string msg("Tm::setToString(): ");
199  msg.append(e.what());
200  throw(std::runtime_error(msg));
201  }
202 }
203 
204 void Tm::dumpTm() {
205  cout << "=== dumpTm() ===" << endl;
206  cout << "tm_year " << m_tm.tm_year << endl;
207  cout << "tm_mon " << m_tm.tm_mon << endl;
208  cout << "tm_mday " << m_tm.tm_mday << endl;
209  cout << "tm_hour " << m_tm.tm_hour << endl;
210  cout << "tm_min " << m_tm.tm_min << endl;
211  cout << "tm_sec " << m_tm.tm_sec << endl;
212  cout << "tm_yday " << m_tm.tm_yday << endl;
213  cout << "tm_wday " << m_tm.tm_wday << endl;
214  cout << "tm_isdst " << m_tm.tm_isdst << endl;
215  cout << "================" << endl;
216 }
uint64_t cmsNanoSeconds() const
Definition: Tm.cc:92
virtual ~Tm()
Definition: Tm.cc:40
void setToCurrentLocalTime()
Definition: Tm.cc:136
struct tm c_tm() const
Definition: Tm.cc:42
void setToCurrentGMTime()
Definition: Tm.cc:141
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
void setToGMTime(time_t t)
Definition: Tm.cc:148
int isNull() const
Definition: Tm.cc:46
std::ostream & operator<<(std::ostream &out, const std::tuple< Types... > &value)
Definition: Utilities.h:32
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
tuple msg
Definition: mps_check.py:285
void dumpTm()
Definition: Tm.cc:204
std::string str() const
Definition: Tm.cc:66
Definition: Tm.h:13
Tm()
Definition: Tm.cc:24
void setNull()
Definition: Tm.cc:54