Moved WEEK class to own file. Renamed to Week and adapted to I2n code style. Adapted...
[libi2ncommon] / src / timefunc.hxx
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  */
9
10 #ifndef __TIMEFUNC_HXX
11 #define __TIMEFUNC_HXX
12
13 #include <string>
14 #include <list>
15
16 #include <week.hpp>
17
18 double prec_time(void);
19
20 int date_to_seconds(const std::string &date);
21
22 std::string make_nice_time(int seconds);
23 std::string format_full_time(int seconds);
24 void seconds_to_hour_minute(int seconds, int *hour, int *minute);
25 std::string output_hour_minute(int hour, int minute, bool h_for_00=true);
26
27 inline 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 }
33
34 std::string get_month_name(unsigned char month);
35
36 /**
37  * @brief structure representing a single (half-open) interval.
38  */
39 class Interval
40 {
41     public:
42         Interval()
43         : m_lower_bound(0)
44         , m_upper_bound(0)
45         , m_weak_mark(0)
46         , m_changed(false)
47         {
48         } //
49
50         Interval( unsigned int start, unsigned int end, int weak_mark= 0 )
51         : m_lower_bound(start)
52         , m_upper_bound(end)
53         , m_weak_mark(weak_mark)
54         , m_changed(false)
55         {
56         } //
57
58
59         void clear();
60
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
75         int weak_mark() const { return m_weak_mark; }
76
77         bool changed() const { return m_changed; }
78
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
86         bool  operator!=(const Interval& other) const
87         {
88             return not (*this == other);
89         } // eo operator!=(const Interval&)
90
91
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
114         int m_weak_mark;
115         bool m_changed;
116
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  */
138 class 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
153         void clear();
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
170         bool contains_exact(const Interval& other) const;
171
172         bool operator==(const Intervals& other) const;
173         bool operator!=(const Intervals& other) const { return not (*this == other) ; }
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
188 /*
189 ** clock funcs:
190 */
191
192
193 bool monotonic_clock_gettime(long int& seconds, long int& nano_seconds);
194 long long monotonic_clock_gettime_nano();
195
196 bool realtime_clock_gettime(long int& seconds, long int& nano_seconds);
197
198
199
200 #endif