diff --git a/src/Database.cpp b/src/Database.cpp index e7f40b3c..1b8fc2f1 100644 --- a/src/Database.cpp +++ b/src/Database.cpp @@ -227,3 +227,52 @@ void Database::createDatafileIfNecessary (int year, int month) } //////////////////////////////////////////////////////////////////////////////// +// The input Datarange has a start and end, for example: +// +// 2016-01-20 to 2016-04-15 +// +// Given the monthly storage scheme, split the Datarange into a vector of +// segmented Dataranges: +// +// 2016-01-20 to 2016-02-01 +// 2016-02-01 to 2016-03-01 +// 2016-03-01 to 2016-04-01 +// 2016-04-01 to 2016-05-15 +// +std::vector Database::segmentRange (const Daterange& range) +{ + std::vector segments; + + auto start_y = range.start ().year (); + auto start_m = range.start ().month (); + + auto end = range.end (); + if (end.toEpoch () == 0) + end = Datetime (); + + auto end_y = end.year (); + auto end_m = end.month (); + + while (start_y < end_y || + (start_y == end_y && start_m <= end_m)) + { + // Capture date before incrementing month. + Datetime segmentStart (start_m, 1, start_y); + + // Next month. + start_m += 1; + if (start_m > 12) + { + start_y += 1; + start_m = 1; + } + + // Capture date after incrementing month. + Datetime segmentEnd (start_m, 1, start_y); + segments.push_back (Daterange (segmentStart, segmentEnd)); + } + + return segments; +} + +//////////////////////////////////////////////////////////////////////////////// diff --git a/src/Database.h b/src/Database.h index d45d8975..5a890844 100644 --- a/src/Database.h +++ b/src/Database.h @@ -29,6 +29,7 @@ #include #include +#include #include #include @@ -54,6 +55,7 @@ public: private: std::string currentDataFile () const; void createDatafileIfNecessary (int, int); + std::vector segmentRange (const Daterange&); private: std::string _location {"~/.timewarrior/data"};