1 #ifndef DATE_TIME_PERIOD_HPP___
2 #define DATE_TIME_PERIOD_HPP___
4 /* Copyright (c) 2002,2003 CrystalClear Software, Inc.
5 * Use, modification and distribution is subject to the
6 * Boost Software License, Version 1.0. (See accompanying
7 * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
8 * Author: Jeff Garland, Bart Garst
13 This file contain the implementation of the period abstraction. This is
14 basically the same idea as a range. Although this class is intended for
15 use in the time library, it is pretty close to general enough for other
20 #include "boost/operators.hpp"
25 //!Provides generalized period type useful in date-time systems
26 /*!This template uses a class to represent a time point within the period
27 and another class to represent a duration. As a result, this class is
28 not appropriate for use when the number and duration representation
29 are the same (eg: in the regular number domain).
31 A period can be specified by providing either the begining point and
32 a duration or the begining point and the end point( end is NOT part
33 of the period but 1 unit past it. A period will be "invalid" if either
34 end_point <= begin_point or the given duration is <= 0. Any valid period
35 will return false for is_null().
37 Zero length periods are also considered invalid. Zero length periods are
38 periods where the begining and end points are the same, or, the given
39 duration is zero. For a zero length period, the last point will be one
40 unit less than the begining point.
42 In the case that the begin and last are the same, the period has a
45 The best way to handle periods is usually to provide a begining point and
46 a duration. So, day1 + 7 days is a week period which includes all of the
47 first day and 6 more days (eg: Sun to Sat).
50 template<class point_rep
, class duration_rep
>
51 class period
: private
52 boost::less_than_comparable
<period
<point_rep
, duration_rep
>
53 , boost::equality_comparable
< period
<point_rep
, duration_rep
>
57 typedef point_rep point_type
;
58 typedef duration_rep duration_type
;
60 period(point_rep first_point
, point_rep end_point
);
61 period(point_rep first_point
, duration_rep len
);
62 point_rep
begin() const;
63 point_rep
end() const;
64 point_rep
last() const;
65 duration_rep
length() const;
67 bool operator==(const period
& rhs
) const;
68 bool operator<(const period
& rhs
) const;
69 void shift(const duration_rep
& d
);
70 void expand(const duration_rep
& d
);
71 bool contains(const point_rep
& point
) const;
72 bool contains(const period
& other
) const;
73 bool intersects(const period
& other
) const;
74 bool is_adjacent(const period
& other
) const;
75 bool is_before(const point_rep
& point
) const;
76 bool is_after(const point_rep
& point
) const;
77 period
intersection(const period
& other
) const;
78 period
merge(const period
& other
) const;
79 period
span(const period
& other
) const;
85 //! create a period from begin to last eg: [begin,end)
86 /*! If end <= begin then the period will be invalid
88 template<class point_rep
, class duration_rep
>
90 period
<point_rep
,duration_rep
>::period(point_rep first_point
,
91 point_rep end_point
) :
93 last_(end_point
- duration_rep::unit())
96 //! create a period as [begin, begin+len)
97 /*! If len is <= 0 then the period will be invalid
99 template<class point_rep
, class duration_rep
>
101 period
<point_rep
,duration_rep
>::period(point_rep first_point
, duration_rep len
) :
103 last_(first_point
+ len
-duration_rep::unit())
107 //! Return the first element in the period
108 template<class point_rep
, class duration_rep
>
110 point_rep period
<point_rep
,duration_rep
>::begin() const
115 //! Return one past the last element
116 template<class point_rep
, class duration_rep
>
118 point_rep period
<point_rep
,duration_rep
>::end() const
120 return last_
+ duration_rep::unit();
123 //! Return the last item in the period
124 template<class point_rep
, class duration_rep
>
126 point_rep period
<point_rep
,duration_rep
>::last() const
131 //! True if period is ill formed (length is zero or less)
132 template<class point_rep
, class duration_rep
>
134 bool period
<point_rep
,duration_rep
>::is_null() const
136 return end() <= begin_
;
139 //! Return the length of the period
140 template<class point_rep
, class duration_rep
>
142 duration_rep period
<point_rep
,duration_rep
>::length() const
144 if(last_
< begin_
){ // invalid period
145 return last_
+duration_rep::unit() - begin_
;
148 return end() - begin_
; // normal case
152 //! Equality operator
153 template<class point_rep
, class duration_rep
>
155 bool period
<point_rep
,duration_rep
>::operator==(const period
& rhs
) const
157 return ((begin_
== rhs
.begin_
) &&
158 (last_
== rhs
.last_
));
161 //! Strict as defined by rhs.last <= lhs.last
162 template<class point_rep
, class duration_rep
>
164 bool period
<point_rep
,duration_rep
>::operator<(const period
& rhs
) const
166 return (last_
< rhs
.begin_
);
170 //! Shift the start and end by the specified amount
171 template<class point_rep
, class duration_rep
>
173 void period
<point_rep
,duration_rep
>::shift(const duration_rep
& d
)
179 /** Expands the size of the period by the duration on both ends.
192 * [----------------------]
198 template<class point_rep
, class duration_rep
>
200 void period
<point_rep
,duration_rep
>::expand(const duration_rep
& d
)
206 //! True if the point is inside the period, zero length periods contain no points
207 template<class point_rep
, class duration_rep
>
209 bool period
<point_rep
,duration_rep
>::contains(const point_rep
& point
) const
211 return ((point
>= begin_
) &&
216 //! True if this period fully contains (or equals) the other period
217 template<class point_rep
, class duration_rep
>
219 bool period
<point_rep
,duration_rep
>::contains(const period
<point_rep
,duration_rep
>& other
) const
221 return ((begin_
<= other
.begin_
) && (last_
>= other
.last_
));
225 //! True if periods are next to each other without a gap.
226 /* In the example below, p1 and p2 are adjacent, but p3 is not adjacent
227 * with either of p1 or p2.
234 template<class point_rep
, class duration_rep
>
237 period
<point_rep
,duration_rep
>::is_adjacent(const period
<point_rep
,duration_rep
>& other
) const
239 return (other
.begin() == end() ||
240 begin_
== other
.end());
244 //! True if all of the period is prior or t < start
245 /* In the example below only point 1 would evaluate to true.
253 template<class point_rep
, class duration_rep
>
256 period
<point_rep
,duration_rep
>::is_after(const point_rep
& t
) const
260 return false; //null period isn't after
266 //! True if all of the period is prior to the passed point or end <= t
267 /* In the example below points 4 and 5 return true.
275 template<class point_rep
, class duration_rep
>
278 period
<point_rep
,duration_rep
>::is_before(const point_rep
& t
) const
282 return false; //null period isn't before anything
289 //! True if the periods overlap in any way
290 /* In the example below p1 intersects with p2, p4, and p6.
300 template<class point_rep
, class duration_rep
>
302 bool period
<point_rep
,duration_rep
>::intersects(const period
<point_rep
,duration_rep
>& other
) const
304 return ( contains(other
.begin_
) ||
305 other
.contains(begin_
) ||
306 ((other
.begin_
< begin_
) && (other
.last_
>= begin_
)));
309 //! Returns the period of intersection or invalid range no intersection
310 template<class point_rep
, class duration_rep
>
312 period
<point_rep
,duration_rep
>
313 period
<point_rep
,duration_rep
>::intersection(const period
<point_rep
,duration_rep
>& other
) const
315 if (begin_
> other
.begin_
) {
316 if (last_
<= other
.last_
) { //case2
320 return period
<point_rep
,duration_rep
>(begin_
, other
.end());
323 if (last_
<= other
.last_
) { //case3
324 return period
<point_rep
,duration_rep
>(other
.begin_
, this->end());
332 //! Returns the union of intersecting periods -- or null period
335 template<class point_rep
, class duration_rep
>
337 period
<point_rep
,duration_rep
>
338 period
<point_rep
,duration_rep
>::merge(const period
<point_rep
,duration_rep
>& other
) const
340 if (this->intersects(other
)) {
341 if (begin_
< other
.begin_
) {
342 return period
<point_rep
,duration_rep
>(begin_
, last_
> other
.last_
? this->end() : other
.end());
345 return period
<point_rep
,duration_rep
>(other
.begin_
, last_
> other
.last_
? this->end() : other
.end());
348 return period
<point_rep
,duration_rep
>(begin_
,begin_
); // no intersect return null
351 //! Combine two periods with earliest start and latest end.
352 /*! Combines two periods and any gap between them such that
353 * start = min(p1.start, p2.start)
354 * end = max(p1.end , p2.end)
359 * [-----------p3----------)
362 template<class point_rep
, class duration_rep
>
364 period
<point_rep
,duration_rep
>
365 period
<point_rep
,duration_rep
>::span(const period
<point_rep
,duration_rep
>& other
) const
367 point_rep
start((begin_
< other
.begin_
) ? begin() : other
.begin());
368 point_rep
newend((last_
< other
.last_
) ? other
.end() : this->end());
369 return period
<point_rep
,duration_rep
>(start
, newend
);
373 } } //namespace date_time