increase libi2ncommon version as it is incompatible after dividing it by licenses...
[libi2ncommon] / src / timefunc.hxx
CommitLineData
1b5dfd98
TJ
1/** @file
2 * @brief time related functions.
3 *
4 * @copyright Copyright © 2001-2008 by Intra2net AG
5 * @license commercial
6 * @contact info@intra2net.com
7 *
8 */
e93545dd
GE
9
10#ifndef __TIMEFUNC_HXX
11#define __TIMEFUNC_HXX
12
1b5dfd98 13#include <string>
1b5dfd98
TJ
14#include <list>
15
0c7e72d7 16#include <week.hpp>
f1499910 17
e93545dd
GE
18double prec_time(void);
19
20int date_to_seconds(const std::string &date);
21
22std::string make_nice_time(int seconds);
23std::string format_full_time(int seconds);
87869870 24void seconds_to_hour_minute(int seconds, int *hour, int *minute);
1344894d 25std::string output_hour_minute(int hour, int minute, bool h_for_00=true);
2c66f490
GE
26
27inline std::string output_hour_minute_from_seconds(int seconds)
28{
29 int hour, minute;
30 seconds_to_hour_minute(seconds,&hour,&minute);
31 return output_hour_minute(hour,minute);
32}
e93545dd 33
4e157d1d
TJ
34std::string get_month_name(unsigned char month);
35
1b5dfd98
TJ
36/**
37 * @brief structure representing a single (half-open) interval.
38 */
39class Interval
40{
41 public:
42 Interval()
43 : m_lower_bound(0)
44 , m_upper_bound(0)
80f30818
TJ
45 , m_weak_mark(0)
46 , m_changed(false)
1b5dfd98
TJ
47 {
48 } //
49
80f30818 50 Interval( unsigned int start, unsigned int end, int weak_mark= 0 )
1b5dfd98
TJ
51 : m_lower_bound(start)
52 , m_upper_bound(end)
80f30818
TJ
53 , m_weak_mark(weak_mark)
54 , m_changed(false)
1b5dfd98
TJ
55 {
56 } //
57
d181c3bc
TJ
58
59 void clear();
60
1b5dfd98
TJ
61 bool is_valid() const
62 {
63 return m_lower_bound <= m_upper_bound;
64 } // eo is_valid() const
65
66 bool empty() const
67 {
68 return m_lower_bound == m_upper_bound;
69 } // eo empty() const
70
71
72 unsigned int lower_bound() const { return m_lower_bound; }
73 unsigned int upper_bound() const { return m_upper_bound; }
74
80f30818
TJ
75 int weak_mark() const { return m_weak_mark; }
76
77 bool changed() const { return m_changed; }
78
1b5dfd98
TJ
79
80 bool operator== (const Interval& other) const
81 {
82 return m_lower_bound == other.m_lower_bound and m_upper_bound == other.m_upper_bound;
83 } // eo operator==(const Interval&)
84
85
ebc3b584
TJ
86 bool operator!=(const Interval& other) const
87 {
88 return not (*this == other);
89 } // eo operator!=(const Interval&)
90
91
1b5dfd98
TJ
92 /**
93 * @brief less operator. compares only the start times!
94 * @param other the other interval to compare with.
95 * @return @a true if the current start is less than the other start.
96 */
97 bool operator<(const Interval& other) const
98 {
99 return m_lower_bound < other.m_lower_bound;
100 } // eo operator<(const Interval&)
101
102
103 bool intersects(const Interval& other) const;
104 bool contains(const Interval& other) const;
105
106
107 protected:
108
109 friend class Intervals;
110
111 unsigned int m_lower_bound;
112 unsigned int m_upper_bound;
113
80f30818
TJ
114 int m_weak_mark;
115 bool m_changed;
116
1b5dfd98
TJ
117}; // eo Interval
118
119
120
121/**
122 * @brief structure representing a combination of single disjoint (half open) intervals.
123 *
124 * Basic idea is that this structure provides an interface for adding and
125 * subtracting single intervals and keeps the internal list of intervals as
126 * a list of disjoint intervals.
127 *
128 * @note the list is sorted by the start; lower comes first.
129 *
130 * @note the class provides some methods similar to STL container classes;
131 * i.e. it can be used with (at least some) STL algorithms.
132 *
133 * @internal
134 * we use a std::list for the intervals; this means we don't invalidate all
135 * iterators when we insert or delete within loops...
136 * And we use that fact!!
137 */
138class Intervals
139{
140 public:
141 typedef std::list< Interval > IntervalList;
142 typedef IntervalList::value_type value_type;
143 typedef IntervalList::const_iterator const_iterator;
144 typedef IntervalList::const_iterator iterator; // we allow only const...
145 typedef IntervalList::size_type size_type;
146
147 public:
148 Intervals();
149
150 void add(const Interval& new_frame);
151 void sub(const Interval& new_frame);
152
d181c3bc 153 void clear();
1b5dfd98
TJ
154
155 const_iterator begin() const { return m_intervals.begin(); }
156 const_iterator end() const { return m_intervals.end(); }
157
158 bool empty() const { return m_intervals.empty(); }
159 const Interval& front() const { return m_intervals.front(); }
160 const Interval& back() const { return m_intervals.back(); }
161
162 size_type size() const { return m_intervals.size(); }
163
164 bool intersects(const Interval& other) const;
165 bool intersects(const Intervals& other) const;
166
167 bool contains(const Interval& other) const;
168 bool contains(const Intervals& other) const;
169
e156de7c
TJ
170 bool contains_exact(const Interval& other) const;
171
1b5dfd98 172 bool operator==(const Intervals& other) const;
ebc3b584 173 bool operator!=(const Intervals& other) const { return not (*this == other) ; }
1b5dfd98
TJ
174
175 Intervals& operator+=(const Interval& other);
176 Intervals& operator-=(const Interval& other);
177
178 Intervals& operator+=(const Intervals& other);
179 Intervals& operator-=(const Intervals& other);
180
181 protected:
182
183 std::list< Interval > m_intervals;
184
185}; // eo Intervals
186
187
96d0be2e
TJ
188/*
189** clock funcs:
190*/
191
192
193bool monotonic_clock_gettime(long int& seconds, long int& nano_seconds);
b7e17426 194long long monotonic_clock_gettime_nano();
96d0be2e
TJ
195
196bool realtime_clock_gettime(long int& seconds, long int& nano_seconds);
197
1b5dfd98
TJ
198
199
e93545dd 200#endif