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