Timestamp.cpp
Go to the documentation of this file.
00001 /*
00002  * File: Timestamp.cpp
00003  * Author: Dorian Galvez-Lopez
00004  * Date: March 2009
00005  * Description: timestamping functions
00006  * 
00007  * Note: in windows, this class has a 1ms resolution
00008  *
00009  * 
00010  * This program is free software: you can redistribute it and/or modify
00011  * it under the terms of the GNU Lesser General Public License as published by
00012  * the Free Software Foundation, either version 3 of the License, or
00013  * any later version.
00014  *
00015  * This program is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  * GNU Lesser General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU Lesser General Public License
00021  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00022  *
00023  */
00024 
00025 #include <cstdio>
00026 #include <cstdlib>
00027 #include <ctime>
00028 #include <cmath>
00029 #include <sstream>
00030 #include <iomanip>
00031 
00032 #ifdef WIN32
00033 #include <sys/timeb.h>
00034 #define sprintf sprintf_s
00035 #else
00036 #include <sys/time.h>
00037 #endif
00038 
00039 #include "Timestamp.h"
00040 
00041 using namespace std;
00042 
00043 using namespace DUtils;
00044 
00045 Timestamp::Timestamp(Timestamp::tOptions option)
00046 {
00047   if(option | CURRENT_TIME)
00048     setToCurrentTime();
00049 }
00050 
00051 Timestamp::~Timestamp(void)
00052 {
00053 }
00054 
00055 void Timestamp::setToCurrentTime(){
00056         
00057 #ifdef WIN32
00058         struct __timeb32 timebuffer;
00059         _ftime32_s( &timebuffer ); // C4996
00060         // Note: _ftime is deprecated; consider using _ftime_s instead
00061         m_secs = timebuffer.time;
00062         m_usecs = timebuffer.millitm * 1000;
00063 #else
00064         struct timeval now;
00065         gettimeofday(&now, NULL);
00066         m_secs = now.tv_sec;
00067         m_usecs = now.tv_usec;
00068 #endif
00069 
00070 }
00071 
00072 void Timestamp::setTime(const string &stime){
00073         string::size_type p = stime.find('.');
00074         if(p == string::npos){
00075                 m_secs = atol(stime.c_str());
00076         }else{
00077                 m_secs = atol(stime.substr(0, p).c_str());
00078                 
00079                 string s_usecs = stime.substr(p+1, 6);
00080                 m_usecs = atol(stime.substr(p+1).c_str());
00081                 m_usecs *= (unsigned long)pow(10.0, double(6 - s_usecs.length()));
00082         }
00083 }
00084 
00085 double Timestamp::getFloatTime() const {
00086         return double(m_secs) + double(m_usecs)/1000000.0;
00087 }
00088 
00089 string Timestamp::getStringTime() const {
00090         char buf[32];
00091         sprintf(buf, "%.6lf", this->getFloatTime());
00092         return string(buf);
00093 }
00094 
00095 double Timestamp::operator- (const Timestamp &t) const {
00096         return this->getFloatTime() - t.getFloatTime();
00097 }
00098 
00099 Timestamp Timestamp::operator+ (double s) const
00100 {
00101         unsigned long secs = (long)floor(s);
00102         unsigned long usecs = (long)((s - (double)secs) * 1e6);
00103 
00104         Timestamp t;
00105 
00106         const unsigned long max = 1000000ul;
00107 
00108         if(m_usecs + usecs >= max)
00109                 t.setTime(m_secs + secs + 1, m_usecs + usecs - max);
00110         else
00111                 t.setTime(m_secs + secs, m_usecs + usecs);
00112         
00113         return t;
00114 }
00115 
00116 Timestamp Timestamp::operator- (double s) const
00117 {
00118         unsigned long secs = (long)floor(s);
00119         unsigned long usecs = (long)((s - (double)secs) * 1e6);
00120 
00121         Timestamp t;
00122 
00123         const unsigned long max = 1000000ul;
00124 
00125         if(m_usecs < usecs)
00126                 t.setTime(m_secs - secs - 1, max - (usecs - m_usecs));
00127         else
00128                 t.setTime(m_secs - secs, m_usecs - usecs);
00129         
00130         return t;
00131 }
00132 
00133 bool Timestamp::operator> (const Timestamp &t) const
00134 {
00135         if(m_secs > t.m_secs) return true;
00136         else if(m_secs == t.m_secs) return m_usecs > t.m_usecs;
00137         else return false;
00138 }
00139 
00140 bool Timestamp::operator>= (const Timestamp &t) const
00141 {
00142         if(m_secs > t.m_secs) return true;
00143         else if(m_secs == t.m_secs) return m_usecs >= t.m_usecs;
00144         else return false;
00145 }
00146 
00147 bool Timestamp::operator< (const Timestamp &t) const
00148 {
00149         if(m_secs < t.m_secs) return true;
00150         else if(m_secs == t.m_secs) return m_usecs < t.m_usecs;
00151         else return false;
00152 }
00153 
00154 bool Timestamp::operator<= (const Timestamp &t) const
00155 {
00156         if(m_secs < t.m_secs) return true;
00157         else if(m_secs == t.m_secs) return m_usecs <= t.m_usecs;
00158         else return false;
00159 }
00160 
00161 bool Timestamp::operator== (const Timestamp &t) const
00162 {
00163         return(m_secs == t.m_secs && m_usecs == t.m_usecs);
00164 }
00165 
00166 
00167 string Timestamp::Format(bool machine_friendly) const 
00168 {
00169   struct tm tm_time;
00170 
00171   time_t t = (time_t)getFloatTime();
00172   localtime_r(&t, &tm_time);
00173   
00174   char buffer[128];
00175   
00176   if(machine_friendly)
00177   {
00178     strftime(buffer, 128, "%Y%m%d_%H%M%S", &tm_time);
00179   }
00180   else
00181   {
00182     strftime(buffer, 128, "%c", &tm_time); // Thu Aug 23 14:55:02 2001
00183   }
00184   
00185   return string(buffer);
00186 
00187 /*
00188 #else
00189   #warning "Timestamp::Format is not completely supported in Windows"
00190         return Timestamp::Format(getFloatTime());
00191 #endif
00192 */
00193 }
00194 
00195 string Timestamp::Format(double s) {
00196         int days = int(s / (24. * 3600.0));
00197         s -= days * (24. * 3600.0);
00198         int hours = int(s / 3600.0);
00199         s -= hours * 3600;
00200         int minutes = int(s / 60.0);
00201         s -= minutes * 60;
00202         int seconds = int(s);
00203         int ms = int((s - seconds)*1e6);
00204 
00205         stringstream ss;
00206         ss.fill('0');
00207         bool b;
00208         if((b = (days > 0))) ss << days << "d ";
00209         if((b = (b || hours > 0))) ss << setw(2) << hours << ":";
00210         if((b = (b || minutes > 0))) ss << setw(2) << minutes << ":";
00211         if(b) ss << setw(2);
00212         ss << seconds;
00213         if(!b) ss << "." << setw(6) << ms;
00214 
00215         return ss.str();
00216 }
00217 
00218 


re_vision
Author(s): Dorian Galvez-Lopez
autogenerated on Sun Jan 5 2014 11:33:08