CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_1/src/OnlineDB/EcalCondDB/src/Tm.cc

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 // Default Constructor
00024 Tm::Tm()
00025 {
00026   this->setNull();
00027 }
00028 
00029 
00030 
00031 // Initialized Constructor
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     //micros = PLUS_INF_MICROS;
00042     this->setToCmsNanoTime(micros);
00043   } else {
00044     this->setToMicrosTime(micros);
00045   }
00046 }
00047 
00048 
00049 // Destructor
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::unixTime() const 
00122 {
00123   return microsTime()/1000000;
00124 }
00125 
00126 uint64_t Tm::microsTime() const
00127 {
00128   uint64_t result = 0;
00129   /*  
00130   result += (uint64_t)ceil((m_tm.tm_year - 70 ) * 365.25) * 24 * 3600;
00131   result += (m_tm.tm_yday) * 24 * 3600;
00132   result += m_tm.tm_hour * 3600;
00133   result += m_tm.tm_min * 60;
00134   result += m_tm.tm_sec;
00135   return (uint64_t) (result * 1000000);
00136   */
00137 
00138   struct tm time_struct;
00139   time_struct.tm_year=1970-1900;
00140   time_struct.tm_mon=0;
00141   time_struct.tm_mday=1;
00142   time_struct.tm_sec=0;
00143   time_struct.tm_min=0;
00144   time_struct.tm_hour=0;
00145   time_struct.tm_isdst=0;
00146   
00147   time_t t1970=mktime(&time_struct);
00148   tm s = m_tm;
00149   time_t t_this=mktime(&s);
00150 
00151   double x= difftime(t_this,t1970); 
00152   result =(uint64_t) x*1000000;
00153 
00154   return result; 
00155 
00156 }
00157 
00158 void Tm::setToCmsNanoTime(uint64_t nanos) {
00159   setToMicrosTime((nanos >> 32) * 1000000);
00160 }
00161 
00162 void Tm::setToMicrosTime(uint64_t micros)
00163 {
00164   time_t t = micros / 1000000;
00165   if (t >= INT_MAX) {
00166     t = INT_MAX;
00167   }
00168   m_tm = *gmtime(&t);
00169 }
00170 
00171 void Tm::setToCurrentLocalTime()
00172 {
00173   time_t t = time(NULL);
00174   m_tm = *localtime( &t );
00175 }
00176 
00177 void Tm::setToCurrentGMTime()
00178 {
00179   time_t t = time(NULL);
00180   m_tm = *gmtime( &t );
00181 }
00182 
00183 void Tm::setToLocalTime(time_t t)
00184 {
00185   m_tm = *localtime( &t );
00186 }
00187 
00188 void Tm::setToGMTime(time_t t)
00189 {
00190   m_tm = *gmtime( &t );
00191 }
00192 
00193 void Tm::setToString(const string s)
00194   throw(std::runtime_error)
00195 {
00196   sscanf(s.c_str(), "%04d-%02d-%02d %02d:%02d:%02d", 
00197          &m_tm.tm_year, &m_tm.tm_mon, &m_tm.tm_mday,
00198          &m_tm.tm_hour, &m_tm.tm_min, &m_tm.tm_sec);
00199 
00200   try {
00201     if (m_tm.tm_year > 9999 || m_tm.tm_year < 1900) {
00202       throw(std::runtime_error("Year out of bounds"));
00203     } else if (m_tm.tm_mon > 12 || m_tm.tm_mon < 1) {
00204       throw(std::runtime_error("Month out of bounds"));
00205     } else if (m_tm.tm_mday > 31 || m_tm.tm_mday < 1) {
00206       throw(std::runtime_error("Day out of bounds"));
00207     } else if (m_tm.tm_hour > 23 || m_tm.tm_mday < 0) {
00208       throw(std::runtime_error("Hour out of bounds"));
00209     } else if (m_tm.tm_min > 59 || m_tm.tm_min < 0) {
00210       throw(std::runtime_error("Minute out of bounds"));
00211     } else if (m_tm.tm_sec > 59 || m_tm.tm_sec < 0) {
00212       throw(std::runtime_error("Day out of bounds"));
00213     }
00214 
00215     if (m_tm.tm_year >= 2038) {
00216       // take into account UNIX time limits
00217       m_tm.tm_year = 2038;
00218       if (m_tm.tm_mon > 1) {
00219         m_tm.tm_mon = 1;
00220       }
00221       if (m_tm.tm_mday > 19) {
00222         m_tm.tm_mday = 19;
00223       }
00224       if (m_tm.tm_hour > 3) {
00225         m_tm.tm_hour = 3;
00226       }
00227       if (m_tm.tm_min > 14) {
00228         m_tm.tm_min = 14;
00229       }
00230       if (m_tm.tm_sec > 7) {
00231         m_tm.tm_sec = 7;
00232       }
00233     }
00234     m_tm.tm_year -= 1900;
00235     m_tm.tm_mon -= 1;
00236   } catch (std::runtime_error &e) {
00237     this->setNull();
00238     string msg("Tm::setToString():  ");
00239     msg.append(e.what());
00240     throw(std::runtime_error(msg));
00241   }
00242 }
00243 
00244 void Tm::dumpTm()
00245 {
00246   cout << "=== dumpTm() ===" << endl;
00247   cout << "tm_year  " << m_tm.tm_year << endl;
00248   cout << "tm_mon   " << m_tm.tm_mon << endl;
00249   cout << "tm_mday  " << m_tm.tm_mday << endl;
00250   cout << "tm_hour  " << m_tm.tm_hour << endl;
00251   cout << "tm_min   " << m_tm.tm_min << endl;
00252   cout << "tm_sec   " << m_tm.tm_sec << endl;
00253   cout << "tm_yday  " << m_tm.tm_yday << endl;
00254   cout << "tm_wday  " << m_tm.tm_wday << endl;
00255   cout << "tm_isdst " << m_tm.tm_isdst << endl;
00256   cout << "================" << endl;
00257 }