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&)
205 bool operator!=(const Interval& other) const
207 return not (*this == other);
208 } // eo operator!=(const Interval&)
212 * @brief less operator. compares only the start times!
213 * @param other the other interval to compare with.
214 * @return @a true if the current start is less than the other start.
216 bool operator<(const Interval& other) const
218 return m_lower_bound < other.m_lower_bound;
219 } // eo operator<(const Interval&)
222 bool intersects(const Interval& other) const;
223 bool contains(const Interval& other) const;
228 friend class Intervals;
230 unsigned int m_lower_bound;
231 unsigned int m_upper_bound;
241 * @brief structure representing a combination of single disjoint (half open) intervals.
243 * Basic idea is that this structure provides an interface for adding and
244 * subtracting single intervals and keeps the internal list of intervals as
245 * a list of disjoint intervals.
247 * @note the list is sorted by the start; lower comes first.
249 * @note the class provides some methods similar to STL container classes;
250 * i.e. it can be used with (at least some) STL algorithms.
253 * we use a std::list for the intervals; this means we don't invalidate all
254 * iterators when we insert or delete within loops...
255 * And we use that fact!!
260 typedef std::list< Interval > IntervalList;
261 typedef IntervalList::value_type value_type;
262 typedef IntervalList::const_iterator const_iterator;
263 typedef IntervalList::const_iterator iterator; // we allow only const...
264 typedef IntervalList::size_type size_type;
269 void add(const Interval& new_frame);
270 void sub(const Interval& new_frame);
274 const_iterator begin() const { return m_intervals.begin(); }
275 const_iterator end() const { return m_intervals.end(); }
277 bool empty() const { return m_intervals.empty(); }
278 const Interval& front() const { return m_intervals.front(); }
279 const Interval& back() const { return m_intervals.back(); }
281 size_type size() const { return m_intervals.size(); }
283 bool intersects(const Interval& other) const;
284 bool intersects(const Intervals& other) const;
286 bool contains(const Interval& other) const;
287 bool contains(const Intervals& other) const;
289 bool operator==(const Intervals& other) const;
290 bool operator!=(const Intervals& other) const { return not (*this == other) ; }
292 Intervals& operator+=(const Interval& other);
293 Intervals& operator-=(const Interval& other);
295 Intervals& operator+=(const Intervals& other);
296 Intervals& operator-=(const Intervals& other);
300 std::list< Interval > m_intervals;