libUTL++
TimeSpan.h
1 #pragma once
2 
4 
5 #include <libutl/Duration.h>
6 #include <libutl/Span.h>
7 #include <libutl/Time.h>
8 
10 
11 UTL_NS_BEGIN;
12 
14 
24 
26 class TimeSpan : public ObjectSpan<Time, Duration>
27 {
30 
31 public:
37  {
38  _begin = rhs.begin();
39  _end = rhs.end();
40  }
41 
47  TimeSpan(const Time& begin, const Time& end)
48  {
49  set(begin, end);
50  }
51 
53  virtual int compare(const Object& rhs) const;
54 
56  Duration
57  duration() const
58  {
59  return _end - _begin;
60  }
61 
63  uint_t numSpanned(const Duration& dur) const;
64 
69  uint_t numSpanned(uint_t unit) const;
70 
72  void
73  shift(const Duration& rhs)
74  {
75  _begin += rhs;
76  _end += rhs;
77  }
78 
83  bool
84  spans(uint_t unit, uint_t num = 1) const
85  {
86  return (numSpanned(unit) >= num);
87  }
88 
90  const TimeSpan& operator+=(const Duration& rhs)
91  {
92  shift(rhs);
93  return *this;
94  }
95 
97  const TimeSpan& operator-=(const Duration& rhs)
98  {
99  shift(-rhs);
100  return *this;
101  }
102 };
103 
105 
106 UTL_NS_END;
#define UTL_CLASS_DEFID
Default init() and deInit() (which are merely place-holders).
Definition: macros.h:532
const T & begin() const
Get the beginning of the span.
Definition: Span.h:72
TimeSpan(const Span< Time, Duration > &rhs)
Constructor.
Definition: TimeSpan.h:36
Span of object-derived objects.
Definition: Span.h:284
const TimeSpan & operator-=(const Duration &rhs)
Subtract the given duration from the begin and end times.
Definition: TimeSpan.h:97
#define UTL_CLASS_DECL_NT_TPL2(DC, BC, T1, T2)
Declaration of standard UTL++ functionality for a non-template class that inherits from a template cl...
Definition: macros.h:701
const T & end() const
Get the end of the span.
Definition: Span.h:86
const TimeSpan & operator+=(const Duration &rhs)
Add the given duration to the begin and end times.
Definition: TimeSpan.h:90
Duration of time.
Definition: Duration.h:40
unsigned int uint_t
Unsigned integer.
Definition: types.h:59
TimeSpan(const Time &begin, const Time &end)
Constructor.
Definition: TimeSpan.h:47
Date/time.
Definition: Time.h:148
Duration duration() const
Get the duration of the span.
Definition: TimeSpan.h:57
Time span.
Definition: TimeSpan.h:26
void shift(const Duration &rhs)
Add the given duration to the begin and end times.
Definition: TimeSpan.h:73
Root of UTL++ class hierarchy.
Definition: Object.h:52
int compare(bool lhs, bool rhs)
Compare two boolean values.
bool spans(uint_t unit, uint_t num=1) const
Determine whether self spans the given multiple of the given unit.
Definition: TimeSpan.h:84