##

Time.hh
Go to the documentation of this file.
/*
* Copyright (C) 2012 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#ifndef _GAZEBO_COMMON_TIME_HH_
#define _GAZEBO_COMMON_TIME_HH_
#include <string>
#include <stdlib.h>
#include <time.h>
#include <iostream>
#include "gazebo/common/CommonTypes.hh"
#include "gazebo/util/system.hh"
// forward declaration
struct timeval;
namespace gazebo
{
namespace common
{
#define GZ_SEC_TO_HOUR(_s) ((_s) / 3600.0)
class GZ_COMMON_VISIBLE Time
{
public: static const Time Zero;
public: static const Time Second;
public: static const Time Hour;
public: enum FormatOption
{
DAYS = 0,
HOURS = 1,
MINUTES = 2,
SECONDS = 3,
MILLISECONDS = 4
};
public: Time();
public: Time(const Time &_time);
// cppcheck-suppress noExplicitConstructor
public: Time(const struct timeval &_tv);
// cppcheck-suppress noExplicitConstructor
public: Time(const struct timespec &_tv);
public: Time(int32_t _sec, int32_t _nsec);
// cppcheck-suppress noExplicitConstructor
public: Time(double _time);
public: virtual ~Time();
public: static Time Maximum();
public: static const Time &GetWallTime();
public: static const std::string &GetWallTimeAsISOString();
public: void SetToWallTime();
public: void Set(int32_t _sec, int32_t _nsec);
public: void Set(double _seconds);
public: double Double() const;
public: float Float() const;
public: std::string FormattedString(FormatOption _start = DAYS,
FormatOption _end = MILLISECONDS) const;
public: static Time Sleep(const common::Time &_time);
public: static Time MSleep(unsigned int _ms);
public: static Time NSleep(unsigned int _ns);
public: Time &operator =(const struct timeval &_tv);
public: Time &operator =(const struct timespec &_tv);
public: Time &operator =(const Time &_time);
public: Time operator +(const struct timeval &_tv) const;
public: Time operator +(const struct timespec &_tv) const;
public: const Time &operator +=(const struct timeval &_tv);
public: const Time &operator +=(const struct timespec &_tv);
public: Time operator +(const Time &_time) const;
public: const Time &operator +=(const Time &_time);
public: Time operator -(const struct timeval &_tv) const;
public: const Time &operator -=(const struct timeval &_tv);
public: Time operator -(const struct timespec &_tv) const;
public: const Time &operator -=(const struct timespec &_tv);
public: Time operator -(const Time &_time) const;
public: const Time &operator -=(const Time &_time);
public: Time operator *(const struct timeval &_tv) const;
public: const Time &operator *=(const struct timeval &_tv);
public: Time operator *(const struct timespec &_tv) const;
public: const Time &operator *=(const struct timespec &_tv);
public: Time operator *(const Time &_time) const;
public: const Time &operator *=(const Time &_time);
public: Time operator /(const struct timeval &_tv) const;
public: const Time &operator /=(const struct timeval &_tv);
public: Time operator /(const struct timespec &_tv) const;
public: const Time &operator /=(const struct timespec &_tv);
public: Time operator /(const Time &_time) const;
public: const Time &operator /=(const Time &time);
public: bool operator ==(const struct timeval &_tv) const;
public: bool operator ==(const struct timespec &_tv) const;
public: bool operator ==(const Time &_time) const;
public: bool operator ==(double _time) const;
public: bool operator!=(const struct timeval &_tv) const;
public: bool operator!=(const struct timespec &_tv) const;
public: bool operator!=(const Time &_time) const;
public: bool operator!=(double _time) const;
public: bool operator<(const struct timeval &_tv) const;
public: bool operator<(const struct timespec &_tv) const;
public: bool operator<(const Time &_time) const;
public: bool operator<(double _time) const;
public: bool operator<=(const struct timeval &_tv) const;
public: bool operator<=(const struct timespec &_tv) const;
public: bool operator<=(const Time &_time) const;
public: bool operator<=(double _time) const;
public: bool operator>(const struct timeval &_tv) const;
public: bool operator>(const struct timespec &_tv) const;
public: bool operator>(const Time &_time) const;
public: bool operator>(double _time) const;
public: bool operator>=(const struct timeval &_tv) const;
public: bool operator>=(const struct timespec &_tv) const;
public: bool operator>=(const Time &_time) const;
public: bool operator>=(double _time) const;
public: static inline double SecToNano(double _sec)
{ return _sec * 1e9;}
public: static inline double MilToNano(double _ms)
{ return _ms * 1e6;}
public: static inline double MicToNano(double _ms)
{ return _ms * 1e3;}
public: friend std::ostream &operator<<(std::ostream &_out,
const gazebo::common::Time &_time)
{
_out << _time.sec << " " << _time.nsec;
return _out;
}
public: friend std::istream &operator>>(std::istream &_in,
gazebo::common::Time &_time)
{
// Skip white spaces
_in.setf(std::ios_base::skipws);
_in >> _time.sec >> _time.nsec;
return _in;
}
public: int32_t sec;
public: int32_t nsec;
public: static const int32_t nsInSec;
public: static const int32_t nsInMs;
private: static Time wallTime;
private: static std::string wallTimeISO;
private: inline void Correct()
{
// In the case sec and nsec have different signs, normalize
if (this->sec > 0 && this->nsec < 0)
{
int32_t n = abs(this->nsec / this->nsInSec) + 1;
this->sec -= n;
this->nsec += n * this->nsInSec;
}
if (this->sec < 0 && this->nsec > 0)
{
int32_t n = abs(this->nsec / this->nsInSec) + 1;
this->sec += n;
this->nsec -= n * this->nsInSec;
}
// Make any corrections
this->sec += this->nsec / this->nsInSec;
this->nsec = this->nsec % this->nsInSec;
}
private: static struct timespec clockResolution;
};
}
}
#endif
gazebo::common::Time::MINUTES
Minutes.
Definition: Time.hh:67
gazebo::common::Time::SECONDS
Seconds.
Definition: Time.hh:69
gazebo::common::Time::~Time
virtual ~Time()
Destructor.
gazebo
Forward declarations for the common classes.
Definition: Animation.hh:26
gazebo::common::Time
A Time class, can be used to hold wall- or sim-time. stored as sec and nano-sec.
Definition: Time.hh:47
gazebo::common::Time::Float
float Float() const
Get the time as a float.
gazebo::common::Time::FormatOption
FormatOption
Definition: Time.hh:60
gazebo::common::Time::operator-
Time operator-(const struct timeval &_tv) const
Subtraction operator.
common
common
Definition: FuelModelDatabase.hh:37
gazebo::common::Time::Maximum
static Time Maximum()
Get a Time object that represents the maximum amount of time that this class can represent.
gazebo::common::Time::DAYS
Days.
Definition: Time.hh:63
gazebo::common::Time::operator!=
bool operator!=(const struct timeval &_tv) const
Equal to operator.
gazebo::common::Time::GetWallTime
static const Time & GetWallTime()
Get the wall time.
system.hh
gazebo::common::Time::operator<
bool operator<(const struct timeval &_tv) const
Less than operator.
gazebo::common::Time::operator=
Time & operator=(const struct timeval &_tv)
Assignment operator.
gazebo::common::Time::Hour
static const Time Hour
A static time variable set to an hour: common::Time(3600, 0).
Definition: Time.hh:56
gazebo::common::Time::nsInSec
static const int32_t nsInSec
Constant multiplier to convert from nanoseconds to seconds.
Definition: Time.hh:465
CommonTypes.hh
gazebo::common::Time::operator/=
const Time & operator/=(const struct timeval &_tv)
Division assignment operator.
gazebo::common::Time::operator+
Time operator+(const struct timeval &_tv) const
Addition operators.
gazebo::common::Time::sec
int32_t sec
Seconds.
Definition: Time.hh:459
gazebo::common::Time::operator==
bool operator==(const struct timeval &_tv) const
Equal to operator.
gazebo::common::Time::Set
void Set(int32_t _sec, int32_t _nsec)
Set to sec and nsec.
gazebo::common::Time::FormattedString
std::string FormattedString(FormatOption _start=DAYS, FormatOption _end=MILLISECONDS) const
Get the time as a string formatted as "DD hh:mm:ss.mmm", with the option to choose the start/end.
gazebo::common::Time::operator*=
const Time & operator*=(const struct timeval &_tv)
Multiplication assignment operator.
gazebo::common::Time::SecToNano
static double SecToNano(double _sec)
Convert seconds to nanoseconds.
Definition: Time.hh:419
gazebo::common::Time::nsInMs
static const int32_t nsInMs
Constant multiplier to convert from nanoseconds to milliseconds.
Definition: Time.hh:469
gazebo::common::Time::MILLISECONDS
Milliseconds.
Definition: Time.hh:71
gazebo::common::Time::MicToNano
static double MicToNano(double _ms)
Convert microseconds to nanoseconds.
Definition: Time.hh:431
gazebo::common::Time::operator/
Time operator/(const struct timeval &_tv) const
Division operator.
gazebo::common::Time::operator<=
bool operator<=(const struct timeval &_tv) const
Less than or equal to operator.
gazebo::common::Time::Zero
static const Time Zero
A static zero time variable set to common::Time(0, 0).
Definition: Time.hh:50
gazebo::msgs::Set
GAZEBO_VISIBLE void Set(common::Image &_img, const msgs::Image &_msg)
Convert a msgs::Image to a common::Image.
gazebo::common::Time::MilToNano
static double MilToNano(double _ms)
Convert milliseconds to nanoseconds.
Definition: Time.hh:425
gazebo::common::Time::operator>
bool operator>(const struct timeval &_tv) const
Greater than operator.
gazebo::common::Time::HOURS
Hours.
Definition: Time.hh:65
gazebo::common::Time::Sleep
static Time Sleep(const common::Time &_time)
Sleep for the specified time.
gazebo::common::Time::nsec
int32_t nsec
Nanoseconds.
Definition: Time.hh:462
gazebo::common::Time::operator+=
const Time & operator+=(const struct timeval &_tv)
Addition assignment operator.
gazebo::common::Time::operator-=
const Time & operator-=(const struct timeval &_tv)
Subtraction assignment operator.
gazebo::common::Time::Double
double Double() const
Get the time as a double.
gazebo::common::Time::MSleep
static Time MSleep(unsigned int _ms)
Millisecond sleep.
gazebo::common::Time::SetToWallTime
void SetToWallTime()
Set the time to the wall time.
gazebo::common::Time::Time
Time()
Constructors.
gazebo::common::Time::operator>>
friend std::istream & operator>>(std::istream &_in, gazebo::common::Time &_time)
Stream extraction operator.
Definition: Time.hh:449
gazebo::common::Time::Second
static const Time Second
A static time variable set to a second: common::Time(1, 0).
Definition: Time.hh:53
gazebo::common::Time::operator<<
friend std::ostream & operator<<(std::ostream &_out, const gazebo::common::Time &_time)
Stream insertion operator.
Definition: Time.hh:438
gazebo::common::Time::operator>=
bool operator>=(const struct timeval &_tv) const
Greater than or equal operator.
gazebo::common::Time::GetWallTimeAsISOString
static const std::string & GetWallTimeAsISOString()
Get the wall time as an ISO string: YYYY-MM-DDTHH:MM:SS.
gazebo::common::Time::NSleep
static Time NSleep(unsigned int _ns)
Nano sleep.
gazebo::common::Time::operator*
Time operator*(const struct timeval &_tv) const
Multiplication operator.