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