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