/* The software in this package is distributed under the GNU General Public License version 2 (with a special exception described below). A copy of GNU General Public License (GPL) is included in this distribution, in the file COPYING.GPL. As a special exception, if other files instantiate templates or use macros or inline functions from this file, or you compile this file and link it with other works to produce a work based on this file, this file does not by itself cause the resulting work to be covered by the GNU General Public License. However the source code for this file must still be made available in accordance with section (3) of the GNU General Public License. This exception does not invalidate any other reasons why a work based on this file might be covered by the GNU General Public License. */ /** @file * @brief time related functions. * * @copyright Copyright © 2001-2008 by Intra2net AG */ #ifndef __TIMEFUNC_HXX #define __TIMEFUNC_HXX #include #include #include double prec_time(void); time_t date_to_seconds(const std::string &date); std::string make_nice_time(int seconds); std::string format_full_time(time_t seconds); std::string format_date(time_t seconds); void seconds_to_hour_minute(int seconds, int *hour, int *minute); void split_daysec(int daysec, int *outhours=NULL, int *outminutes=NULL, int *outseconds=NULL); std::string output_hour_minute(int hour, int minute, bool h_for_00=true, int seconds=0); inline std::string output_hour_minute_from_seconds(int seconds) { int hour, minute; split_daysec(seconds,&hour,&minute); return output_hour_minute(hour,minute); } std::string get_month_name(unsigned char month); /** * @brief structure representing a single (half-open) interval. */ class Interval { public: Interval() : m_lower_bound(0) , m_upper_bound(0) , m_weak_mark(0) , m_changed(false) { } // Interval( unsigned int start, unsigned int end, int weak_mark= 0 ) : m_lower_bound(start) , m_upper_bound(end) , m_weak_mark(weak_mark) , m_changed(false) { } // void clear(); bool is_valid() const { return m_lower_bound <= m_upper_bound; } // eo is_valid() const bool empty() const { return m_lower_bound == m_upper_bound; } // eo empty() const unsigned int lower_bound() const { return m_lower_bound; } unsigned int upper_bound() const { return m_upper_bound; } int weak_mark() const { return m_weak_mark; } bool changed() const { return m_changed; } bool operator== (const Interval& other) const { return m_lower_bound == other.m_lower_bound and m_upper_bound == other.m_upper_bound; } // eo operator==(const Interval&) bool operator!=(const Interval& other) const { return not (*this == other); } // eo operator!=(const Interval&) /** * @brief less operator. compares only the start times! * @param other the other interval to compare with. * @return @a true if the current start is less than the other start. */ bool operator<(const Interval& other) const { return m_lower_bound < other.m_lower_bound; } // eo operator<(const Interval&) bool intersects(const Interval& other) const; bool contains(const Interval& other) const; protected: friend class Intervals; unsigned int m_lower_bound; unsigned int m_upper_bound; int m_weak_mark; bool m_changed; }; // eo Interval /** * @brief structure representing a combination of single disjoint (half open) intervals. * * Basic idea is that this structure provides an interface for adding and * subtracting single intervals and keeps the internal list of intervals as * a list of disjoint intervals. * * @note the list is sorted by the start; lower comes first. * * @note the class provides some methods similar to STL container classes; * i.e. it can be used with (at least some) STL algorithms. * * @internal * we use a std::list for the intervals; this means we don't invalidate all * iterators when we insert or delete within loops... * And we use that fact!! */ class Intervals { public: typedef std::list< Interval > IntervalList; typedef IntervalList::value_type value_type; typedef IntervalList::const_iterator const_iterator; typedef IntervalList::const_iterator iterator; // we allow only const... typedef IntervalList::size_type size_type; public: Intervals(); void add(const Interval& new_frame); void sub(const Interval& new_frame); void clear(); const_iterator begin() const { return m_intervals.begin(); } const_iterator end() const { return m_intervals.end(); } bool empty() const { return m_intervals.empty(); } const Interval& front() const { return m_intervals.front(); } const Interval& back() const { return m_intervals.back(); } size_type size() const { return m_intervals.size(); } bool intersects(const Interval& other) const; bool intersects(const Intervals& other) const; bool contains(const Interval& other) const; bool contains(const Intervals& other) const; bool contains_exact(const Interval& other) const; bool operator==(const Intervals& other) const; bool operator!=(const Intervals& other) const { return not (*this == other) ; } Intervals& operator+=(const Interval& other); Intervals& operator-=(const Interval& other); Intervals& operator+=(const Intervals& other); Intervals& operator-=(const Intervals& other); protected: std::list< Interval > m_intervals; }; // eo Intervals /* ** clock funcs: */ bool monotonic_clock_gettime(long int& seconds, long int& nano_seconds); long long monotonic_clock_gettime_nano(); bool realtime_clock_gettime(long int& seconds, long int& nano_seconds); #endif