diff --git a/CMakeLists.txt b/CMakeLists.txt index 9afd534..28a73f1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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_FEATURES_FOR_COMPILER_DETECTION_HEADER cxx_thread_local) set(META_VERSION_MAJOR 5) -set(META_VERSION_MINOR 7) -set(META_VERSION_PATCH 1) +set(META_VERSION_MINOR 8) +set(META_VERSION_PATCH 0) # find required 3rd party libraries include(3rdParty) diff --git a/chrono/timespan.h b/chrono/timespan.h index 07273db..7bb4874 100644 --- a/chrono/timespan.h +++ b/chrono/timespan.h @@ -64,8 +64,13 @@ public: constexpr bool 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*=(double factor); + TimeSpan &operator/=(double factor); std::string toString(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 { 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(m_ticks) / static_cast(other.m_ticks); +} + /*! * \brief Adds another TimeSpan to the current instance. */ @@ -375,6 +404,24 @@ inline TimeSpan &TimeSpan::operator-=(const TimeSpan &other) 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 * according the given \a format. diff --git a/tests/chronotests.cpp b/tests/chronotests.cpp index 3b67616..7c92ea2 100644 --- a/tests/chronotests.cpp +++ b/tests/chronotests.cpp @@ -266,6 +266,9 @@ void ChronoTests::testOperators() dateTime += TimeSpan::fromDays(365); CPPUNIT_ASSERT_EQUAL(2000, dateTime.year()); 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)); } /*!