27 #include <OnixS/Core/Time/DateTime.h> 28 #include <OnixS/Core/Time/Operators.h> 29 #include <OnixS/Core/Time/Formatters.h> 31 #include "NamespaceHelper.h" 34 namespace Core = OnixS::Time;
36 ONIXS_HANDLER_NAMESPACE_BEGIN
42 convert(
const TimeSpan& timeSpan)
44 return Core::TimeSpan(
45 timeSpan.totalSeconds(),
46 Core::NanosecondSubseconds(timeSpan.nanoseconds()));
51 convert(
const Core::TimeSpan& timeSpan)
54 timeSpan.totalSeconds(),
55 timeSpan.nanoseconds());
59 throwBadTimeSpanFormat(
60 const std::string& timeSpan)
62 throw std::logic_error(std::string(
"Time format is invalid: ") + timeSpan);
66 const TimeSpan TimeSpan::Zero(0, 0);
69 : seconds_(0), nanoseconds_(0)
82 Core::NanosecondSubseconds(nanoseconds)));
95 Core::NanosecondSubseconds(nanoseconds)));
104 Normalized(totalSeconds, Core::NanosecondSubseconds(nanoseconds));
106 seconds_ = Normalized.totalSeconds();
107 nanoseconds_ = Normalized.nanoseconds();
112 : seconds_(other.seconds_),
113 nanoseconds_(other.nanoseconds_)
120 return convert(*this).days();
126 return convert(*this).hours();
132 return convert(*this).minutes();
138 return convert(*this).seconds();
144 return convert(*this).milliseconds();
150 return convert(*this).microseconds();
157 return convert(*
this) == convert(other);
164 return convert(*
this) != convert(other);
171 return convert(*
this) < convert(other);
178 return convert(*
this) > convert(other);
185 Core::TimeSpan result(convert(*
this));
187 result += convert(other);
189 return (*
this = convert(result));
197 Core::TimeSpan result(convert(*
this));
199 result -= convert(other);
201 return (*
this = convert(result));
208 seconds_ = other.seconds_;
209 nanoseconds_ = other.nanoseconds_;
223 Core::HHMMSSFormatter()(convert(*
this), str);
229 Core::HHMMSSmsecFormatter()(convert(*
this), str);
235 Core::SDHHMMSSnsecFormatter()(convert(*
this), str);
241 throw std::logic_error(
"Unknown TimeSpan serialization format specified.");
250 Core::TimeSpan parsedSpan;
252 if (!Core::TimeSpan::parse(
253 str.data(), str.size(), parsedSpan))
255 throwBadTimeSpanFormat(str);
258 return convert(parsedSpan);
261 ONIXS_HANDLER_NAMESPACE_END
bool operator>(const TimeSpan &other) const
Checks whether time interval greater than other one.
static TimeSpan deserialize(const std::string &str)
De-serializes time interval from its text presentation.
TimeSpan & operator+=(const TimeSpan &other)
Adds time interval to current one.
TimeSpan & operator=(const TimeSpan &other)
Re-assigns time interval from other one.
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.
long long totalSeconds() const
Whole number of seconds in time interval.
TimeSpan()
Initializes zero span.
bool operator==(const TimeSpan &other) const
Compares with other instance for equality.
void toString(std::string &str, TimeSpanFormat format=TimeSpanFormats::SDHHMMSSnsec) const
bool operator!=(const TimeSpan &other) const
Compares with other instance for in-equality.