18b03148858b25cbac8e7c1cd0fff5e3c5bbc16f
[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 std::string format_date(time_t seconds);
41 void seconds_to_hour_minute(int seconds, int *hour, int *minute);
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);
44
45 inline std::string output_hour_minute_from_seconds(int seconds)
46 {
47     int hour, minute;
48     split_daysec(seconds,&hour,&minute);
49     return output_hour_minute(hour,minute);
50 }
51
52 std::string get_month_name(unsigned char month);
53
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)
63         , m_weak_mark(0)
64         , m_changed(false)
65         {
66         } //
67
68         Interval( unsigned int start, unsigned int end, int weak_mark= 0 )
69         : m_lower_bound(start)
70         , m_upper_bound(end)
71         , m_weak_mark(weak_mark)
72         , m_changed(false)
73         {
74         } //
75
76
77         void clear();
78
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
93         int weak_mark() const { return m_weak_mark; }
94
95         bool changed() const { return m_changed; }
96
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
104         bool  operator!=(const Interval& other) const
105         {
106             return not (*this == other);
107         } // eo operator!=(const Interval&)
108
109
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
132         int m_weak_mark;
133         bool m_changed;
134
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
171         void clear();
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
188         bool contains_exact(const Interval& other) const;
189
190         bool operator==(const Intervals& other) const;
191         bool operator!=(const Intervals& other) const { return not (*this == other) ; }
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
206 /*
207 ** clock funcs:
208 */
209
210
211 bool monotonic_clock_gettime(long int& seconds, long int& nano_seconds);
212 long long monotonic_clock_gettime_nano();
213
214 bool realtime_clock_gettime(long int& seconds, long int& nano_seconds);
215
216
217
218 #endif