libi2ncommon: (reinhard) added global_config module (as separate lib and package...
[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 49 // throws out_of_range if illegal week
fd669708
TJ
50 WEEK(const std::string& daystring)
51 { set(daystring); }
21f2d3db
TJ
52
53 WEEK(const std::bitset<7> &_days)
54 : days(_days)
f1499910 55 { }
21f2d3db 56
6781ce49
GE
57 WEEK()
58 { }
21f2d3db 59
f1499910
GE
60 void clear()
61 { days.reset(); }
21f2d3db 62
f1499910
GE
63 operator std::bitset<7>() const
64 { return days; }
21f2d3db 65
fd669708 66 void set(const std::string& daystring);
ae3915ac
GE
67 void set(WEEKDAY d, bool value=true)
68 { days[d]=value; }
21f2d3db 69
aff80c26
TJ
70 bool get(WEEKDAY d) const { return days[d]; }
71 bool is_set(WEEKDAY d) const { return days[d]; }
72
ae3915ac
GE
73 bool all_set() const
74 { return (days.count()==7); }
75 bool none_set() const
76 { return days.none(); }
21f2d3db 77
f1499910
GE
78 std::string get_daystring() const;
79 std::string get_displaystring() const;
80 std::string get_netfilterstring() const;
21f2d3db 81
f1499910
GE
82 static std::string get_day_display(WEEKDAY day);
83 static std::string get_english_display(WEEKDAY day);
21f2d3db
TJ
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 */
124inline 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 */
137inline 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 */
150inline WEEK operator^(const WEEK& lhs, const WEEK& rhs)
151{
152 WEEK result(lhs);
153 return result ^= rhs;
154} // eo operator&(const WEEK&,const WEEK&)
f1499910 155
1b5dfd98
TJ
156
157/**
158 * @brief structure representing a single (half-open) interval.
159 */
160class Interval
161{
162 public:
163 Interval()
164 : m_lower_bound(0)
165 , m_upper_bound(0)
80f30818
TJ
166 , m_weak_mark(0)
167 , m_changed(false)
1b5dfd98
TJ
168 {
169 } //
170
80f30818 171 Interval( unsigned int start, unsigned int end, int weak_mark= 0 )
1b5dfd98
TJ
172 : m_lower_bound(start)
173 , m_upper_bound(end)
80f30818
TJ
174 , m_weak_mark(weak_mark)
175 , m_changed(false)
1b5dfd98
TJ
176 {
177 } //
178
d181c3bc
TJ
179
180 void clear();
181
1b5dfd98
TJ
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
80f30818
TJ
196 int weak_mark() const { return m_weak_mark; }
197
198 bool changed() const { return m_changed; }
199
1b5dfd98
TJ
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
ebc3b584
TJ
207 bool operator!=(const Interval& other) const
208 {
209 return not (*this == other);
210 } // eo operator!=(const Interval&)
211
212
1b5dfd98
TJ
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
80f30818
TJ
235 int m_weak_mark;
236 bool m_changed;
237
1b5dfd98
TJ
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 */
259class 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
d181c3bc 274 void clear();
1b5dfd98
TJ
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
e156de7c
TJ
291 bool contains_exact(const Interval& other) const;
292
1b5dfd98 293 bool operator==(const Intervals& other) const;
ebc3b584 294 bool operator!=(const Intervals& other) const { return not (*this == other) ; }
1b5dfd98
TJ
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
96d0be2e
TJ
309/*
310** clock funcs:
311*/
312
313
314bool monotonic_clock_gettime(long int& seconds, long int& nano_seconds);
315
316bool realtime_clock_gettime(long int& seconds, long int& nano_seconds);
317
1b5dfd98
TJ
318
319
e93545dd 320#endif