00001
00002
00003 #include <time.h>
00004 #include <iostream>
00005 #include <string>
00006 #include <stdexcept>
00007 #include <math.h>
00008
00009 #include "OnlineDB/EcalCondDB/interface/Tm.h"
00010
00011 using namespace std;
00012
00013
00014 Tm::Tm()
00015 {
00016 this->setNull();
00017 }
00018
00019
00020
00021
00022 Tm::Tm(struct tm* initTm)
00023 {
00024 m_tm = *initTm;
00025 }
00026
00027 Tm::Tm(uint64_t micros)
00028 {
00029 this->setNull();
00030 this->setToMicrosTime(micros);
00031 }
00032
00033
00034
00035 Tm::~Tm()
00036 {
00037 }
00038
00039
00040
00041 struct tm Tm::c_tm() const
00042 {
00043 return m_tm;
00044 }
00045
00046
00047
00048 int Tm::isNull() const
00049 {
00050 if (m_tm.tm_year == 0
00051 && m_tm.tm_mon == 0
00052 && m_tm.tm_mday == 0 ) {
00053 return 1;
00054 } else { return 0; }
00055 }
00056
00057
00058
00059 void Tm::setNull()
00060 {
00061 m_tm.tm_hour = 0;
00062 m_tm.tm_isdst = 0;
00063 m_tm.tm_mday = 0;
00064 m_tm.tm_min = 0;
00065 m_tm.tm_mon = 0;
00066 m_tm.tm_sec = 0;
00067 m_tm.tm_wday = 0;
00068 m_tm.tm_yday = 0;
00069 m_tm.tm_year = 0;
00070 }
00071
00072
00073
00074 string Tm::str() const
00075 {
00076 if (this->isNull()) {
00077 return "";
00078 }
00079
00080 char timebuf[20] = "";
00081 strftime(timebuf, 20, "%Y-%m-%d %H:%M:%S", &m_tm);
00082 return string(timebuf);
00083 }
00084
00085
00086
00087 uint64_t Tm::microsTime() const
00088 {
00089 uint64_t result = 0;
00090
00091 result += (uint64_t)ceil((m_tm.tm_year + 1900 - 1970) * 365.25) * 24 * 3600;
00092 result += m_tm.tm_yday * 24 * 3600;
00093 result += m_tm.tm_hour * 3600;
00094 result += m_tm.tm_min * 60;
00095 result += m_tm.tm_sec;
00096
00097 return (uint64_t) (result * 1000000);
00098 }
00099
00100 void Tm::setToMicrosTime(uint64_t micros)
00101 {
00102 time_t t = micros / 1000000;
00103 m_tm = *gmtime(&t);
00104 }
00105
00106 void Tm::setToCurrentLocalTime()
00107 {
00108 time_t t = time(NULL);
00109 m_tm = *localtime( &t );
00110 }
00111
00112 void Tm::setToCurrentGMTime()
00113 {
00114 time_t t = time(NULL);
00115 m_tm = *gmtime( &t );
00116 }
00117
00118 void Tm::setToLocalTime(time_t t)
00119 {
00120 m_tm = *localtime( &t );
00121 }
00122
00123 void Tm::setToGMTime(time_t t)
00124 {
00125 m_tm = *gmtime( &t );
00126 }
00127
00128 void Tm::setToString(const string s)
00129 throw(runtime_error)
00130 {
00131 sscanf(s.c_str(), "%04d-%02d-%02d %02d:%02d:%02d",
00132 &m_tm.tm_year, &m_tm.tm_mon, &m_tm.tm_mday,
00133 &m_tm.tm_hour, &m_tm.tm_min, &m_tm.tm_sec);
00134
00135 try {
00136 if (m_tm.tm_year > 9999 || m_tm.tm_year < 1970) {
00137 throw(runtime_error("Year out of bounds"));
00138 } else if (m_tm.tm_mon > 12 || m_tm.tm_mon < 1) {
00139 throw(runtime_error("Month out of bounds"));
00140 } else if (m_tm.tm_mday > 31 || m_tm.tm_mday < 1) {
00141 throw(runtime_error("Day out of bounds"));
00142 } else if (m_tm.tm_hour > 23 || m_tm.tm_mday < 0) {
00143 throw(runtime_error("Hour out of bounds"));
00144 } else if (m_tm.tm_min > 59 || m_tm.tm_min < 0) {
00145 throw(runtime_error("Minute out of bounds"));
00146 } else if (m_tm.tm_sec > 59 || m_tm.tm_sec < 0) {
00147 throw(runtime_error("Day out of bounds"));
00148 }
00149
00150 m_tm.tm_year -= 1900;
00151 m_tm.tm_mon -= 1;
00152 } catch (runtime_error &e) {
00153 this->setNull();
00154 string msg("Tm::setToString(): ");
00155 msg.append(e.what());
00156 throw(runtime_error(msg));
00157 }
00158 }
00159
00160 void Tm::dumpTm()
00161 {
00162 cout << "=== dumpTm() ===" << endl;
00163 cout << "tm_year " << m_tm.tm_year << endl;
00164 cout << "tm_mon " << m_tm.tm_mon << endl;
00165 cout << "tm_mday " << m_tm.tm_mday << endl;
00166 cout << "tm_hour " << m_tm.tm_hour << endl;
00167 cout << "tm_min " << m_tm.tm_min << endl;
00168 cout << "tm_sec " << m_tm.tm_sec << endl;
00169 cout << "tm_yday " << m_tm.tm_yday << endl;
00170 cout << "tm_wday " << m_tm.tm_wday << endl;
00171 cout << "tm_isdst " << m_tm.tm_isdst << endl;
00172 cout << "================" << endl;
00173 }