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)
53 WEEK(const std::bitset<7> &_days)
63 operator std::bitset<7>() const
66 void set(const std::string& daystring);
67 void set(WEEKDAY d, bool value=true)
70 bool get(WEEKDAY d) const { return days[d]; }
71 bool is_set(WEEKDAY d) const { return days[d]; }
74 { return (days.count()==7); }
76 { return days.none(); }
78 std::string get_daystring() const;
79 std::string get_displaystring() const;
80 std::string get_netfilterstring() const;
82 static std::string get_day_display(WEEKDAY day);
83 static std::string get_english_display(WEEKDAY day);
85 // some operators for convenience:
87 WEEK& operator&=(const WEEK& rhs)
93 WEEK& operator|=(const WEEK& rhs)
99 WEEK& operator^=(const WEEK& rhs)
105 bool operator==(const WEEK& rhs)
107 return days == rhs.days;
110 bool operator!=(const WEEK& rhs)
112 return days != rhs.days;
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.
124 inline WEEK operator&(const WEEK& lhs, const WEEK& rhs)
127 return result &= rhs;
128 } // eo operator&(const WEEK&,const WEEK&)
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.
137 inline WEEK operator|(const WEEK& lhs, const WEEK& rhs)
140 return result |= rhs;
141 } // eo operator&(const WEEK&,const WEEK&)
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.
150 inline WEEK operator^(const WEEK& lhs, const WEEK& rhs)
153 return result ^= rhs;
154 } // eo operator&(const WEEK&,const WEEK&)
158 * @brief structure representing a single (half-open) interval.
171 Interval( unsigned int start, unsigned int end, int weak_mark= 0 )
172 : m_lower_bound(start)
174 , m_weak_mark(weak_mark)
182 bool is_valid() const
184 return m_lower_bound <= m_upper_bound;
185 } // eo is_valid() const
189 return m_lower_bound == m_upper_bound;
190 } // eo empty() const
193 unsigned int lower_bound() const { return m_lower_bound; }
194 unsigned int upper_bound() const { return m_upper_bound; }
196 int weak_mark() const { return m_weak_mark; }
198 bool changed() const { return m_changed; }
201 bool operator== (const Interval& other) const
203 return m_lower_bound == other.m_lower_bound and m_upper_bound == other.m_upper_bound;
204 } // eo operator==(const Interval&)
207 bool operator!=(const Interval& other) const
209 return not (*this == other);
210 } // eo operator!=(const Interval&)
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.
218 bool operator<(const Interval& other) const
220 return m_lower_bound < other.m_lower_bound;
221 } // eo operator<(const Interval&)
224 bool intersects(const Interval& other) const;
225 bool contains(const Interval& other) const;
230 friend class Intervals;
232 unsigned int m_lower_bound;
233 unsigned int m_upper_bound;
243 * @brief structure representing a combination of single disjoint (half open) intervals.
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.
249 * @note the list is sorted by the start; lower comes first.
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.
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!!
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;
271 void add(const Interval& new_frame);
272 void sub(const Interval& new_frame);
276 const_iterator begin() const { return m_intervals.begin(); }
277 const_iterator end() const { return m_intervals.end(); }
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(); }
283 size_type size() const { return m_intervals.size(); }
285 bool intersects(const Interval& other) const;
286 bool intersects(const Intervals& other) const;
288 bool contains(const Interval& other) const;
289 bool contains(const Intervals& other) const;
291 bool contains_exact(const Interval& other) const;
293 bool operator==(const Intervals& other) const;
294 bool operator!=(const Intervals& other) const { return not (*this == other) ; }
296 Intervals& operator+=(const Interval& other);
297 Intervals& operator-=(const Interval& other);
299 Intervals& operator+=(const Intervals& other);
300 Intervals& operator-=(const Intervals& other);
304 std::list< Interval > m_intervals;
314 bool monotonic_clock_gettime(long int& seconds, long int& nano_seconds);
315 long long monotonic_clock_gettime_nano();
317 bool realtime_clock_gettime(long int& seconds, long int& nano_seconds);