CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Tm.cc
Go to the documentation of this file.
1 #include <time.h>
2 #include <iostream>
3 #include <string>
4 #include <stdexcept>
5 #include <math.h>
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
25 {
26  this->setNull();
27 }
28 
29 
30 
31 // Initialized Constructor
32 Tm::Tm(struct tm* initTm)
33 {
34  m_tm = *initTm;
35 }
36 
38 {
39  this->setNull();
40  if (micros > PLUS_INF_MICROS) {
41  //micros = PLUS_INF_MICROS;
42  this->setToCmsNanoTime(micros);
43  } else {
44  this->setToMicrosTime(micros);
45  }
46 }
47 
48 
49 // Destructor
51 {
52 }
53 
54 
55 
56 struct tm Tm::c_tm() const
57 {
58  return m_tm;
59 }
60 
61 
62 
63 int Tm::isNull() const
64 {
65  if (m_tm.tm_year == 0
66  && m_tm.tm_mon == 0
67  && m_tm.tm_mday == 0 ) {
68  return 1;
69  } else { return 0; }
70 }
71 
72 
73 
75 {
76  m_tm.tm_hour = 0;
77  m_tm.tm_isdst = 0;
78  m_tm.tm_mday = 0;
79  m_tm.tm_min = 0;
80  m_tm.tm_mon = 0;
81  m_tm.tm_sec = 0;
82  m_tm.tm_wday = 0;
83  m_tm.tm_yday = 0;
84  m_tm.tm_year = 0;
85 }
86 
87 
88 
89 string Tm::str() const
90 {
91  if (this->isNull()) {
92  return "";
93  }
94 
104  char timebuf[20] = "";
105  if (this->microsTime() >= PLUS_INF_MICROS) {
106  sprintf(timebuf, "9999-12-12 23:59:59");
107  } else {
108  Tm dummy_Tm;
109  dummy_Tm.setToGMTime(this->microsTime()/1000000);
110  struct tm dummy_tm = dummy_Tm.c_tm();
111  strftime(timebuf, 20, "%Y-%m-%d %H:%M:%S", &dummy_tm);
112  }
113  return string(timebuf);
114 }
115 
117 {
118  return microsTime()/1000000 << 32;
119 }
120 
122 {
123  return microsTime()/1000000;
124 }
125 
127 {
128  uint64_t result = 0;
129  /*
130  result += (uint64_t)ceil((m_tm.tm_year - 70 ) * 365.25) * 24 * 3600;
131  result += (m_tm.tm_yday) * 24 * 3600;
132  result += m_tm.tm_hour * 3600;
133  result += m_tm.tm_min * 60;
134  result += m_tm.tm_sec;
135  return (uint64_t) (result * 1000000);
136  */
137 
138  struct tm time_struct;
139  time_struct.tm_year=1970-1900;
140  time_struct.tm_mon=0;
141  time_struct.tm_mday=1;
142  time_struct.tm_sec=0;
143  time_struct.tm_min=0;
144  time_struct.tm_hour=0;
145  time_struct.tm_isdst=0;
146 
147  time_t t1970=mktime(&time_struct);
148  tm s = m_tm;
149  time_t t_this=mktime(&s);
150 
151  double x= difftime(t_this,t1970);
152  result =(uint64_t) x*1000000;
153 
154  return result;
155 
156 }
157 
159  setToMicrosTime((nanos >> 32) * 1000000);
160 }
161 
163 {
164  time_t t = micros / 1000000;
165  if (t >= INT_MAX) {
166  t = INT_MAX;
167  }
168  m_tm = *gmtime(&t);
169 }
170 
172 {
173  time_t t = time(NULL);
174  m_tm = *localtime( &t );
175 }
176 
178 {
179  time_t t = time(NULL);
180  m_tm = *gmtime( &t );
181 }
182 
183 void Tm::setToLocalTime(time_t t)
184 {
185  m_tm = *localtime( &t );
186 }
187 
188 void Tm::setToGMTime(time_t t)
189 {
190  m_tm = *gmtime( &t );
191 }
192 
193 void Tm::setToString(const string s)
194  throw(std::runtime_error)
195 {
196  sscanf(s.c_str(), "%04d-%02d-%02d %02d:%02d:%02d",
197  &m_tm.tm_year, &m_tm.tm_mon, &m_tm.tm_mday,
198  &m_tm.tm_hour, &m_tm.tm_min, &m_tm.tm_sec);
199 
200  try {
201  if (m_tm.tm_year > 9999 || m_tm.tm_year < 1900) {
202  throw(std::runtime_error("Year out of bounds"));
203  } else if (m_tm.tm_mon > 12 || m_tm.tm_mon < 1) {
204  throw(std::runtime_error("Month out of bounds"));
205  } else if (m_tm.tm_mday > 31 || m_tm.tm_mday < 1) {
206  throw(std::runtime_error("Day out of bounds"));
207  } else if (m_tm.tm_hour > 23 || m_tm.tm_mday < 0) {
208  throw(std::runtime_error("Hour out of bounds"));
209  } else if (m_tm.tm_min > 59 || m_tm.tm_min < 0) {
210  throw(std::runtime_error("Minute out of bounds"));
211  } else if (m_tm.tm_sec > 59 || m_tm.tm_sec < 0) {
212  throw(std::runtime_error("Day out of bounds"));
213  }
214 
215  if (m_tm.tm_year >= 2038) {
216  // take into account UNIX time limits
217  m_tm.tm_year = 2038;
218  if (m_tm.tm_mon > 1) {
219  m_tm.tm_mon = 1;
220  }
221  if (m_tm.tm_mday > 19) {
222  m_tm.tm_mday = 19;
223  }
224  if (m_tm.tm_hour > 3) {
225  m_tm.tm_hour = 3;
226  }
227  if (m_tm.tm_min > 14) {
228  m_tm.tm_min = 14;
229  }
230  if (m_tm.tm_sec > 7) {
231  m_tm.tm_sec = 7;
232  }
233  }
234  m_tm.tm_year -= 1900;
235  m_tm.tm_mon -= 1;
236  } catch (std::runtime_error &e) {
237  this->setNull();
238  string msg("Tm::setToString(): ");
239  msg.append(e.what());
240  throw(std::runtime_error(msg));
241  }
242 }
243 
245 {
246  cout << "=== dumpTm() ===" << endl;
247  cout << "tm_year " << m_tm.tm_year << endl;
248  cout << "tm_mon " << m_tm.tm_mon << endl;
249  cout << "tm_mday " << m_tm.tm_mday << endl;
250  cout << "tm_hour " << m_tm.tm_hour << endl;
251  cout << "tm_min " << m_tm.tm_min << endl;
252  cout << "tm_sec " << m_tm.tm_sec << endl;
253  cout << "tm_yday " << m_tm.tm_yday << endl;
254  cout << "tm_wday " << m_tm.tm_wday << endl;
255  cout << "tm_isdst " << m_tm.tm_isdst << endl;
256  cout << "================" << endl;
257 }
tuple t
Definition: tree.py:139
#define NULL
Definition: scimark2.h:8
virtual ~Tm()
Definition: Tm.cc:50
void setToCurrentLocalTime()
Definition: Tm.cc:171
ostream & operator<<(std::ostream &o, vector< std::string > const &iValue)
Definition: refresh.cc:45
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
tuple result
Definition: query.py:137
void setToString(const std::string s)
Definition: Tm.cc:193
void setToGMTime(time_t t)
Definition: Tm.cc:188
uint64_t cmsNanoSeconds() const
Definition: Tm.cc:116
tuple out
Definition: dbtoconf.py:99
unsigned long long uint64_t
Definition: Time.h:15
void setToLocalTime(time_t t)
Definition: Tm.cc:183
string const
Definition: compareJSON.py:14
std::string str() const
Definition: Tm.cc:89
int isNull() const
Definition: Tm.cc:63
tuple cout
Definition: gather_cfg.py:121
void dumpTm()
Definition: Tm.cc:244
Definition: Tm.h:13
Tm()
Definition: Tm.cc:24
void setNull()
Definition: Tm.cc:74