2 * @brief time related functions.
4 * @copyright Copyright © 2001-2008 by Intra2net AG
6 * @contact info@intra2net.com
10 #ifndef __TIMEFUNC_HXX
11 #define __TIMEFUNC_HXX
18 double prec_time(void);
20 int date_to_seconds(const std::string &date);
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);
27 inline std::string output_hour_minute_from_seconds(int seconds)
30 seconds_to_hour_minute(seconds,&hour,&minute);
31 return output_hour_minute(hour,minute);
34 std::string get_month_name(unsigned char month);
38 * @brief represents some days of a week.
47 enum WEEKDAY { SU=0, MO=1, TU=2, WE=3, TH=4, FR=5, SA=6 };
49 // throws out_of_range if illegal week
50 WEEK(const std::string& daystring);
52 WEEK(const std::bitset<7> &_days)
62 operator std::bitset<7>() const
65 void set(WEEKDAY d, bool value=true)
68 bool get(WEEKDAY d) const { return days[d]; }
69 bool is_set(WEEKDAY d) const { return days[d]; }
72 { return (days.count()==7); }
74 { return days.none(); }
76 std::string get_daystring() const;
77 std::string get_displaystring() const;
78 std::string get_netfilterstring() const;
80 static std::string get_day_display(WEEKDAY day);
81 static std::string get_english_display(WEEKDAY day);
83 // some operators for convenience:
85 WEEK& operator&=(const WEEK& rhs)
91 WEEK& operator|=(const WEEK& rhs)
97 WEEK& operator^=(const WEEK& rhs)
103 bool operator==(const WEEK& rhs)
105 return days == rhs.days;
108 bool operator!=(const WEEK& rhs)
110 return days != rhs.days;
117 * @brief delivers a week containing the days which are in both weeks.
118 * @param lhs first week.
119 * @param rhs second week.
120 * @return week which has only those days which are in both weeks.
122 inline WEEK operator&(const WEEK& lhs, const WEEK& rhs)
125 return result &= rhs;
126 } // eo operator&(const WEEK&,const WEEK&)
130 * @brief delivers a week containing the days which are at least in one of both weeks.
131 * @param lhs first week.
132 * @param rhs second week.
133 * @return week which has only those days which are at least in one of both weeks.
135 inline WEEK operator|(const WEEK& lhs, const WEEK& rhs)
138 return result |= rhs;
139 } // eo operator&(const WEEK&,const WEEK&)
143 * @brief delivers a week containing the days which are in only one of both weeks.
144 * @param lhs first week.
145 * @param rhs second week.
146 * @return week which has only those days which are in only one of both weeks.
148 inline WEEK operator^(const WEEK& lhs, const WEEK& rhs)
151 return result ^= rhs;
152 } // eo operator&(const WEEK&,const WEEK&)
156 * @brief structure representing a single (half-open) interval.
169 Interval( unsigned int start, unsigned int end, int weak_mark= 0 )
170 : m_lower_bound(start)
172 , m_weak_mark(weak_mark)
180 bool is_valid() const
182 return m_lower_bound <= m_upper_bound;
183 } // eo is_valid() const
187 return m_lower_bound == m_upper_bound;
188 } // eo empty() const
191 unsigned int lower_bound() const { return m_lower_bound; }
192 unsigned int upper_bound() const { return m_upper_bound; }
194 int weak_mark() const { return m_weak_mark; }
196 bool changed() const { return m_changed; }
199 bool operator== (const Interval& other) const
201 return m_lower_bound == other.m_lower_bound and m_upper_bound == other.m_upper_bound;
202 } // eo operator==(const Interval&)
206 * @brief less operator. compares only the start times!
207 * @param other the other interval to compare with.
208 * @return @a true if the current start is less than the other start.
210 bool operator<(const Interval& other) const
212 return m_lower_bound < other.m_lower_bound;
213 } // eo operator<(const Interval&)
216 bool intersects(const Interval& other) const;
217 bool contains(const Interval& other) const;
222 friend class Intervals;
224 unsigned int m_lower_bound;
225 unsigned int m_upper_bound;
235 * @brief structure representing a combination of single disjoint (half open) intervals.
237 * Basic idea is that this structure provides an interface for adding and
238 * subtracting single intervals and keeps the internal list of intervals as
239 * a list of disjoint intervals.
241 * @note the list is sorted by the start; lower comes first.
243 * @note the class provides some methods similar to STL container classes;
244 * i.e. it can be used with (at least some) STL algorithms.
247 * we use a std::list for the intervals; this means we don't invalidate all
248 * iterators when we insert or delete within loops...
249 * And we use that fact!!
254 typedef std::list< Interval > IntervalList;
255 typedef IntervalList::value_type value_type;
256 typedef IntervalList::const_iterator const_iterator;
257 typedef IntervalList::const_iterator iterator; // we allow only const...
258 typedef IntervalList::size_type size_type;
263 void add(const Interval& new_frame);
264 void sub(const Interval& new_frame);
268 const_iterator begin() const { return m_intervals.begin(); }
269 const_iterator end() const { return m_intervals.end(); }
271 bool empty() const { return m_intervals.empty(); }
272 const Interval& front() const { return m_intervals.front(); }
273 const Interval& back() const { return m_intervals.back(); }
275 size_type size() const { return m_intervals.size(); }
277 bool intersects(const Interval& other) const;
278 bool intersects(const Intervals& other) const;
280 bool contains(const Interval& other) const;
281 bool contains(const Intervals& other) const;
283 bool operator==(const Intervals& other) const;
284 bool operator!=(const Intervals& other) const {return not (*this == other) ; }
286 Intervals& operator+=(const Interval& other);
287 Intervals& operator-=(const Interval& other);
289 Intervals& operator+=(const Intervals& other);
290 Intervals& operator-=(const Intervals& other);
294 std::list< Interval > m_intervals;