[MERGE] libi2ncommon: (gerd) add WEEK::set(daystring)
[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             { set(daystring); }
52
53         WEEK(const std::bitset<7> &_days)
54             : days(_days)
55             { }
56
57         WEEK()
58             { }
59
60         void clear()
61             { days.reset(); }
62
63         operator std::bitset<7>() const
64             { return days; }
65
66         void set(const std::string& daystring);
67         void set(WEEKDAY d, bool value=true)
68             { days[d]=value; }
69
70         bool get(WEEKDAY d) const { return days[d]; }
71         bool is_set(WEEKDAY d) const { return days[d]; }
72
73         bool all_set() const
74             { return (days.count()==7); }
75         bool none_set() const
76             { return days.none(); }
77
78         std::string get_daystring() const;
79         std::string get_displaystring() const;
80         std::string get_netfilterstring() const;
81
82         static std::string get_day_display(WEEKDAY day);
83         static std::string get_english_display(WEEKDAY day);
84
85         // some operators for convenience:
86
87         WEEK& operator&=(const WEEK& rhs)
88         {
89             days &= rhs.days;
90             return *this;
91         }
92
93         WEEK& operator|=(const WEEK& rhs)
94         {
95             days|= rhs.days;
96             return *this;
97         }
98
99         WEEK& operator^=(const WEEK& rhs)
100         {
101             days^= rhs.days;
102             return *this;
103         }
104
105         bool operator==(const WEEK& rhs)
106         {
107             return days == rhs.days;
108         }
109
110         bool operator!=(const WEEK& rhs)
111         {
112             return days != rhs.days;
113         }
114
115 }; // eo class WEEK
116
117
118 /**
119  * @brief delivers a week containing the days which are in both weeks.
120  * @param lhs first week.
121  * @param rhs second week.
122  * @return week which has only those days which are in both weeks.
123  */
124 inline WEEK operator&(const WEEK& lhs, const WEEK& rhs)
125 {
126     WEEK result(lhs);
127     return result &= rhs;
128 } // eo operator&(const WEEK&,const WEEK&)
129
130
131 /**
132  * @brief delivers a week containing the days which are at least in one of both weeks.
133  * @param lhs first week.
134  * @param rhs second week.
135  * @return week which has only those days which are at least in one of both weeks.
136  */
137 inline WEEK operator|(const WEEK& lhs, const WEEK& rhs)
138 {
139     WEEK result(lhs);
140     return result |= rhs;
141 } // eo operator&(const WEEK&,const WEEK&)
142
143
144 /**
145  * @brief delivers a week containing the days which are in only one of both weeks.
146  * @param lhs first week.
147  * @param rhs second week.
148  * @return week which has only those days which are in only one of both weeks.
149  */
150 inline WEEK operator^(const WEEK& lhs, const WEEK& rhs)
151 {
152     WEEK result(lhs);
153     return result ^= rhs;
154 } // eo operator&(const WEEK&,const WEEK&)
155
156
157 /**
158  * @brief structure representing a single (half-open) interval.
159  */
160 class Interval
161 {
162     public:
163         Interval()
164         : m_lower_bound(0)
165         , m_upper_bound(0)
166         , m_weak_mark(0)
167         , m_changed(false)
168         {
169         } //
170
171         Interval( unsigned int start, unsigned int end, int weak_mark= 0 )
172         : m_lower_bound(start)
173         , m_upper_bound(end)
174         , m_weak_mark(weak_mark)
175         , m_changed(false)
176         {
177         } //
178
179
180         void clear();
181
182         bool is_valid() const
183         {
184             return m_lower_bound <= m_upper_bound;
185         } // eo is_valid() const
186
187         bool empty() const
188         {
189             return m_lower_bound == m_upper_bound;
190         } // eo empty() const
191
192
193         unsigned int lower_bound() const { return m_lower_bound; }
194         unsigned int upper_bound() const { return m_upper_bound; }
195
196         int weak_mark() const { return m_weak_mark; }
197
198         bool changed() const { return m_changed; }
199
200
201         bool operator== (const Interval& other) const
202         {
203             return m_lower_bound == other.m_lower_bound and m_upper_bound == other.m_upper_bound;
204         } // eo operator==(const Interval&)
205
206
207         bool  operator!=(const Interval& other) const
208         {
209             return not (*this == other);
210         } // eo operator!=(const Interval&)
211
212
213         /**
214          * @brief less operator. compares only the start times!
215          * @param other the other interval to compare with.
216          * @return @a true if the current start is less than the other start.
217          */
218         bool operator<(const Interval& other) const
219         {
220             return m_lower_bound < other.m_lower_bound;
221         } // eo operator<(const Interval&)
222
223
224         bool intersects(const Interval& other) const;
225         bool contains(const Interval& other) const;
226
227
228     protected:
229
230         friend class Intervals;
231
232         unsigned int m_lower_bound;
233         unsigned int m_upper_bound;
234
235         int m_weak_mark;
236         bool m_changed;
237
238 }; // eo Interval
239
240
241
242 /**
243  * @brief structure representing a combination of single disjoint (half open) intervals.
244  *
245  * Basic idea is that this structure provides an interface for adding and
246  * subtracting single intervals and keeps the internal list of intervals as
247  * a list of disjoint intervals.
248  *
249  * @note the list is sorted by the start; lower comes first.
250  *
251  * @note the class provides some methods similar to STL container classes;
252  * i.e. it can be used with (at least some) STL algorithms.
253  * 
254  * @internal
255  * we use a std::list for the intervals; this means we don't invalidate all
256  * iterators when we insert or delete within loops...
257  * And we use that fact!!
258  */
259 class Intervals
260 {
261     public:
262         typedef std::list< Interval > IntervalList;
263         typedef IntervalList::value_type        value_type;
264         typedef IntervalList::const_iterator    const_iterator;
265         typedef IntervalList::const_iterator    iterator; // we allow only const...
266         typedef IntervalList::size_type         size_type;
267
268     public:
269         Intervals();
270
271         void add(const Interval& new_frame);
272         void sub(const Interval& new_frame);
273
274         void clear();
275
276         const_iterator begin() const { return m_intervals.begin(); }
277         const_iterator end() const { return m_intervals.end(); }
278
279         bool empty() const { return m_intervals.empty(); }
280         const Interval& front() const { return m_intervals.front(); }
281         const Interval& back() const { return m_intervals.back(); }
282
283         size_type size() const { return m_intervals.size(); }
284
285         bool intersects(const Interval& other) const;
286         bool intersects(const Intervals& other) const;
287
288         bool contains(const Interval& other) const;
289         bool contains(const Intervals& other) const;
290
291         bool contains_exact(const Interval& other) const;
292
293         bool operator==(const Intervals& other) const;
294         bool operator!=(const Intervals& other) const { return not (*this == other) ; }
295
296         Intervals& operator+=(const Interval& other);
297         Intervals& operator-=(const Interval& other);
298
299         Intervals& operator+=(const Intervals& other);
300         Intervals& operator-=(const Intervals& other);
301
302     protected:
303
304         std::list< Interval > m_intervals;
305
306 }; // eo Intervals
307
308
309 /*
310 ** clock funcs:
311 */
312
313
314 bool monotonic_clock_gettime(long int& seconds, long int& nano_seconds);
315
316 bool realtime_clock_gettime(long int& seconds, long int& nano_seconds);
317
318
319
320 #endif