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