TimeDelta add +(pos), -(neg) operation

This commit is contained in:
fasiondog 2019-12-25 02:45:24 +08:00
parent 503e3460ed
commit 2e753f7968
3 changed files with 32 additions and 7 deletions

View File

@ -92,6 +92,11 @@ public:
return m_duration.is_negative();
}
/** 求绝对值 */
TimeDelta abs() const {
return TimeDelta::fromTicks(std::abs(ticks()));
}
/** 转换为 boost::posix_time::time_duration */
bt::time_duration time_duration() const {
return m_duration;
@ -115,11 +120,6 @@ public:
//
/////////////////////////////////////////////////////////////////
/** 求绝对值 */
TimeDelta abs(TimeDelta td) const {
return TimeDelta::fromTicks(std::abs(td.ticks()));
}
/** 两个时长相加 */
TimeDelta operator+(TimeDelta td) const {
return TimeDelta(td.m_duration + m_duration);
@ -130,6 +130,16 @@ public:
return TimeDelta(m_duration - td.m_duration);
}
/** + 号,返回相同值 */
TimeDelta operator+() const {
return *this;
}
/** - 号, 求负值,相当于 TimeDelta(0) 减自身 */
TimeDelta operator-() const {
return TimeDelta::fromTicks(-ticks());
}
/** 时长乘以系数,结果四舍五入 */
TimeDelta operator*(double p) const;

View File

@ -233,6 +233,14 @@ BOOST_AUTO_TEST_CASE(test_TimeDelta_operator) {
BOOST_CHECK(td.microseconds() == 0);
BOOST_CHECK(td.ticks() == -90000000000LL);
/** @arg + 号 */
BOOST_CHECK(+TimeDelta(1) == TimeDelta(1));
BOOST_CHECK(+TimeDelta(-1) == TimeDelta(-1));
/** @arg -号 */
BOOST_CHECK(-TimeDelta(1) == TimeDelta(-1));
BOOST_CHECK(-TimeDelta(-1) == TimeDelta(1));
/** @arg 相减结果为0 */
td = TimeDelta(1, 0, 1) - TimeDelta(0, 24, 0, 60);
BOOST_CHECK(td.isNegative() == false);

View File

@ -17,6 +17,11 @@ using namespace hku;
double (TimeDelta::*TimeDelta_div_1)(TimeDelta) const = &TimeDelta::operator/;
TimeDelta (TimeDelta::*TimeDelta_div_2)(double) const = &TimeDelta::operator/;
TimeDelta (TimeDelta::*TimeDelta_pos)() const = &TimeDelta::operator+;
TimeDelta (TimeDelta::*TimeDelta_add)(TimeDelta) const = &TimeDelta::operator+;
TimeDelta (TimeDelta::*TimeDelta_neg)() const = &TimeDelta::operator-;
TimeDelta (TimeDelta::*TimeDelta_sub)(TimeDelta) const = &TimeDelta::operator-;
void export_TimeDelta() {
class_<TimeDelta>("TimeDelta", init<>())
.def(init<int64_t, int64_t, int64_t, int64_t, int64_t, int64_t>(
@ -54,8 +59,10 @@ void export_TimeDelta() {
.def("__le__", &TimeDelta::operator>=)
.def("__abs__", &TimeDelta::abs)
.def("__add__", &TimeDelta::operator+)
.def("__sub__", &TimeDelta::operator-)
.def("__add__", TimeDelta_add)
.def("__pos__", TimeDelta_pos)
.def("__sub__", TimeDelta_sub)
.def("__neg__", TimeDelta_neg)
.def("__mul__", &TimeDelta::operator*)
.def("__rmul__", &TimeDelta::operator*)
.def("__truediv__", TimeDelta_div_1)