Go to the documentation of this file.00001 #include <time.h>
00002 #include <iostream>
00003 #include <string>
00004 #include <stdexcept>
00005 #include <math.h>
00006 #include <cstdio>
00007
00008 #include "OnlineDB/EcalCondDB/interface/Tm.h"
00009
00010 using namespace std;
00011
00018 std::ostream& operator<<(std::ostream &out, const Tm &t) {
00019 out << t.str();
00020 return out;
00021 }
00022
00023
00024 Tm::Tm()
00025 {
00026 this->setNull();
00027 }
00028
00029
00030
00031
00032 Tm::Tm(struct tm* initTm)
00033 {
00034 m_tm = *initTm;
00035 }
00036
00037 Tm::Tm(uint64_t micros)
00038 {
00039 this->setNull();
00040 if (micros > PLUS_INF_MICROS) {
00041
00042 this->setToCmsNanoTime(micros);
00043 } else {
00044 this->setToMicrosTime(micros);
00045 }
00046 }
00047
00048
00049
00050 Tm::~Tm()
00051 {
00052 }
00053
00054
00055
00056 struct tm Tm::c_tm() const
00057 {
00058 return m_tm;
00059 }
00060
00061
00062
00063 int Tm::isNull() const
00064 {
00065 if (m_tm.tm_year == 0
00066 && m_tm.tm_mon == 0
00067 && m_tm.tm_mday == 0 ) {
00068 return 1;
00069 } else { return 0; }
00070 }
00071
00072
00073
00074 void Tm::setNull()
00075 {
00076 m_tm.tm_hour = 0;
00077 m_tm.tm_isdst = 0;
00078 m_tm.tm_mday = 0;
00079 m_tm.tm_min = 0;
00080 m_tm.tm_mon = 0;
00081 m_tm.tm_sec = 0;
00082 m_tm.tm_wday = 0;
00083 m_tm.tm_yday = 0;
00084 m_tm.tm_year = 0;
00085 }
00086
00087
00088
00089 string Tm::str() const
00090 {
00091 if (this->isNull()) {
00092 return "";
00093 }
00094
00104 char timebuf[20] = "";
00105 if (this->microsTime() >= PLUS_INF_MICROS) {
00106 sprintf(timebuf, "9999-12-12 23:59:59");
00107 } else {
00108 Tm dummy_Tm;
00109 dummy_Tm.setToGMTime(this->microsTime()/1000000);
00110 struct tm dummy_tm = dummy_Tm.c_tm();
00111 strftime(timebuf, 20, "%Y-%m-%d %H:%M:%S", &dummy_tm);
00112 }
00113 return string(timebuf);
00114 }
00115
00116 uint64_t Tm::cmsNanoSeconds() const
00117 {
00118 return microsTime()/1000000 << 32;
00119 }
00120
00121 uint64_t Tm::microsTime() const
00122 {
00123 uint64_t result = 0;
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133 struct tm time_struct;
00134 time_struct.tm_year=1970-1900;
00135 time_struct.tm_mon=0;
00136 time_struct.tm_mday=1;
00137 time_struct.tm_sec=0;
00138 time_struct.tm_min=0;
00139 time_struct.tm_hour=0;
00140 time_struct.tm_isdst=0;
00141
00142 time_t t1970=mktime(&time_struct);
00143 tm s = m_tm;
00144 time_t t_this=mktime(&s);
00145
00146 double x= difftime(t_this,t1970);
00147 result =(uint64_t) x*1000000;
00148
00149 return result;
00150
00151 }
00152
00153 void Tm::setToCmsNanoTime(uint64_t nanos) {
00154 setToMicrosTime((nanos >> 32) * 1000000);
00155 }
00156
00157 void Tm::setToMicrosTime(uint64_t micros)
00158 {
00159 time_t t = micros / 1000000;
00160 if (t >= INT_MAX) {
00161 t = INT_MAX;
00162 }
00163 m_tm = *gmtime(&t);
00164 }
00165
00166 void Tm::setToCurrentLocalTime()
00167 {
00168 time_t t = time(NULL);
00169 m_tm = *localtime( &t );
00170 }
00171
00172 void Tm::setToCurrentGMTime()
00173 {
00174 time_t t = time(NULL);
00175 m_tm = *gmtime( &t );
00176 }
00177
00178 void Tm::setToLocalTime(time_t t)
00179 {
00180 m_tm = *localtime( &t );
00181 }
00182
00183 void Tm::setToGMTime(time_t t)
00184 {
00185 m_tm = *gmtime( &t );
00186 }
00187
00188 void Tm::setToString(const string s)
00189 throw(std::runtime_error)
00190 {
00191 sscanf(s.c_str(), "%04d-%02d-%02d %02d:%02d:%02d",
00192 &m_tm.tm_year, &m_tm.tm_mon, &m_tm.tm_mday,
00193 &m_tm.tm_hour, &m_tm.tm_min, &m_tm.tm_sec);
00194
00195 try {
00196 if (m_tm.tm_year > 9999 || m_tm.tm_year < 1900) {
00197 throw(std::runtime_error("Year out of bounds"));
00198 } else if (m_tm.tm_mon > 12 || m_tm.tm_mon < 1) {
00199 throw(std::runtime_error("Month out of bounds"));
00200 } else if (m_tm.tm_mday > 31 || m_tm.tm_mday < 1) {
00201 throw(std::runtime_error("Day out of bounds"));
00202 } else if (m_tm.tm_hour > 23 || m_tm.tm_mday < 0) {
00203 throw(std::runtime_error("Hour out of bounds"));
00204 } else if (m_tm.tm_min > 59 || m_tm.tm_min < 0) {
00205 throw(std::runtime_error("Minute out of bounds"));
00206 } else if (m_tm.tm_sec > 59 || m_tm.tm_sec < 0) {
00207 throw(std::runtime_error("Day out of bounds"));
00208 }
00209
00210 if (m_tm.tm_year >= 2038) {
00211
00212 m_tm.tm_year = 2038;
00213 if (m_tm.tm_mon > 1) {
00214 m_tm.tm_mon = 1;
00215 }
00216 if (m_tm.tm_mday > 19) {
00217 m_tm.tm_mday = 19;
00218 }
00219 if (m_tm.tm_hour > 3) {
00220 m_tm.tm_hour = 3;
00221 }
00222 if (m_tm.tm_min > 14) {
00223 m_tm.tm_min = 14;
00224 }
00225 if (m_tm.tm_sec > 7) {
00226 m_tm.tm_sec = 7;
00227 }
00228 }
00229 m_tm.tm_year -= 1900;
00230 m_tm.tm_mon -= 1;
00231 } catch (std::runtime_error &e) {
00232 this->setNull();
00233 string msg("Tm::setToString(): ");
00234 msg.append(e.what());
00235 throw(std::runtime_error(msg));
00236 }
00237 }
00238
00239 void Tm::dumpTm()
00240 {
00241 cout << "=== dumpTm() ===" << endl;
00242 cout << "tm_year " << m_tm.tm_year << endl;
00243 cout << "tm_mon " << m_tm.tm_mon << endl;
00244 cout << "tm_mday " << m_tm.tm_mday << endl;
00245 cout << "tm_hour " << m_tm.tm_hour << endl;
00246 cout << "tm_min " << m_tm.tm_min << endl;
00247 cout << "tm_sec " << m_tm.tm_sec << endl;
00248 cout << "tm_yday " << m_tm.tm_yday << endl;
00249 cout << "tm_wday " << m_tm.tm_wday << endl;
00250 cout << "tm_isdst " << m_tm.tm_isdst << endl;
00251 cout << "================" << endl;
00252 }