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