311 lines
8.1 KiB
C++
311 lines
8.1 KiB
C++
/*
|
|
* File: GenericTime.cc
|
|
* Author: jgaebler
|
|
*
|
|
* Created on November 2, 2010, 3:39 PM
|
|
*/
|
|
#include "GenericTime.h"
|
|
#include "common/Exception.h"
|
|
|
|
namespace ubeeme {
|
|
namespace moversight {
|
|
|
|
/**
|
|
* @brief Output operator for generic time
|
|
* @param out The output stream
|
|
* @param o the time to print
|
|
* @return the resulting output
|
|
*/
|
|
std::ostream & operator<<(std::ostream & out, const GenericTime & o) {
|
|
BaseTime t = o.getTime();
|
|
out << t;
|
|
return out;
|
|
}
|
|
|
|
/**
|
|
* @brief Multiplication operation
|
|
* @param a The multiplicator
|
|
* @param b The multiplicand.
|
|
* @return The result of the multiplication.
|
|
*/
|
|
GenericTime operator*(double const a, GenericTime const & b) {
|
|
return b * a;
|
|
}
|
|
|
|
/**
|
|
* @brief Addition operation.
|
|
* @param t1 first summand
|
|
* @param t2 second summand
|
|
* @return the resulting time
|
|
*/
|
|
GenericTime operator+(double const & t1, GenericTime const & t2) {
|
|
return GenericTime(static_cast<int> (t1 + t2.dbl()));
|
|
}
|
|
#if UBEEME
|
|
|
|
/**
|
|
* @brief Addition operation.
|
|
* @param t1 first summand
|
|
* @param t2 second summand
|
|
* @return the resulting time
|
|
*/
|
|
GenericTime operator+(BaseTime const & t1, GenericTime const & t2) {
|
|
return GenericTime(t1.msec() + t2.dbl());
|
|
}
|
|
|
|
/**
|
|
* @brief Addition operation.
|
|
* @param t1 first summand
|
|
* @param t2 second summand
|
|
* @return the resulting time
|
|
*/
|
|
GenericTime operator-(BaseTime const & t1, GenericTime const & t2) {
|
|
return GenericTime(t1.msec() - t2.dbl());
|
|
}
|
|
|
|
/**
|
|
* @brief Addition operation.
|
|
* @param t1 first summand
|
|
* @param t2 second summand
|
|
* @return the resulting time
|
|
*/
|
|
GenericTime operator*(BaseTime const & t1, GenericTime const & t2) {
|
|
return GenericTime(t1.msec() * t2.dbl());
|
|
}
|
|
|
|
/**
|
|
* @brief Addition operation.
|
|
* @param t1 first summand
|
|
* @param t2 second summand
|
|
* @return the resulting time
|
|
*/
|
|
GenericTime operator/(BaseTime const & t1, GenericTime const & t2) {
|
|
return GenericTime(t1.msec() / t2.dbl());
|
|
}
|
|
#endif
|
|
/**
|
|
* @brief Constructor. Creates a GenericTime (pointing to 0).
|
|
*/
|
|
#if OMNETPP
|
|
GenericTime::GenericTime() : time(0) {
|
|
#else
|
|
GenericTime::GenericTime() {
|
|
#endif
|
|
}
|
|
|
|
|
|
/**
|
|
* @brief Constructor. Creates a GenericTime, represent a given milli second value
|
|
* @param msec The milli second value to represent
|
|
*/
|
|
#if OMNETPP
|
|
|
|
GenericTime::GenericTime(int msec) : time(msec) {
|
|
#else
|
|
|
|
GenericTime::GenericTime(int msec) {
|
|
time = time.addMSecs(msec);
|
|
#endif
|
|
}
|
|
|
|
/**
|
|
* @brief Constructor. Creates a GenericTime, based on the given BaseTime.
|
|
* @param t The time to initalies the GenericTime.
|
|
*/
|
|
GenericTime::GenericTime(BaseTime t) : time(t) {
|
|
}
|
|
|
|
/**
|
|
* @brief Copy constructor
|
|
* @param orig The instance to copy.
|
|
*/
|
|
GenericTime::GenericTime(GenericTime const & orig) : time(orig.time) {
|
|
}
|
|
|
|
/**
|
|
* @brief Operator +
|
|
* @param t The parameter to add.
|
|
* @return The operation result.
|
|
*/
|
|
GenericTime GenericTime::operator+(GenericTime const & t) const {
|
|
return time + t.time;
|
|
}
|
|
|
|
/**
|
|
* @brief Operator -
|
|
* @param t The parameter to subtract.
|
|
* @return The operation result.
|
|
*/
|
|
GenericTime GenericTime::operator-(GenericTime const & t) const {
|
|
return time - t.time;
|
|
}
|
|
|
|
/**
|
|
* @brief Operator * multiply the time with a given value.
|
|
* @param v The multiply factor.
|
|
* @return The operation result.
|
|
*/
|
|
GenericTime GenericTime::operator*(double const & v) const {
|
|
return time * v;
|
|
}
|
|
|
|
/**
|
|
* @brief Operator <
|
|
* @param t The parameter to compare.
|
|
* @return True, if less, false otherwise.
|
|
*/
|
|
bool GenericTime::operator<(GenericTime const & t) const {
|
|
|
|
return time < t.time;
|
|
|
|
}
|
|
|
|
/**
|
|
* @brief Operator >
|
|
* @param t The parameter to compare.
|
|
* @return True, if greater, false otherwise.
|
|
*/
|
|
bool GenericTime::operator>(GenericTime const & t) const {
|
|
|
|
return time > t.time;
|
|
|
|
}
|
|
|
|
/**
|
|
* @brief Operator /
|
|
* @param t The parameter to divide.
|
|
* @return The operation result.
|
|
*/
|
|
GenericTime GenericTime::operator/(GenericTime const & t) const {
|
|
return GenericTime(time / t.dbl());
|
|
}
|
|
|
|
/**
|
|
* @brief Assignment operator
|
|
* @param other The instance to assign
|
|
* @return A reference to the local instance
|
|
*/
|
|
GenericTime & GenericTime::operator=(const GenericTime & other) {
|
|
|
|
if (this != &other) { // protect against invalid self-assignment
|
|
|
|
time = other.time;
|
|
|
|
}//End if
|
|
|
|
// by convention, always return *this
|
|
return *this;
|
|
|
|
}
|
|
|
|
/**
|
|
* @brief Returns the internal time object.
|
|
* @return The internal time.
|
|
*/
|
|
BaseTime
|
|
GenericTime::getTime() const {
|
|
return time;
|
|
}
|
|
|
|
/**
|
|
* @brief Returns the absolute value of the current time instance.
|
|
* @return The absolute value of the current time instance.
|
|
*/
|
|
GenericTime
|
|
GenericTime::abs() const {
|
|
|
|
#if OMNETPP
|
|
if (time > 0) {
|
|
return GenericTime(time);
|
|
}
|
|
#else
|
|
GenericTime zeroTime;
|
|
if (time > zeroTime.getTime()) {
|
|
return GenericTime(time);
|
|
}
|
|
#endif
|
|
|
|
return GenericTime(time * -1);
|
|
}
|
|
|
|
/**
|
|
* @brief Returns a double value re-presentation of the current time.
|
|
* @return The double value of the time.
|
|
*/
|
|
double
|
|
GenericTime::dbl() const {
|
|
#if OMNETPP
|
|
return time.dbl();
|
|
#else
|
|
return time.toString( "hhmm.ss" ).toDouble();
|
|
#endif
|
|
}
|
|
|
|
/**
|
|
* @brief Converts the current instance into a millisecond representation
|
|
* @return The current time, in milli seconds.
|
|
*/
|
|
int
|
|
GenericTime::toMSecs() const {
|
|
|
|
#if OMNETPP
|
|
if (time.getScale() == SimTime::SCALEEXP_MS) {
|
|
return time.raw();
|
|
}//End if
|
|
|
|
throw NotImplementedYetException("toMSecs - time scale currently not supported");
|
|
#else
|
|
return time.toString("ss.zzz").toDouble()*1000;
|
|
#endif
|
|
}
|
|
|
|
/**
|
|
* @brief Returns the current time
|
|
* @return The current time.
|
|
*/
|
|
GenericTime
|
|
GenericTime::currentTime() {
|
|
#if OMNETPP
|
|
return GenericTime(simTime());
|
|
#else
|
|
return GenericTime(BaseTime::currentTime());
|
|
#endif
|
|
|
|
}
|
|
|
|
/**
|
|
* @brief Converts the current time into a string.
|
|
* @return The current time as string.
|
|
*/
|
|
std::string
|
|
GenericTime::toString() {
|
|
#if OMNETPP
|
|
return time.str();
|
|
#else
|
|
return time.toString().toStdString();
|
|
#endif
|
|
}
|
|
|
|
|
|
/**
|
|
* @brief Converts a string to time. Throws an error if there is an
|
|
* error during conversion. Accepted format is:
|
|
* \<number\> or (\<number\>\<unit\>)+.
|
|
* @param str The string to convert.
|
|
* @return The resulting time.
|
|
*/
|
|
GenericTime
|
|
GenericTime::fromString(std::string str) {
|
|
#if OMNETPP
|
|
return GenericTime(BaseTime::parse(str.c_str()));
|
|
#else
|
|
return GenericTime(BaseTime::fromString(str.c_str(), "hh:mm:ss:zz"));
|
|
#endif
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|