KanoopCommonQt 2.1.1
Kanoop foundational Qt utility library
Loading...
Searching...
No Matches
DateRange Class Reference

A time range defined by a start and end QDateTime. More...

#include <daterange.h>

Classes

class  List
 A sortable, consolidatable list of DateRange objects. More...
 

Public Member Functions

 DateRange ()
 Default constructor — creates an invalid (empty) date range.
 
 DateRange (const QDateTime &startTime, const QDateTime &endTime)
 Construct a DateRange from explicit start and end times.
 
 DateRange (const QDateTime &startTime, const TimeSpan &duration)
 Construct a DateRange from a start time and duration.
 
 DateRange (const DateRange &other)
 Copy constructor.
 
DateRangeoperator= (const DateRange &other)
 Assignment operator.
 
DateRange operator+ (const TimeSpan &other) const
 Shift both endpoints forward by a TimeSpan.
 
DateRange operator- (const TimeSpan &other) const
 Shift both endpoints backward by a TimeSpan.
 
void operator+= (const TimeSpan &other)
 Shift both endpoints forward by a TimeSpan in place.
 
void operator-= (const TimeSpan &other)
 Shift both endpoints backward by a TimeSpan in place.
 
bool operator== (const DateRange &other) const
 Equality comparison.
 
bool operator!= (const DateRange &other) const
 Inequality comparison.
 
QDateTime startTime () const
 Return the range start time.
 
void setStartTime (const QDateTime &value)
 Set the range start time.
 
QDateTime endTime () const
 Return the range end time.
 
void setEndTime (const QDateTime &value)
 Set the range end time.
 
TimeSpan duration () const
 Return the absolute duration of the range.
 
QDateTime midpoint () const
 Return the midpoint of the range.
 
bool contains (const QDateTime &time) const
 Test whether a timestamp falls within the range (inclusive).
 
bool contains (const DateRange &other) const
 Test whether another DateRange is entirely contained within this one.
 
bool intersects (const DateRange &other) const
 Test whether another DateRange is intersects with this one.
 
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.
 
QString toString () const
 Format the range as a human-readable string.
 
bool isValid () const
 Check whether both endpoints are valid QDateTime values.
 

Static Public Member Functions

static DateRange fromMidpoint (const QDateTime &midpoint, const TimeSpan &duration)
 Create a DateRange centred on a midpoint with the given duration.
 
static DateRange merged (const DateRange &a, const DateRange &b)
 Return the union of two ranges (static convenience overload).
 

Detailed Description

A time range defined by a start and end QDateTime.

DateRange represents a contiguous span of time. It supports arithmetic with TimeSpan, overlap/containment queries, and consolidation of overlapping ranges via DateRange::List.

Represents a contiguous date/time range with start and end timestamps.

Definition at line 17 of file daterange.h.

Constructor & Destructor Documentation

◆ DateRange() [1/4]

DateRange::DateRange ( )
inline

Default constructor — creates an invalid (empty) date range.

Definition at line 21 of file daterange.h.

◆ DateRange() [2/4]

DateRange::DateRange ( const QDateTime &  startTime,
const QDateTime &  endTime 
)
inline

Construct a DateRange from explicit start and end times.

Parameters
startTimeRange begin timestamp
endTimeRange end timestamp

Definition at line 28 of file daterange.h.

◆ DateRange() [3/4]

DateRange::DateRange ( const QDateTime &  startTime,
const TimeSpan duration 
)
inline

Construct a DateRange from a start time and duration.

Parameters
startTimeRange begin timestamp
durationLength of the range

Definition at line 36 of file daterange.h.

◆ DateRange() [4/4]

DateRange::DateRange ( const DateRange other)

Copy constructor.

Parameters
otherDateRange to copy.

Member Function Documentation

◆ contains() [1/2]

bool DateRange::contains ( const DateRange other) const
inline

Test whether another DateRange is entirely contained within this one.

Parameters
otherRange to test
Returns
true if other is fully within this range

Definition at line 134 of file daterange.h.

References contains(), endTime(), and startTime().

Referenced by contains().

◆ contains() [2/2]

bool DateRange::contains ( const QDateTime &  time) const
inline

Test whether a timestamp falls within the range (inclusive).

Parameters
timeTimestamp to test
Returns
true if time >= startTime && time <= endTime

Definition at line 127 of file daterange.h.

◆ duration()

TimeSpan DateRange::duration ( ) const
inline

Return the absolute duration of the range.

Returns
TimeSpan between start and end

Definition at line 114 of file daterange.h.

References TimeSpan::absDiff().

◆ endTime()

QDateTime DateRange::endTime ( ) const
inline

Return the range end time.

Returns
End QDateTime

Definition at line 102 of file daterange.h.

Referenced by DateRange::List::consolidated(), and contains().

◆ fromMidpoint()

static DateRange DateRange::fromMidpoint ( const QDateTime &  midpoint,
const TimeSpan duration 
)
static

Create a DateRange centred on a midpoint with the given duration.

Parameters
midpointCentre of the range
durationTotal length of the range
Returns
DateRange symmetrically straddling midpoint

◆ intersects()

bool DateRange::intersects ( const DateRange other) const

Test whether another DateRange is intersects with this one.

Parameters
otherRange to test
Returns
true if other is fully within this range

◆ isValid()

bool DateRange::isValid ( ) const
inline

Check whether both endpoints are valid QDateTime values.

Returns
true if startTime and endTime are valid

Definition at line 175 of file daterange.h.

◆ merged() [1/2]

static DateRange DateRange::merged ( const DateRange a,
const DateRange b 
)
inlinestatic

Return the union of two ranges (static convenience overload).

Parameters
aFirst range
bSecond range
Returns
DateRange spanning both

Definition at line 163 of file daterange.h.

References merged().

◆ merged() [2/2]

DateRange DateRange::merged ( const DateRange other) const

Return the union of this range and another.

Parameters
otherRange to merge with
Returns
DateRange spanning both ranges

Referenced by merged().

◆ midpoint()

QDateTime DateRange::midpoint ( ) const
inline

Return the midpoint of the range.

Returns
QDateTime at the centre of the range

Definition at line 120 of file daterange.h.

◆ operator!=()

bool DateRange::operator!= ( const DateRange other) const

Inequality comparison.

Parameters
otherDateRange to compare.
Returns
True if the ranges differ.

◆ operator+()

DateRange DateRange::operator+ ( const TimeSpan other) const

Shift both endpoints forward by a TimeSpan.

Parameters
otherAmount to add
Returns
New shifted DateRange

◆ operator+=()

void DateRange::operator+= ( const TimeSpan other)

Shift both endpoints forward by a TimeSpan in place.

Parameters
otherAmount to add.

◆ operator-()

DateRange DateRange::operator- ( const TimeSpan other) const

Shift both endpoints backward by a TimeSpan.

Parameters
otherAmount to subtract
Returns
New shifted DateRange

◆ operator-=()

void DateRange::operator-= ( const TimeSpan other)

Shift both endpoints backward by a TimeSpan in place.

Parameters
otherAmount to subtract.

◆ operator=()

DateRange & DateRange::operator= ( const DateRange other)

Assignment operator.

Parameters
otherDateRange to assign from.
Returns
Reference to this.

◆ operator==()

bool DateRange::operator== ( const DateRange other) const

Equality comparison.

Parameters
otherDateRange to compare.
Returns
True if both endpoints are equal.

◆ setEndTime()

void DateRange::setEndTime ( const QDateTime &  value)
inline

Set the range end time.

Parameters
valueNew end QDateTime

Definition at line 108 of file daterange.h.

◆ setStartTime()

void DateRange::setStartTime ( const QDateTime &  value)
inline

Set the range start time.

Parameters
valueNew start QDateTime

Definition at line 96 of file daterange.h.

◆ startTime()

QDateTime DateRange::startTime ( ) const
inline

Return the range start time.

Returns
Start QDateTime

Definition at line 90 of file daterange.h.

Referenced by DateRange::List::consolidated(), and contains().

◆ subRange()

DateRange DateRange::subRange ( const DateRange other) const

Return the intersection of this range with another.

Parameters
otherRange to intersect with
Returns
Overlapping sub-range, or an invalid DateRange if no overlap

◆ toString()

QString DateRange::toString ( ) const

Format the range as a human-readable string.

Returns
String representation of start and end times

The documentation for this class was generated from the following file: