| 1 |
/* |
| 2 |
* Interval.h - TaskJuggler |
| 3 |
* |
| 4 |
* SPDX-FileCopyrightText: 2001, 2002, 2003, 2004 Chris Schlaeger <[email protected]> |
| 5 |
* SPDX-FileCopyrightText: 2011 Dag Andersen <[email protected]> |
| 6 |
* |
| 7 |
* This program is free software; you can redistribute it and/or modify |
| 8 |
* it under the terms of version 2 of the GNU General Public License as |
| 9 |
* published by the Free Software Foundation. |
| 10 |
* |
| 11 |
* $Id$ |
| 12 |
*/ |
| 13 |
|
| 14 |
#ifndef _XInterval_h_ |
| 15 |
#define _XInterval_h_ |
| 16 |
|
| 17 |
#include "plantj_export.h" |
| 18 |
|
| 19 |
#include <stdio.h> |
| 20 |
#include "Utility.h" |
| 21 |
|
| 22 |
#include <QDebug> |
| 23 |
#include <QMetaType> |
| 24 |
|
| 25 |
namespace TJ |
| 26 |
{ |
| 27 |
|
| 28 |
class PLANTJ_EXPORT Interval |
| 29 |
{ |
| 30 |
public: |
| 31 |
Interval(time_t s, time_t e) : start(s), end(e) { } |
| 32 |
Interval(time_t s) : start(s), end(s) { } |
| 33 |
Interval() : start(0), end(0) { } |
| 34 |
Interval(const Interval& iv) : start(iv.start), end(iv.end) { } |
| 35 |
virtual ~Interval() { } |
| 36 |
|
| 37 |
bool isNull() const { return !(start < end); } |
| 38 |
|
| 39 |
bool contains(time_t date) const |
| 40 |
{ |
| 41 |
return (start <= date) && (date <= end); |
| 42 |
} |
| 43 |
|
| 44 |
bool contains(const Interval& i) const |
| 45 |
{ |
| 46 |
return (start <= i.start) && (i.end <= end); |
| 47 |
} |
| 48 |
bool overlap(const Interval& i) |
| 49 |
{ |
| 50 |
// Sets the interval to the overlapping interval. |
| 51 |
if (end <= i.start || start >= i.end) |
| 52 |
{ |
| 53 |
// Intervals do not overlap. |
| 54 |
end = start - 1; |
| 55 |
return false; |
| 56 |
} |
| 57 |
if (start < i.start) |
| 58 |
start = i.start; |
| 59 |
if (end > i.end) |
| 60 |
end = i.end; |
| 61 |
return true; |
| 62 |
} |
| 63 |
bool overlaps(const Interval& i) const |
| 64 |
{ |
| 65 |
return (start <= end && i.start <= i.end && |
| 66 |
((start <= i.start && i.start <= end) || |
| 67 |
(i.start <= start && start <= i.end))); |
| 68 |
} |
| 69 |
bool prepend(const Interval& i) |
| 70 |
{ |
| 71 |
if (((i.end + 1) == start) && (i.start < start)) |
| 72 |
{ |
| 73 |
start = i.start; |
| 74 |
return true; |
| 75 |
} |
| 76 |
return false; |
| 77 |
} |
| 78 |
bool append(const Interval& i) |
| 79 |
{ |
| 80 |
if (((end + 1) == i.start) && ((end + 1) <= i.end)) |
| 81 |
{ |
| 82 |
end = i.end; |
| 83 |
return true; |
| 84 |
} |
| 85 |
return false; |
| 86 |
} |
| 87 |
int compare(const Interval& i) const |
| 88 |
{ |
| 89 |
if (end < i.start) |
| 90 |
return -1; // interval is below i |
| 91 |
else if (i.end < start) |
| 92 |
return 1; // interval above below i |
| 93 |
else |
| 94 |
return 0; // interval overlap |
| 95 |
} |
| 96 |
bool operator==(const Interval& i) const |
| 97 |
{ |
| 98 |
return start == i.start && end == i.end; |
| 99 |
} |
| 100 |
bool operator!=(const Interval& i) const |
| 101 |
{ |
| 102 |
return start != i.start || end != i.end; |
| 103 |
} |
| 104 |
|
| 105 |
time_t getStart() const { return start; } |
| 106 |
void setStart(time_t s) { start = s; } |
| 107 |
|
| 108 |
time_t getEnd() const { return end; } |
| 109 |
void setEnd(time_t e) { end = e; } |
| 110 |
|
| 111 |
time_t getDuration() const { return end >= start ? end - start + 1: 0; } |
| 112 |
|
| 113 |
const Interval& firstDay() |
| 114 |
{ |
| 115 |
start = midnight(start); |
| 116 |
end = sameTimeNextDay(start) - 1; |
| 117 |
return *this; |
| 118 |
} |
| 119 |
|
| 120 |
const Interval& firstWeek(bool weekStartsMonday) |
| 121 |
{ |
| 122 |
start = beginOfWeek(start, weekStartsMonday); |
| 123 |
end = sameTimeNextWeek(start) - 1; |
| 124 |
return *this; |
| 125 |
} |
| 126 |
|
| 127 |
const Interval& firstMonth() |
| 128 |
{ |
| 129 |
start = beginOfMonth(start); |
| 130 |
end = sameTimeNextMonth(start) - 1; |
| 131 |
return *this; |
| 132 |
} |
| 133 |
|
| 134 |
const Interval& firstQuarter() |
| 135 |
{ |
| 136 |
start = beginOfQuarter(start); |
| 137 |
end = sameTimeNextQuarter(start) - 1; |
| 138 |
return *this; |
| 139 |
} |
| 140 |
|
| 141 |
const Interval& firstYear() |
| 142 |
{ |
| 143 |
start = beginOfYear(start); |
| 144 |
end = sameTimeNextYear(start) - 1; |
| 145 |
return *this; |
| 146 |
} |
| 147 |
|
| 148 |
protected: |
| 149 |
/// The start of the time interval. |
| 150 |
time_t start; |
| 151 |
/// The end of the time interval. This value is part of the interval. |
| 152 |
time_t end; |
| 153 |
} ; |
| 154 |
|
| 155 |
} // namespace TJ |
| 156 |
|
| 157 |
Q_DECLARE_TYPEINFO(TJ::Interval, Q_MOVABLE_TYPE); |
| 158 |
Q_DECLARE_METATYPE(TJ::Interval) |
| 159 |
|
| 160 |
PLANTJ_EXPORT QDebug operator<<(QDebug dbg, const TJ::Interval *i); |
| 161 |
PLANTJ_EXPORT QDebug operator<<(QDebug dbg, const TJ::Interval &i); |
| 162 |
|
| 163 |
#endif |