File indexing completed on 2025-01-30 09:35:24
0001 #ifndef DATE_TIME_PERIOD_HPP___
0002 #define DATE_TIME_PERIOD_HPP___
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020 #include <boost/operators.hpp>
0021 #include <boost/date_time/compiler_config.hpp>
0022
0023
0024 namespace boost {
0025 namespace date_time {
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051 template<class point_rep, class duration_rep>
0052 class BOOST_SYMBOL_VISIBLE period : private
0053 boost::less_than_comparable<period<point_rep, duration_rep>
0054 , boost::equality_comparable< period<point_rep, duration_rep>
0055 > >
0056 {
0057 public:
0058 typedef point_rep point_type;
0059 typedef duration_rep duration_type;
0060
0061 BOOST_CXX14_CONSTEXPR period(point_rep first_point, point_rep end_point);
0062 BOOST_CXX14_CONSTEXPR period(point_rep first_point, duration_rep len);
0063 BOOST_CXX14_CONSTEXPR point_rep begin() const;
0064 BOOST_CXX14_CONSTEXPR point_rep end() const;
0065 BOOST_CXX14_CONSTEXPR point_rep last() const;
0066 BOOST_CXX14_CONSTEXPR duration_rep length() const;
0067 BOOST_CXX14_CONSTEXPR bool is_null() const;
0068 BOOST_CXX14_CONSTEXPR bool operator==(const period& rhs) const;
0069 BOOST_CXX14_CONSTEXPR bool operator<(const period& rhs) const;
0070 BOOST_CXX14_CONSTEXPR void shift(const duration_rep& d);
0071 BOOST_CXX14_CONSTEXPR void expand(const duration_rep& d);
0072 BOOST_CXX14_CONSTEXPR bool contains(const point_rep& point) const;
0073 BOOST_CXX14_CONSTEXPR bool contains(const period& other) const;
0074 BOOST_CXX14_CONSTEXPR bool intersects(const period& other) const;
0075 BOOST_CXX14_CONSTEXPR bool is_adjacent(const period& other) const;
0076 BOOST_CXX14_CONSTEXPR bool is_before(const point_rep& point) const;
0077 BOOST_CXX14_CONSTEXPR bool is_after(const point_rep& point) const;
0078 BOOST_CXX14_CONSTEXPR period intersection(const period& other) const;
0079 BOOST_CXX14_CONSTEXPR period merge(const period& other) const;
0080 BOOST_CXX14_CONSTEXPR period span(const period& other) const;
0081 private:
0082 point_rep begin_;
0083 point_rep last_;
0084 };
0085
0086
0087
0088
0089 template<class point_rep, class duration_rep>
0090 inline BOOST_CXX14_CONSTEXPR
0091 period<point_rep,duration_rep>::period(point_rep first_point,
0092 point_rep end_point) :
0093 begin_(first_point),
0094 last_(end_point - duration_rep::unit())
0095 {}
0096
0097
0098
0099
0100 template<class point_rep, class duration_rep>
0101 inline BOOST_CXX14_CONSTEXPR
0102 period<point_rep,duration_rep>::period(point_rep first_point, duration_rep len) :
0103 begin_(first_point),
0104 last_(first_point + len-duration_rep::unit())
0105 { }
0106
0107
0108
0109 template<class point_rep, class duration_rep>
0110 inline BOOST_CXX14_CONSTEXPR
0111 point_rep period<point_rep,duration_rep>::begin() const
0112 {
0113 return begin_;
0114 }
0115
0116
0117 template<class point_rep, class duration_rep>
0118 inline BOOST_CXX14_CONSTEXPR
0119 point_rep period<point_rep,duration_rep>::end() const
0120 {
0121 return last_ + duration_rep::unit();
0122 }
0123
0124
0125 template<class point_rep, class duration_rep>
0126 inline BOOST_CXX14_CONSTEXPR
0127 point_rep period<point_rep,duration_rep>::last() const
0128 {
0129 return last_;
0130 }
0131
0132
0133 template<class point_rep, class duration_rep>
0134 inline BOOST_CXX14_CONSTEXPR
0135 bool period<point_rep,duration_rep>::is_null() const
0136 {
0137 return end() <= begin_;
0138 }
0139
0140
0141 template<class point_rep, class duration_rep>
0142 inline BOOST_CXX14_CONSTEXPR
0143 duration_rep period<point_rep,duration_rep>::length() const
0144 {
0145 if(last_ < begin_){
0146 return last_+duration_rep::unit() - begin_;
0147 }
0148 else{
0149 return end() - begin_;
0150 }
0151 }
0152
0153
0154 template<class point_rep, class duration_rep>
0155 inline BOOST_CXX14_CONSTEXPR
0156 bool period<point_rep,duration_rep>::operator==(const period& rhs) const
0157 {
0158 return ((begin_ == rhs.begin_) &&
0159 (last_ == rhs.last_));
0160 }
0161
0162
0163 template<class point_rep, class duration_rep>
0164 inline BOOST_CXX14_CONSTEXPR
0165 bool period<point_rep,duration_rep>::operator<(const period& rhs) const
0166 {
0167 return (last_ < rhs.begin_);
0168 }
0169
0170
0171
0172 template<class point_rep, class duration_rep>
0173 inline BOOST_CXX14_CONSTEXPR
0174 void period<point_rep,duration_rep>::shift(const duration_rep& d)
0175 {
0176 begin_ = begin_ + d;
0177 last_ = last_ + d;
0178 }
0179
0180
0181
0182
0183
0184
0185
0186
0187
0188
0189
0190
0191
0192
0193
0194
0195
0196
0197
0198
0199 template<class point_rep, class duration_rep>
0200 inline BOOST_CXX14_CONSTEXPR
0201 void period<point_rep,duration_rep>::expand(const duration_rep& d)
0202 {
0203 begin_ = begin_ - d;
0204 last_ = last_ + d;
0205 }
0206
0207
0208 template<class point_rep, class duration_rep>
0209 inline BOOST_CXX14_CONSTEXPR
0210 bool period<point_rep,duration_rep>::contains(const point_rep& point) const
0211 {
0212 return ((point >= begin_) &&
0213 (point <= last_));
0214 }
0215
0216
0217
0218 template<class point_rep, class duration_rep>
0219 inline BOOST_CXX14_CONSTEXPR
0220 bool period<point_rep,duration_rep>::contains(const period<point_rep,duration_rep>& other) const
0221 {
0222 return ((begin_ <= other.begin_) && (last_ >= other.last_));
0223 }
0224
0225
0226
0227
0228
0229
0230
0231
0232
0233
0234
0235 template<class point_rep, class duration_rep>
0236 inline BOOST_CXX14_CONSTEXPR
0237 bool period<point_rep,duration_rep>::is_adjacent(const period<point_rep,duration_rep>& other) const
0238 {
0239 return (other.begin() == end() ||
0240 begin_ == other.end());
0241 }
0242
0243
0244
0245
0246
0247
0248
0249
0250
0251
0252
0253 template<class point_rep, class duration_rep>
0254 inline BOOST_CXX14_CONSTEXPR
0255 bool period<point_rep,duration_rep>::is_after(const point_rep& t) const
0256 {
0257 if (is_null())
0258 {
0259 return false;
0260 }
0261
0262 return t < begin_;
0263 }
0264
0265
0266
0267
0268
0269
0270
0271
0272
0273
0274 template<class point_rep, class duration_rep>
0275 inline BOOST_CXX14_CONSTEXPR
0276 bool period<point_rep,duration_rep>::is_before(const point_rep& t) const
0277 {
0278 if (is_null())
0279 {
0280 return false;
0281 }
0282
0283 return last_ < t;
0284 }
0285
0286
0287
0288
0289
0290
0291
0292
0293
0294
0295
0296
0297
0298 template<class point_rep, class duration_rep>
0299 inline BOOST_CXX14_CONSTEXPR
0300 bool period<point_rep,duration_rep>::intersects(const period<point_rep,duration_rep>& other) const
0301 {
0302 return ( contains(other.begin_) ||
0303 other.contains(begin_) ||
0304 ((other.begin_ < begin_) && (other.last_ >= begin_)));
0305 }
0306
0307
0308 template<class point_rep, class duration_rep>
0309 inline BOOST_CXX14_CONSTEXPR
0310 period<point_rep,duration_rep>
0311 period<point_rep,duration_rep>::intersection(const period<point_rep,duration_rep>& other) const
0312 {
0313 if (begin_ > other.begin_) {
0314 if (last_ <= other.last_) {
0315 return *this;
0316 }
0317
0318 return period<point_rep,duration_rep>(begin_, other.end());
0319 }
0320 else {
0321 if (last_ <= other.last_) {
0322 return period<point_rep,duration_rep>(other.begin_, this->end());
0323 }
0324
0325 return other;
0326 }
0327
0328 }
0329
0330
0331
0332
0333 template<class point_rep, class duration_rep>
0334 inline BOOST_CXX14_CONSTEXPR
0335 period<point_rep,duration_rep>
0336 period<point_rep,duration_rep>::merge(const period<point_rep,duration_rep>& other) const
0337 {
0338 if (this->intersects(other)) {
0339 if (begin_ < other.begin_) {
0340 return period<point_rep,duration_rep>(begin_, last_ > other.last_ ? this->end() : other.end());
0341 }
0342
0343 return period<point_rep,duration_rep>(other.begin_, last_ > other.last_ ? this->end() : other.end());
0344
0345 }
0346 return period<point_rep,duration_rep>(begin_,begin_);
0347 }
0348
0349
0350
0351
0352
0353
0354
0355
0356
0357
0358
0359
0360 template<class point_rep, class duration_rep>
0361 inline BOOST_CXX14_CONSTEXPR
0362 period<point_rep,duration_rep>
0363 period<point_rep,duration_rep>::span(const period<point_rep,duration_rep>& other) const
0364 {
0365 point_rep start((begin_ < other.begin_) ? begin() : other.begin());
0366 point_rep newend((last_ < other.last_) ? other.end() : this->end());
0367 return period<point_rep,duration_rep>(start, newend);
0368 }
0369
0370
0371 } }
0372
0373
0374
0375 #endif