From 44f19d0c0193f8731f07a21a2950f9163b3cfe5e Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Thu, 30 Jun 2016 16:45:54 -0400 Subject: [PATCH] helper: Added new quantizeToNMinutes function --- src/helper.cpp | 13 +++++++++++++ src/timew.h | 1 + 2 files changed, 14 insertions(+) diff --git a/src/helper.cpp b/src/helper.cpp index 6aff6cea..28921949 100644 --- a/src/helper.cpp +++ b/src/helper.cpp @@ -296,6 +296,19 @@ int quantizeTo15Minutes (const int minutes) return minutes + 15 - deviation; } +//////////////////////////////////////////////////////////////////////////////// +int quantizeToNMinutes (const int minutes, const int N) +{ + if (minutes % N == 0) + return minutes; + + auto deviation = minutes % N; + if (deviation < N/2) + return minutes - deviation; + + return minutes + N - deviation; +} + //////////////////////////////////////////////////////////////////////////////// // Check rules to see if day is a holiday. bool dayIsHoliday (const Rules& rules, const Datetime& day) diff --git a/src/timew.h b/src/timew.h index 71c97a11..45c81f1e 100644 --- a/src/timew.h +++ b/src/timew.h @@ -70,6 +70,7 @@ std::string jsonFromIntervals (const std::vector &); Palette createPalette (const Rules&); std::map createTagColorMap (const Rules&, Palette&, const std::vector &); int quantizeTo15Minutes (const int); +int quantizeToNMinutes (const int, const int); bool dayIsHoliday (const Rules&, const Datetime&); bool findHint (const CLI&, const std::string&); std::string minimalDelta (const Datetime&, const Datetime&);