11#include <Kanoop/timespan.h>
12#include "kanoopcommon.h"
28 DateRange(
const QDateTime& startTime,
const QDateTime& endTime) :
29 _startTime(startTime), _endTime(endTime) {}
37 _startTime(startTime), _endTime(startTime.addMSecs(duration.totalMilliseconds())) {}
102 QDateTime
endTime()
const {
return _endTime; }
108 void setEndTime(
const QDateTime& value) { _endTime = value; }
120 QDateTime
midpoint()
const {
return _startTime.addMSecs((duration() / 2).totalMilliseconds()); }
127 bool contains(
const QDateTime& time)
const {
return time >= _startTime && time <= _endTime; }
175 bool isValid()
const {
return _startTime.isValid() && _endTime.isValid(); }
180 class List :
public QList<DateRange>
191 std::sort(sorted.begin(), sorted.end(), [](
const DateRange& a,
const DateRange& b) { return a.startTime() < b.startTime(); });
193 if(sorted.count() < 2) {
199 for (
int i = 1; i < size(); ++i) {
209 result.append(current);
213 result.append(current);
220 QDateTime _startTime;
A sortable, consolidatable list of DateRange objects.
List consolidated() const
Return a new list with overlapping/adjacent ranges merged.
A time range defined by a start and end QDateTime.
DateRange(const QDateTime &startTime, const TimeSpan &duration)
Construct a DateRange from a start time and duration.
void setStartTime(const QDateTime &value)
Set the range start time.
static DateRange merged(const DateRange &a, const DateRange &b)
Return the union of two ranges (static convenience overload).
DateRange subRange(const DateRange &other) const
Return the intersection of this range with another.
DateRange merged(const DateRange &other) const
Return the union of this range and another.
bool intersects(const DateRange &other) const
Test whether another DateRange is intersects with this one.
DateRange operator-(const TimeSpan &other) const
Shift both endpoints backward by a TimeSpan.
DateRange(const DateRange &other)
Copy constructor.
bool contains(const QDateTime &time) const
Test whether a timestamp falls within the range (inclusive).
static DateRange fromMidpoint(const QDateTime &midpoint, const TimeSpan &duration)
Create a DateRange centred on a midpoint with the given duration.
void operator+=(const TimeSpan &other)
Shift both endpoints forward by a TimeSpan in place.
void setEndTime(const QDateTime &value)
Set the range end time.
DateRange operator+(const TimeSpan &other) const
Shift both endpoints forward by a TimeSpan.
DateRange & operator=(const DateRange &other)
Assignment operator.
QDateTime endTime() const
Return the range end time.
void operator-=(const TimeSpan &other)
Shift both endpoints backward by a TimeSpan in place.
DateRange(const QDateTime &startTime, const QDateTime &endTime)
Construct a DateRange from explicit start and end times.
bool isValid() const
Check whether both endpoints are valid QDateTime values.
bool operator==(const DateRange &other) const
Equality comparison.
QString toString() const
Format the range as a human-readable string.
DateRange()
Default constructor — creates an invalid (empty) date range.
QDateTime midpoint() const
Return the midpoint of the range.
bool contains(const DateRange &other) const
Test whether another DateRange is entirely contained within this one.
QDateTime startTime() const
Return the range start time.
TimeSpan duration() const
Return the absolute duration of the range.
bool operator!=(const DateRange &other) const
Inequality comparison.
static TimeSpan absDiff(const QDateTime &t1, const QDateTime &t2)
Compute the absolute (non-negative) difference between two QDateTime values.