2 The software in this package is distributed under the GNU General
3 Public License version 2 (with a special exception described below).
5 A copy of GNU General Public License (GPL) is included in this distribution,
6 in the file COPYING.GPL.
8 As a special exception, if other files instantiate templates or use macros
9 or inline functions from this file, or you compile this file and link it
10 with other works to produce a work based on this file, this file
11 does not by itself cause the resulting work to be covered
12 by the GNU General Public License.
14 However the source code for this file must still be made available
15 in accordance with section (3) of the GNU General Public License.
17 This exception does not invalidate any other reasons why a work based
18 on this file might be covered by the GNU General Public License.
21 * @brief time related functions.
23 * @copyright Copyright © 2001-2008 by Intra2net AG
26 #ifndef __TIMEFUNC_HXX
27 #define __TIMEFUNC_HXX
33 #include <boost/optional.hpp>
37 double prec_time(void);
39 time_t date_to_seconds(const std::string &date);
41 std::string make_nice_time(int seconds);
42 std::string format_full_time(time_t seconds);
43 std::string format_date(time_t seconds);
44 void seconds_to_hour_minute(int seconds, int *hour, int *minute);
45 void split_daysec(int daysec, int *outhours=NULL, int *outminutes=NULL, int *outseconds=NULL);
46 std::string output_hour_minute(int hour, int minute, bool h_for_00=true, int seconds=0);
48 inline std::string output_hour_minute_from_seconds(int seconds)
51 split_daysec(seconds,&hour,&minute);
52 return output_hour_minute(hour,minute);
55 std::string get_month_name(unsigned char month);
58 * @brief structure representing a single (half-open) interval.
71 Interval( unsigned int start, unsigned int end, int weak_mark= 0 )
72 : m_lower_bound(start)
74 , m_weak_mark(weak_mark)
84 return m_lower_bound <= m_upper_bound;
85 } // eo is_valid() const
89 return m_lower_bound == m_upper_bound;
93 unsigned int lower_bound() const { return m_lower_bound; }
94 unsigned int upper_bound() const { return m_upper_bound; }
96 int weak_mark() const { return m_weak_mark; }
98 bool changed() const { return m_changed; }
101 bool operator== (const Interval& other) const
103 return m_lower_bound == other.m_lower_bound and m_upper_bound == other.m_upper_bound;
104 } // eo operator==(const Interval&)
107 bool operator!=(const Interval& other) const
109 return not (*this == other);
110 } // eo operator!=(const Interval&)
114 * @brief less operator. compares only the start times!
115 * @param other the other interval to compare with.
116 * @return @a true if the current start is less than the other start.
118 bool operator<(const Interval& other) const
120 return m_lower_bound < other.m_lower_bound;
121 } // eo operator<(const Interval&)
124 bool intersects(const Interval& other) const;
125 bool contains(const Interval& other) const;
130 friend class Intervals;
132 unsigned int m_lower_bound;
133 unsigned int m_upper_bound;
143 * @brief structure representing a combination of single disjoint (half open) intervals.
145 * Basic idea is that this structure provides an interface for adding and
146 * subtracting single intervals and keeps the internal list of intervals as
147 * a list of disjoint intervals.
149 * @note the list is sorted by the start; lower comes first.
151 * @note the class provides some methods similar to STL container classes;
152 * i.e. it can be used with (at least some) STL algorithms.
155 * we use a std::list for the intervals; this means we don't invalidate all
156 * iterators when we insert or delete within loops...
157 * And we use that fact!!
162 typedef std::list< Interval > IntervalList;
163 typedef IntervalList::value_type value_type;
164 typedef IntervalList::const_iterator const_iterator;
165 typedef IntervalList::const_iterator iterator; // we allow only const...
166 typedef IntervalList::size_type size_type;
171 void add(const Interval& new_frame);
172 void sub(const Interval& new_frame);
176 const_iterator begin() const { return m_intervals.begin(); }
177 const_iterator end() const { return m_intervals.end(); }
179 bool empty() const { return m_intervals.empty(); }
180 const Interval& front() const { return m_intervals.front(); }
181 const Interval& back() const { return m_intervals.back(); }
183 size_type size() const { return m_intervals.size(); }
185 bool intersects(const Interval& other) const;
186 bool intersects(const Intervals& other) const;
188 bool contains(const Interval& other) const;
189 bool contains(const Intervals& other) const;
191 bool contains_exact(const Interval& other) const;
193 bool operator==(const Intervals& other) const;
194 bool operator!=(const Intervals& other) const { return not (*this == other) ; }
196 Intervals& operator+=(const Interval& other);
197 Intervals& operator-=(const Interval& other);
199 Intervals& operator+=(const Intervals& other);
200 Intervals& operator-=(const Intervals& other);
204 std::list< Interval > m_intervals;
214 bool monotonic_clock_gettime(long int& seconds, long int& nano_seconds);
215 long long monotonic_clock_gettime_nano();
217 bool realtime_clock_gettime(long int& seconds, long int& nano_seconds);
225 * represent the clock id options from clock_gettime(2) as
226 * a pair of enums. by default, CLOCK_MONOTONIC_COARSE is used
227 * everywhere since that’s what we want in most cases.
230 mono, /* CLOCK_MONOTONIC_COARSE */
231 real, /* CLOCK_REALIME_COARSE */
232 boot, /* CLOCK_BOOTTIME */
233 cpu, /* CLOCK_CPUTIME_* */
237 * for clocks that support it: non-coarse or raw variants; if a variant
238 * does not apply to a given clock, it is ignored
242 exact, /* mono, real: not (*_COARSE) */
243 raw, /* mono: _RAW */
244 process, /* cpu: *_PROCESS_* */
245 thread, /* cpu: *_THREAD_* */
248 } /* [namespace type] */
254 struct timespec value;
257 const enum type::id id;
258 const enum type::variant variant;
261 /* ctors *************************************************************/
264 Time (const enum type::id id = type::mono,
265 const enum type::variant var = type::dflt);
267 /* value access ******************************************************/
270 inline const struct timespec &get_time (void)
271 { return this->value; }
273 inline const time_t &get_sec (void)
274 { return this->value.tv_sec; };
276 inline const long &get_nsec (void)
277 { return this->value.tv_nsec; }
279 int64_t as_nanosec (void);
281 long as_nanosec_L (void);
283 /* setting time ******************************************************/
287 set (const struct timespec &ts)
288 { this->value = ts; };
291 set (const time_t sec, const long nsec)
293 this->value.tv_sec = sec;
294 this->value.tv_nsec = nsec;
301 }; /* [class Time] */
303 boost::optional<Time>
304 now (const enum type::id id = type::mono,
305 const enum type::variant var = type::dflt);
307 } /* [namespace clock] */
309 } /* [namespace I2n] */