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>
21 #include <boost/date_time/compiler_config.hpp>
26 //!Provides generalized period type useful in date-time systems
27 /*!This template uses a class to represent a time point within the period
28 and another class to represent a duration. As a result, this class is
29 not appropriate for use when the number and duration representation
30 are the same (eg: in the regular number domain).
32 A period can be specified by providing either the begining point and
33 a duration or the begining point and the end point( end is NOT part
34 of the period but 1 unit past it. A period will be "invalid" if either
35 end_point <= begin_point or the given duration is <= 0. Any valid period
36 will return false for is_null().
38 Zero length periods are also considered invalid. Zero length periods are
39 periods where the begining and end points are the same, or, the given
40 duration is zero. For a zero length period, the last point will be one
41 unit less than the begining point.
43 In the case that the begin and last are the same, the period has a
46 The best way to handle periods is usually to provide a begining point and
47 a duration. So, day1 + 7 days is a week period which includes all of the
48 first day and 6 more days (eg: Sun to Sat).
51 template<class point_rep, class duration_rep>
52 class BOOST_SYMBOL_VISIBLE period : private
53 boost::less_than_comparable<period<point_rep, duration_rep>
54 , boost::equality_comparable< period<point_rep, duration_rep>
58 typedef point_rep point_type;
59 typedef duration_rep duration_type;
61 period(point_rep first_point, point_rep end_point);
62 period(point_rep first_point, duration_rep len);
63 point_rep begin() const;
64 point_rep end() const;
65 point_rep last() const;
66 duration_rep length() const;
68 bool operator==(const period& rhs) const;
69 bool operator<(const period& rhs) const;
70 void shift(const duration_rep& d);
71 void expand(const duration_rep& d);
72 bool contains(const point_rep& point) const;
73 bool contains(const period& other) const;
74 bool intersects(const period& other) const;
75 bool is_adjacent(const period& other) const;
76 bool is_before(const point_rep& point) const;
77 bool is_after(const point_rep& point) const;
78 period intersection(const period& other) const;
79 period merge(const period& other) const;
80 period span(const period& other) const;
86 //! create a period from begin to last eg: [begin,end)
87 /*! If end <= begin then the period will be invalid
89 template<class point_rep, class duration_rep>
91 period<point_rep,duration_rep>::period(point_rep first_point,
92 point_rep end_point) :
94 last_(end_point - duration_rep::unit())
97 //! create a period as [begin, begin+len)
98 /*! If len is <= 0 then the period will be invalid
100 template<class point_rep, class duration_rep>
102 period<point_rep,duration_rep>::period(point_rep first_point, duration_rep len) :
104 last_(first_point + len-duration_rep::unit())
108 //! Return the first element in the period
109 template<class point_rep, class duration_rep>
111 point_rep period<point_rep,duration_rep>::begin() const
116 //! Return one past the last element
117 template<class point_rep, class duration_rep>
119 point_rep period<point_rep,duration_rep>::end() const
121 return last_ + duration_rep::unit();
124 //! Return the last item in the period
125 template<class point_rep, class duration_rep>
127 point_rep period<point_rep,duration_rep>::last() const
132 //! True if period is ill formed (length is zero or less)
133 template<class point_rep, class duration_rep>
135 bool period<point_rep,duration_rep>::is_null() const
137 return end() <= begin_;
140 //! Return the length of the period
141 template<class point_rep, class duration_rep>
143 duration_rep period<point_rep,duration_rep>::length() const
145 if(last_ < begin_){ // invalid period
146 return last_+duration_rep::unit() - begin_;
149 return end() - begin_; // normal case
153 //! Equality operator
154 template<class point_rep, class duration_rep>
156 bool period<point_rep,duration_rep>::operator==(const period& rhs) const
158 return ((begin_ == rhs.begin_) &&
159 (last_ == rhs.last_));
162 //! Strict as defined by rhs.last <= lhs.last
163 template<class point_rep, class duration_rep>
165 bool period<point_rep,duration_rep>::operator<(const period& rhs) const
167 return (last_ < rhs.begin_);
171 //! Shift the start and end by the specified amount
172 template<class point_rep, class duration_rep>
174 void period<point_rep,duration_rep>::shift(const duration_rep& d)
180 /** Expands the size of the period by the duration on both ends.
193 * [----------------------]
199 template<class point_rep, class duration_rep>
201 void period<point_rep,duration_rep>::expand(const duration_rep& d)
207 //! True if the point is inside the period, zero length periods contain no points
208 template<class point_rep, class duration_rep>
210 bool period<point_rep,duration_rep>::contains(const point_rep& point) const
212 return ((point >= begin_) &&
217 //! True if this period fully contains (or equals) the other period
218 template<class point_rep, class duration_rep>
220 bool period<point_rep,duration_rep>::contains(const period<point_rep,duration_rep>& other) const
222 return ((begin_ <= other.begin_) && (last_ >= other.last_));
226 //! True if periods are next to each other without a gap.
227 /* In the example below, p1 and p2 are adjacent, but p3 is not adjacent
228 * with either of p1 or p2.
235 template<class point_rep, class duration_rep>
238 period<point_rep,duration_rep>::is_adjacent(const period<point_rep,duration_rep>& other) const
240 return (other.begin() == end() ||
241 begin_ == other.end());
245 //! True if all of the period is prior or t < start
246 /* In the example below only point 1 would evaluate to true.
254 template<class point_rep, class duration_rep>
257 period<point_rep,duration_rep>::is_after(const point_rep& t) const
261 return false; //null period isn't after
267 //! True if all of the period is prior to the passed point or end <= t
268 /* In the example below points 4 and 5 return true.
276 template<class point_rep, class duration_rep>
279 period<point_rep,duration_rep>::is_before(const point_rep& t) const
283 return false; //null period isn't before anything
290 //! True if the periods overlap in any way
291 /* In the example below p1 intersects with p2, p4, and p6.
301 template<class point_rep, class duration_rep>
303 bool period<point_rep,duration_rep>::intersects(const period<point_rep,duration_rep>& other) const
305 return ( contains(other.begin_) ||
306 other.contains(begin_) ||
307 ((other.begin_ < begin_) && (other.last_ >= begin_)));
310 //! Returns the period of intersection or invalid range no intersection
311 template<class point_rep, class duration_rep>
313 period<point_rep,duration_rep>
314 period<point_rep,duration_rep>::intersection(const period<point_rep,duration_rep>& other) const
316 if (begin_ > other.begin_) {
317 if (last_ <= other.last_) { //case2
321 return period<point_rep,duration_rep>(begin_, other.end());
324 if (last_ <= other.last_) { //case3
325 return period<point_rep,duration_rep>(other.begin_, this->end());
333 //! Returns the union of intersecting periods -- or null period
336 template<class point_rep, class duration_rep>
338 period<point_rep,duration_rep>
339 period<point_rep,duration_rep>::merge(const period<point_rep,duration_rep>& other) const
341 if (this->intersects(other)) {
342 if (begin_ < other.begin_) {
343 return period<point_rep,duration_rep>(begin_, last_ > other.last_ ? this->end() : other.end());
346 return period<point_rep,duration_rep>(other.begin_, last_ > other.last_ ? this->end() : other.end());
349 return period<point_rep,duration_rep>(begin_,begin_); // no intersect return null
352 //! Combine two periods with earliest start and latest end.
353 /*! Combines two periods and any gap between them such that
354 * start = min(p1.start, p2.start)
355 * end = max(p1.end , p2.end)
360 * [-----------p3----------)
363 template<class point_rep, class duration_rep>
365 period<point_rep,duration_rep>
366 period<point_rep,duration_rep>::span(const period<point_rep,duration_rep>& other) const
368 point_rep start((begin_ < other.begin_) ? begin() : other.begin());
369 point_rep newend((last_ < other.last_) ? other.end() : this->end());
370 return period<point_rep,duration_rep>(start, newend);
374 } } //namespace date_time