Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members

period.h

Go to the documentation of this file.
00001 #ifndef DV_UTIL_PERIOD 00002 #define DV_UTIL_PERIOD 00003 // $Id: period.h,v 1.4 2002/10/18 11:41:26 dvermeir Exp $ 00004 00005 #include <stdexcept> 00006 #include <dvutil/date.h> 00007 00008 namespace Dv { 00009 namespace Util { 00010 00011 //! Class representing a duration. 00012 /*! This is intended for administrative applications, e.g. adding 00013 a duration of 24 hrs to a Date will result in the next day, 00014 same time, even if a daylight savings time switch happens in 00015 the period. 00016 00017 There are 3 incompatible units: 00018 <ul> 00019 <li> seconds (and minutes, hours, days, weeks). 00020 <li> months (same reasoning as above). 00021 <li> years (idem) 00022 </ul> 00023 00024 */ 00025 class Duration { 00026 public: 00027 //! Rather inconvenient, the static functions below are probably more useful. 00028 Duration(size_t years, size_t months, size_t seconds): years_(years), 00029 months_(months), seconds_(seconds) {} 00030 00031 //@{ 00032 /*! Convenience replacements for constructor. 00033 */ 00034 static Duration mins(size_t mins, size_t secs = 0); 00035 static Duration hrs(size_t hrs, size_t mins=0, size_t secs = 0); 00036 static Duration days(size_t days, size_t hrs=0, size_t mins=0, size_t secs = 0); 00037 static Duration weeks(size_t wks, size_t days=0, size_t hrs=0, 00038 size_t mins=0, size_t secs = 0); 00039 static Duration months(size_t months); 00040 static Duration years(size_t years, size_t months=0); 00041 //@} 00042 00043 //! Add two duration: separately adds the differen second/month/year units. 00044 Duration operator+(const Duration& d) const; 00045 //! Add a duration to a duration: separately adds the differen second/month/year units. 00046 Duration operator+=(const Duration& d); 00047 00048 //! Number of years of duration. 00049 size_t years() const { return years_; } 00050 //! Number of months of duration. 00051 size_t months() const { return months_; } 00052 //! Number of seconds of duration. 00053 size_t seconds() const { return seconds_; } 00054 00055 //! Return string representation of duration. 00056 /*! This has the form 00057 \code 00058 YY-MM-DD HH:MM:SS 00059 \endcode 00060 */ 00061 std::string str() const; 00062 private: 00063 size_t years_; 00064 size_t months_; 00065 unsigned long seconds_; 00066 }; 00067 00068 //! A Period is simply a Date with a Duration. 00069 class Period { 00070 public: 00071 //! Constructor. 00072 Period(const Date& start, const Duration& size): start_(start), size_(size) {} 00073 //! Constructor, the duration will be such that <code>start + size = end</code>. 00074 Period(Date start, Date end) throw (std::runtime_error); 00075 00076 //! Start date of period. 00077 Date start() const { return start_; } 00078 //! End date of period, i.e. <code>start + size</code>. 00079 Date end() const; 00080 //! Size of period. 00081 const Duration& size() const { return size_; } 00082 private: 00083 Date start_; 00084 Duration size_; 00085 }; 00086 00087 }} 00088 00089 //! Write <code>d.str()</code>. 00090 std::ostream& 00091 operator<<(std::ostream& os, const Dv::Util::Duration& u); 00092 00093 //! Simply compares years, months, seconds separately. 00094 bool 00095 operator==(const Dv::Util::Duration& u1, const Dv::Util::Duration& u2); 00096 00097 //! Compute Date just after period of size <code>u</code>, starting at <code>d</code>. 00098 /*! Sometimes give strange results: e.g. 00099 \code 00100 2000-01-31 + 1 month = 2000-03-2. 00101 \endcode 00102 */ 00103 Dv::Util::Date 00104 operator+(const Dv::Util::Date& d, const Dv::Util::Duration& u); 00105 00106 #endif 00107

dvutil-0.13.15 [30 December, 2004]