2 * Interval.h - TaskJuggler
4 * Copyright (c) 2001, 2002, 2003, 2004 by Chris Schlaeger <cs@kde.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of version 2 of the GNU General Public License as
8 * published by the Free Software Foundation.
22 Interval(time_t s, time_t e) : start(s), end(e) { }
23 Interval(time_t s) : start(s), end(s) { }
24 Interval() : start(0), end(0) { }
25 Interval(const Interval& iv) : start(iv.start), end(iv.end) { }
26 virtual ~Interval() { }
28 bool isNull() const { return !(start < end); }
30 bool contains(time_t date) const
32 return (start <= date) && (date <= end);
35 bool contains(const Interval& i) const
37 return (start <= i.start) && (i.end <= end);
39 bool overlap(const Interval& i)
41 // Sets the interval to the overlapping interval.
42 if (end <= i.start || start >= i.end)
44 // Intervals do not overlap.
54 bool overlaps(const Interval& i) const
56 return (start <= end && i.start <= i.end &&
57 ((start <= i.start && i.start <= end) ||
58 (i.start <= start && start <= i.end)));
60 bool prepend(const Interval& i)
62 if (((i.end + 1) == start) && (i.start < start))
69 bool append(const Interval& i)
71 if (((end + 1) == i.start) && ((end + 1) <= i.end))
78 int compare(const Interval& i) const
81 return -1; // interval is below i
82 else if (i.end < start)
83 return 1; // interval above below i
85 return 0; // interval overlap
87 bool operator==(const Interval& i) const
89 return start == i.start && end == i.end;
91 bool operator!=(const Interval& i) const
93 return start != i.start || end != i.end;
96 time_t getStart() const { return start; }
97 void setStart(time_t s) { start = s; }
99 time_t getEnd() const { return end; }
100 void setEnd(time_t e) { end = e; }
102 time_t getDuration() const { return end >= start ? end - start + 1: 0; }
104 const Interval& firstDay()
106 start = midnight(start);
107 end = sameTimeNextDay(start) - 1;
111 const Interval& firstWeek(bool weekStartsMonday)
113 start = beginOfWeek(start, weekStartsMonday);
114 end = sameTimeNextWeek(start) - 1;
118 const Interval& firstMonth()
120 start = beginOfMonth(start);
121 end = sameTimeNextMonth(start) - 1;
125 const Interval& firstQuarter()
127 start = beginOfQuarter(start);
128 end = sameTimeNextQuarter(start) - 1;
132 const Interval& firstYear()
134 start = beginOfYear(start);
135 end = sameTimeNextYear(start) - 1;
140 /// The start of the time interval.
142 /// The end of the time interval. This value is part of the interval.