25 #include <OnixS/Core/Time/TimeSpan.h> 26 #include <OnixS/Core/Time/Operators.h> 32 namespace Core = OnixS::Time;
40 return Core::TimeSpan(
42 Core::NanosecondSubseconds(timeSpan.
nanoseconds()));
48 convert(
const Core::TimeSpan& timeSpan)
51 timeSpan.totalSeconds(),
52 timeSpan.nanoseconds());
56 throwBadTimeSpanFormat(
57 const std::string& timeSpan)
59 throw std::logic_error(std::string(
"Time format is invalid: ") + timeSpan);
66 : seconds_(0), nanoseconds_(0)
78 seconds, Core::NanosecondSubseconds(nanoseconds)));
90 seconds, Core::NanosecondSubseconds(nanoseconds)));
99 Normalized(totalSeconds, Core::NanosecondSubseconds(nanoseconds));
101 seconds_ = Normalized.totalSeconds();
102 nanoseconds_ = Normalized.nanoseconds();
107 : seconds_(other.seconds_),
108 nanoseconds_(other.nanoseconds_)
115 return convert(*this).days();
121 return convert(*this).hours();
127 return convert(*this).minutes();
133 return convert(*this).seconds();
139 return convert(*this).milliseconds();
145 return convert(*this).microseconds();
152 return convert(*
this) == convert(other);
159 return convert(*
this) != convert(other);
166 return convert(*
this) < convert(other);
173 return convert(*
this) > convert(other);
180 Core::TimeSpan result(convert(*
this));
182 result += convert(other);
184 return (*
this = convert(result));
192 Core::TimeSpan result(convert(*
this));
194 result -= convert(other);
196 return (*
this = convert(result));
203 seconds_ = other.seconds_;
204 nanoseconds_ = other.nanoseconds_;
218 Core::HHMMSSFormatter()(convert(*
this), str);
224 Core::HHMMSSmsecFormatter()(convert(*
this), str);
230 Core::SDHHMMSSnsecFormatter()(convert(*
this), str);
236 throw std::logic_error(
"Unknown TimeSpan serialization format specified.");
245 Core::TimeSpan parsedSpan;
247 if (!Core::TimeSpan::parse(
248 str.data(), str.size(), parsedSpan))
250 throwBadTimeSpanFormat(str);
253 return convert(parsedSpan);
void toString(std::string &str, TimeSpanFormat format=TimeSpanFormats::SDHHMMSSnsec) const
TimeSpan()
Initializes zero span.
bool operator>(const TimeSpan &other) const
Checks whether time interval greater than other one.
long long totalSeconds() const
Whole number of seconds in time interval.
TimeSpan & operator-=(const TimeSpan &other)
Subtracts time interval from current one.
bool operator<(const TimeSpan &other) const
Checks whether time interval less than other one.
TimeSpan & operator=(const TimeSpan &other)
Re-assigns time interval from other one.
TimeSpan & operator+=(const TimeSpan &other)
Adds time interval to current one.
static TimeSpan deserialize(const std::string &str)
De-serializes time interval from its text presentation.
bool operator==(const TimeSpan &other) const
Compares with other instance for equality.
bool operator!=(const TimeSpan &other) const
Compares with other instance for in-equality.