OSDN Git Service

fix compile problem
[tjqt4port/tj2qt4.git] / taskjuggler / Interval.h
1 /*
2  * Interval.h - TaskJuggler
3  *
4  * Copyright (c) 2001, 2002, 2003, 2004 by Chris Schlaeger <cs@kde.org>
5  *
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.
9  *
10  * $Id$
11  */
12
13 #ifndef _Interval_h_
14 #define _Interval_h_
15
16 #include <stdio.h>
17 #include "Utility.h"
18
19 class Interval
20 {
21 public:
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() { }
27
28     bool isNull() const { return !(start < end); }
29
30     bool contains(time_t date) const
31     {
32         return (start <= date) && (date <= end);
33     }
34
35     bool contains(const Interval& i) const
36     {
37         return (start <= i.start) && (i.end <= end);
38     }
39     bool overlap(const Interval& i)
40     {
41         // Sets the interval to the overlapping interval.
42         if (end <= i.start || start >= i.end)
43         {
44             // Intervals do not overlap.
45             end = start - 1;
46             return false;
47         }
48         if (start < i.start)
49             start = i.start;
50         if (end > i.end)
51             end = i.end;
52         return true;
53     }
54     bool overlaps(const Interval& i) const
55     {
56         return (start <= end && i.start <= i.end &&
57                 ((start <= i.start && i.start <= end) ||
58                  (i.start <= start && start <= i.end)));
59     }
60     bool prepend(const Interval& i)
61     {
62         if (((i.end + 1) == start) && (i.start < start))
63         {
64             start = i.start;
65             return true;
66         }
67         return false;
68     }
69     bool append(const Interval& i)
70     {
71         if (((end + 1) == i.start) && ((end + 1) <= i.end))
72         {
73             end = i.end;
74             return true;
75         }
76         return false;
77     }
78     int compare(const Interval& i) const
79     {
80         if (end < i.start)
81             return -1;  // interval is below i
82         else if (i.end < start)
83             return 1;   // interval above below i
84         else
85             return 0;   // interval overlap
86     }
87     bool operator==(const Interval& i) const
88     {
89         return start == i.start && end == i.end;
90     }
91     bool operator!=(const Interval& i) const
92     {
93         return start != i.start || end != i.end;
94     } 
95
96     time_t getStart() const { return start; }
97     void setStart(time_t s) { start = s; }
98
99     time_t getEnd() const { return end; }
100     void setEnd(time_t e) { end = e; }
101
102     time_t getDuration() const { return end >= start ? end - start + 1: 0; }
103
104     const Interval& firstDay()
105     {
106         start = midnight(start);
107         end = sameTimeNextDay(start) - 1;
108         return *this;
109     }
110
111     const Interval& firstWeek(bool weekStartsMonday)
112     {
113         start = beginOfWeek(start, weekStartsMonday);
114         end = sameTimeNextWeek(start) - 1;
115         return *this;
116     }
117
118     const Interval& firstMonth()
119     {
120         start = beginOfMonth(start);
121         end = sameTimeNextMonth(start) - 1;
122         return *this;
123     }
124
125     const Interval& firstQuarter()
126     {
127         start = beginOfQuarter(start);
128         end = sameTimeNextQuarter(start) - 1;
129         return *this;
130     }
131
132     const Interval& firstYear()
133     {
134         start = beginOfYear(start);
135         end = sameTimeNextYear(start) - 1;
136         return *this;
137     }
138
139 protected:
140     /// The start of the time interval.
141     time_t start;
142     /// The end of the time interval. This value is part of the interval.
143     time_t end;
144 } ;
145
146 #endif