Add a few arithmetic ops to TimeSpan
This commit is contained in:
1 parent
99f3a845b8
commit
bfd837764a
1 file changed
+56
@@ -38,6 +38,7 @@ class TimeSpan {
|
||||
constexpr std::int64_t micros() const;
|
||||
constexpr std::int64_t millis() const;
|
||||
constexpr std::int64_t seconds() const;
|
||||
constexpr std::int64_t ticks() const;
|
||||
|
||||
constexpr double total_millis() const;
|
||||
constexpr double total_seconds() const;
|
||||
@@ -48,6 +49,8 @@ class TimeSpan {
|
||||
private:
|
||||
// nanos
|
||||
std::int64_t ticks_{};
|
||||
friend constexpr TimeSpan &operator-=(TimeSpan &lhs, TimeSpan rhs) noexcept;
|
||||
friend constexpr TimeSpan &operator+=(TimeSpan &lhs, TimeSpan rhs) noexcept;
|
||||
};
|
||||
|
||||
constexpr bool operator==(TimeSpan lhs, TimeSpan rhs) noexcept;
|
||||
@@ -57,7 +60,18 @@ constexpr bool operator<=(TimeSpan lhs, TimeSpan rhs) noexcept;
|
||||
constexpr bool operator>(TimeSpan lhs, TimeSpan rhs) noexcept;
|
||||
constexpr bool operator>=(TimeSpan lhs, TimeSpan rhs) noexcept;
|
||||
constexpr TimeSpan operator-(TimeSpan lhs, TimeSpan rhs) noexcept;
|
||||
constexpr TimeSpan &operator-=(TimeSpan &lhs, TimeSpan rhs) noexcept;
|
||||
constexpr TimeSpan operator+(TimeSpan lhs, TimeSpan rhs) noexcept;
|
||||
constexpr TimeSpan &operator+=(TimeSpan &lhs, TimeSpan rhs) noexcept;
|
||||
|
||||
constexpr TimeSpan operator*(TimeSpan lhs, int rhs) noexcept;
|
||||
constexpr TimeSpan &operator*=(TimeSpan &lhs, int rhs) noexcept;
|
||||
constexpr TimeSpan operator/(TimeSpan lhs, int rhs) noexcept;
|
||||
constexpr TimeSpan &operator/=(TimeSpan &lhs, int rhs) noexcept;
|
||||
constexpr TimeSpan operator*(TimeSpan lhs, std::int64_t rhs) noexcept;
|
||||
constexpr TimeSpan operator/(TimeSpan lhs, std::int64_t rhs) noexcept;
|
||||
TimeSpan operator*(TimeSpan lhs, std::uint64_t rhs) noexcept;
|
||||
TimeSpan operator/(TimeSpan lhs, std::uint64_t rhs) noexcept;
|
||||
|
||||
std::ostream &operator<<(std::ostream &out, const TimeSpan &ts) noexcept;
|
||||
|
||||
@@ -113,6 +127,8 @@ constexpr std::int64_t TimeSpan::seconds() const {
|
||||
return ticks_ / 1'000'000'000;
|
||||
}
|
||||
|
||||
constexpr std::int64_t TimeSpan::ticks() const { return ticks_; }
|
||||
|
||||
constexpr double TimeSpan::total_millis() const {
|
||||
return static_cast<double>(ticks_) / 1000'000;
|
||||
}
|
||||
@@ -154,10 +170,50 @@ constexpr TimeSpan operator-(TimeSpan lhs, TimeSpan rhs) noexcept {
|
||||
return TimeSpan{lhs.nanos() - rhs.nanos()};
|
||||
}
|
||||
|
||||
constexpr TimeSpan &operator-=(TimeSpan &lhs, TimeSpan rhs) noexcept {
|
||||
lhs.ticks_ -= rhs.ticks_;
|
||||
return lhs;
|
||||
}
|
||||
|
||||
constexpr TimeSpan operator+(TimeSpan lhs, TimeSpan rhs) noexcept {
|
||||
return TimeSpan{lhs.nanos() + rhs.nanos()};
|
||||
}
|
||||
|
||||
constexpr TimeSpan &operator+=(TimeSpan &lhs, TimeSpan rhs) noexcept {
|
||||
lhs.ticks_ += rhs.ticks_;
|
||||
return lhs;
|
||||
}
|
||||
|
||||
constexpr TimeSpan operator*(TimeSpan lhs, int rhs) noexcept {
|
||||
return TimeSpan{lhs.ticks() * rhs};
|
||||
}
|
||||
|
||||
constexpr TimeSpan operator/(TimeSpan lhs, int rhs) noexcept {
|
||||
return TimeSpan{lhs.ticks() / rhs};
|
||||
}
|
||||
|
||||
constexpr TimeSpan operator*(TimeSpan lhs, std::int64_t rhs) noexcept {
|
||||
return TimeSpan{lhs.ticks() * rhs};
|
||||
}
|
||||
|
||||
constexpr TimeSpan operator/(TimeSpan lhs, std::int64_t rhs) noexcept {
|
||||
return TimeSpan{lhs.ticks() / rhs};
|
||||
}
|
||||
|
||||
inline TimeSpan operator*(TimeSpan lhs, std::uint64_t rhs) noexcept {
|
||||
bts_assert(
|
||||
rhs <
|
||||
static_cast<std::uint64_t>(std::numeric_limits<std::int64_t>::max()));
|
||||
return TimeSpan{lhs.ticks() * static_cast<std::int64_t>(rhs)};
|
||||
}
|
||||
|
||||
inline TimeSpan operator/(TimeSpan lhs, std::uint64_t rhs) noexcept {
|
||||
bts_assert(
|
||||
rhs <
|
||||
static_cast<std::uint64_t>(std::numeric_limits<std::int64_t>::max()));
|
||||
return TimeSpan{lhs.ticks() / static_cast<std::int64_t>(rhs)};
|
||||
}
|
||||
|
||||
} // namespace bts::core
|
||||
|
||||
FMT_IMPL(bts::core::TimeSpan, t, "{}", t.to_iso_string());
|
||||
Reference in new issue
Block a user