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