00001 // OSB library ************************************************ -*- C++ -*- 00006 /* 00007 AUTHOR(S): Stephan Broennimann (vb) 00008 00009 RCS information 00010 $Name: OSB_060808 $ 00011 $Revision: 1.12 $ 00012 00013 License 00014 OSB rating and billing library for communication networks 00015 Copyright (C) 2004, 2005, 2006 OSB systems 00016 00017 This file may be distributed and/or modify under the terms of the 00018 GNU General Public License (GPL) as published by the Free Software 00019 Foundation which is provided in the file LICENSE.GPL included in the 00020 packaging of this file. 00021 00022 The file is distributed in the hope that it will be useful, but WITHOUT 00023 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00024 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 00025 for more details. 00026 00027 Holders of a OSB Commercial License may use this file under the terms 00028 and conditions of this commercial license. 00029 */ 00030 #ifndef _PERIOD_H_ 00031 #define _PERIOD_H_ 00032 00033 // ************************************************************************ 00034 // included header files 00035 // + standard includes 00036 00037 // + libraries 00038 00039 // + local headers 00040 #include "chrono.h" 00041 #include "osberror.h" 00042 00043 // + class declarations 00044 00045 // ************************************************************************ 00046 // namespace extensions 00047 namespace OSB_LIB { 00048 00049 // ************************************************************************ 00050 // forward declarations 00051 00052 // ************************************************************************ 00053 // type definitions 00054 00055 // ************************************************************************ 00056 // class definitions 00068 class Period { 00069 public: 00080 struct Less { 00086 inline bool operator()( 00087 const Period& lhs, 00088 const Period& rhs 00089 ); 00090 }; // struct Less 00091 00092 // defined below 00093 class Overlap; 00094 00095 public: 00102 static Period infinite(); 00103 00110 explicit Period( 00111 const DateTime& ts = DateTime() 00112 ); 00113 00122 Period(const DateTime& from, const DateTime& to); 00123 00124 public: 00126 00127 00128 DateTime from() const; 00130 DateTime to() const; 00132 bool isTimestamp() const; 00134 00143 bool contains(const DateTime& ts) const; 00144 00152 bool overlap(const Period& rhs) const; 00153 00174 bool less(const Period& rhs) const; 00175 00176 private: 00178 DateTime from_; 00180 DateTime to_; 00182 bool isTs_; 00183 }; // class Period 00184 00191 class Period::Overlap : public OsbException { 00193 static ErrorNo errNo_; 00194 public: 00200 Overlap( 00201 const Period& lhs, 00202 const Period& rhs 00203 ); 00204 public: 00206 const Period& lhs() const; 00208 const Period& rhs() const; 00209 private: 00211 Period lhs_; 00213 Period rhs_; 00214 }; // class Period::Overlap 00215 } // namespace OSB_LIB 00216 00217 // ************************************************************************ 00218 // inline definitions 00219 namespace OSB_LIB { 00220 00221 // ******************************************************************** 00222 // class Period 00223 inline Period Period::infinite() 00224 { 00225 return Period(DateTime::min(), 00226 DateTime::max()); 00227 } 00228 00229 inline Period::Period( 00230 const DateTime& ts 00231 ) : from_(ts), to_(ts), isTs_(true) 00232 { 00233 // empty 00234 } 00235 00236 inline DateTime Period::from() const 00237 { 00238 return from_; 00239 } 00240 00241 inline DateTime Period::to() const 00242 { 00243 return to_; 00244 } 00245 00246 inline bool Period::isTimestamp() const 00247 { 00248 return isTs_; 00249 } 00250 00251 inline bool Period::contains(const DateTime& ts) const 00252 { 00253 // a time stamp can not contain another time stamp 00254 if (isTs_) return false; 00255 // an unknown time stamp never is within a period 00256 if (ts.notSet()) return false; 00257 00258 // note: if upper boundary of period is infinite 00259 // then any ts is less than the upper boundary 00260 // (except if not set, but this is handled above) 00261 return ( (from_ <= ts || from_ == DateTime::min()) 00262 && (ts < to_ || to_ == DateTime::max())); 00263 } 00264 00265 inline bool Period::overlap(const Period& rhs) const 00266 { 00267 if (isTs_ || rhs.isTs_) return false; 00268 00269 return contains(rhs.from_) || rhs.contains(from_); 00270 } 00271 00272 inline bool Period::less(const Period& rhs) const 00273 { 00274 // lhs is TS, rhs is period or TS: 00275 // in both cases compare the from dates 00276 if (isTs_) return from_ < rhs.from_; 00277 // lhs is period, rhs is TS 00278 else if (rhs.isTs_) return to_ <= rhs.from_; 00279 00280 // two periods: check for overlap 00281 if (overlap(rhs)) { 00282 throw Overlap(*this, rhs); 00283 } 00284 00285 return (from_ < rhs.from_); 00286 } 00287 00288 // ******************************************************************** 00289 // struct Period::Less 00290 inline bool Period::Less::operator()( 00291 const Period& lhs, 00292 const Period& rhs 00293 ) 00294 { 00295 return lhs.less(rhs); 00296 } 00297 } // namespace OSB_LIB 00298 #endif // #ifndef _PERIOD_H_