[MERGE] libi2ncommon: (reinhard) added getter to WEEK...
[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         /**
206          * @brief less operator. compares only the start times!
207          * @param other the other interval to compare with.
208          * @return @a true if the current start is less than the other start.
209          */
210         bool operator<(const Interval& other) const
211         {
212             return m_lower_bound < other.m_lower_bound;
213         } // eo operator<(const Interval&)
214
215
216         bool intersects(const Interval& other) const;
217         bool contains(const Interval& other) const;
218
219
220     protected:
221
222         friend class Intervals;
223
224         unsigned int m_lower_bound;
225         unsigned int m_upper_bound;
226
227         int m_weak_mark;
228         bool m_changed;
229
230 }; // eo Interval
231
232
233
234 /**
235  * @brief structure representing a combination of single disjoint (half open) intervals.
236  *
237  * Basic idea is that this structure provides an interface for adding and
238  * subtracting single intervals and keeps the internal list of intervals as
239  * a list of disjoint intervals.
240  *
241  * @note the list is sorted by the start; lower comes first.
242  *
243  * @note the class provides some methods similar to STL container classes;
244  * i.e. it can be used with (at least some) STL algorithms.
245  * 
246  * @internal
247  * we use a std::list for the intervals; this means we don't invalidate all
248  * iterators when we insert or delete within loops...
249  * And we use that fact!!
250  */
251 class Intervals
252 {
253     public:
254         typedef std::list< Interval > IntervalList;
255         typedef IntervalList::value_type        value_type;
256         typedef IntervalList::const_iterator    const_iterator;
257         typedef IntervalList::const_iterator    iterator; // we allow only const...
258         typedef IntervalList::size_type         size_type;
259
260     public:
261         Intervals();
262
263         void add(const Interval& new_frame);
264         void sub(const Interval& new_frame);
265
266         void clear();
267
268         const_iterator begin() const { return m_intervals.begin(); }
269         const_iterator end() const { return m_intervals.end(); }
270
271         bool empty() const { return m_intervals.empty(); }
272         const Interval& front() const { return m_intervals.front(); }
273         const Interval& back() const { return m_intervals.back(); }
274
275         size_type size() const { return m_intervals.size(); }
276
277         bool intersects(const Interval& other) const;
278         bool intersects(const Intervals& other) const;
279
280         bool contains(const Interval& other) const;
281         bool contains(const Intervals& other) const;
282
283         bool operator==(const Intervals& other) const;
284         bool operator!=(const Intervals& other) const {return not (*this == other) ; }
285
286         Intervals& operator+=(const Interval& other);
287         Intervals& operator-=(const Interval& other);
288
289         Intervals& operator+=(const Intervals& other);
290         Intervals& operator-=(const Intervals& other);
291
292     protected:
293
294         std::list< Interval > m_intervals;
295
296 }; // eo Intervals
297
298
299
300
301 #endif