From 1a95e81043e3894097120bf750b639550bb7c2d8 Mon Sep 17 00:00:00 2001 From: Gawain Bolton <45885849+gawain-bolton@users.noreply.github.com> Date: Fri, 1 Nov 2019 12:08:45 +0100 Subject: [PATCH] Add time_duration helper functions: (#113) 1. is_positive() - Return boolean value to indicate whether or not time duration is positive. 2. is_zero() - Return boolean value to indicate whether or not time duration is zero. 3. abs() - Return a time_duration which is the absolute value of time duration. Added documentation for these helper functions and improved existing documentation to indicate constness, return values or static functions. --- include/boost/date_time/time_duration.hpp | 16 ++++++ test/posix_time/testduration.cpp | 12 ++++- xmldoc/time_duration.xml | 61 +++++++++++++++++------ 3 files changed, 72 insertions(+), 17 deletions(-) diff --git a/include/boost/date_time/time_duration.hpp b/include/boost/date_time/time_duration.hpp index c52779be5..161ee686c 100644 --- a/include/boost/date_time/time_duration.hpp +++ b/include/boost/date_time/time_duration.hpp @@ -144,10 +144,26 @@ namespace date_time { { return duration_type(ticks_ * (-1)); } + duration_type abs() const + { + if ( is_negative() ) + { + return invert_sign(); + } + return duration_type(ticks_); + } bool is_negative() const { return ticks_ < 0; } + bool is_zero() const + { + return ticks_ == 0; + } + bool is_positive() const + { + return ticks_ > 0; + } bool operator<(const time_duration& rhs) const { return ticks_ < rhs.ticks_; diff --git a/test/posix_time/testduration.cpp b/test/posix_time/testduration.cpp index 74a4a8e5b..061089290 100644 --- a/test/posix_time/testduration.cpp +++ b/test/posix_time/testduration.cpp @@ -35,8 +35,18 @@ main() time_duration td3(td1.hours(),td1.minutes(),td1.seconds()); check("total up elements", td1 == td3); td1 = -td1; // td1 == "-1:25:00" + check("invert_sign",td3 == td1.invert_sign()); + check("invert_sign",td1 == td3.invert_sign()); + check("abs",td3 == td1.abs()); + check("abs",td3 == td3.abs()); + check("is_positive",td3.is_positive()); + check("is_positive",!td1.is_positive()); + check("is_negative",td1.is_negative()); + check("is_negative",!td3.is_negative()); + check("is_zero",!td1.is_zero()); + check("is_zero",(td1 - td1).is_zero()); td3 = time_duration(td1.hours(),td1.minutes(),td1.seconds()); - check("total up elements-invered sign", td1 == td3); + check("total up elements-inverted sign", td1 == td3); time_duration t_1(0,1,40); diff --git a/xmldoc/time_duration.xml b/xmldoc/time_duration.xml index c31fb4d3c..fde1387be 100644 --- a/xmldoc/time_duration.xml +++ b/xmldoc/time_duration.xml @@ -256,7 +256,7 @@ neg_td.minutes(); // --> -2 - boost::int64_t seconds() + boost::int64_t seconds() const Get the normalized number of second +/-(0..59) (will give unpredictable results if calling time_duration is a special_value). @@ -267,7 +267,7 @@ neg_td.seconds(); // --> -3 - boost::int64_t total_seconds() + boost::int64_t total_seconds() const Get the total number of seconds truncating any fractional seconds (will give unpredictable results if calling time_duration is a special_value). @@ -278,7 +278,7 @@ td.total_seconds(); - boost::int64_t total_milliseconds() + boost::int64_t total_milliseconds() const Get the total number of milliseconds truncating any remaining digits (will give unpredictable results if calling time_duration is a special_value). @@ -291,7 +291,7 @@ td.total_milliseconds(); - boost::int64_t total_microseconds() + boost::int64_t total_microseconds() const Get the total number of microseconds truncating any remaining digits (will give unpredictable results if calling time_duration is a special_value). @@ -304,7 +304,7 @@ td.total_microseconds(); - boost::int64_t total_nanoseconds() + boost::int64_t total_nanoseconds() const Get the total number of nanoseconds truncating any remaining digits (will give unpredictable results if calling time_duration is a special_value). @@ -318,7 +318,7 @@ td.total_nanoseconds(); - boost::int64_t fractional_seconds() + boost::int64_t fractional_seconds() const Get the number of fractional seconds (will give unpredictable results if calling time_duration is a special_value). @@ -327,8 +327,8 @@ td.fractional_seconds(); // --> 1000 - bool is_negative() - True if duration is negative. + bool is_negative() const + True if and only if duration is negative. time_duration td(-1,0,0); @@ -336,16 +336,45 @@ td.is_negative(); // --> true - time_duration invert_sign() - Generate a new duration with the sign inverted/ + bool is_zero() const + True if and only if duration is zero. - time_duration td(-1,0,0); + time_duration td(0,0,0); +td.is_zero(); // --> true + + + + bool is_positive() const + True if and only if duration is positive. + + + time_duration td(1,0,0); +td.is_positive(); // --> true + + + + time_duration invert_sign() const + Generate a new duration with the sign inverted. + + + time_duration td(-1,0,0); td.invert_sign(); // --> 01:00:00 - date_time::time_resolutions resolution() + time_duration abs() const + Generate a new duration with the absolute value of the time duration. + + + time_duration td(-1,0,0); +td.abs(); // --> 01:00:00 + time_duration td(+1,0,0); +td.abs(); // --> 01:00:00 + + + + date_time::time_resolutions time_duration::resolution() Describes the resolution capability of the time_duration class. time_resolutions is an enum of resolution possibilities ranging from seconds to nanoseconds. @@ -353,8 +382,8 @@ td.invert_sign(); // --> 01:00:00 - time_duration::num_fractional_digits() - Returns an unsigned short holding the number of fractional digits the time resolution has. + unsigned short time_duration::num_fractional_digits() + Returns the number of fractional digits the time resolution has. unsigned short secs; @@ -363,7 +392,7 @@ secs = time_duration::num_fractional_digits(); - time_duration::ticks_per_second() + boost::int64_t time_duration::ticks_per_second() Return the number of ticks in a second. For example, if the duration supports nanoseconds then the returned result will be 1,000,000,000 (1e+9). @@ -380,7 +409,7 @@ td.ticks() // --> 1000 - time_duration unit() + time_duration time_duration::unit() Return smallest possible unit of duration type (1 nanosecond).