9 #ifndef CPPCOMMON_TIME_TIMESPAN_H
10 #define CPPCOMMON_TIME_TIMESPAN_H
35 explicit Timespan(int64_t duration) noexcept : _duration(duration) {}
40 template <
class Rep,
class Period>
41 explicit Timespan(
const std::chrono::duration<Rep, Period>& duration) noexcept : _duration(std::chrono::duration_cast<std::chrono::nanoseconds>(duration).count()) {}
47 { _duration = duration;
return *
this; }
58 { _duration += offset;
return *
this; }
60 { _duration += offset.total();
return *
this; }
63 { _duration -= offset;
return *
this; }
65 { _duration -= offset.total();
return *
this; }
68 {
return Timespan(timespan.total() + offset); }
70 {
return Timespan(offset + timespan.total()); }
72 {
return Timespan(timespan1.total() + timespan2.total()); }
75 {
return Timespan(timespan.total() - offset); }
77 {
return Timespan(offset - timespan.total()); }
79 {
return Timespan(timespan1.total() - timespan2.total()); }
83 {
return timespan.
total() == offset; }
85 {
return offset == timespan.
total(); }
87 {
return timespan1.
total() == timespan2.total(); }
90 {
return timespan.
total() != offset; }
92 {
return offset != timespan.
total(); }
94 {
return timespan1.
total() != timespan2.total(); }
97 {
return timespan.
total() > offset; }
99 {
return offset > timespan.
total(); }
101 {
return timespan1.
total() > timespan2.total(); }
104 {
return timespan.
total() < offset; }
106 {
return offset < timespan.
total(); }
108 {
return timespan1.
total() < timespan2.total(); }
111 {
return timespan.
total() >= offset; }
113 {
return offset >= timespan.
total(); }
115 {
return timespan1.
total() >= timespan2.total(); }
118 {
return timespan.
total() <= offset; }
120 {
return offset <= timespan.
total(); }
122 {
return timespan1.
total() <= timespan2.total(); }
125 std::chrono::system_clock::duration
chrono() const noexcept
126 {
return std::chrono::duration_cast<std::chrono::system_clock::duration>(std::chrono::nanoseconds(_duration)); }
130 {
return _duration / (24 * 60 * 60 * 1000000000ll); }
133 {
return _duration / (60 * 60 * 1000000000ll); }
136 {
return _duration / (60 * 1000000000ll); }
139 {
return _duration / 1000000000; }
142 {
return _duration / 1000000; }
145 {
return _duration / 1000; }
148 {
return _duration; }
152 {
return _duration; }
156 {
return Timespan(
days * 24 * 60 * 60 * 1000000000ll); }
friend Timespan operator-(const Timespan ×pan1, const Timespan ×pan2) noexcept
static Timespan milliseconds(int64_t milliseconds) noexcept
Create the timespan based on the given milliseconds value.
Timespan() noexcept
Initialize timespan with a zero time duration.
Timespan & operator+=(const Timespan &offset) noexcept
int64_t total() const noexcept
Get total value of the current timespan (total nanoseconds)
friend Timespan operator+(int64_t offset, const Timespan ×pan) noexcept
static Timespan microseconds(int64_t microseconds) noexcept
Create the timespan based on the given microseconds value.
friend bool operator<(const Timespan ×pan1, const Timespan ×pan2) noexcept
Timespan & operator-=(const Timespan &offset) noexcept
friend bool operator<=(const Timespan ×pan1, const Timespan ×pan2) noexcept
Timespan & operator=(Timespan &&) noexcept=default
Timespan(const Timespan &) noexcept=default
int64_t seconds() const noexcept
Get total seconds of the current timespan.
Timespan & operator+=(int64_t offset) noexcept
Timespan(Timespan &&) noexcept=default
static Timespan seconds(int64_t seconds) noexcept
Create the timespan based on the given seconds value.
friend bool operator<=(const Timespan ×pan, int64_t offset) noexcept
friend Timespan operator-(const Timespan ×pan, int64_t offset) noexcept
friend bool operator>(const Timespan ×pan1, const Timespan ×pan2) noexcept
friend bool operator<(const Timespan ×pan, int64_t offset) noexcept
Timespan & operator-=(int64_t offset) noexcept
int64_t days() const noexcept
Get total days of the current timespan.
static Timespan hours(int64_t hours) noexcept
Create the timespan based on the given hours value.
Timespan(int64_t duration) noexcept
Initialize timespan with a given time duration value in nanoseconds.
static Timespan minutes(int64_t minutes) noexcept
Create the timespan based on the given minutes value.
friend bool operator>=(const Timespan ×pan, int64_t offset) noexcept
friend bool operator==(const Timespan ×pan, int64_t offset) noexcept
friend bool operator==(const Timespan ×pan1, const Timespan ×pan2) noexcept
int64_t microseconds() const noexcept
Get total microseconds of the current timespan.
Timespan operator-() const
friend bool operator>(int64_t offset, const Timespan ×pan) noexcept
friend Timespan operator+(const Timespan ×pan, int64_t offset) noexcept
int64_t milliseconds() const noexcept
Get total milliseconds of the current timespan.
std::chrono::system_clock::duration chrono() const noexcept
Convert timespan to the std::chrono nanoseconds duration.
int64_t minutes() const noexcept
Get total minutes of the current timespan.
friend bool operator<(int64_t offset, const Timespan ×pan) noexcept
static Timespan nanoseconds(int64_t nanoseconds) noexcept
Create the timespan based on the given nanoseconds value.
friend bool operator==(int64_t offset, const Timespan ×pan) noexcept
Timespan(const std::chrono::duration< Rep, Period > &duration) noexcept
Initialize timespan with a given std::chrono duration.
int64_t nanoseconds() const noexcept
Get total nanoseconds of the current timespan.
static Timespan days(int64_t days) noexcept
Create the timespan based on the given days value.
friend bool operator!=(const Timespan ×pan, int64_t offset) noexcept
void swap(Timespan ×pan) noexcept
Swap two instances.
friend bool operator!=(int64_t offset, const Timespan ×pan) noexcept
Timespan & operator=(const Timespan &) noexcept=default
friend Timespan operator+(const Timespan ×pan1, const Timespan ×pan2) noexcept
friend bool operator<=(int64_t offset, const Timespan ×pan) noexcept
friend bool operator!=(const Timespan ×pan1, const Timespan ×pan2) noexcept
friend bool operator>(const Timespan ×pan, int64_t offset) noexcept
friend bool operator>=(int64_t offset, const Timespan ×pan) noexcept
friend bool operator>=(const Timespan ×pan1, const Timespan ×pan2) noexcept
static Timespan zero() noexcept
Get zero timespan.
int64_t hours() const noexcept
Get total hours of the current timespan.
friend Timespan operator-(int64_t offset, const Timespan ×pan) noexcept
C++ Common project definitions.
Timespan inline implementation.