[MERGE] libi2ncommon: (reinhard) added operator!= to Interval; aded some doc.
[libi2ncommon] / src / timefunc.hxx
CommitLineData
1b5dfd98
TJ
1/** @file
2 * @brief time related functions.
3 *
4 * @copyright Copyright © 2001-2008 by Intra2net AG
5 * @license commercial
6 * @contact info@intra2net.com
7 *
8 */
e93545dd
GE
9
10#ifndef __TIMEFUNC_HXX
11#define __TIMEFUNC_HXX
12
1b5dfd98 13#include <string>
f1499910 14#include <bitset>
1b5dfd98
TJ
15#include <list>
16
f1499910 17
e93545dd
GE
18double prec_time(void);
19
20int date_to_seconds(const std::string &date);
21
22std::string make_nice_time(int seconds);
23std::string format_full_time(int seconds);
87869870 24void seconds_to_hour_minute(int seconds, int *hour, int *minute);
1344894d 25std::string output_hour_minute(int hour, int minute, bool h_for_00=true);
2c66f490
GE
26
27inline std::string output_hour_minute_from_seconds(int seconds)
28{
29 int hour, minute;
30 seconds_to_hour_minute(seconds,&hour,&minute);
31 return output_hour_minute(hour,minute);
32}
e93545dd 33
4e157d1d
TJ
34std::string get_month_name(unsigned char month);
35
21f2d3db
TJ
36
37/**
38 * @brief represents some days of a week.
39 *
40 */
f1499910
GE
41class WEEK
42{
43 private:
44 std::bitset<7> days;
45
46 public:
47 enum WEEKDAY { SU=0, MO=1, TU=2, WE=3, TH=4, FR=5, SA=6 };
21f2d3db 48
f1499910
GE
49 // throws out_of_range if illegal week
50 WEEK(const std::string& daystring);
21f2d3db
TJ
51
52 WEEK(const std::bitset<7> &_days)
53 : days(_days)
f1499910 54 { }
21f2d3db 55
6781ce49
GE
56 WEEK()
57 { }
21f2d3db 58
f1499910
GE
59 void clear()
60 { days.reset(); }
21f2d3db 61
f1499910
GE
62 operator std::bitset<7>() const
63 { return days; }
21f2d3db 64
ae3915ac
GE
65 void set(WEEKDAY d, bool value=true)
66 { days[d]=value; }
21f2d3db 67
aff80c26
TJ
68 bool get(WEEKDAY d) const { return days[d]; }
69 bool is_set(WEEKDAY d) const { return days[d]; }
70
ae3915ac
GE
71 bool all_set() const
72 { return (days.count()==7); }
73 bool none_set() const
74 { return days.none(); }
21f2d3db 75
f1499910
GE
76 std::string get_daystring() const;
77 std::string get_displaystring() const;
78 std::string get_netfilterstring() const;
21f2d3db 79
f1499910
GE
80 static std::string get_day_display(WEEKDAY day);
81 static std::string get_english_display(WEEKDAY day);
21f2d3db
TJ
82
83 // some operators for convenience:
84
85 WEEK& operator&=(const WEEK& rhs)
86 {
87 days &= rhs.days;
88 return *this;
89 }
90
91 WEEK& operator|=(const WEEK& rhs)
92 {
93 days|= rhs.days;
94 return *this;
95 }
96
97 WEEK& operator^=(const WEEK& rhs)
98 {
99 days^= rhs.days;
100 return *this;
101 }
102
103 bool operator==(const WEEK& rhs)
104 {
105 return days == rhs.days;
106 }
107
108 bool operator!=(const WEEK& rhs)
109 {
110 return days != rhs.days;
111 }
112
113}; // eo class WEEK
114
115
116/**
117 * @brief delivers a week containing the days which are in both weeks.
118 * @param lhs first week.
119 * @param rhs second week.
120 * @return week which has only those days which are in both weeks.
121 */
122inline WEEK operator&(const WEEK& lhs, const WEEK& rhs)
123{
124 WEEK result(lhs);
125 return result &= rhs;
126} // eo operator&(const WEEK&,const WEEK&)
127
128
129/**
130 * @brief delivers a week containing the days which are at least in one of both weeks.
131 * @param lhs first week.
132 * @param rhs second week.
133 * @return week which has only those days which are at least in one of both weeks.
134 */
135inline WEEK operator|(const WEEK& lhs, const WEEK& rhs)
136{
137 WEEK result(lhs);
138 return result |= rhs;
139} // eo operator&(const WEEK&,const WEEK&)
140
141
142/**
143 * @brief delivers a week containing the days which are in only one of both weeks.
144 * @param lhs first week.
145 * @param rhs second week.
146 * @return week which has only those days which are in only one of both weeks.
147 */
148inline WEEK operator^(const WEEK& lhs, const WEEK& rhs)
149{
150 WEEK result(lhs);
151 return result ^= rhs;
152} // eo operator&(const WEEK&,const WEEK&)
f1499910 153
1b5dfd98
TJ
154
155/**
156 * @brief structure representing a single (half-open) interval.
157 */
158class Interval
159{
160 public:
161 Interval()
162 : m_lower_bound(0)
163 , m_upper_bound(0)
80f30818
TJ
164 , m_weak_mark(0)
165 , m_changed(false)
1b5dfd98
TJ
166 {
167 } //
168
80f30818 169 Interval( unsigned int start, unsigned int end, int weak_mark= 0 )
1b5dfd98
TJ
170 : m_lower_bound(start)
171 , m_upper_bound(end)
80f30818
TJ
172 , m_weak_mark(weak_mark)
173 , m_changed(false)
1b5dfd98
TJ
174 {
175 } //
176
d181c3bc
TJ
177
178 void clear();
179
1b5dfd98
TJ
180 bool is_valid() const
181 {
182 return m_lower_bound <= m_upper_bound;
183 } // eo is_valid() const
184
185 bool empty() const
186 {
187 return m_lower_bound == m_upper_bound;
188 } // eo empty() const
189
190
191 unsigned int lower_bound() const { return m_lower_bound; }
192 unsigned int upper_bound() const { return m_upper_bound; }
193
80f30818
TJ
194 int weak_mark() const { return m_weak_mark; }
195
196 bool changed() const { return m_changed; }
197
1b5dfd98
TJ
198
199 bool operator== (const Interval& other) const
200 {
201 return m_lower_bound == other.m_lower_bound and m_upper_bound == other.m_upper_bound;
202 } // eo operator==(const Interval&)
203
204
ebc3b584
TJ
205 bool operator!=(const Interval& other) const
206 {
207 return not (*this == other);
208 } // eo operator!=(const Interval&)
209
210
1b5dfd98
TJ
211 /**
212 * @brief less operator. compares only the start times!
213 * @param other the other interval to compare with.
214 * @return @a true if the current start is less than the other start.
215 */
216 bool operator<(const Interval& other) const
217 {
218 return m_lower_bound < other.m_lower_bound;
219 } // eo operator<(const Interval&)
220
221
222 bool intersects(const Interval& other) const;
223 bool contains(const Interval& other) const;
224
225
226 protected:
227
228 friend class Intervals;
229
230 unsigned int m_lower_bound;
231 unsigned int m_upper_bound;
232
80f30818
TJ
233 int m_weak_mark;
234 bool m_changed;
235
1b5dfd98
TJ
236}; // eo Interval
237
238
239
240/**
241 * @brief structure representing a combination of single disjoint (half open) intervals.
242 *
243 * Basic idea is that this structure provides an interface for adding and
244 * subtracting single intervals and keeps the internal list of intervals as
245 * a list of disjoint intervals.
246 *
247 * @note the list is sorted by the start; lower comes first.
248 *
249 * @note the class provides some methods similar to STL container classes;
250 * i.e. it can be used with (at least some) STL algorithms.
251 *
252 * @internal
253 * we use a std::list for the intervals; this means we don't invalidate all
254 * iterators when we insert or delete within loops...
255 * And we use that fact!!
256 */
257class Intervals
258{
259 public:
260 typedef std::list< Interval > IntervalList;
261 typedef IntervalList::value_type value_type;
262 typedef IntervalList::const_iterator const_iterator;
263 typedef IntervalList::const_iterator iterator; // we allow only const...
264 typedef IntervalList::size_type size_type;
265
266 public:
267 Intervals();
268
269 void add(const Interval& new_frame);
270 void sub(const Interval& new_frame);
271
d181c3bc 272 void clear();
1b5dfd98
TJ
273
274 const_iterator begin() const { return m_intervals.begin(); }
275 const_iterator end() const { return m_intervals.end(); }
276
277 bool empty() const { return m_intervals.empty(); }
278 const Interval& front() const { return m_intervals.front(); }
279 const Interval& back() const { return m_intervals.back(); }
280
281 size_type size() const { return m_intervals.size(); }
282
283 bool intersects(const Interval& other) const;
284 bool intersects(const Intervals& other) const;
285
286 bool contains(const Interval& other) const;
287 bool contains(const Intervals& other) const;
288
289 bool operator==(const Intervals& other) const;
ebc3b584 290 bool operator!=(const Intervals& other) const { return not (*this == other) ; }
1b5dfd98
TJ
291
292 Intervals& operator+=(const Interval& other);
293 Intervals& operator-=(const Interval& other);
294
295 Intervals& operator+=(const Intervals& other);
296 Intervals& operator-=(const Intervals& other);
297
298 protected:
299
300 std::list< Interval > m_intervals;
301
302}; // eo Intervals
303
304
305
306
e93545dd 307#endif