From 605df50fbde48f1edba2f9be398b3b06abb8018d Mon Sep 17 00:00:00 2001 From: Robert Date: Wed, 18 Oct 2017 17:27:27 +0200 Subject: [PATCH] [fix] util.secondsToClock 00:60 should be 01:00 (#3371) --- frontend/util.lua | 14 +++++++++----- spec/unit/util_spec.lua | 8 ++++++++ 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/frontend/util.lua b/frontend/util.lua index 3004ee466..a36e744c8 100644 --- a/frontend/util.lua +++ b/frontend/util.lua @@ -75,18 +75,22 @@ function util.secondsToClock(seconds, withoutSeconds) seconds = tonumber(seconds) if seconds == 0 or seconds ~= seconds then if withoutSeconds then - return "00:00"; + return "00:00" else - return "00:00:00"; + return "00:00:00" end else local round = withoutSeconds and require("optmath").round or math.floor - local hours = string.format("%02.f", math.floor(seconds / 3600)); - local mins = string.format("%02.f", round(seconds / 60 - (hours * 60))); + local hours = string.format("%02.f", math.floor(seconds / 3600)) + local mins = string.format("%02.f", round(seconds / 60 - (hours * 60))) + if mins == "60" then + mins = string.format("%02.f", 0) + hours = string.format("%02.f", hours + 1) + end if withoutSeconds then return hours .. ":" .. mins end - local secs = string.format("%02.f", math.floor(seconds - hours * 3600 - mins * 60)); + local secs = string.format("%02.f", math.floor(seconds - hours * 3600 - mins * 60)) return hours .. ":" .. mins .. ":" .. secs end end diff --git a/spec/unit/util_spec.lua b/spec/unit/util_spec.lua index fa890a2c0..0aeb5f7a8 100644 --- a/spec/unit/util_spec.lua +++ b/spec/unit/util_spec.lua @@ -329,6 +329,14 @@ describe("util module", function() util.secondsToClock(110, true)) assert.is_equal("00:02", util.secondsToClock(120, true)) + assert.is_equal("01:00", + util.secondsToClock(3600, true)) + assert.is_equal("01:00", + util.secondsToClock(3599, true)) + assert.is_equal("01:00", + util.secondsToClock(3570, true)) + assert.is_equal("00:59", + util.secondsToClock(3569, true)) end) it("should convert seconds to 00:00:00 format", function() assert.is_equal("00:00:00",