diff --git a/common/log.go b/common/log.go index 9c09529..c565671 100644 --- a/common/log.go +++ b/common/log.go @@ -8,16 +8,16 @@ import ( /* Info: - common.Log.Info("Info") + Log.Info("Info") Debug: - common.Log.Debug("Debug") + Log.Debug("Debug") Error: - common.Log.Error("Error") + Log.Error("Error") Warn: - common.Log.Warn("Warn") + Log.Warn("Warn") */ var Log = &logrus.Logger{ Out: os.Stdout, diff --git a/common/uptime.go b/common/uptime.go index 685534a..2678d7e 100644 --- a/common/uptime.go +++ b/common/uptime.go @@ -3,9 +3,9 @@ package common import ( "time" - "github.com/MedzikUser/go-utils/convert" + "github.com/MedzikUser/go-utils/utils" ) func Uptime(start time.Time) string { - return convert.Seconds(time.Since(start)) + return utils.FormatSeconds(time.Since(start)) } diff --git a/convert/round_test.go b/convert/round_test.go deleted file mode 100644 index 133a861..0000000 --- a/convert/round_test.go +++ /dev/null @@ -1,31 +0,0 @@ -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_test.go b/convert/seconds_test.go deleted file mode 100644 index 809e8f5..0000000 --- a/convert/seconds_test.go +++ /dev/null @@ -1,39 +0,0 @@ -package convert - -import ( - "testing" - "time" -) - -func TestSeconds1(t *testing.T) { - d, err := time.ParseDuration("48h1m2s") - if err != nil { - t.Fatal(err) - } - out := Seconds(d) - 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) - } -} diff --git a/convert/round.go b/utils/round.go similarity index 85% rename from convert/round.go rename to utils/round.go index 86a216c..bd7a57c 100644 --- a/convert/round.go +++ b/utils/round.go @@ -1,4 +1,4 @@ -package convert +package utils func Round(val float64) int { if val < 0 { diff --git a/utils/round_test.go b/utils/round_test.go new file mode 100644 index 0000000..2904293 --- /dev/null +++ b/utils/round_test.go @@ -0,0 +1,29 @@ +package utils + +import "testing" + +func TestRound(t *testing.T) { + // 1 + r := Round(0.1) + if r != 0 { + t.Fatal(r) + } + + // 2 + r = Round(0.9) + if r != 0 { + t.Fatal(r) + } + + // 3 + r = Round(25.2) + if r != 25 { + t.Fatal(r) + } + + // 4 + r = Round(46.9998655) + if r != 46 { + t.Fatal(r) + } +} diff --git a/convert/seconds.go b/utils/seconds.go similarity index 87% rename from convert/seconds.go rename to utils/seconds.go index 8bd9db5..8435b2b 100644 --- a/convert/seconds.go +++ b/utils/seconds.go @@ -1,12 +1,11 @@ -package convert +package utils import ( "strconv" "time" ) -// FIXME: -func Seconds(s time.Duration) string { +func FormatSeconds(s time.Duration) string { hours := Round(s.Hours()) minutes := Round(s.Minutes()) seconds := Round(s.Seconds()) @@ -55,5 +54,9 @@ func Seconds(s time.Duration) string { format += strconv.Itoa(seconds) + "s" } + if len(format) == 0 { + format = "0s" + } + return format } diff --git a/utils/seconds_test.go b/utils/seconds_test.go new file mode 100644 index 0000000..f802448 --- /dev/null +++ b/utils/seconds_test.go @@ -0,0 +1,52 @@ +package utils + +import ( + "testing" + "time" +) + +func TestFormatSeconds(t *testing.T) { + // 1 + d, err := time.ParseDuration("48h1m2s") + if err != nil { + t.Fatal(err) + } + + out := FormatSeconds(d) + if out != "2d 1m 2s" { + t.Fatal(out) + } + + // 2 + d, err = time.ParseDuration("48h5h1m2s") + if err != nil { + t.Fatal(err) + } + + out = FormatSeconds(d) + if out != "2d 5h 1m 2s" { + t.Fatal(out) + } + + // 3 + d, err = time.ParseDuration("48h5h60s") + if err != nil { + t.Fatal(err) + } + + out = FormatSeconds(d) + if out != "2d 5h 1m" { + t.Fatal(out) + } + + // 4 + d, err = time.ParseDuration("0s") + if err != nil { + t.Fatal(err) + } + + out = FormatSeconds(d) + if out != "0s" { + t.Fatal(out) + } +}