123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377 |
- #ifndef DATE_TIME_PERIOD_HPP___
- #define DATE_TIME_PERIOD_HPP___
- #include "boost/operators.hpp"
- namespace boost {
- namespace date_time {
-
-
- template<class point_rep, class duration_rep>
- class period : private
- boost::less_than_comparable<period<point_rep, duration_rep>
- , boost::equality_comparable< period<point_rep, duration_rep>
- > >
- {
- public:
- typedef point_rep point_type;
- typedef duration_rep duration_type;
- period(point_rep first_point, point_rep end_point);
- period(point_rep first_point, duration_rep len);
- point_rep begin() const;
- point_rep end() const;
- point_rep last() const;
- duration_rep length() const;
- bool is_null() const;
- bool operator==(const period& rhs) const;
- bool operator<(const period& rhs) const;
- void shift(const duration_rep& d);
- void expand(const duration_rep& d);
- bool contains(const point_rep& point) const;
- bool contains(const period& other) const;
- bool intersects(const period& other) const;
- bool is_adjacent(const period& other) const;
- bool is_before(const point_rep& point) const;
- bool is_after(const point_rep& point) const;
- period intersection(const period& other) const;
- period merge(const period& other) const;
- period span(const period& other) const;
- private:
- point_rep begin_;
- point_rep last_;
- };
-
-
- template<class point_rep, class duration_rep>
- inline
- period<point_rep,duration_rep>::period(point_rep first_point,
- point_rep end_point) :
- begin_(first_point),
- last_(end_point - duration_rep::unit())
- {}
-
-
- template<class point_rep, class duration_rep>
- inline
- period<point_rep,duration_rep>::period(point_rep first_point, duration_rep len) :
- begin_(first_point),
- last_(first_point + len-duration_rep::unit())
- { }
-
- template<class point_rep, class duration_rep>
- inline
- point_rep period<point_rep,duration_rep>::begin() const
- {
- return begin_;
- }
-
- template<class point_rep, class duration_rep>
- inline
- point_rep period<point_rep,duration_rep>::end() const
- {
- return last_ + duration_rep::unit();
- }
-
- template<class point_rep, class duration_rep>
- inline
- point_rep period<point_rep,duration_rep>::last() const
- {
- return last_;
- }
-
- template<class point_rep, class duration_rep>
- inline
- bool period<point_rep,duration_rep>::is_null() const
- {
- return end() <= begin_;
- }
-
- template<class point_rep, class duration_rep>
- inline
- duration_rep period<point_rep,duration_rep>::length() const
- {
- if(last_ < begin_){
- return last_+duration_rep::unit() - begin_;
- }
- else{
- return end() - begin_;
- }
- }
-
- template<class point_rep, class duration_rep>
- inline
- bool period<point_rep,duration_rep>::operator==(const period& rhs) const
- {
- return ((begin_ == rhs.begin_) &&
- (last_ == rhs.last_));
- }
-
- template<class point_rep, class duration_rep>
- inline
- bool period<point_rep,duration_rep>::operator<(const period& rhs) const
- {
- return (last_ < rhs.begin_);
- }
-
- template<class point_rep, class duration_rep>
- inline
- void period<point_rep,duration_rep>::shift(const duration_rep& d)
- {
- begin_ = begin_ + d;
- last_ = last_ + d;
- }
-
- template<class point_rep, class duration_rep>
- inline
- void period<point_rep,duration_rep>::expand(const duration_rep& d)
- {
- begin_ = begin_ - d;
- last_ = last_ + d;
- }
-
- template<class point_rep, class duration_rep>
- inline
- bool period<point_rep,duration_rep>::contains(const point_rep& point) const
- {
- return ((point >= begin_) &&
- (point <= last_));
- }
-
- template<class point_rep, class duration_rep>
- inline
- bool period<point_rep,duration_rep>::contains(const period<point_rep,duration_rep>& other) const
- {
- return ((begin_ <= other.begin_) && (last_ >= other.last_));
- }
-
-
- template<class point_rep, class duration_rep>
- inline
- bool
- period<point_rep,duration_rep>::is_adjacent(const period<point_rep,duration_rep>& other) const
- {
- return (other.begin() == end() ||
- begin_ == other.end());
- }
-
-
- template<class point_rep, class duration_rep>
- inline
- bool
- period<point_rep,duration_rep>::is_after(const point_rep& t) const
- {
- if (is_null())
- {
- return false;
- }
-
- return t < begin_;
- }
-
-
- template<class point_rep, class duration_rep>
- inline
- bool
- period<point_rep,duration_rep>::is_before(const point_rep& t) const
- {
- if (is_null())
- {
- return false;
- }
-
- return last_ < t;
- }
-
-
- template<class point_rep, class duration_rep>
- inline
- bool period<point_rep,duration_rep>::intersects(const period<point_rep,duration_rep>& other) const
- {
- return ( contains(other.begin_) ||
- other.contains(begin_) ||
- ((other.begin_ < begin_) && (other.last_ >= begin_)));
- }
-
- template<class point_rep, class duration_rep>
- inline
- period<point_rep,duration_rep>
- period<point_rep,duration_rep>::intersection(const period<point_rep,duration_rep>& other) const
- {
- if (begin_ > other.begin_) {
- if (last_ <= other.last_) {
- return *this;
- }
-
- return period<point_rep,duration_rep>(begin_, other.end());
- }
- else {
- if (last_ <= other.last_) {
- return period<point_rep,duration_rep>(other.begin_, this->end());
- }
-
- return other;
- }
-
- }
-
-
- template<class point_rep, class duration_rep>
- inline
- period<point_rep,duration_rep>
- period<point_rep,duration_rep>::merge(const period<point_rep,duration_rep>& other) const
- {
- if (this->intersects(other)) {
- if (begin_ < other.begin_) {
- return period<point_rep,duration_rep>(begin_, last_ > other.last_ ? this->end() : other.end());
- }
-
- return period<point_rep,duration_rep>(other.begin_, last_ > other.last_ ? this->end() : other.end());
-
- }
- return period<point_rep,duration_rep>(begin_,begin_);
- }
-
-
- template<class point_rep, class duration_rep>
- inline
- period<point_rep,duration_rep>
- period<point_rep,duration_rep>::span(const period<point_rep,duration_rep>& other) const
- {
- point_rep start((begin_ < other.begin_) ? begin() : other.begin());
- point_rep newend((last_ < other.last_) ? other.end() : this->end());
- return period<point_rep,duration_rep>(start, newend);
- }
- } }
- #endif
|