From a4ed7c9def219d6bf82d1ed05f02d2a6e07496ed Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Sun, 17 Apr 2016 18:20:41 -0400 Subject: [PATCH] Filter, Timeline: Converted from a pair of Datetime objects to a Daterange --- src/Filter.cpp | 34 ++++++++----------------- src/Filter.h | 12 +++------ src/Timeline.cpp | 12 +++------ src/Timeline.h | 7 +++--- src/commands/CmdReport.cpp | 4 +-- src/helper.cpp | 51 +++++++++++++++++++------------------- 6 files changed, 48 insertions(+), 72 deletions(-) diff --git a/src/Filter.cpp b/src/Filter.cpp index 0924fbec..7e77be79 100644 --- a/src/Filter.cpp +++ b/src/Filter.cpp @@ -31,33 +31,21 @@ //////////////////////////////////////////////////////////////////////////////// bool Filter::empty () const { - return _start.toEpoch () == 0 && - _end.toEpoch () == 0 && - _tags.size () == 0; + return _range.start ().toEpoch () == 0 && + _range.end ().toEpoch () == 0 && + _tags.size () == 0; } //////////////////////////////////////////////////////////////////////////////// -Datetime Filter::start () const +Daterange Filter::range () const { - return _start; + return _range; } //////////////////////////////////////////////////////////////////////////////// -void Filter::start (Datetime value) +void Filter::range (const Daterange& range) { - _start = value; -} - -//////////////////////////////////////////////////////////////////////////////// -Datetime Filter::end () const -{ - return _end; -} - -//////////////////////////////////////////////////////////////////////////////// -void Filter::end (Datetime value) -{ - _end = value; + _range = range; } //////////////////////////////////////////////////////////////////////////////// @@ -78,10 +66,10 @@ std::string Filter::dump () const { std::stringstream out; - out << "Filter _start '" - << _start.toEpoch () - << "' _end '" - << _end.toEpoch () + out << "Filter _range.start '" + << _range.start ().toEpoch () + << "' _range.end '" + << _range.end ().toEpoch () << "' _tags"; for (auto& tag : _tags) diff --git a/src/Filter.h b/src/Filter.h index 87ccd4d3..e93bf458 100644 --- a/src/Filter.h +++ b/src/Filter.h @@ -27,7 +27,7 @@ #ifndef INCLUDED_FILTER #define INCLUDED_FILTER -#include +#include #include #include @@ -37,11 +37,8 @@ public: Filter () = default; bool empty () const; - Datetime start () const; - void start (Datetime); - - Datetime end () const; - void end (Datetime); + Daterange range () const; + void range (const Daterange&); std::set tags () const; void tag (const std::string&); @@ -49,8 +46,7 @@ public: std::string dump () const; private: - Datetime _start {0}; - Datetime _end {0}; + Daterange _range {}; std::set _tags {}; }; diff --git a/src/Timeline.cpp b/src/Timeline.cpp index 40e4b6be..ef91c636 100644 --- a/src/Timeline.cpp +++ b/src/Timeline.cpp @@ -49,15 +49,9 @@ //////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////// -void Timeline::start (const Datetime& when) +void Timeline::range (const Daterange& range) { - _range.start (when); -} - -//////////////////////////////////////////////////////////////////////////////// -void Timeline::end (const Datetime& when) -{ - _range.end (when); + _range = range; } //////////////////////////////////////////////////////////////////////////////// @@ -102,7 +96,7 @@ std::string Timeline::dump () const { std::stringstream out; - out << "Timeline _range " << _range.dump (); + out << "Timeline _range " << _range.start ().toISO () << " - " << _range.end ().toISO () << "\n"; for (auto& i : _inclusions) out << " " << i.dump (); for (auto& e : _exclusions) diff --git a/src/Timeline.h b/src/Timeline.h index e974bcb8..bc8effdc 100644 --- a/src/Timeline.h +++ b/src/Timeline.h @@ -27,9 +27,9 @@ #ifndef INCLUDED_TIMELINE #define INCLUDED_TIMELINE -#include #include #include +#include #include #include @@ -37,8 +37,7 @@ class Timeline { public: Timeline () = default; - void start (const Datetime&); - void end (const Datetime&); + void range (const Daterange&); void include (const Interval&); void exclude (const Exclusion&); @@ -48,7 +47,7 @@ public: std::string dump () const; private: - Interval _range {}; + Daterange _range {}; std::vector _inclusions {}; std::vector _exclusions {}; }; diff --git a/src/commands/CmdReport.cpp b/src/commands/CmdReport.cpp index 9492c2e6..b83ade3b 100644 --- a/src/commands/CmdReport.cpp +++ b/src/commands/CmdReport.cpp @@ -84,8 +84,8 @@ int CmdReport ( auto intervals = timeline.tracked (rules); // Compose Header info. - rules.set ("temp.report.start", filter.start ().toEpoch () > 0 ? filter.start ().toISO () : ""); - rules.set ("temp.report.end", filter.end ().toEpoch () > 0 ? filter.end ().toISO () : ""); + rules.set ("temp.report.start", filter.range ().start ().toEpoch () > 0 ? filter.range ().start ().toISO () : ""); + rules.set ("temp.report.end", filter.range ().end ().toEpoch () > 0 ? filter.range ().end ().toISO () : ""); std::string combinedTags; for (auto& tag : filter.tags ()) { diff --git a/src/helper.cpp b/src/helper.cpp index 376585d5..8df63bb0 100644 --- a/src/helper.cpp +++ b/src/helper.cpp @@ -177,8 +177,8 @@ Filter createFilterFromCLI (const CLI& cli) if (args.size () == 1 && args[0] == "") { - filter.start (Datetime (start)); - filter.end (Datetime ("now")); + filter.range ().start (Datetime (start)); + filter.range ().end (Datetime ("now")); } // from @@ -186,8 +186,8 @@ Filter createFilterFromCLI (const CLI& cli) args[0] == "from" && args[1] == "") { - filter.start (Datetime (start)); - filter.end (Datetime ("now")); + filter.range ().start (Datetime (start)); + filter.range ().end (Datetime ("now")); } // to/- @@ -196,8 +196,8 @@ Filter createFilterFromCLI (const CLI& cli) (args[1] == "to" || args[1] == "-") && args[2] == "") { - filter.start (Datetime (start)); - filter.end (Datetime (end)); + filter.range ().start (Datetime (start)); + filter.range ().end (Datetime (end)); } // from/since to/- @@ -207,8 +207,8 @@ Filter createFilterFromCLI (const CLI& cli) (args[2] == "to" || args[2] == "-") && args[3] == "") { - filter.start (Datetime (start)); - filter.end (Datetime (end)); + filter.range ().start (Datetime (start)); + filter.range ().end (Datetime (end)); } // for @@ -217,8 +217,8 @@ Filter createFilterFromCLI (const CLI& cli) args[1] == "for" && args[2] == "") { - filter.start (Datetime (start)); - filter.end (Datetime (start) + Duration (duration).toTime_t ()); + filter.range ().start (Datetime (start)); + filter.range ().end (Datetime (start) + Duration (duration).toTime_t ()); } // from/since for @@ -228,8 +228,8 @@ Filter createFilterFromCLI (const CLI& cli) args[2] == "for" && args[3] == "") { - filter.start (Datetime (start)); - filter.end (Datetime (start) + Duration (duration).toTime_t ()); + filter.range ().start (Datetime (start)); + filter.range ().end (Datetime (start) + Duration (duration).toTime_t ()); } // before @@ -238,8 +238,8 @@ Filter createFilterFromCLI (const CLI& cli) args[1] == "before" && args[2] == "") { - filter.end (Datetime (start) - Duration (duration).toTime_t ()); - filter.end (Datetime (start)); + filter.range ().end (Datetime (start) - Duration (duration).toTime_t ()); + filter.range ().end (Datetime (start)); } // after @@ -248,16 +248,16 @@ Filter createFilterFromCLI (const CLI& cli) args[1] == "after" && args[2] == "") { - filter.start (Datetime (start)); - filter.end (Datetime (start) + Duration (duration).toTime_t ()); + filter.range ().start (Datetime (start)); + filter.range ().end (Datetime (start) + Duration (duration).toTime_t ()); } // else if (args.size () == 1 && args[0] == "") { - filter.start (Datetime ("now") - Duration (duration).toTime_t ()); - filter.end (Datetime ("now")); + filter.range ().start (Datetime ("now") - Duration (duration).toTime_t ()); + filter.range ().end (Datetime ("now")); } // Unrecognized date range construct. @@ -273,8 +273,8 @@ Filter createFilterFromCLI (const CLI& cli) Interval createIntervalFromFilter (const Filter& filter) { Interval interval; - interval.start (filter.start ()); - interval.end (filter.end ()); + interval.range ().start (filter.range ().start ()); + interval.range ().end (filter.range ().end ()); for (auto& tag : filter.tags ()) interval.tag (tag); @@ -303,8 +303,7 @@ Timeline createTimelineFromData ( const Filter& filter) { Timeline t; - t.start (filter.start ()); - t.end (filter.end ()); + t.range (filter.range ()); // Add filtered intervals. for (auto& line : database.allLines ()) @@ -348,13 +347,13 @@ Interval getLatestInterval (Database& database) // are found in the interval. bool intervalMatchesFilter (const Interval& interval, const Filter& filter) { - if ((filter.start ().toEpoch () == 0 && - filter.end ().toEpoch () == 0) + if ((filter.range ().start ().toEpoch () == 0 && + filter.range ().end ().toEpoch () == 0) || - (interval.end () > filter.start () && - interval.start () < filter.end ())) + (interval.end () > filter.range ().start () && + interval.start () < filter.range ().end ())) { for (auto& tag : filter.tags ()) if (! interval.hasTag (tag))