[MERGE] libi2ncommon: (reinhard) added clear() method to Interval and Intervals.
[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 class WEEK
37 {
38     private:
39         std::bitset<7> days;
40
41     public:
42         enum WEEKDAY { SU=0, MO=1, TU=2, WE=3, TH=4, FR=5, SA=6 };
43         
44         // throws out_of_range if illegal week
45         WEEK(const std::string& daystring);
46         
47         WEEK(const std::bitset<7> &days)
48             : days(days)
49             { }
50             
51         WEEK()
52             { }
53             
54         void clear()
55             { days.reset(); }
56             
57         operator std::bitset<7>() const
58             { return days; }
59             
60         void set(WEEKDAY d, bool value=true)
61             { days[d]=value; }
62         
63         bool all_set() const
64             { return (days.count()==7); }
65         bool none_set() const
66             { return days.none(); }
67         
68         std::string get_daystring() const;
69         std::string get_displaystring() const;
70         std::string get_netfilterstring() const;
71         
72         static std::string get_day_display(WEEKDAY day);
73         static std::string get_english_display(WEEKDAY day);
74 };
75
76
77 /**
78  * @brief structure representing a single (half-open) interval.
79  */
80 class Interval
81 {
82     public:
83         Interval()
84         : m_lower_bound(0)
85         , m_upper_bound(0)
86         {
87         } //
88
89         Interval( unsigned int start, unsigned int end )
90         : m_lower_bound(start)
91         , m_upper_bound(end)
92         {
93         } //
94
95
96         void clear();
97
98         bool is_valid() const
99         {
100             return m_lower_bound <= m_upper_bound;
101         } // eo is_valid() const
102
103         bool empty() const
104         {
105             return m_lower_bound == m_upper_bound;
106         } // eo empty() const
107
108
109         unsigned int lower_bound() const { return m_lower_bound; }
110         unsigned int upper_bound() const { return m_upper_bound; }
111
112
113         bool operator== (const Interval& other) const
114         {
115             return m_lower_bound == other.m_lower_bound and m_upper_bound == other.m_upper_bound;
116         } // eo operator==(const Interval&)
117
118
119         /**
120          * @brief less operator. compares only the start times!
121          * @param other the other interval to compare with.
122          * @return @a true if the current start is less than the other start.
123          */
124         bool operator<(const Interval& other) const
125         {
126             return m_lower_bound < other.m_lower_bound;
127         } // eo operator<(const Interval&)
128
129
130         bool intersects(const Interval& other) const;
131         bool contains(const Interval& other) const;
132
133
134     protected:
135
136         friend class Intervals;
137
138         unsigned int m_lower_bound;
139         unsigned int m_upper_bound;
140
141 }; // eo Interval
142
143
144
145 /**
146  * @brief structure representing a combination of single disjoint (half open) intervals.
147  *
148  * Basic idea is that this structure provides an interface for adding and
149  * subtracting single intervals and keeps the internal list of intervals as
150  * a list of disjoint intervals.
151  *
152  * @note the list is sorted by the start; lower comes first.
153  *
154  * @note the class provides some methods similar to STL container classes;
155  * i.e. it can be used with (at least some) STL algorithms.
156  * 
157  * @internal
158  * we use a std::list for the intervals; this means we don't invalidate all
159  * iterators when we insert or delete within loops...
160  * And we use that fact!!
161  */
162 class Intervals
163 {
164     public:
165         typedef std::list< Interval > IntervalList;
166         typedef IntervalList::value_type        value_type;
167         typedef IntervalList::const_iterator    const_iterator;
168         typedef IntervalList::const_iterator    iterator; // we allow only const...
169         typedef IntervalList::size_type         size_type;
170
171     public:
172         Intervals();
173
174         void add(const Interval& new_frame);
175         void sub(const Interval& new_frame);
176
177         void clear();
178
179         const_iterator begin() const { return m_intervals.begin(); }
180         const_iterator end() const { return m_intervals.end(); }
181
182         bool empty() const { return m_intervals.empty(); }
183         const Interval& front() const { return m_intervals.front(); }
184         const Interval& back() const { return m_intervals.back(); }
185
186         size_type size() const { return m_intervals.size(); }
187
188         bool intersects(const Interval& other) const;
189         bool intersects(const Intervals& other) const;
190
191         bool contains(const Interval& other) const;
192         bool contains(const Intervals& other) const;
193
194         bool operator==(const Intervals& other) const;
195         bool operator!=(const Intervals& other) const {return not (*this == other) ; }
196
197         Intervals& operator+=(const Interval& other);
198         Intervals& operator-=(const Interval& other);
199
200         Intervals& operator+=(const Intervals& other);
201         Intervals& operator-=(const Intervals& other);
202
203     protected:
204
205         std::list< Interval > m_intervals;
206
207 }; // eo Intervals
208
209
210
211
212 #endif