Mantid
Loading...
Searching...
No Matches
DateAndTime.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
8
9#include <boost/date_time/date.hpp>
10#include <boost/date_time/time.hpp>
11
12#include <cmath>
13#include <exception>
14#include <limits>
15#include <memory>
16#include <ostream>
17#include <stdexcept>
18
19namespace Mantid {
20
21using namespace Types::Core;
22namespace Kernel {
23
24TimeInterval::TimeInterval(const Types::Core::DateAndTime &from, const Types::Core::DateAndTime &to) : m_start(from) {
25 if (to > from)
26 m_stop = to;
27 else
28 m_stop = from;
29}
30
31TimeInterval::TimeInterval(const std::string &from, const std::string &to) {
32 const DateAndTime fromObj(from);
33 const DateAndTime toObj(to);
34
35 m_start = fromObj;
36 if (toObj > fromObj)
37 m_stop = toObj;
38 else
39 m_stop = fromObj;
40}
41
42bool TimeInterval::overlaps(const TimeInterval *other) const {
43 const auto &thisBegin = this->start();
44 const auto &thisEnd = this->stop();
45 const auto &otherBegin = other->start();
46 const auto &otherEnd = other->stop();
47
48 return ((otherBegin < thisEnd) && (otherBegin >= thisBegin)) || ((otherEnd < thisEnd) && (otherEnd >= thisBegin)) ||
49 ((thisBegin < otherEnd) && (thisBegin >= otherBegin)) || ((thisEnd < otherEnd) && (thisEnd >= otherBegin));
50}
51
52bool TimeInterval::overlaps(const TimeInterval &other) const { return this->overlaps(&other); }
53
60 if (!isValid() || !ti.isValid() || !this->overlaps(&ti))
61 return TimeInterval();
62
63 const auto t1 = std::max(start(), ti.start());
64 const auto t2 = std::min(stop(), ti.stop());
65
66 return t1 < t2 ? TimeInterval(t1, t2) : TimeInterval();
67}
68
70 if (stop() < ti.start())
71 return true;
72 else if (stop() == ti.start())
73 return start() < ti.start();
74 else
75 return false;
76}
78 if (start() > ti.stop())
79 return true;
80 else if (start() == ti.stop())
81 return start() > ti.start();
82 else
83 return false;
84}
85
86Types::Core::time_duration TimeInterval::length() const { return m_stop - m_start; }
87
88double TimeInterval::duration() const { return Types::Core::DateAndTime::secondsFromDuration(this->length()); }
89
90bool TimeInterval::operator==(const TimeInterval &ti) const { return (start() == ti.start()) && (stop() == ti.stop()); }
91
93std::string TimeInterval::begin_str() const { return boost::posix_time::to_simple_string(this->m_start.to_ptime()); }
94
96std::string TimeInterval::end_str() const { return boost::posix_time::to_simple_string(this->m_stop.to_ptime()); }
97
98std::ostream &operator<<(std::ostream &s, const Mantid::Kernel::TimeInterval &t) {
99 s << t.start().toSimpleString() << " - " << t.stop().toSimpleString();
100 return s;
101}
102
103} // namespace Kernel
104
105} // namespace Mantid
Represents a time interval.
Definition DateAndTime.h:25
TimeInterval()
Default constructor.
Definition DateAndTime.h:28
const Types::Core::DateAndTime & start() const
Beginning of the interval.
Definition DateAndTime.h:34
bool overlaps(const TimeInterval *other) const
Return true if the SplittingInterval overlaps with this one.
bool isValid() const
True if the interval is not empty.
Definition DateAndTime.h:38
Types::Core::DateAndTime m_stop
end
Definition DateAndTime.h:70
const Types::Core::DateAndTime & stop() const
End of the interval.
Definition DateAndTime.h:36
bool operator>(const TimeInterval &ti) const
bool operator==(const TimeInterval &ti) const
TimeInterval intersection(const TimeInterval &ti) const
Returns an intersection of two intervals.
double duration() const
in seconds
bool operator<(const TimeInterval &ti) const
Returns true if this interval ends before ti starts.
Types::Core::time_duration length() const
Interval length (in seconds?)
Types::Core::DateAndTime m_start
begin
Definition DateAndTime.h:68
std::string end_str() const
String representation of the end time.
std::string begin_str() const
String representation of the begin time.
MANTID_KERNEL_DLL std::ostream & operator<<(std::ostream &, CPUTimer &)
Convenience function to provide for easier debug printing.
Definition CPUTimer.cpp:86
Helper class which provides the Collimation Length for SANS instruments.