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