From c87a921cb5937cd6898b5154ba1d07fb3e022eb2 Mon Sep 17 00:00:00 2001 From: MedzikUser <87065584+MedzikUser@users.noreply.github.com> Date: Thu, 12 Aug 2021 22:47:54 +0200 Subject: [PATCH] fix convert/seconds.go, add test to round --- common/uptime.go | 9 +++- convert/round_test.go | 31 ++++++++++++++ convert/seconds.go | 92 +++++++++++++++-------------------------- convert/seconds_test.go | 34 +++++++++++++-- 4 files changed, 102 insertions(+), 64 deletions(-) create mode 100644 convert/round_test.go diff --git a/common/uptime.go b/common/uptime.go index 8387d86..685534a 100644 --- a/common/uptime.go +++ b/common/uptime.go @@ -1,6 +1,11 @@ package common -// TODO: -func Uptime() { +import ( + "time" + "github.com/MedzikUser/go-utils/convert" +) + +func Uptime(start time.Time) string { + return convert.Seconds(time.Since(start)) } diff --git a/convert/round_test.go b/convert/round_test.go new file mode 100644 index 0000000..133a861 --- /dev/null +++ b/convert/round_test.go @@ -0,0 +1,31 @@ +package convert + +import "testing" + +func TestRound1(t *testing.T) { + r := Round(0.1) + if r != 0 { + t.Fatal(r) + } +} + +func TestRound2(t *testing.T) { + r := Round(0.9) + if r != 0 { + t.Fatal(r) + } +} + +func TestRound3(t *testing.T) { + r := Round(25.2) + if r != 25 { + t.Fatal(r) + } +} + +func TestRound4(t *testing.T) { + r := Round(46.9998655) + if r != 46 { + t.Fatal(r) + } +} diff --git a/convert/seconds.go b/convert/seconds.go index 4d4ca63..8bd9db5 100644 --- a/convert/seconds.go +++ b/convert/seconds.go @@ -11,73 +11,49 @@ func Seconds(s time.Duration) string { minutes := Round(s.Minutes()) seconds := Round(s.Seconds()) - hours, days := HoursToDays(hours) - days, months := DaysToMonths(days) - months, years := MonthsToYears(months) - - var format string - - if years > 0 { - format += strconv.Itoa(years) + " years " - months -= years * 12 - } - - if months > 0 { - format += strconv.Itoa(months) + " months " - days -= months * 30 - } - - if days > 0 { - format += strconv.Itoa(days) + " days " - hours -= days * 24 - } - - if hours > 0 { - format += strconv.Itoa(hours) + " hours " - minutes -= hours * 24 - } - - if minutes > 0 { - format += strconv.Itoa(minutes) + " minutes " - seconds -= minutes * 60 - } - - if seconds > 0 { - format += strconv.Itoa(seconds) + " seconds" - } - - return format -} - -func HoursToDays(hours int) (int, int) { - var days int + var ( + days int + format string + ) for hours/24 > 0 { days++ hours -= 24 } - return hours, days -} - -func DaysToMonths(days int) (int, int) { - var months int - - for days/30 > 0 { - months++ - days -= 30 + if days > 0 { + format += strconv.Itoa(days) + "d" } - return days, months -} + if hours > 0 { + if len(format) >= 1 { + format += " " + } -func MonthsToYears(months int) (int, int) { - var years int - - for months/12 > 0 { - years++ - months -= 12 + format += strconv.Itoa(hours) + "h" + } else if days > 0 { + hours = days * 24 } - return months, years + minutes -= Round(s.Hours()) * 60 + + if minutes > 0 { + if len(format) >= 1 { + format += " " + } + + format += strconv.Itoa(minutes) + "m" + } + + seconds -= Round(s.Minutes()) * 60 + + if seconds > 0 { + if len(format) >= 1 { + format += " " + } + + format += strconv.Itoa(seconds) + "s" + } + + return format } diff --git a/convert/seconds_test.go b/convert/seconds_test.go index 75533da..809e8f5 100644 --- a/convert/seconds_test.go +++ b/convert/seconds_test.go @@ -5,9 +5,35 @@ import ( "time" ) -func TestSeconds(t *testing.T) { - d, err := time.ParseDuration("48h") - t.Error("1", err) +func TestSeconds1(t *testing.T) { + d, err := time.ParseDuration("48h1m2s") + if err != nil { + t.Fatal(err) + } out := Seconds(d) - t.Error("2", out) + if out != "2d 1m 2s" { + t.Fatal(out) + } +} + +func TestSeconds2(t *testing.T) { + d, err := time.ParseDuration("48h5h1m2s") + if err != nil { + t.Fatal(err) + } + out := Seconds(d) + if out != "2d 5h 1m 2s" { + t.Fatal(out) + } +} + +func TestSeconds3(t *testing.T) { + d, err := time.ParseDuration("48h5h60s") + if err != nil { + t.Fatal(err) + } + out := Seconds(d) + if out != "2d 5h 1m" { + t.Fatal(out) + } }