fix unit test for pid_of in case of systemd
[libi2ncommon] / src / timefunc.hxx
CommitLineData
0e23f538
TJ
1/*
2The software in this package is distributed under the GNU General
3Public License version 2 (with a special exception described below).
4
5A copy of GNU General Public License (GPL) is included in this distribution,
6in the file COPYING.GPL.
7
8As a special exception, if other files instantiate templates or use macros
9or inline functions from this file, or you compile this file and link it
10with other works to produce a work based on this file, this file
11does not by itself cause the resulting work to be covered
12by the GNU General Public License.
13
14However the source code for this file must still be made available
15in accordance with section (3) of the GNU General Public License.
16
17This exception does not invalidate any other reasons why a work based
18on 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
34double prec_time(void);
35
36int date_to_seconds(const std::string &date);
37
38std::string make_nice_time(int seconds);
7839bd53 39std::string format_full_time(time_t seconds);
87869870 40void seconds_to_hour_minute(int seconds, int *hour, int *minute);
c0368918
GE
41void split_daysec(int daysec, int *outhours=NULL, int *outminutes=NULL, int *outseconds=NULL);
42std::string output_hour_minute(int hour, int minute, bool h_for_00=true, int seconds=0);
2c66f490
GE
43
44inline std::string output_hour_minute_from_seconds(int seconds)
45{
46 int hour, minute;
c0368918 47 split_daysec(seconds,&hour,&minute);
2c66f490
GE
48 return output_hour_minute(hour,minute);
49}
e93545dd 50
4e157d1d
TJ
51std::string get_month_name(unsigned char month);
52
1b5dfd98
TJ
53/**
54 * @brief structure representing a single (half-open) interval.
55 */
56class Interval
57{
58 public:
59 Interval()
60 : m_lower_bound(0)
61 , m_upper_bound(0)
80f30818
TJ
62 , m_weak_mark(0)
63 , m_changed(false)
1b5dfd98
TJ
64 {
65 } //
66
80f30818 67 Interval( unsigned int start, unsigned int end, int weak_mark= 0 )
1b5dfd98
TJ
68 : m_lower_bound(start)
69 , m_upper_bound(end)
80f30818
TJ
70 , m_weak_mark(weak_mark)
71 , m_changed(false)
1b5dfd98
TJ
72 {
73 } //
74
d181c3bc
TJ
75
76 void clear();
77
1b5dfd98
TJ
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
80f30818
TJ
92 int weak_mark() const { return m_weak_mark; }
93
94 bool changed() const { return m_changed; }
95
1b5dfd98
TJ
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
ebc3b584
TJ
103 bool operator!=(const Interval& other) const
104 {
105 return not (*this == other);
106 } // eo operator!=(const Interval&)
107
108
1b5dfd98
TJ
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
80f30818
TJ
131 int m_weak_mark;
132 bool m_changed;
133
1b5dfd98
TJ
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 */
155class 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
d181c3bc 170 void clear();
1b5dfd98
TJ
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
e156de7c
TJ
187 bool contains_exact(const Interval& other) const;
188
1b5dfd98 189 bool operator==(const Intervals& other) const;
ebc3b584 190 bool operator!=(const Intervals& other) const { return not (*this == other) ; }
1b5dfd98
TJ
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
96d0be2e
TJ
205/*
206** clock funcs:
207*/
208
209
210bool monotonic_clock_gettime(long int& seconds, long int& nano_seconds);
b7e17426 211long long monotonic_clock_gettime_nano();
96d0be2e
TJ
212
213bool realtime_clock_gettime(long int& seconds, long int& nano_seconds);
214
1b5dfd98
TJ
215
216
e93545dd 217#endif