fixed some issues
This commit is contained in:
246
Thirdparty/DBoW2/DUtils/Timestamp.cpp
vendored
Normal file
246
Thirdparty/DBoW2/DUtils/Timestamp.cpp
vendored
Normal file
@@ -0,0 +1,246 @@
|
||||
/*
|
||||
* File: Timestamp.cpp
|
||||
* Author: Dorian Galvez-Lopez
|
||||
* Date: March 2009
|
||||
* Description: timestamping functions
|
||||
*
|
||||
* Note: in windows, this class has a 1ms resolution
|
||||
*
|
||||
* License: see the LICENSE.txt file
|
||||
*
|
||||
*/
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
#include <cmath>
|
||||
#include <sstream>
|
||||
#include <iomanip>
|
||||
|
||||
#ifdef _WIN32
|
||||
#ifndef WIN32
|
||||
#define WIN32
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifdef WIN32
|
||||
#include <sys/timeb.h>
|
||||
#define sprintf sprintf_s
|
||||
#else
|
||||
#include <sys/time.h>
|
||||
#endif
|
||||
|
||||
#include "Timestamp.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
using namespace DUtils;
|
||||
|
||||
Timestamp::Timestamp(Timestamp::tOptions option)
|
||||
{
|
||||
if(option & CURRENT_TIME)
|
||||
setToCurrentTime();
|
||||
else if(option & ZERO)
|
||||
setTime(0.);
|
||||
}
|
||||
|
||||
Timestamp::~Timestamp(void)
|
||||
{
|
||||
}
|
||||
|
||||
bool Timestamp::empty() const
|
||||
{
|
||||
return m_secs == 0 && m_usecs == 0;
|
||||
}
|
||||
|
||||
void Timestamp::setToCurrentTime(){
|
||||
|
||||
#ifdef WIN32
|
||||
struct __timeb32 timebuffer;
|
||||
_ftime32_s( &timebuffer ); // C4996
|
||||
// Note: _ftime is deprecated; consider using _ftime_s instead
|
||||
m_secs = timebuffer.time;
|
||||
m_usecs = timebuffer.millitm * 1000;
|
||||
#else
|
||||
struct timeval now;
|
||||
gettimeofday(&now, NULL);
|
||||
m_secs = now.tv_sec;
|
||||
m_usecs = now.tv_usec;
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
void Timestamp::setTime(const string &stime){
|
||||
string::size_type p = stime.find('.');
|
||||
if(p == string::npos){
|
||||
m_secs = atol(stime.c_str());
|
||||
m_usecs = 0;
|
||||
}else{
|
||||
m_secs = atol(stime.substr(0, p).c_str());
|
||||
|
||||
string s_usecs = stime.substr(p+1, 6);
|
||||
m_usecs = atol(stime.substr(p+1).c_str());
|
||||
m_usecs *= (unsigned long)pow(10.0, double(6 - s_usecs.length()));
|
||||
}
|
||||
}
|
||||
|
||||
void Timestamp::setTime(double s)
|
||||
{
|
||||
m_secs = (unsigned long)s;
|
||||
m_usecs = (s - (double)m_secs) * 1e6;
|
||||
}
|
||||
|
||||
double Timestamp::getFloatTime() const {
|
||||
return double(m_secs) + double(m_usecs)/1000000.0;
|
||||
}
|
||||
|
||||
string Timestamp::getStringTime() const {
|
||||
char buf[32];
|
||||
sprintf(buf, "%.6lf", this->getFloatTime());
|
||||
return string(buf);
|
||||
}
|
||||
|
||||
double Timestamp::operator- (const Timestamp &t) const {
|
||||
return this->getFloatTime() - t.getFloatTime();
|
||||
}
|
||||
|
||||
Timestamp& Timestamp::operator+= (double s)
|
||||
{
|
||||
*this = *this + s;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Timestamp& Timestamp::operator-= (double s)
|
||||
{
|
||||
*this = *this - s;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Timestamp Timestamp::operator+ (double s) const
|
||||
{
|
||||
unsigned long secs = (long)floor(s);
|
||||
unsigned long usecs = (long)((s - (double)secs) * 1e6);
|
||||
|
||||
return this->plus(secs, usecs);
|
||||
}
|
||||
|
||||
Timestamp Timestamp::plus(unsigned long secs, unsigned long usecs) const
|
||||
{
|
||||
Timestamp t;
|
||||
|
||||
const unsigned long max = 1000000ul;
|
||||
|
||||
if(m_usecs + usecs >= max)
|
||||
t.setTime(m_secs + secs + 1, m_usecs + usecs - max);
|
||||
else
|
||||
t.setTime(m_secs + secs, m_usecs + usecs);
|
||||
|
||||
return t;
|
||||
}
|
||||
|
||||
Timestamp Timestamp::operator- (double s) const
|
||||
{
|
||||
unsigned long secs = (long)floor(s);
|
||||
unsigned long usecs = (long)((s - (double)secs) * 1e6);
|
||||
|
||||
return this->minus(secs, usecs);
|
||||
}
|
||||
|
||||
Timestamp Timestamp::minus(unsigned long secs, unsigned long usecs) const
|
||||
{
|
||||
Timestamp t;
|
||||
|
||||
const unsigned long max = 1000000ul;
|
||||
|
||||
if(m_usecs < usecs)
|
||||
t.setTime(m_secs - secs - 1, max - (usecs - m_usecs));
|
||||
else
|
||||
t.setTime(m_secs - secs, m_usecs - usecs);
|
||||
|
||||
return t;
|
||||
}
|
||||
|
||||
bool Timestamp::operator> (const Timestamp &t) const
|
||||
{
|
||||
if(m_secs > t.m_secs) return true;
|
||||
else if(m_secs == t.m_secs) return m_usecs > t.m_usecs;
|
||||
else return false;
|
||||
}
|
||||
|
||||
bool Timestamp::operator>= (const Timestamp &t) const
|
||||
{
|
||||
if(m_secs > t.m_secs) return true;
|
||||
else if(m_secs == t.m_secs) return m_usecs >= t.m_usecs;
|
||||
else return false;
|
||||
}
|
||||
|
||||
bool Timestamp::operator< (const Timestamp &t) const
|
||||
{
|
||||
if(m_secs < t.m_secs) return true;
|
||||
else if(m_secs == t.m_secs) return m_usecs < t.m_usecs;
|
||||
else return false;
|
||||
}
|
||||
|
||||
bool Timestamp::operator<= (const Timestamp &t) const
|
||||
{
|
||||
if(m_secs < t.m_secs) return true;
|
||||
else if(m_secs == t.m_secs) return m_usecs <= t.m_usecs;
|
||||
else return false;
|
||||
}
|
||||
|
||||
bool Timestamp::operator== (const Timestamp &t) const
|
||||
{
|
||||
return(m_secs == t.m_secs && m_usecs == t.m_usecs);
|
||||
}
|
||||
|
||||
|
||||
string Timestamp::Format(bool machine_friendly) const
|
||||
{
|
||||
struct tm tm_time;
|
||||
|
||||
time_t t = (time_t)getFloatTime();
|
||||
|
||||
#ifdef WIN32
|
||||
localtime_s(&tm_time, &t);
|
||||
#else
|
||||
localtime_r(&t, &tm_time);
|
||||
#endif
|
||||
|
||||
char buffer[128];
|
||||
|
||||
if(machine_friendly)
|
||||
{
|
||||
strftime(buffer, 128, "%Y%m%d_%H%M%S", &tm_time);
|
||||
}
|
||||
else
|
||||
{
|
||||
strftime(buffer, 128, "%c", &tm_time); // Thu Aug 23 14:55:02 2001
|
||||
}
|
||||
|
||||
return string(buffer);
|
||||
}
|
||||
|
||||
string Timestamp::Format(double s) {
|
||||
int days = int(s / (24. * 3600.0));
|
||||
s -= days * (24. * 3600.0);
|
||||
int hours = int(s / 3600.0);
|
||||
s -= hours * 3600;
|
||||
int minutes = int(s / 60.0);
|
||||
s -= minutes * 60;
|
||||
int seconds = int(s);
|
||||
int ms = int((s - seconds)*1e6);
|
||||
|
||||
stringstream ss;
|
||||
ss.fill('0');
|
||||
bool b;
|
||||
if((b = (days > 0))) ss << days << "d ";
|
||||
if((b = (b || hours > 0))) ss << setw(2) << hours << ":";
|
||||
if((b = (b || minutes > 0))) ss << setw(2) << minutes << ":";
|
||||
if(b) ss << setw(2);
|
||||
ss << seconds;
|
||||
if(!b) ss << "." << setw(6) << ms;
|
||||
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
|
||||
204
Thirdparty/DBoW2/DUtils/Timestamp.h
vendored
Normal file
204
Thirdparty/DBoW2/DUtils/Timestamp.h
vendored
Normal file
@@ -0,0 +1,204 @@
|
||||
/*
|
||||
* File: Timestamp.h
|
||||
* Author: Dorian Galvez-Lopez
|
||||
* Date: March 2009
|
||||
* Description: timestamping functions
|
||||
* License: see the LICENSE.txt file
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef __D_TIMESTAMP__
|
||||
#define __D_TIMESTAMP__
|
||||
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
namespace DUtils {
|
||||
|
||||
/// Timestamp
|
||||
class Timestamp
|
||||
{
|
||||
public:
|
||||
|
||||
/// Options to initiate a timestamp
|
||||
enum tOptions
|
||||
{
|
||||
NONE = 0,
|
||||
CURRENT_TIME = 0x1,
|
||||
ZERO = 0x2
|
||||
};
|
||||
|
||||
public:
|
||||
|
||||
/**
|
||||
* Creates a timestamp
|
||||
* @param option option to set the initial time stamp
|
||||
*/
|
||||
Timestamp(Timestamp::tOptions option = NONE);
|
||||
|
||||
/**
|
||||
* Destructor
|
||||
*/
|
||||
virtual ~Timestamp(void);
|
||||
|
||||
/**
|
||||
* Says if the timestamp is "empty": seconds and usecs are both 0, as
|
||||
* when initiated with the ZERO flag
|
||||
* @return true iif secs == usecs == 0
|
||||
*/
|
||||
bool empty() const;
|
||||
|
||||
/**
|
||||
* Sets this instance to the current time
|
||||
*/
|
||||
void setToCurrentTime();
|
||||
|
||||
/**
|
||||
* Sets the timestamp from seconds and microseconds
|
||||
* @param secs: seconds
|
||||
* @param usecs: microseconds
|
||||
*/
|
||||
inline void setTime(unsigned long secs, unsigned long usecs){
|
||||
m_secs = secs;
|
||||
m_usecs = usecs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the timestamp in seconds and microseconds
|
||||
* @param secs seconds
|
||||
* @param usecs microseconds
|
||||
*/
|
||||
inline void getTime(unsigned long &secs, unsigned long &usecs) const
|
||||
{
|
||||
secs = m_secs;
|
||||
usecs = m_usecs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the timestamp from a string with the time in seconds
|
||||
* @param stime: string such as "1235603336.036609"
|
||||
*/
|
||||
void setTime(const string &stime);
|
||||
|
||||
/**
|
||||
* Sets the timestamp from a number of seconds from the epoch
|
||||
* @param s seconds from the epoch
|
||||
*/
|
||||
void setTime(double s);
|
||||
|
||||
/**
|
||||
* Returns this timestamp as the number of seconds in (long) float format
|
||||
*/
|
||||
double getFloatTime() const;
|
||||
|
||||
/**
|
||||
* Returns this timestamp as the number of seconds in fixed length string format
|
||||
*/
|
||||
string getStringTime() const;
|
||||
|
||||
/**
|
||||
* Returns the difference in seconds between this timestamp (greater) and t (smaller)
|
||||
* If the order is swapped, a negative number is returned
|
||||
* @param t: timestamp to subtract from this timestamp
|
||||
* @return difference in seconds
|
||||
*/
|
||||
double operator- (const Timestamp &t) const;
|
||||
|
||||
/**
|
||||
* Returns a copy of this timestamp + s seconds + us microseconds
|
||||
* @param s seconds
|
||||
* @param us microseconds
|
||||
*/
|
||||
Timestamp plus(unsigned long s, unsigned long us) const;
|
||||
|
||||
/**
|
||||
* Returns a copy of this timestamp - s seconds - us microseconds
|
||||
* @param s seconds
|
||||
* @param us microseconds
|
||||
*/
|
||||
Timestamp minus(unsigned long s, unsigned long us) const;
|
||||
|
||||
/**
|
||||
* Adds s seconds to this timestamp and returns a reference to itself
|
||||
* @param s seconds
|
||||
* @return reference to this timestamp
|
||||
*/
|
||||
Timestamp& operator+= (double s);
|
||||
|
||||
/**
|
||||
* Substracts s seconds to this timestamp and returns a reference to itself
|
||||
* @param s seconds
|
||||
* @return reference to this timestamp
|
||||
*/
|
||||
Timestamp& operator-= (double s);
|
||||
|
||||
/**
|
||||
* Returns a copy of this timestamp + s seconds
|
||||
* @param s: seconds
|
||||
*/
|
||||
Timestamp operator+ (double s) const;
|
||||
|
||||
/**
|
||||
* Returns a copy of this timestamp - s seconds
|
||||
* @param s: seconds
|
||||
*/
|
||||
Timestamp operator- (double s) const;
|
||||
|
||||
/**
|
||||
* Returns whether this timestamp is at the future of t
|
||||
* @param t
|
||||
*/
|
||||
bool operator> (const Timestamp &t) const;
|
||||
|
||||
/**
|
||||
* Returns whether this timestamp is at the future of (or is the same as) t
|
||||
* @param t
|
||||
*/
|
||||
bool operator>= (const Timestamp &t) const;
|
||||
|
||||
/**
|
||||
* Returns whether this timestamp and t represent the same instant
|
||||
* @param t
|
||||
*/
|
||||
bool operator== (const Timestamp &t) const;
|
||||
|
||||
/**
|
||||
* Returns whether this timestamp is at the past of t
|
||||
* @param t
|
||||
*/
|
||||
bool operator< (const Timestamp &t) const;
|
||||
|
||||
/**
|
||||
* Returns whether this timestamp is at the past of (or is the same as) t
|
||||
* @param t
|
||||
*/
|
||||
bool operator<= (const Timestamp &t) const;
|
||||
|
||||
/**
|
||||
* Returns the timestamp in a human-readable string
|
||||
* @param machine_friendly if true, the returned string is formatted
|
||||
* to yyyymmdd_hhmmss, without weekday or spaces
|
||||
* @note This has not been tested under Windows
|
||||
* @note The timestamp is truncated to seconds
|
||||
*/
|
||||
string Format(bool machine_friendly = false) const;
|
||||
|
||||
/**
|
||||
* Returns a string version of the elapsed time in seconds, with the format
|
||||
* xd hh:mm:ss, hh:mm:ss, mm:ss or s.us
|
||||
* @param s: elapsed seconds (given by getFloatTime) to format
|
||||
*/
|
||||
static string Format(double s);
|
||||
|
||||
|
||||
protected:
|
||||
/// Seconds
|
||||
unsigned long m_secs; // seconds
|
||||
/// Microseconds
|
||||
unsigned long m_usecs; // microseconds
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user