#ifndef _utl_TimeRange_h_ #define _utl_TimeRange_h_ #include #include namespace utl { /** \class TimeRange TimeRange.h "utl/TimeRange.h" \brief Time interval defined by two TimeStamps. The interval defined by start and stop times is inclusive on both ends. \author Darko Veberic \date 13 Feb 2007 \version $Id$ \ingroup time */ class TimeRange { public: TimeRange() { } TimeRange(const TimeStamp& start, const TimeStamp& stop) { SetSorted(start, stop); } TimeRange(const TimeStamp& start, const TimeInterval& duration) { SetSorted(start, start + duration); } TimeInterval GetDuration() const { return fStopTime - fStartTime; } const TimeStamp& GetStartTime() const { return fStartTime; } const TimeStamp& GetStopTime() const { return fStopTime; } /// Set time range void SetTimeRange(const TimeStamp& start, const TimeStamp& stop) { SetSorted(start, stop); } void SetTimeRange(const TimeStamp& start, const TimeInterval& duration) { fStartTime = start; fStopTime = start + duration; } bool operator==(const TimeRange& tr) const { return fStartTime == tr.fStartTime && fStopTime == tr.fStopTime; } bool operator!=(const TimeRange& tr) const { return !operator==(tr); } bool operator<(const TimeRange& tr) const { return fStartTime < tr.fStartTime; } bool operator>(const TimeRange& tr) const { return fStopTime > tr.fStopTime; } /// this works also with operator==() bool IsInRange(const TimeStamp& ts) const { return fStartTime <= ts && ts < fStopTime; } bool IsInRangeInclusive(const TimeStamp& ts) const { return fStartTime <= ts && ts <= fStopTime; } bool operator==(const TimeStamp& ts) const { return IsInRange(ts); } bool operator!=(const TimeStamp& ts) const { return !operator==(ts); } bool operator<(const TimeStamp& ts) const { return fStopTime < ts; } bool operator>(const TimeStamp& ts) const { return fStartTime > ts; } bool HasCommonTime(const TimeRange& tr) const { return tr.fStopTime >= fStartTime && tr.fStartTime <= fStopTime; } private: void SetSorted(const TimeStamp& start, const TimeStamp& stop) { if (start < stop) { fStartTime = start; fStopTime = stop; } else { fStartTime = stop; fStopTime = start; } } TimeStamp fStartTime; TimeStamp fStopTime; }; inline bool operator==(const TimeStamp& ts, const TimeRange& tr) { return tr == ts; } inline bool operator!=(const TimeStamp& ts, const TimeRange& tr) { return tr != ts; } inline bool operator<(const TimeStamp& ts, const TimeRange& tr) { return tr < ts; } inline bool operator>(const TimeStamp& ts, const TimeRange& tr) { return tr > ts; } } #endif // Configure (x)emacs for this file ... // Local Variables: // mode: c++ // compile-command: "make -C .. -k" // End: