[MERGE] libi2ncommon: (reinhard) added operator!= to Interval; aded some doc.
[libi2ncommon] / src / timefunc.hxx
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  */
9
10 #ifndef __TIMEFUNC_HXX
11 #define __TIMEFUNC_HXX
12
13 #include <string>
14 #include <bitset>
15 #include <list>
16
17
18 double prec_time(void);
19
20 int date_to_seconds(const std::string &date);
21
22 std::string make_nice_time(int seconds);
23 std::string format_full_time(int seconds);
24 void seconds_to_hour_minute(int seconds, int *hour, int *minute);
25 std::string output_hour_minute(int hour, int minute, bool h_for_00=true);
26
27 inline 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 }
33
34 std::string get_month_name(unsigned char month);
35
36
37 /**
38  * @brief represents some days of a week.
39  *
40  */
41 class 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 };
48
49         // throws out_of_range if illegal week
50         WEEK(const std::string& daystring);
51
52         WEEK(const std::bitset<7> &_days)
53             : days(_days)
54             { }
55
56         WEEK()
57             { }
58
59         void clear()
60             { days.reset(); }
61
62         operator std::bitset<7>() const
63             { return days; }
64
65         void set(WEEKDAY d, bool value=true)
66             { days[d]=value; }
67
68         bool get(WEEKDAY d) const { return days[d]; }
69         bool is_set(WEEKDAY d) const { return days[d]; }
70
71         bool all_set() const
72             { return (days.count()==7); }
73         bool none_set() const
74             { return days.none(); }
75
76         std::string get_daystring() const;
77         std::string get_displaystring() const;
78         std::string get_netfilterstring() const;
79
80         static std::string get_day_display(WEEKDAY day);
81         static std::string get_english_display(WEEKDAY day);
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  */
122 inline 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  */
135 inline 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  */
148 inline WEEK operator^(const WEEK& lhs, const WEEK& rhs)
149 {
150     WEEK result(lhs);
151     return result ^= rhs;
152 } // eo operator&(const WEEK&,const WEEK&)
153
154
155 /**
156  * @brief structure representing a single (half-open) interval.
157  */
158 class Interval
159 {
160     public:
161         Interval()
162         : m_lower_bound(0)
163         , m_upper_bound(0)
164         , m_weak_mark(0)
165         , m_changed(false)
166         {
167         } //
168
169         Interval( unsigned int start, unsigned int end, int weak_mark= 0 )
170         : m_lower_bound(start)
171         , m_upper_bound(end)
172         , m_weak_mark(weak_mark)
173         , m_changed(false)
174         {
175         } //
176
177
178         void clear();
179
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
194         int weak_mark() const { return m_weak_mark; }
195
196         bool changed() const { return m_changed; }
197
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
205         bool  operator!=(const Interval& other) const
206         {
207             return not (*this == other);
208         } // eo operator!=(const Interval&)
209
210
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
233         int m_weak_mark;
234         bool m_changed;
235
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  */
257 class 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
272         void clear();
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;
290         bool operator!=(const Intervals& other) const { return not (*this == other) ; }
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
307 #endif