From 26862ed2efe73cb2ec3344a1c6ac3b0a69a199d8 Mon Sep 17 00:00:00 2001 From: Paul van Tilburg Date: Sat, 25 Oct 2014 21:53:30 +0200 Subject: [PATCH] Add private method for the TimeEntry model to round time The method uses the configuration to determine the resolution and is inspired by the following StackOverflow answer: https://stackoverflow.com/a/4182700. --- stoptime.rb | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/stoptime.rb b/stoptime.rb index 845e770..f8b1c79 100644 --- a/stoptime.rb +++ b/stoptime.rb @@ -354,6 +354,21 @@ module StopTime::Models def hours_total ((self.end - self.start) / 1.hour).round(2) end + + ####### + private + + def round_time(t) + config = Config.instance + res = config["time_resolution"] + down = t - (t.to_i % res.minutes) + up = down + res.minutes + + diff_down = t - down + diff_up = up - t + + diff_down < diff_up ? down : up + end end # == The invoice class