libtime++: Date and time calculation
ranges.cc
Go to the documentation of this file.
1 
34 #define TF_RANGES_CC_VERSION \
35  "TF_RANGES_CC V1.0 "
36 
37 #include <libtime++.h>
38 #include<iostream>
39 
40 namespace libtime {
41 
43  const TAbsoluteTime& end):
44  Mbegin(begin), Mend(end)
45  {
46  libtime_assert((end>=begin),"ERROR (Trange): illegal range!");
47  }
48 
50  bool TRange::includes(const TAbsoluteTime& date) const
51  {
52  return((date>=Mbegin)&&(date<=Mend));
53  }
54 
56  bool TRange::includes(const TRange& range) const
57  {
58  return((range.begin()>=Mbegin)&&(range.end()<=Mend));
59  }
60 
62  bool TRange::overlaps(const TRange& range) const
63  {
64  return((range.end()>=Mbegin)&&(range.begin()<=Mend));
65  }
66 
68  TRange TRange::largestcommon(const TRange& range) const
69  {
70  libtime_assert((this->overlaps(range)),
71  "ERROR (Trange::largestcommon): ranges must overlap");
72  return(TRange(Mbegin<range.begin() ? Mbegin : range.begin(),
73  Mend>range.end() ? Mend : range.end()));
74  }
75 
77  TRange TRange::smallestcommon(const TRange& range) const
78  {
79  libtime_assert((this->overlaps(range)),
80  "ERROR (Trange::smallestcommon): ranges must overlap");
81  return(TRange(Mbegin>range.begin() ? Mbegin : range.begin(),
82  Mend<range.end() ? Mend : range.end()));
83  }
84 
87  {
88  Mbegin+=dt;
89  Mend+=dt;
90  return(*this);
91  }
92 
95  {
96  Mbegin-=dt;
97  Mend-=dt;
98  return(*this);
99  }
100 
103  {
104  TRange retval(*this);
105  retval.delay(dt);
106  return(retval);
107  }
108 
111  {
112  TRange retval(*this);
113  retval.advance(dt);
114  return(retval);
115  }
116 
118  void TRange::expand(const TRange& range)
119  {
120  Mbegin = Mbegin < range.begin() ? Mbegin : range.begin();
121  Mend = Mend > range.end() ? Mend : range.end();
122  }
123 
125  void TRange::shrink(const TRange& range)
126  {
127  libtime_assert((this->overlaps(range)),
128  "ERROR (Trange::shrink): ranges must overlap");
129  Mbegin = Mbegin > range.begin() ? Mbegin : range.begin();
130  Mend = Mend < range.end() ? Mend : range.end();
131  }
132 
133 } // namespace libtime
134 
135 /* ----- END OF ranges.cc ----- */
TAbsoluteTime begin() const
Definition: libtime++.h:617
time range
Definition: libtime++.h:612
TAbsoluteTime end() const
Definition: libtime++.h:618
#define libtime_assert(C, M)
Check an assertion and report by throwing an exception.
Definition: libtime++.h:714
TAbsoluteTime Mend
Definition: libtime++.h:633
class to contain relative times
Definition: libtime++.h:201
TRange largestcommon(const TRange &) const
find largest range spanned by both
Definition: ranges.cc:68
TRange & advance(const TRelativeTime &)
advance this range by dt
Definition: ranges.cc:94
TRange advancedby(const TRelativeTime &) const
return a range advanced by dt
Definition: ranges.cc:110
void expand(const TRange &)
set to largest range spanned by both
Definition: ranges.cc:118
bool overlaps(const TRange &) const
true if other range overlaps this one
Definition: ranges.cc:62
TRange & delay(const TRelativeTime &)
delay this range by dt
Definition: ranges.cc:86
void shrink(const TRange &)
set to smallest range common to both
Definition: ranges.cc:125
bool includes(const TAbsoluteTime &) const
true if date is within range
Definition: ranges.cc:50
TRange delayedby(const TRelativeTime &) const
return a range delayed by dt
Definition: ranges.cc:102
class to contain absolute times
Definition: libtime++.h:149
TAbsoluteTime Mbegin
Definition: libtime++.h:632
TRange smallestcommon(const TRange &) const
find largest range common to both
Definition: ranges.cc:77