Add operators '*' and '/' to TimeSpan

This commit is contained in:
Martchus 2020-10-24 22:10:25 +02:00
parent 3a8ae77477
commit 1f4fabcd9f
3 changed files with 53 additions and 3 deletions

View File

@ -113,8 +113,8 @@ set(META_APP_URL "https://github.com/${META_APP_AUTHOR}/${META_PROJECT_NAME}")
set(META_APP_DESCRIPTION "Useful C++ classes and routines such as argument parser, IO and conversion utilities") set(META_APP_DESCRIPTION "Useful C++ classes and routines such as argument parser, IO and conversion utilities")
set(META_FEATURES_FOR_COMPILER_DETECTION_HEADER cxx_thread_local) set(META_FEATURES_FOR_COMPILER_DETECTION_HEADER cxx_thread_local)
set(META_VERSION_MAJOR 5) set(META_VERSION_MAJOR 5)
set(META_VERSION_MINOR 7) set(META_VERSION_MINOR 8)
set(META_VERSION_PATCH 1) set(META_VERSION_PATCH 0)
# find required 3rd party libraries # find required 3rd party libraries
include(3rdParty) include(3rdParty)

View File

@ -64,8 +64,13 @@ public:
constexpr bool operator>=(const TimeSpan &other) const; constexpr bool operator>=(const TimeSpan &other) const;
constexpr TimeSpan operator+(const TimeSpan &other) const; constexpr TimeSpan operator+(const TimeSpan &other) const;
constexpr TimeSpan operator-(const TimeSpan &other) const; constexpr TimeSpan operator-(const TimeSpan &other) const;
constexpr TimeSpan operator*(double factor) const;
constexpr TimeSpan operator/(double factor) const;
constexpr double operator /(TimeSpan other) const;
TimeSpan &operator+=(const TimeSpan &other); TimeSpan &operator+=(const TimeSpan &other);
TimeSpan &operator-=(const TimeSpan &other); TimeSpan &operator-=(const TimeSpan &other);
TimeSpan &operator*=(double factor);
TimeSpan &operator/=(double factor);
std::string toString(TimeSpanOutputFormat format = TimeSpanOutputFormat::Normal, bool fullSeconds = false) const; std::string toString(TimeSpanOutputFormat format = TimeSpanOutputFormat::Normal, bool fullSeconds = false) const;
void toString(std::string &result, TimeSpanOutputFormat format = TimeSpanOutputFormat::Normal, bool fullSeconds = false) const; void toString(std::string &result, TimeSpanOutputFormat format = TimeSpanOutputFormat::Normal, bool fullSeconds = false) const;
@ -350,13 +355,37 @@ constexpr inline TimeSpan TimeSpan::operator+(const TimeSpan &other) const
} }
/*! /*!
* \brief Substracts two TimeSpan instances. * \brief Substracts one TimeSpan instance from another.
*/ */
constexpr inline TimeSpan TimeSpan::operator-(const TimeSpan &other) const constexpr inline TimeSpan TimeSpan::operator-(const TimeSpan &other) const
{ {
return TimeSpan(m_ticks - other.m_ticks); return TimeSpan(m_ticks - other.m_ticks);
} }
/*!
* \brief Multiplies a TimeSpan by the specified \a factor.
*/
constexpr inline TimeSpan TimeSpan::operator*(double factor) const
{
return TimeSpan(m_ticks * factor);
}
/*!
* \brief Divides a TimeSpan by the specified \a factor.
*/
constexpr inline TimeSpan TimeSpan::operator/(double factor) const
{
return TimeSpan(m_ticks / factor);
}
/*!
* \brief Computes the ratio between two TimeSpan instances.
*/
constexpr inline double TimeSpan::operator/(TimeSpan other) const
{
return static_cast<double>(m_ticks) / static_cast<double>(other.m_ticks);
}
/*! /*!
* \brief Adds another TimeSpan to the current instance. * \brief Adds another TimeSpan to the current instance.
*/ */
@ -375,6 +404,24 @@ inline TimeSpan &TimeSpan::operator-=(const TimeSpan &other)
return *this; return *this;
} }
/*!
* \brief Multiplies the current instance by the specified \a factor.
*/
inline TimeSpan &TimeSpan::operator*=(double factor)
{
m_ticks = m_ticks * factor;
return *this;
}
/*!
* \brief Divides the current instance by the specified \a factor.
*/
inline TimeSpan &TimeSpan::operator/=(double factor)
{
m_ticks = m_ticks / factor;
return *this;
}
/*! /*!
* \brief Converts the value of the current TimeSpan object to its equivalent std::string representation * \brief Converts the value of the current TimeSpan object to its equivalent std::string representation
* according the given \a format. * according the given \a format.

View File

@ -266,6 +266,9 @@ void ChronoTests::testOperators()
dateTime += TimeSpan::fromDays(365); dateTime += TimeSpan::fromDays(365);
CPPUNIT_ASSERT_EQUAL(2000, dateTime.year()); CPPUNIT_ASSERT_EQUAL(2000, dateTime.year());
CPPUNIT_ASSERT_EQUAL(5, dateTime.day()); CPPUNIT_ASSERT_EQUAL(5, dateTime.day());
CPPUNIT_ASSERT_EQUAL(TimeSpan::fromDays(1), TimeSpan::fromHours(12) * 2);
CPPUNIT_ASSERT_EQUAL(TimeSpan::fromHours(12), TimeSpan::fromDays(1) / 2);
CPPUNIT_ASSERT_EQUAL(2.0, TimeSpan::fromDays(1) / TimeSpan::fromHours(12));
} }
/*! /*!